-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmultiply_kernel_ompDojk.f90
More file actions
462 lines (446 loc) · 17.5 KB
/
multiply_kernel_ompDojk.f90
File metadata and controls
462 lines (446 loc) · 17.5 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
! -*- mode: F90; mode: font-lock -*-
! ------------------------------------------------------------------------------
! $Id$
! ------------------------------------------------------------------------------
! Module multiply_kernel
! ------------------------------------------------------------------------------
! Code area 2: matrices
! ------------------------------------------------------------------------------
!!****h* Conquest/multiply_kernel
!! NAME
!! multiply_kernel
!! PURPOSE
!! Contains the matrix multiplication kernel subroutines used for
!! Conquest matrix multiplication. This is one of the hottest part
!! of the code.
!! |---------------------------------|
!! | This is the version with OpenMP |
!! |---------------------------------|
!! AUTHOR
!! L.Tong
!! CREATION DATE
!! 2012/08/28
!! MODIFICATION HISTORY
!! SOURCE
!!
module multiply_kernel
!!*****
contains
!!****f* multiply_kernel/m_kern_max *
!!
!! NAME
!! m_kern_max
!! USAGE
!!
!! PURPOSE
!! multiplication kernel for maximal case and reductions thereof.
!!
!! nah_part: the number of atoms in the A-halo that are contained
!! in the current partition K.
!!
!! kseq(k): the unpruned sequence number of atom k in the current
!! partition. `Unpruned' means that the sequence
!! number counts all atoms in the partition, and not just
!! those in the A-halo. The atom in question is specified
!! by its `pruned' sequence number k.
!!
!! k_in_part: temporary variable for current value of kseq(k).
!!
!! k_halo(.): the A-halo sequence number of atom k. The latter is
!! specified by its partition number and its pruned
!! sequence number k in that partition.
!!
!! k_in_halo: temporary variable for current value of k_halo(.)
!!
!! kbeg(kpart): specifies address in k_halo(.) for start of information
!! about atoms in partition kpart. The label kpart
!! goes over all partitions containing atoms in the
!! A-halo.
!!
!! kpart: A-halo seq. no. of current partition K
!!
!! nahnab(k_in_halo):
!! number of atoms in primary set that are A-neighbours of
!! a given atom in the A-halo, the latter being given by its
!! A-halo sequence number k_in_halo.
!!
!! i_prim(.): sequence number of atom i in primary set, this
!! atom being specified as ith neighbour of atom
!! k in the A-halo
!!
!! ibeg(k_in_halo):
!! address in i_prim(.) where index data for atom k
!! start, the latter being specified by k_in_halo.
!!
!! iaaddr(k_in_halo):
!! address in array a where data for atom k start,
!! the latter being specified by k_in_halo.
!!
!! nbnab(k_in_part):
!! number of B-neighbours of atom k, the latter being
!! specified by its (unpruned) seq. no. in partition K.
!!
!! ibaddr(k_in_part):
!! address in array b where data for atom k start.
!!
!! jch2cad(.): address in array c where data for (i,j) pair start.
!!
!! icad: address in jch2cad where info for atom i starts.
!!
!! jbnab2ch(.): the C-halo seq. no. of atom j, the latter being
!! specified as the jth B-neighbour of atom k.
!!
!! ni_in_chalo: total number of atoms in the C-halo.
!!
!! ibpart(.): partition to which each atom j belongs, the latter
!! being specified as jth B-neighbour of atom k.
!!
!! ibseq(.): unpruned sequence number in partition for each
!! atom j, the latter being specified as in ibpart.
!!
!! ibindaddr(k_in_part):
!! address in arrays ibpart(.) and ibseq(.) where
!! data for atom k start.
!!
!! k_off: offset in partition labelling to account for p.b.c.
!!
!! j_halo(.): C-halo sequence number of atom j, the latter being
!! specified in partition labelling.
!!
!! jchbeg(jpart): address in array j_halo(.) where data for
!! partition jpart start.
!! INPUTS
!!
!!
!! USES
!!
!! AUTHOR
!! M.J.Gillan/D.R.Bowler
!! CREATION DATE
!! 25/11/99
!! MODIFICATION HISTORY
!! 20/06/2001 dave
!! Added ROBODoc header
!! 2012/07/05 L.Tong
!! - Added OpenMP directives
!! 2012/08/02 L.Tong
!! - Realsed that it is not possible to achieve maximum
!! efficiency with one piece of code for both OpenMP and standard
!! versions, so decided to use C preprocessors. The preprocessors
!! are supported by all major compilers and should not pose much
!! problem on different platiforms.
!! - The standard code uses less automatic arrays for indexing.
!! But for OpenMP calculations, extra indexing arrays has to be
!! allocated for different threads. While automatic arrays uses
!! stack and allocating and deallocating should be efficient,
!! memory allocations for this hot part of the calculation
!! should be reduced to minimum. Hence the original indexing
!! method in the routine is kept for non-OpenMP versions, while
!! a new indexing method with extra indexing arrays is used for
!! OpenMP version.
!! - The preprocessor variable is OMP, if OMP is defined, use
!! OpenMP version, otherwise use standard
!! SOURCE
!!
subroutine m_kern_max(k_off, kpart, ib_nd_acc, ibaddr, nbnab, &
ibpart, ibseq, bndim2, a, b, c, ahalo, chalo, &
at, mx_absb, mx_part, mx_iprim, lena, lenb, &
lenc, debug)
use datatypes
use matrix_module
use basic_types, only: primary_set
use primary_module, only: bundle
implicit none
! Passed variables
type(matrix_halo) :: ahalo, chalo
type(matrix_trans) :: at
integer :: mx_absb, mx_part, mx_iprim, lena, lenb, lenc
integer :: kpart, k_off
real(double) :: a(lena)
real(double) :: b(lenb)
real(double) :: c(lenc)
integer, optional :: debug
! Remote indices
integer(integ), intent(in) :: ib_nd_acc(:)
integer(integ), intent(in) :: ibaddr(:)
integer(integ), intent(in) :: nbnab(:)
integer(integ), intent(in) :: ibpart(:)
integer(integ), intent(in) :: ibseq(:)
integer(integ), intent(in) :: bndim2(:)
! Local variables
integer :: jbnab2ch(mx_absb) ! Automatic array
integer :: nbkbeg, k, k_in_part, k_in_halo, j, jpart, jseq
integer :: i, nabeg, i_in_prim, icad, nbbeg, j_in_halo, ncbeg
integer :: n1, n2, n3, nb_nd_kbeg
integer :: nd1, nd2, nd3
integer :: naaddr, nbaddr, ncaddr
! OpenMP required indexing variables
integer :: nd1_1st(at%mx_halo), nd2_1st(mx_absb)
! Loop over atoms k in current A-halo partn
do k = 1, ahalo%nh_part(kpart)
k_in_halo = ahalo%j_beg(kpart) + k - 1
k_in_part = ahalo%j_seq(k_in_halo)
nbkbeg = ibaddr(k_in_part)
nb_nd_kbeg = ib_nd_acc(k_in_part)
nd3 = ahalo%ndimj(k_in_halo)
! if(PRESENT(debug)) write(21+debug,*) 'Details1: ',k,nb_nd_kbeg
! for OpenMP sub-array indexing
nd1_1st(1) = 0
do i = 2, at%n_hnab(k_in_halo)
i_in_prim = at%i_prim(at%i_beg(k_in_halo)+i-2)
nd1_1st(i) = nd1_1st(i-1) + nd3 * ahalo%ndimi(i_in_prim)
end do
nd2_1st(1) = 0
do j = 2, nbnab(k_in_part)
nd2_1st(j) = nd2_1st(j-1) + nd3 * bndim2(nbkbeg+j-2)
end do
! transcription of j from partition to C-halo labelling
do j = 1, nbnab(k_in_part)
jpart = ibpart(nbkbeg+j-1) + k_off
jseq = ibseq(nbkbeg+j-1)
jbnab2ch(j) = chalo%i_halo(chalo%i_hbeg(jpart)+jseq-1)
end do
! Loop over primary-set A-neighbours of k
do i = 1, at%n_hnab(k_in_halo)
! nabeg = at%i_beg(k_in_halo) + i - 1
i_in_prim = at%i_prim(at%i_beg(k_in_halo)+i-1)
nd1 = ahalo%ndimi(i_in_prim)
icad = (i_in_prim-1) * chalo%ni_in_halo
! nabeg index for openMP is calculated inside loop, here
nabeg = at%i_nd_beg(k_in_halo) + nd1_1st(i)
!$omp do schedule(runtime)
! Loop over B-neighbours of atom k
do j = 1, nbnab(k_in_part)
! nbbeg = nbkbeg + j - 1
nd2 = bndim2(nbkbeg+j-1)
nbbeg = nb_nd_kbeg + nd2_1st(j)
j_in_halo = jbnab2ch(j)
if (j_in_halo /= 0) then
ncbeg = chalo%i_h2d(icad+j_in_halo)
!nd2 = chalo%ndimj(j_in_halo)
if (ncbeg /= 0 ) then ! multiplication of ndim x ndim blocks
! if (PRESENT(debug)) &
! write (21+debug, *) 'Details2: ', j, nd2, &
! (nabeg-1)/(nd1*nd3), (ncbeg-1)/(nd1*nd2), &
! (nbbeg-1)/(nd2*nd3)
!DIR$ NOPATTERN
do n2 = 1, nd2
nbaddr = nbbeg + nd3 * (n2 - 1)
ncaddr = ncbeg + nd1 * (n2 - 1)
do n1 = 1, nd1
naaddr = nabeg + nd3 * (n1 - 1)
do n3 = 1, nd3
c(ncaddr+n1-1) = c(ncaddr+n1-1) + &
a(naaddr+n3-1) * b(nbaddr+n3-1)
end do
end do
end do
end if
end if ! End of if(j_in_halo.ne.0)
end do ! End of j = 1, nbnab
!$omp end do
end do ! End of i = 1, at%n_hnab
end do ! End of k = 1, nahpart
return
end subroutine m_kern_max
!!*****
!!****f* multiply_kernel/m_kern_min *
!!
!! NAME
!! m_kern_min
!! USAGE
!!
!! PURPOSE
!! multiplication kernel for minimal case and extensions thereof.
!!
!! nah_part: the number of atoms in the A-halo that are contained
!! in the current partition K.
!!
!! kseq(k): the unpruned sequence number of atom k in the current
!! partition. `Unpruned' means that the sequence
!! number counts all atoms in the partition, and not just
!! those in the A-halo. The atom in question is specified
!! by its `pruned' sequence number k.
!!
!! k_in_part: temporary variable for current value of kseq(k).
!!
!! k_halo(.): the A-halo sequence number of atom k. The latter is
!! specified by its partition number and its pruned
!! sequence number k in that partition.
!!
!! k_in_halo: temporary variable for current value of k_halo(.)
!!
!! kbeg(kpart): specifies address in k_halo(.) for start of information
!! about atoms in partition kpart. The label kpart
!! goes over all partitions containing atoms in the
!! A-halo.
!!
!! kpart: A-halo seq. no. of current partition K
!!
!! nahnab(k_in_halo):
!! number of atoms in primary set that are A-neighbours of
!! a given atom in the A-halo, the latter being given by its
!! A-halo sequence number k_in_halo.
!!
!! i_prim(.): sequence number of atom i in primary set, this
!! atom being specified as ith neighbour of atom
!! k in the A-halo
!!
!! ibeg(k_in_halo):
!! address in i_prim(.) where index data for atom k
!! start, the latter being specified by k_in_halo.
!!
!! iaaddr(k_in_halo):
!! address in array a where data for atom k start,
!! the latter being specified by k_in_halo.
!!
!! nbnab(k_in_part):
!! number of B-neighbours of atom k, the latter being
!! specified by its (unpruned) seq. no. in partition K.
!!
!! ibaddr(k_in_part):
!! address in array b where data for atom k start.
!!
!! jch2cad(.): address in array ! where data for (i,j) pair start.
!!
!! icad: address in jch2cad where info for atom i starts.
!!
!! jbnab2ch(.): the C-halo seq. no. of atom j, the latter being
!! specified as the jth B-neighbour of atom k.
!!
!! ni_in_chalo: total number of atoms in the C-halo.
!!
!! ibpart(.): partition to which each atom j belongs, the latter
!! being specified as jth B-neighbour of atom k.
!!
!! ibseq(.): unpruned sequence number in partition for each
!! atom j, the latter being specified as in ibpart.
!!
!! ibindaddr(k_in_part):
!! address in arrays ibpart(.) and ibseq(.) where
!! data for atom k start.
!!
!! k_off: offset in partition labelling to account for p.b.c.
!!
!! j_halo(.): C-halo sequence number of atom j, the latter being
!! specified in partition labelling.
!!
!! jchbeg(jpart): address in array j_halo(.) where data for
!! partition jpart start.
!! INPUTS
!!
!!
!! USES
!!
!! AUTHOR
!! M.J.Gillan/D.R.Bowler
!! CREATION DATE
!! 25/11/99
!! MODIFICATION HISTORY
!! 20/06/2001 dave
!! Added ROBODoc header
!! 2012/07/18 L.Tong
!! - Added OpenMP directives
!! 2012/08/02 L.Tong
!! - Added C preprocessors for switching between codes for openMP
!! and standard, this is done to get the best efficiency. See
!! the notes in m_kern_max for details.
!! SOURCE
!!
subroutine m_kern_min(k_off, kpart, ib_nd_acc, ibaddr, nbnab, &
ibpart, ibseq, bndim2, a, b, c, ahalo, chalo, &
at, mx_absb, mx_part, mx_iprim, lena, lenb, &
lenc)
use datatypes
use matrix_module
use basic_types, only: primary_set
use primary_module, only: bundle
implicit none
! Passed variables
type(matrix_halo) :: ahalo, chalo
type(matrix_trans) :: at
integer :: mx_absb, mx_part, mx_iprim, lena, lenb, lenc
integer :: kpart, k_off
! Remember that a is a local transpose
real(double) :: a(lena)
real(double) :: b(lenb)
real(double) :: c(lenc)
! dimension declarations
integer(integ), intent(in) :: ib_nd_acc(:)
integer(integ), intent(in) :: ibaddr(:)
integer(integ), intent(in) :: nbnab(:)
integer(integ), intent(in) :: ibpart(:)
integer(integ), intent(in) :: ibseq(:)
integer(integ), intent(in) :: bndim2(:)
! Local variables
integer :: jbnab2ch(mx_absb)
integer :: k, k_in_part, k_in_halo, nbkbeg, j, jpart, jseq
integer :: i, nabeg, i_in_prim, icad, nbbeg, j_in_halo, ncbeg
integer :: n1, n2, n3, nb_nd_kbeg
integer :: nd1, nd2, nd3
integer :: naaddr, nbaddr, ncaddr
! For OpenMP
integer :: nd1_1st(at%mx_halo), nd2_1st(mx_absb)
! Loop over atoms k in current A-halo partn
!$omp do schedule(runtime)
do k = 1, ahalo%nh_part(kpart)
k_in_halo = ahalo%j_beg(kpart) + k - 1
k_in_part = ahalo%j_seq(k_in_halo)
nbkbeg = ibaddr(k_in_part)
nb_nd_kbeg = ib_nd_acc(k_in_part)
nd3 = ahalo%ndimj(k_in_halo)
! for OpenMP sub-array indexing
nd1_1st(1) = 0
do i = 2, at%n_hnab(k_in_halo)
i_in_prim = at%i_prim(at%i_beg(k_in_halo)+i-2)
nd1_1st(i) = nd1_1st(i-1) + nd3 * ahalo%ndimi(i_in_prim)
end do
nd2_1st(1) = 0
do j = 2, nbnab(k_in_part)
nd2_1st(j) = nd2_1st(j-1) + nd3 * bndim2(nbkbeg+j-2)
end do
! transcription of j from partition to C-halo labelling
do j = 1, nbnab(k_in_part)
jpart = ibpart(nbkbeg+j-1) + k_off
jseq = ibseq(nbkbeg+j-1)
jbnab2ch(j) = chalo%i_halo(chalo%i_hbeg(jpart)+jseq-1)
end do
! Loop over primary-set A-neighbours of k
do i = 1, at%n_hnab(k_in_halo)
!nabeg=at%i_beg(k_in_halo)+i-1
i_in_prim = at%i_prim(at%i_beg(k_in_halo)+i-1)
nd1 = ahalo%ndimi(i_in_prim)
icad = (i_in_prim-1) * chalo%ni_in_halo
nabeg = at%i_nd_beg(k_in_halo) + nd1_1st(i)
! Loop over B-neighbours of atom k
do j = 1, nbnab(k_in_part)
!nbbeg = nbkbeg + j - 1
nd2 = bndim2(nbkbeg+j-1)
nbbeg = nb_nd_kbeg + nd2_1st(j)
j_in_halo = jbnab2ch(j)
if (j_in_halo /= 0) then
!nd2 = chalo%ndimj(j_in_halo)
ncbeg = chalo%i_h2d(icad+j_in_halo)
if (ncbeg /= 0) then ! multiplication of ndim x ndim blocks
!DIR$ NOPATTERN
do n2=1, nd2
nbaddr = nbbeg + nd3 * (n2 - 1)
ncaddr = ncbeg + nd1 * (n2 - 1)
do n1 = 1, nd1
naaddr = nabeg + nd3 * (n1 - 1)
do n3 = 1, nd3
a(naaddr+n3-1) = a(naaddr+n3-1) + &
c(ncaddr+n1-1) * b(nbaddr+n3-1)
end do
end do
end do
end if
end if
end do
end do
end do
!$omp end do
return
end subroutine m_kern_min
!!*****
end module multiply_kernel