File: cache_test.c

package info (click to toggle)
ga 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ansic: 192,963; fortran: 53,761; f90: 11,218; cpp: 5,784; makefile: 2,248; sh: 1,945; python: 1,734; perl: 534; csh: 134; asm: 106
file content (378 lines) | stat: -rw-r--r-- 9,939 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#if HAVE_CONFIG_H
#   include "config.h"
#endif

#if HAVE_STDIO_H
#   include <stdio.h>
#endif
#if HAVE_MATH_H
#   include <math.h>
#endif
#include <stdlib.h>

#include "ga.h"
#include "macdecls.h"
#include "mp3.h"

#define DIM 2
#define DIMSIZE 1024
#define SIZE DIMSIZE/2
#define MAX_FACTOR 256

void grid_factor(int p, int *idx, int *idy) {
  int i, j;                              
  int ip, ifac, pmax;                    
  int prime[MAX_FACTOR];                 
  int fac[MAX_FACTOR];                   
  int ix, iy;                            
  int ichk;                              

  i = 1;

 //find all prime numbers, besides 1, less than or equal to p
  ip = p;

  pmax = 0;
  for (i=2; i<=ip; i++) {
    ichk = 1;
    for (j=0; j<pmax; j++) {
      if (i%prime[j] == 0) {
        ichk = 0;
        break;
      }
    }
    if (ichk) {
      pmax = pmax + 1;
      if (pmax > MAX_FACTOR) printf("Overflow in grid_factor\n");
      prime[pmax-1] = i;
    }
  }

 //find all prime factors of p
  ip = p;
  ifac = 0;
  for (i=0; i<pmax; i++) {
    while(ip%prime[i] == 0) {
      ifac = ifac + 1;
      fac[ifac-1] = prime[i];
      ip = ip/prime[i];
    }
  }

 //when p itself is prime
  if (ifac==0) {
    ifac++;
    fac[0] = p;
  }


 //find two factors of p of approximately the same size
  *idx = 1;
  *idy = 1;
  for (i = ifac-1; i >= 0; i--) {
    ix = *idx;
    iy = *idy;
    if (ix <= iy) {
      *idx = fac[i]*(*idx);
    } else {
      *idy = fac[i]*(*idy);
    }
  }
}

