File: mulmatpatch.F

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 (418 lines) | stat: -rw-r--r-- 11,535 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#if HAVE_CONFIG_H
#   include "config.fh"
#endif
c $Id: mulmatpatch.F,v 1.6 2005-11-23 10:25:18 manoj Exp $
#define xgemm TEST_DGEMM
#define ygemm TEST_ZGEMM
# define THRESH  1.0d-20
#define MISMATCH(x,y) abs(x-y)/max(1,abs(x)).gt.THRESH
#define NMAX 10
c
c
      program test
      implicit none
#include "mafdecls.fh"
#include "global.fh"
      logical status
      integer bufsize, gasize
c
c***  Initialize a message passing library
c
#include "mp3.fh"
c
      call ga_initialize()
      if(ga_nodeid().eq.0)then
         print *,' GA initialized'
         call ffflush(6)
      endif
c
c     we want to force distribution of innermost loop in nga_mulmat_patch
c     by providing less buffer memory than needed
c
      if(ga_uses_ma()) then
              gasize = (NMAX**4 * 3)/ga_nnodes()
      else
              gasize =0
      endif
      bufsize = (NMAX/2 + 1)*(NMAX/3 + 1)*2 + ((NMAX/2 + 1)**2)
      bufsize = bufsize*6/7
      status = ma_init(MT_DBL, 10, gasize+bufsize+500000)
      if (.not. status)call ga_error( 'ma_init failed', -1)
      if(ga_nodeid().eq.0)then
         print *,' '
         print *,'CHECKING MATRIX MULTIPLICATION FOR PATCHES '
c        print *, 'gasize and bufsize are ', gasize, bufsize
         print *,' '
      endif
      call dpatch_test()
      call dpatch_test2()
c
      if(ga_nodeid().eq.0) print *,'All tests successful '
c      
      call ga_terminate()
c
      call MP_FINALIZE()
      end



      subroutine dpatch_test()
      implicit none
#include "mafdecls.fh"
#include "global.fh"
#include "testutil.fh"
c
      integer n,m, dim, dim1
      parameter (n = NMAX)
      parameter (m = n*n)
      parameter (dim = 4)
      parameter (dim1 = 3)
      double precision a(n,n), b(n,n), c(n,n), buf(m), val
      double precision alpha, beta
      integer nproc, me 
      integer i, j, ailo, ajlo, bilo, bjlo, base, iran
      integer alo(dim), ahi(dim), ald(dim1), chunk(dim)
      integer blo(dim), bhi(dim)
      integer clo(dim), chi(dim)
      integer bufld(dim)
      integer g_a1, g_b1, g_c1 
      integer rows, cols, loop
      logical status, dist_same 
      iran(i) = int(drand(0)*real(i)) + 1
      dist_same = .false.
c
      me = ga_nodeid()
      nproc = ga_nnodes()
c
      do j = 1, n
         do i = 1, n
            a(i,j) = i-1 + (j-1)*n
            b(i,j) = i+j 
         enddo
      enddo
c
      ailo = iran(n/2)
      ajlo = iran(n/2)
      bilo = iran(n/2)
      bjlo = iran(n/2)
c
      do i = 1, dim
         alo(i) = 1
         ahi(i) = n
         chunk(i) = -1
         if (i.ne.dim) ald(i) = n
      enddo
c
      status = nga_create(MT_DBL, dim, ahi, 'a1', chunk, g_a1)
      if (.not. status) then
         write(6,*) ' nga_create failed'
         call ffflush(6)
         call ga_error('... exiting ',0)
      endif
c
      if(dist_same) then
         status = ga_duplicate(g_a1, g_b1, 'a1_duplicated')
         if(.not.ga_compare_distr(g_a1, g_b1))
     $           call ga_error("g_b1 distribution different",0) 
         status = ga_duplicate(g_a1, g_c1, 'a1_duplicated_again')
         if(.not.ga_compare_distr(g_a1, g_c1))
     $           call ga_error("g_c1 distribution different",0) 
      else
         chunk(dim) = n
         status = nga_create(MT_DBL, dim, ahi, 'b1', chunk, g_b1)
         if (.not. status) call ga_error('nga_create failed:b1',0) 
         chunk(dim) = 0
         chunk(dim1) = n
         status = nga_create(MT_DBL, dim, ahi, 'c1', chunk, g_c1)
         if (.not. status) call ga_error('nga_create failed:c1',0) 
      endif
c
      if (me .eq. 0) then
         print *, ' '
         write(6,*)'> Checking nga_matmul_patch ... '
         call ffflush(6)
      endif
