16
/* utilities for GA test programs */
19
#define N 10 /* first dimension */
20
#define NDIM 4 /* number of dimensions */
27
/*\ fill n-dimensional array section with value
29
void fill_patch(double *ptr, int dim[], int ld[], int ndim, double val)
34
case 0: GA_Error("fill_patch: error",ndim);
35
case 1: for(i=0;i <dim[0];i++)ptr[i]=val;
37
case 2: for(i=0; i< dim[0]; i++){
38
double *arr = ptr + i*ld[0];
39
for(j=0; j< dim[1]; j++)arr[j]=val;
43
for(i=0; i<ndim-1; i++)stride *=ld[i];
44
for(i=0; i<dim[0]; i++){
45
double *arr = ptr + stride*i;
46
fill_patch(arr, dim+1, ld+1, ndim-1, val);
56
int me=GA_Nodeid(), nproc=GA_Nnodes(), proc, loop;
57
int dims[NDIM], lo[NDIM], hi[NDIM], block[NDIM], ld[NDIM-1];
58
int64_t lo64[NDIM], hi64[NDIM];
59
int i,d,*proclist, offset;
60
int adims[NDIM], ndim,type;
69
/***** create array A with default distribution *****/
70
if(me==0){printf("Creating array A\n"); fflush(stdout);}
71
for(i = 0; i<NDIM; i++)dims[i] = N*(i+1);
73
g_a = GA_Create_handle();
74
GA_Set_data(g_a,NDIM,dims,MT_F_DBL);
75
GA_Set_array_name(g_a,"array A");
76
(void)GA_Allocate(g_a);
78
g_a = NGA_Create(MT_F_DBL, NDIM, dims, "array A", NULL);
80
if(!g_a) GA_Error("create failed: A",1);
81
if(me==0)printf("OK\n\n");
83
/* print info about array we got */
84
NGA_Inquire(g_a, &type, &ndim, adims);
85
GA_Print_distribution(g_a);
88
/* duplicate array A with ga_create irreg rather than ga_duplicate
89
* -- want to show distribution control
90
* -- with ga_duplicate it would be g_b=GA_Duplicate(g_a,name)
92
if(me==0)printf("\nReconstructing distribution description for A\n");
94
/* get memory for arrays describing distribution */
95
proclist = (int*)malloc(nproc*sizeof(int));
96
if(!proclist)GA_Error("malloc failed for proclist",1);
97
regions = (patch_t*)malloc(nproc*sizeof(patch_t));
98
if(!regions)GA_Error("malloc failed for regions",1);
99
map = (int*)malloc((nproc+ndim)*sizeof(int)); /* ubound= nproc+mdim */
100
if(!map)GA_Error("malloc failed for map",1);
102
/* first find out how array g_a is distributed */
103
for(i=0;i<ndim;i++)lo[i]=BASE;
104
for(i=0;i<ndim;i++)hi[i]=adims[i] -1 + BASE;
105
for(i=0;i<ndim;i++)lo64[i]=BASE;
106
for(i=0;i<ndim;i++)hi64[i]=adims[i] -1 + BASE;
107
proc = NGA_Locate_nnodes(g_a, lo, hi);
108
if(proc<1) GA_Error("error in NGA_Locate_nnodes",proc);
109
proc = NGA_Locate_nnodes64(g_a, lo64, hi64);
110
if(proc<1) GA_Error("error in NGA_Locate_nnodes",proc);
111
proc = NGA_Locate_region(g_a, lo, hi, (int*)regions, proclist);
112
if(proc<1) GA_Error("error in NGA_Locate_region",proc);
114
/* determine blocking for each dimension */
115
for(i=0;i<ndim;i++)block[i]=0;
116
for(i=0;i<ndim;i++)adims[i]=0;
119
for(d=0; d<ndim; d++)
121
if( regions[i].hi[d]>adims[d] ){
122
map[offset] = regions[i].lo[d];
125
adims[d]= regions[i].hi[d];
129
printf("Distribution map contains %d elements\n",offset);
130
print_subscript("number of blocks for each dimension",ndim,block,"\n");
131
print_subscript("distribution map",offset,map,"\n\n");
135
if(me==0)printf("Creating array B applying distribution of A\n");
137
# ifdef USE_DUPLICATE
138
g_b = GA_Duplicate(g_a,"array B");
140
g_b = NGA_Create_irreg(MT_F_DBL, NDIM, dims, "array B", block,map);
142
if(!g_b) GA_Error("create failed: B",1);
143
if(me==0)printf("OK\n\n");
144
free(proclist); free(regions); free(map);
146
GA_Print_distribution(g_b);
151
printf("\nCompare distributions of A and B\n");
152
if(GA_Compare_distr(g_a,g_b))
153
printf("Failure: distributions NOT identical\n");
155
printf("Success: distributions identical\n");
161
printf("\nAccessing local elements of A: set them to the owner process id\n");
166
NGA_Distribution(g_a,me,lo,hi);
168
if(hi[0]>=0){/* -1 means no elements stored on this processor */
171
NGA_Access(g_a, lo,hi, &ptr, ld);
172
for(i=0;i<ndim;i++)locdim[i]=hi[i]-lo[i]+1;
173
fill_patch(ptr, locdim, ld, ndim,(double)me);
176
for(i=0;i<nproc; i++){
177
if(me==i && hi[0]>=0){
179
sprintf(msg,"%d: leading dimensions",me);
180
print_subscript(msg,ndim-1,ld,"\n");
187
if(me==0)printf("\nRandomly checking the update using ga_get on array sections\n");
190
/* show ga_get working and verify array updates
191
* every process does N random gets
192
* for simplicity get only a single row at a time
194
srand(me); /* different seed for every process */
195
hi[ndim-1]=adims[ndim-1] -1 + BASE;
196
for(i=1;i<ndim-1; i++)ld[i]=1; ld[ndim-2]=adims[ndim-1] -1 + BASE;
198
/* get buffer memory */
199
buf = (double*)malloc(adims[ndim-1]*sizeof(double));
200
if(!buf)GA_Error("malloc failed for buf",1);
202
/* half of the processes check the result */
204
for(loop = 0; loop< N; loop++){ /* task parallel loop */
206
for (i= 0; i < ndim -1; i ++){
207
lo[i] = hi[i] = rand()%adims[i]+BASE;
210
/* print_subscript("getting",ndim,lo,"\n");*/
211
NGA_Get(g_a,lo,hi,buf,ld);
214
for(i=0;i<adims[ndim-1]; i++){
215
int p = NGA_Locate(g_a, lo);
216
if((double)p != buf[i]) {
218
sprintf(msg,"%d: wrong value: %d != %f a",me, p, buf[i]);
219
print_subscript(msg,ndim,lo,"\n");
220
GA_Error("Error - bye",i);
229
if(me==0)printf("OK\n");
240
Integer heap=300000, stack=300000;
245
GA_INIT(argc,argv); /* initialize GA */
250
if(me==0) printf("Using %d processes\n\n",nproc);
252
if(!MA_init((Integer)MT_F_DBL, stack/nproc, heap/nproc))
253
GA_Error("MA_init failed bytes= %d",stack+heap);
257
int i, *list = (int*)malloc(nproc*sizeof(int));
258
if(!list)GA_Error("malloc failed",nproc);
260
for(i=0; i<nproc;i++)list[i]=nproc-1-i;
262
GA_Register_proclist(list, nproc);
267
if(GA_Uses_fapi())GA_Error("Program runs with C API only",1);
271
if(me==0)printf("\nAll tests successful\n\n");