int main(int argc, char **argv) {

  int g_a, g_b, g_c;
  int one = 1;
  int rank, nprocs, kdim;
  int i, j, k;
  int ipx, ipy;
  int pdx, pdy;
  int xdim, ydim;
  int xbl, ybl;
  int xcdim, ycdim;
  int xnbl, ynbl;
  int xcnt, ycnt;
  int nb, ind, istart;
  int local_test;
  int ldtest = 5;
  int *local_A = NULL;
  int *local_B = NULL;
  double delta_t;
  int test;
  int dimsize;

  MPI_Init(&argc, &argv);

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  MA_init(C_INT, 1000, 1000);
  GA_Initialize();

  nprocs = GA_Nnodes();  
  rank = GA_Nodeid();   

  for (test = 0; test <= 1; test++) {
    for (dimsize = 4; dimsize <=4096; dimsize *= 2){
      if (rank == 0) fprintf(stderr,"dim:%d start\n",dimsize);

      int *abuf, *bbuf, *cbuf, *ctest;
      int full_size = dimsize*dimsize;
      int alo[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int ahi[DIM] = {dimsize-1,dimsize-1};
      int blo[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int bhi[DIM] = {dimsize-1,dimsize-1};
      int clo[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int chi[DIM] = {dimsize-1,dimsize-1};
      int loC[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int hiC[DIM] = {dimsize-1,dimsize-1};
      int loA[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int hiA[DIM] = {dimsize-1,dimsize-1};
      int loB[DIM] = {dimsize-dimsize,dimsize-dimsize};
      int hiB[DIM] = {dimsize-1,dimsize-1};
      int dims[DIM]={dimsize,dimsize};
      int ldb = hiB[0]-loB[0]+1;
      int ldc = hiC[0]-loC[0]+1;
      int lda = hiA[0]-loA[0]+1;
      int nlo, ldC;

      char* read_only = "read_only";
      char* read_cache = "read_cache";

      //subarray variables
      int sub_size;
      int *fullarray_test = (int*)malloc(full_size*sizeof(int));
      int *subarray_test;
      int loS[DIM], hiS[DIM];
      int lds;

      local_A=(int*)malloc(full_size*sizeof(int));
      for (i=0;i<full_size;i++){
        *(local_A + i) = i;
      }

      local_B=(int*)malloc(full_size*sizeof(int));
      for (i=0; i<full_size; i++){
        *(local_B + i) = i;
      }

      g_a = NGA_Create(C_INT, DIM, dims, "array_A", NULL);
      g_b = NGA_Create(C_INT, DIM, dims, "array_B", NULL);
      g_c = NGA_Create(C_INT, DIM, dims, "array_C", NULL);

      //fill GAs a and b with values to be multipled
      GA_Zero(g_a);
      GA_Zero(g_b);
      GA_Zero(g_c);

      NGA_Put(g_a, alo, ahi, local_A, &lda);
      NGA_Put(g_b, blo, bhi, local_B, &ldb);

      GA_Sync();
 
      if (test == 0) {
      NGA_Set_property(g_a, read_only);
      NGA_Set_property(g_b, read_only);
      }      
      if (test == 1) {
        NGA_Set_property(g_a, read_cache);
        NGA_Set_property(g_b, read_cache);
      }

      GA_Sync();

      delta_t = GA_Wtime();

      grid_factor(nprocs, &pdx, &pdy);

      //coordinates of processor for grid
      ipx = rank%pdx;
      ipy = (rank-ipx)/pdx; 
      xdim = dimsize;
      ydim = dimsize;

      if (dimsize <= 64) {
        xbl = dimsize/2;
        ybl = dimsize/2;
      } else {
        xbl = dimsize/2;
        ybl = dimsize/2;
      }

      //total number of blocks on each dimension
      xnbl = xdim/xbl;
      ynbl = ydim/ybl;
      if ((xnbl * xbl) < xdim) xnbl++;
      if ((ynbl * ybl) < ydim) ynbl++;

      xcnt = ipx;
      ycnt = ipy;

      GA_Sync();

      while (ycnt < ynbl) {
        int num_blocks, offset;
        int elemsize, ld;
        int *a_buf = NULL;
        int *b_buf = NULL;
        int *c_buf = NULL;
        int test_buf;
        int size_a, size_b, size_c;

        loC[0] = xcnt * xbl;
        loC[1] = ycnt * ybl;
        hiC[0] = (xcnt + 1) * xbl - 1;
        hiC[1] = (ycnt + 1) * ybl - 1;

        if (hiC[0] >= xdim) hiC[0] = xdim - 1;
        if (hiC[0] >= ydim) hiC[0] = ydim - 1;

	// stop to avoid FPE
	if ( hiC[0] < loC[0] ){
	  fprintf(stderr,"%d: bad values for HiC LoC: %d  %d\n", rank, hiC[0], loC[0]);
	  fprintf(stderr,"%dL xcnt xbl: %d  %d\n", rank, xcnt, xbl);
	  GA_Error("cache_test: div by zero", 0);
	}
        // Calculating number of blocks for inner dimension
        num_blocks = dimsize/(hiC[0]-loC[0]+1);
        if (num_blocks*(hiC[0]-loC[0]+1) < dimsize) num_blocks++;

        //set up buffers
        offset = 0;
        elemsize = sizeof(int);

        c_buf = (void*)malloc((hiC[0]-loC[0]+1)*(hiC[1]-loC[1]+1)*elemsize);
        a_buf = (void*)malloc((hiC[0]-loC[0]+1)*(hiC[1]-loC[1]+1)*elemsize);
        b_buf = (void*)malloc((hiC[0]-loC[0]+1)*(hiC[1]-loC[1]+1)*elemsize);
        
        test_buf = (hiC[0]-loC[0]+1)*(hiC[1]-loC[1]+1)*elemsize;
        size_c = (hiC[0]-loC[0]+1)*(hiC[1]-loC[1]+1);
        ldC = hiC[1]-loC[1]+1;
        
        // calculate starting block index
        istart = (loC[0]-clo[0])/(hiC[0]-loC[0]+1);
        
        // loop over block pairs
        for (nb=0; nb<num_blocks; nb++) {
          
          ind = istart + nb;
          ind = ind%num_blocks;
          
          nlo = alo[1]+ind*(hiC[0]-loC[0]+1);
          loA[0] = loC[0];
          hiA[0] = hiC[0];
          loA[1] = nlo;
          hiA[1] = loA[1]+(hiC[0]-loC[0]);
          if (hiA[1] > ahi[1]) hiA[1] = ahi[1];
          ld = hiA[0]-loA[0]+1;
          size_a = (hiA[0]-loA[0]+1)*(hiA[1]-loA[1]+1);
          
          NGA_Get(g_a,loA,hiA,a_buf,&ld);       
          
          if (dimsize > 4) {
          sub_size = (hiC[0]-loC[0]-1)*(hiC[1]-loC[1]-1);
          subarray_test = (int*)malloc(sub_size*sizeof(int)); 
          
          loS[0] = loA[0]+1;
          loS[1] = loA[1]+1;
          hiS[0] = hiA[0]-1;
          hiS[1] = hiA[1]-1;
          lds = hiS[0]-loS[0]+1;

          NGA_Get(g_a,loS,hiS,subarray_test,&lds);
          }          

          loB[1] = loC[1];
          hiB[1] = hiC[1];
          nlo = blo[0]+ind*(hiC[0]-loC[0]+1);
          loB[0] = nlo;
          hiB[0] = loB[0]+(hiC[0]-loC[0]);
          if (hiB[0] > bhi[0]) hiB[0] = bhi[0];
          ld = hiB[0]-loB[0]+1;
          size_b = (hiB[0]-loB[0]+1)*(hiB[1]-loB[1]+1);        

          NGA_Get(g_b,loB,hiB,b_buf,&ld);

          xcdim = hiC[0] - loC[0] + 1;
          ycdim = hiC[1] - loC[1] + 1;

          for (i = 0; i < xcdim; i++) {
            for (j = 0; j < ycdim; j++) {
              c_buf[i*ycdim+j] = 0.0;
            }
          }
          //transpose B to reduce page faults
          kdim = hiA[1] - loA[1] + 1;
          for (i = 0; i < xcdim; i++) {
            for (j = 0; j < ycdim; j++) {
              for (k = 0; k < kdim; k++) {
                c_buf[i*ycdim+j] += a_buf[i*kdim+k]*b_buf[k*ycdim+j];
              }
            }
          }

          NGA_Acc(g_c, loC, hiC, c_buf, &ldC, &one);

        }
         
        // multiplication is done, free buffers 
        free(a_buf);
        free(b_buf);
        free(c_buf);

        xcnt += pdx;

        if (xcnt >= xnbl) {
          xcnt = ipx;
          ycnt += pdy;
        }
      }

      delta_t = GA_Wtime()-delta_t;      
      if (dimsize == 0 & rank == 0) printf("\n"); 
      if (test == 0 && rank == 0) printf("READ  - DIMSIZE: %5d  Time (us): %7.4f\n",dimsize,delta_t*1.0e6);
      if (test == 1 && rank == 0) printf("CACHE - DIMSIZE: %5d  Time (us): %7.4f\n",dimsize,delta_t*1.0e6);

      GA_Sync();
      NGA_Unset_property(g_a);
      NGA_Unset_property(g_b);

#if 0 
      /* check multipy for correctness */
      abuf = (int*)malloc(sizeof(int)*dimsize*dimsize);
      bbuf = (int*)malloc(sizeof(int)*dimsize*dimsize);
      cbuf = (int*)malloc(sizeof(int)*dimsize*dimsize);
      ctest = (int*)malloc(sizeof(int)*dimsize*dimsize);
      /* get data from matrices a, b, c */
      NGA_Get(g_a,alo,ahi,abuf,&dimsize);
      NGA_Get(g_b,blo,bhi,bbuf,&dimsize);
      NGA_Get(g_c,clo,chi,ctest,&dimsize);
      for (i=0; i<dimsize; i++) {
        for (j=0; j<dimsize; j++) {
          cbuf[i*dimsize+j] = 0;
          for (k=0; k<dimsize; k++) {
            cbuf[i*dimsize+j] += abuf[i*dimsize+k]*bbuf[k*dimsize+j];
          }
          if (cbuf[i*dimsize+j] != ctest[i*dimsize+j] && rank == 0) {
            printf("mismatch for pair [%d,%d] expected: %d actual: %d\n",i,j,
                cbuf[i*dimsize+j],ctest[i*dimsize+j]);
          }
        }
      }
      free(abuf);
      free(bbuf);
      free(cbuf);
      free(ctest);
#endif

      GA_Destroy(g_a);
      GA_Destroy(g_b);
      GA_Destroy(g_c);
      free(local_A);
      free(local_B);     
    }
  }
  GA_Terminate();
  MPI_Finalize();
}