Nek5000
SEM for Incompressible NS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
crs_test.c
Go to the documentation of this file.
1 #include <stddef.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include "c99.h"
7 #include "name.h"
8 #include "fail.h"
9 #include "types.h"
10 #include "mem.h"
11 #include "gs_defs.h"
12 #include "comm.h"
13 #include "gs.h"
14 #include "crs.h"
15 
16 void test(const struct comm *const comm)
17 {
18  const double A[16] = { 2, -1, -1, 0,
19  -1, 2, 0, -1,
20  -1, 0, 2, -1,
21  0, -1, -1, 2 };
22  const uint Ai[16] = { 0, 0, 0, 0,
23  1, 1, 1, 1,
24  2, 2, 2, 2,
25  3, 3, 3, 3 },
26  Aj[16] = { 0, 1, 2, 3,
27  0, 1, 2, 3,
28  0, 1, 2, 3,
29  0, 1, 2, 3 };
30  ulong xid[4]; slong uid[4];
31  double x[4]={1,1,1,1}, b[4], bmean;
32  uint i, w, gn, px, py;
33 
34  slong *xgid=0; double *xg=0; struct gs_data *gsh;
35 
36  struct crs_data *crs;
37 
38  w = ceil(sqrt(comm->np)); gn = (w+1)*(w+1);
39 
40  if(comm->id==0) printf("arranging procs in a %u x %u square\n", w, w);
41 
42  px = comm->id%w, py = comm->id/w;
43  b[0] = xid[0] = (w+1)*py +px+1;
44  b[1] = xid[1] = (w+1)*py +px+2;
45  b[2] = xid[2] = (w+1)*(py+1)+px+1;
46  b[3] = xid[3] = (w+1)*(py+1)+px+2;
47 
48  gn = comm_reduce_slong(comm, gs_max, (const slong*)&xid[3],1);
49  bmean = comm_reduce_double(comm, gs_add, b,4)/gn;
50 
51  gsh = gs_setup((const slong*)xid,4, comm,0,gs_crystal_router,0);
52  gs(x,gs_double,gs_add,0,gsh,0);
53  gs(b,gs_double,gs_add,0,gsh,0);
54  for(i=0;i<4;++i) b[i]=xid[i]-bmean/x[i];
55  gs(b,gs_double,gs_add,0,gsh,0);
56  gs_free(gsh);
57 
58  gsh = gs_setup((const slong*)xid,4, comm,1,gs_crystal_router,0);
59  for(i=0;i<4;++i) uid[i]=comm->id;
60  gs(uid,gs_slong,gs_min,0,gsh,0);
61  gs_free(gsh);
62  for(i=0;i<4;++i) uid[i] = (uid[i]==comm->id?(slong)xid[i]:-(slong)xid[i]);
63 
64  if(comm->id==0) {
65  xgid = tmalloc(slong, gn);
66  xg = tmalloc(double,gn);
67  for(i=0;i<gn;++i) xgid[i] = -(slong)(i+1);
68  for(i=0;i<4;++i) xgid[xid[i]-1] = uid[i];
69  }
70  gsh = gs_setup(comm->id?uid:xgid,comm->id?4:gn, comm,0,gs_crystal_router,0);
71 
72 
73  if(comm->id==0) for(i=0;i<4;++i) xg[xid[i]-1]=b[i];
74  gs(comm->id?b:xg,gs_double,gs_add, 0, gsh, 0);
75  if(comm->id==0) for(i=0;i<gn;++i) printf("b[%u] = %g\n",i,xg[i]);
76  for(i=0;i<4;++i) b[i]=xid[i]-bmean/x[i];
77 
78  crs = crs_setup(4,xid, 16,Ai,Aj,A, 1, comm);
79 
80  crs_solve(x,crs,b);
81 
82  crs_stats(crs);
83 
84  crs_free(crs);
85 
86  if(comm->id==0) for(i=0;i<4;++i) xg[xid[i]-1]=x[i];
87  gs(comm->id?x:xg,gs_double,gs_add, 0, gsh, 0);
88  if(comm->id==0) for(i=0;i<gn;++i) printf("x[%u] = %g\n",i,xg[i]);
89 
90  gs_free(gsh);
91  if(comm->id==0) free(xg), free(xgid);
92 }
93 
94 int main(int narg, char* arg[])
95 {
96  comm_ext world; int np;
97  struct comm comm;
98 #ifdef MPI
99  MPI_Init(&narg,&arg);
100  world = MPI_COMM_WORLD;
101  MPI_Comm_size(world,&np);
102 #else
103  world=0, np=1;
104 #endif
105 
106  comm_init(&comm,world);
107  test(&comm);
108  comm_free(&comm);
109 
110 #ifdef MPI
111  MPI_Finalize();
112 #endif
113 
114  return 0;
115 }
116 
#define slong
Definition: types.h:74
#define gs_slong
Definition: gs_defs.h:66
#define crs_stats
Definition: crs.h:10
#define uint
Definition: types.h:70
#define tmalloc(type, count)
Definition: mem.h:91
void test(const struct comm *const comm)
Definition: crs_test.c:16
#define gs
Definition: gs.c:26
#define x
#define crs_solve
Definition: crs.h:9
ulong A[NUM][SI]
Definition: sort_test.c:17
Definition: comm.h:85
#define gs_setup
Definition: gs.c:29
static void comm_free(struct comm *c)
Definition: comm.h:176
#define crs_free
Definition: crs.h:11
#define gs_free
Definition: gs.c:30
int main(int narg, char *arg[])
Definition: crs_test.c:94
const uint Ai[3][32]
Definition: xxt_test.c:80
int comm_ext
Definition: comm.h:69
uint np
Definition: comm.h:86
#define ulong
Definition: types.h:75
for i
Definition: xxt_test.m:74
const uint Aj[3][32]
Definition: xxt_test.c:85
Definition: gs.c:1039
#define crs_setup
Definition: crs.h:8
Gather/Scatter Library interface.
uint id
Definition: comm.h:86
establishes some macros to establish naming conventions
static uint np
Definition: findpts_test.c:63
static void comm_init(struct comm *c, comm_ext ce)
Definition: comm.h:133