c
      alo(1) = 1
      ahi(1) = n
      blo(1) = 1
      bhi(1) = n
      bufld(1) = n
      do j = 2, dim-1
        alo(j) = j
        ahi(j) = j
        blo(j) = j
        bhi(j) = j
        clo(j) = j
        chi(j) = j
        bufld(j) = 1
      enddo
      do j = 1+me, n, nproc 
         alo(dim) = j
         ahi(dim) = j
         blo(dim) = j
         bhi(dim) = j
         call nga_put(g_a1,alo,ahi,a(1,j),bufld) 
         call nga_put(g_b1,blo,bhi,b(1,j),bufld)
      enddo
c
      call ga_sync()
      alo(1) = ailo
      ahi(1) = ailo+1
c     ahi(1) = ailo+n/2
      alo(dim) = ajlo
      ahi(dim) = ajlo
c     ahi(dim) = ajlo+n/3
      blo(1) = bilo
      bhi(1) = bilo
c     bhi(1) = bilo+n/3
      blo(dim) = bjlo
      bhi(dim) = bjlo
c     bhi(dim) = bjlo+n/2
      clo(1) = bilo
      chi(1) = bilo+1
c     chi(1) = bilo+n/2
      clo(dim) = bjlo
      chi(dim) = bjlo
c     chi(dim) = bjlo+n/2
c
      call nga_matmul_patch('n','n', 1d0, 0d0, 
     $                      g_a1, alo, ahi,
     $                      g_b1, blo, bhi,
     $                      g_c1, clo, chi)
      call xgemm('n','n',2,2,1,1d0,a(ailo,ajlo), n,
     $            b(bilo,bjlo),n, 0d0, c, n)
c     call xgemm('n','n',n/2+1,n/2+1,n/3+1,1d0,a(ailo,ajlo), n,
c    $            b(bilo,bjlo),n, 0d0, c, n)
c
      bufld(1) = n/2 + 1
      call nga_get(g_c1,clo,chi,buf,bufld)
      base = 0
      do j = 1, 1
         if(Mod(j,nproc).eq.me) then
            do i = 1, 2
               base = base+1
               if(ABS(buf(base)- c(i,j)).gt.1d-8) then
                  print *,me, ' error ',i,j, buf(base), c(i,j)
                  call ga_error('exiting ...',0)
               endif
            enddo
         else
            base = base +n/2+1
         endif
      enddo
c
      call ga_sync()
      if (me .eq. 0) then
         write(6,*)'  a*b: OK '
         call ffflush(6)
      endif
c
      call ga_sync()
      alo(1) = ailo
      ahi(1) = ailo+n/2
      alo(dim) = ajlo
      ahi(dim) = ajlo+n/3
      blo(1) = bilo
      bhi(1) = bilo+n/3
      blo(dim) = bjlo
      bhi(dim) = bjlo+n/2
      clo(1) = bilo
      chi(1) = bilo+n/2
      clo(dim) = bjlo
      chi(dim) = bjlo+n/2
c
      call nga_matmul_patch('n','n', 1d0, 0d0, 
     $                      g_a1, alo, ahi,
     $                      g_b1, blo, bhi,
     $                      g_c1, clo, chi)
      call xgemm('n','n',n/2+1,n/2+1,n/3+1,1d0,a(ailo,ajlo), n,
     $            b(bilo,bjlo),n, 0d0, c, n)
c
      bufld(1) = n/2 + 1
      call nga_get(g_c1,clo,chi,buf,bufld)
      base = 0
      do j = 1, 1+n/2
         if(Mod(j,nproc).eq.me) then
            do i = 1, 1+n/2
               base = base+1
               if(ABS(buf(base)- c(i,j)).gt.1d-8) then
                  print *,me, ' error ',i,j, buf(base), c(i,j)
                  call ga_error('exiting ...',0)
               endif
            enddo
         else
            base = base +n/2+1
         endif
      enddo
c
      call ga_sync()
      if (me .eq. 0) then
         write(6,*)'  a*b: OK '
         call ffflush(6)
      endif
c
      call ga_sync()
      call nga_matmul_patch('t','n', 1d0, 0d0,
     $                      g_a1, alo, ahi,
     $                      g_b1, blo, bhi,
     $                      g_c1, clo, chi)
      call xgemm('t','n',n/2+1,n/2+1,n/3+1,1d0,a(ajlo,ailo), n,
     $            b(bilo,bjlo),n, 0d0, c, n)
      call nga_get(g_c1,clo,chi,buf,bufld)
      base = 0
      do j = 1, 1+n/2
         if(Mod(j,nproc).eq.me) then
            do i = 1, 1+n/2
               base = base+1
               if(ABS(buf(base)- c(i,j)).gt.1d-8) then
                  print *,me, ' error ',i,j, buf(base), c(i,j)
                  call ga_error('exiting ...',0)
               endif
            enddo
         else
            base = base +n/2+1
         endif
      enddo
c
      call ga_sync()
      if (me .eq. 0) then
         write(6,*)'  trans(a)*b: OK '
         call ffflush(6)
      endif
c
      call ga_sync()
      call nga_matmul_patch('n','t', 1d0, 0d0,
     $                      g_a1, alo, ahi,
     $                      g_b1, blo, bhi,
     $                      g_c1, clo, chi)
      call xgemm('n','t',n/2+1,n/2+1,n/3+1,1d0,a(ailo,ajlo), n,
     $            b(bjlo,bilo),n, 0d0, c, n)
      call nga_get(g_c1,clo,chi,buf,bufld)
      base = 0
      do j = 1, 1+n/2
         if(Mod(j,nproc).eq.me) then
            do i = 1, 1+n/2
               base = base+1
               if(ABS(buf(base)- c(i,j)).gt.1d-8) then
                  print *,me, ' error ',i,j, buf(base), c(i,j)
                  call ga_error('exiting ...',0)
               endif
            enddo
         else
            base = base +n/2+1
         endif
      enddo
c
      call ga_sync()
      if (me .eq. 0) then
         write(6,*)'  a*trans(b): OK '
         call ffflush(6)
      endif
c
      call nga_matmul_patch('t','t', 1d0, 0d0,
     $                      g_a1, alo, ahi,
     $                      g_b1, blo, bhi,
     $                      g_c1, clo, chi)
      call xgemm('t','t',n/2+1,n/2+1,n/3+1,1d0,a(ajlo,ailo), n,
     $            b(bjlo,bilo),n, 0d0, c, n)
      call nga_get(g_c1,clo,chi,buf,bufld)
      base = 0
      do j = 1, 1+n/2
         if(Mod(j,nproc).eq.me) then
            do i = 1, 1+n/2
               base = base+1
               if(ABS(buf(base)- c(i,j)).gt.1d-8) then
                  print *,me, ' error ',i,j, buf(base), c(i,j)
                  call ga_error('exiting ...',0)
               endif
            enddo
         else
            base = base +n/2+1
         endif
      enddo
c
      call ga_sync()
      if (me .eq. 0) then
         write(6,*)'  trans(a)*trans(b): OK '
         call ffflush(6)
      endif
c
      status = ga_destroy(g_a1)
      status = status .and. ga_destroy(g_b1)
      status = status .and. ga_destroy(g_c1)
      if(.not. status) print *, 'ga_destroy failed'
c
      end


      subroutine dpatch_test2()
      implicit none
#include "mafdecls.fh"
#include "global.fh"
#include "testutil.fh"
      integer n,m, dim, dim1
      parameter (n = 10)
      parameter (dim = 3)
      parameter (dim1 = 2)
      double precision alpha, beta, dot
      integer nproc, me
      integer i, j, ailo, ajlo, bilo, bjlo, base, iran
      integer alo(dim), ahi(dim), ald(dim1), chunk(dim)
      integer blo(dim), bhi(dim)
      integer clo(dim), chi(dim)
      integer g_a, g_b, g_c
      logical status
c
      me = ga_nodeid()
      nproc = ga_nnodes()
c
      do i = 1, dim
         chunk(i) = -1
         chi(i) =n
         alo(i) =1
         blo(i) =1
         clo(i) =1
         if (i.ne.dim) ald(i) = n
      enddo
c
      ahi(1)=n
      ahi(2)=1
      status = nga_create(MT_DBL, dim1, ahi, 'a', chunk, g_a)
      if(.not. status) call ga_error('create 1 failed',0)
c
      status = nga_create(MT_DBL, dim1, ahi, 'b', chunk, g_b)
      if(.not. status) call ga_error('create 2 failed',0)
c
      status = nga_create(MT_DBL, dim, chi, 'c', chunk, g_c)
      if(.not. status) call ga_error('create 2 failed',0)
c
      call ga_fill(g_a,1d0)
      call ga_fill(g_b,1d0)
      call ga_zero(g_c)

      if(me.eq.0) print *,'  [1:N,1:N,1:1]=[1,N]x[N,1]^t'
      chi(dim) = 1
      bhi(1)=1
      bhi(2)=n
      call nga_matmul_patch('n','t', 1d0, 0d0,
     $                      g_a, alo, ahi,
     $                      g_b, blo, bhi,
     $                      g_c, clo, chi)
      
c
c     call nga_print_patch(g_c,clo,chi)
      dot = nga_ddot_patch(g_c,'n', clo, chi, g_c,'n', clo, chi)
      if(abs(dot- 1d0*n*n) .gt. 0.01) then
        print *, 'error ', dot, n*n
      else
        if(me.eq.0)print *,'  OK'
      endif
      status = ga_destroy(g_a)
      status = status .and. ga_destroy(g_b)
      status = status .and. ga_destroy(g_c)
      if(.not. status) print *, 'ga_destroy failed'
c
      end