-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdbcsr_tensor_index.F
398 lines (326 loc) · 15 KB
/
dbcsr_tensor_index.F
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
!--------------------------------------------------------------------------------------------------!
! Copyright (C) by the DBCSR developers group - All rights reserved !
! This file is part of the DBCSR library. !
! !
! For information on the license, see the LICENSE file. !
! For further information please visit https://dbcsr.cp2k.org !
! SPDX-License-Identifier: GPL-2.0+ !
!--------------------------------------------------------------------------------------------------!
MODULE dbcsr_tensor_index
!! tensor index and mapping to DBCSR index
USE dbcsr_allocate_wrap, ONLY: allocate_any
USE dbcsr_kinds, ONLY: int_8
#include "base/dbcsr_base_uses.f90"
#:include "dbcsr_tensor.fypp"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dbcsr_tensor_index'
PUBLIC :: &
combine_tensor_index, &
combine_pgrid_index, &
create_nd_to_2d_mapping, &
destroy_nd_to_2d_mapping, &
get_2d_indices_tensor, &
get_2d_indices_pgrid, &
dbcsr_t_get_mapping_info, &
get_nd_indices_tensor, &
get_nd_indices_pgrid, &
nd_to_2d_mapping, &
ndims_mapping, &
split_tensor_index, &
split_pgrid_index, &
ndims_mapping_row, &
ndims_mapping_column, &
dbcsr_t_inverse_order, &
permute_index
TYPE nd_to_2d_mapping
INTEGER :: ndim_nd = -1
INTEGER :: ndim1_2d = -1
INTEGER :: ndim2_2d = -1
INTEGER, DIMENSION(:), ALLOCATABLE :: dims_nd
INTEGER(KIND=int_8), DIMENSION(2) :: dims_2d = -1_int_8
INTEGER, DIMENSION(:), ALLOCATABLE :: dims1_2d
INTEGER, DIMENSION(:), ALLOCATABLE :: dims2_2d
INTEGER, DIMENSION(:), ALLOCATABLE :: map1_2d
INTEGER, DIMENSION(:), ALLOCATABLE :: map2_2d
INTEGER, DIMENSION(:), ALLOCATABLE :: map_nd
INTEGER :: base = -1
LOGICAL :: col_major = .FALSE.
END TYPE nd_to_2d_mapping
CONTAINS
SUBROUTINE create_nd_to_2d_mapping(map, dims, map1_2d, map2_2d, base, col_major)
!! Create all data needed to quickly map between nd index and 2d index.
TYPE(nd_to_2d_mapping), INTENT(OUT) :: map
!! index mapping data
INTEGER, DIMENSION(:), INTENT(IN) :: dims, map1_2d, map2_2d
!! nd sizes
!! which nd-indices map to first matrix index and in which order
!! which nd-indices map to second matrix index and in which order
INTEGER, INTENT(IN), OPTIONAL :: base
!! base index (1 for Fortran-style, 0 for C-style, default is 1)
LOGICAL, INTENT(IN), OPTIONAL :: col_major
!! whether index should be column major order (.TRUE. for Fortran-style, .FALSE. for C-style, default is .TRUE.).
INTEGER :: i
IF (PRESENT(col_major)) THEN
map%col_major = col_major
ELSE
map%col_major = .TRUE.
END IF
IF (PRESENT(base)) THEN
map%base = base
ELSE
map%base = 1
END IF
map%ndim1_2d = SIZE(map1_2d)
map%ndim2_2d = SIZE(map2_2d)
map%ndim_nd = SIZE(dims)
CALL allocate_any(map%map1_2d, source=map1_2d)
CALL allocate_any(map%map2_2d, source=map2_2d)
CALL allocate_any(map%dims_nd, source=dims)
CALL allocate_any(map%dims1_2d, source=dims(map1_2d))
CALL allocate_any(map%dims2_2d, source=dims(map2_2d))
ALLOCATE (map%map_nd(map%ndim_nd))
map%map_nd(map1_2d) = (/(i, i=1, SIZE(map1_2d))/)
map%map_nd(map2_2d) = (/(i + SIZE(map1_2d), i=1, SIZE(map2_2d))/)
map%dims_2d = [PRODUCT(INT(map%dims1_2d, KIND=int_8)), PRODUCT(INT(map%dims2_2d, KIND=int_8))]
END SUBROUTINE create_nd_to_2d_mapping
SUBROUTINE destroy_nd_to_2d_mapping(map)
TYPE(nd_to_2d_mapping), INTENT(INOUT) :: map
DEALLOCATE (map%dims1_2d)
DEALLOCATE (map%dims2_2d)
DEALLOCATE (map%map1_2d)
DEALLOCATE (map%map2_2d)
DEALLOCATE (map%map_nd)
DEALLOCATE (map%dims_nd)
END SUBROUTINE destroy_nd_to_2d_mapping
PURE FUNCTION ndims_mapping(map)
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
INTEGER :: ndims_mapping
ndims_mapping = map%ndim_nd
END FUNCTION
PURE FUNCTION ndims_mapping_row(map)
!! how many tensor dimensions are mapped to matrix row
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
INTEGER :: ndims_mapping_row
ndims_mapping_row = map%ndim1_2d
END FUNCTION
PURE FUNCTION ndims_mapping_column(map)
!! how many tensor dimensions are mapped to matrix column
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
INTEGER :: ndims_mapping_column
ndims_mapping_column = map%ndim2_2d
END FUNCTION
PURE SUBROUTINE dbcsr_t_get_mapping_info(map, ndim_nd, ndim1_2d, ndim2_2d, dims_2d_i8, dims_2d, dims_nd, dims1_2d, dims2_2d, &
map1_2d, map2_2d, map_nd, base, col_major)
!! get mapping info
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
!! index mapping data.
INTEGER, INTENT(OUT), OPTIONAL :: ndim_nd, ndim1_2d, ndim2_2d
!! number of dimensions
!! number of dimensions that map to first 2d index
!! number of dimensions that map to first 2d index
INTEGER(KIND=int_8), DIMENSION(2), INTENT(OUT), OPTIONAL :: dims_2d_i8
INTEGER, DIMENSION(2), INTENT(OUT), OPTIONAL :: dims_2d
!! 2d dimensions
INTEGER, DIMENSION(ndims_mapping(map)), &
INTENT(OUT), OPTIONAL :: dims_nd
!! nd dimensions
INTEGER, DIMENSION(ndims_mapping_row(map)), INTENT(OUT), &
OPTIONAL :: dims1_2d
!! dimensions that map to first 2d index
INTEGER, DIMENSION(ndims_mapping_column(map)), INTENT(OUT), &
OPTIONAL :: dims2_2d
!! dimensions that map to second 2d index
INTEGER, DIMENSION(ndims_mapping_row(map)), INTENT(OUT), &
OPTIONAL :: map1_2d
!! indices that map to first 2d index
INTEGER, DIMENSION(ndims_mapping_column(map)), INTENT(OUT), &
OPTIONAL :: map2_2d
!! indices that map to second 2d index
INTEGER, DIMENSION(ndims_mapping(map)), &
INTENT(OUT), OPTIONAL :: map_nd
!! inverse of [map1_2d, map2_2d]
INTEGER, INTENT(OUT), OPTIONAL :: base
!! base index
LOGICAL, INTENT(OUT), OPTIONAL :: col_major
!! is index in column major order
IF (PRESENT(ndim_nd)) ndim_nd = map%ndim_nd
IF (PRESENT(ndim1_2d)) ndim1_2d = map%ndim1_2d
IF (PRESENT(ndim2_2d)) ndim2_2d = map%ndim2_2d
IF (PRESENT(dims_2d_i8)) dims_2d_i8(:) = map%dims_2d(:)
IF (PRESENT(dims_2d)) dims_2d(:) = INT(map%dims_2d(:))
IF (PRESENT(dims_nd)) THEN
dims_nd(:) = map%dims_nd(:)
END IF
IF (PRESENT(dims1_2d)) THEN
dims1_2d(:) = map%dims1_2d
END IF
IF (PRESENT(dims2_2d)) THEN
dims2_2d(:) = map%dims2_2d
END IF
IF (PRESENT(map1_2d)) THEN
map1_2d(:) = map%map1_2d
END IF
IF (PRESENT(map2_2d)) THEN
map2_2d(:) = map%map2_2d
END IF
IF (PRESENT(map_nd)) THEN
map_nd(:) = map%map_nd(:)
END IF
IF (PRESENT(base)) THEN
base = map%base
END IF
IF (PRESENT(col_major)) THEN
col_major = map%col_major
END IF
END SUBROUTINE dbcsr_t_get_mapping_info
PURE FUNCTION combine_tensor_index(ind_in, dims) RESULT(ind_out)
!! transform nd index to flat index
INTEGER, DIMENSION(:), INTENT(IN) :: ind_in, dims
!! nd index
!! nd dimensions
INTEGER(KIND=int_8) :: ind_out
!! flat index
INTEGER :: i_dim
ind_out = ind_in(SIZE(dims))
DO i_dim = SIZE(dims) - 1, 1, -1
ind_out = (ind_out - 1)*dims(i_dim) + ind_in(i_dim)
END DO
END FUNCTION
PURE FUNCTION combine_pgrid_index(ind_in, dims) RESULT(ind_out)
!! transform nd index to flat index
INTEGER, DIMENSION(:), INTENT(IN) :: ind_in, dims
!! nd index
!! nd dimensions
INTEGER :: ind_out
!! flat index
INTEGER :: i_dim
ind_out = ind_in(1)
DO i_dim = 2, SIZE(dims)
ind_out = ind_out*dims(i_dim) + ind_in(i_dim)
END DO
END FUNCTION
PURE FUNCTION split_tensor_index(ind_in, dims) RESULT(ind_out)
!! transform flat index to nd index
INTEGER(KIND=int_8), INTENT(IN) :: ind_in
!! flat index
INTEGER, DIMENSION(:), INTENT(IN) :: dims
!! nd dimensions
INTEGER, DIMENSION(SIZE(dims)) :: ind_out
!! nd index
INTEGER(KIND=int_8) :: tmp
INTEGER :: i_dim
tmp = ind_in
DO i_dim = 1, SIZE(dims)
ind_out(i_dim) = INT(MOD(tmp - 1, INT(dims(i_dim), int_8)) + 1)
tmp = (tmp - 1)/dims(i_dim) + 1
END DO
END FUNCTION
PURE FUNCTION split_pgrid_index(ind_in, dims) RESULT(ind_out)
!! transform flat index to nd index
INTEGER, INTENT(IN) :: ind_in
!! flat index
INTEGER, DIMENSION(:), INTENT(IN) :: dims
!! nd dimensions
INTEGER, DIMENSION(SIZE(dims)) :: ind_out
!! nd index
INTEGER :: tmp
INTEGER :: i_dim
tmp = ind_in
DO i_dim = SIZE(dims), 1, -1
ind_out(i_dim) = MOD(tmp, dims(i_dim))
tmp = tmp/dims(i_dim)
END DO
END FUNCTION
PURE FUNCTION get_2d_indices_tensor(map, ind_in) RESULT(ind_out)
!! transform nd index to 2d index, using info from index mapping.
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
!! index mapping
INTEGER, DIMENSION(map%ndim_nd), INTENT(IN) :: ind_in
!! nd index
INTEGER(KIND=int_8), DIMENSION(2) :: ind_out
!! 2d index
INTEGER :: i
INTEGER, DIMENSION(${maxrank}$) :: ind_tmp
DO i = 1, map%ndim1_2d
ind_tmp(i) = ind_in(map%map1_2d(i))
END DO
ind_out(1) = combine_tensor_index(ind_tmp(:map%ndim1_2d), map%dims1_2d)
DO i = 1, map%ndim2_2d
ind_tmp(i) = ind_in(map%map2_2d(i))
END DO
ind_out(2) = combine_tensor_index(ind_tmp(:map%ndim2_2d), map%dims2_2d)
END FUNCTION
PURE FUNCTION get_2d_indices_pgrid(map, ind_in) RESULT(ind_out)
!! transform nd index to 2d index, using info from index mapping.
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
!! index mapping
INTEGER, DIMENSION(map%ndim_nd), INTENT(IN) :: ind_in
!! nd index
INTEGER, DIMENSION(2) :: ind_out
!! 2d index
INTEGER :: i
INTEGER, DIMENSION(${maxrank}$) :: ind_tmp
DO i = 1, map%ndim1_2d
ind_tmp(i) = ind_in(map%map1_2d(i))
END DO
ind_out(1) = combine_pgrid_index(ind_tmp(:map%ndim1_2d), map%dims1_2d)
DO i = 1, map%ndim2_2d
ind_tmp(i) = ind_in(map%map2_2d(i))
END DO
ind_out(2) = combine_pgrid_index(ind_tmp(:map%ndim2_2d), map%dims2_2d)
END FUNCTION
PURE FUNCTION get_nd_indices_tensor(map, ind_in) RESULT(ind_out)
!! transform 2d index to nd index, using info from index mapping.
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
!! index mapping
INTEGER(KIND=int_8), DIMENSION(2), INTENT(IN) :: ind_in
!! 2d index
INTEGER, DIMENSION(map%ndim_nd) :: ind_out
!! nd index
INTEGER, DIMENSION(${maxrank}$) :: ind_tmp
INTEGER :: i
ind_tmp(:map%ndim1_2d) = split_tensor_index(ind_in(1), map%dims1_2d)
DO i = 1, map%ndim1_2d
ind_out(map%map1_2d(i)) = ind_tmp(i)
END DO
ind_tmp(:map%ndim2_2d) = split_tensor_index(ind_in(2), map%dims2_2d)
DO i = 1, map%ndim2_2d
ind_out(map%map2_2d(i)) = ind_tmp(i)
END DO
END FUNCTION
PURE FUNCTION get_nd_indices_pgrid(map, ind_in) RESULT(ind_out)
!! transform 2d index to nd index, using info from index mapping.
TYPE(nd_to_2d_mapping), INTENT(IN) :: map
!! index mapping
INTEGER, DIMENSION(2), INTENT(IN) :: ind_in
!! 2d index
INTEGER, DIMENSION(map%ndim_nd) :: ind_out
!! nd index
ind_out(map%map1_2d) = split_pgrid_index(ind_in(1), map%dims1_2d)
ind_out(map%map2_2d) = split_pgrid_index(ind_in(2), map%dims2_2d)
END FUNCTION
PURE FUNCTION dbcsr_t_inverse_order(order)
!! Invert order
INTEGER, DIMENSION(:), INTENT(IN) :: order
INTEGER, DIMENSION(SIZE(order)) :: dbcsr_t_inverse_order
INTEGER :: i
dbcsr_t_inverse_order(order) = (/(i, i=1, SIZE(order))/)
END FUNCTION
SUBROUTINE permute_index(map_in, map_out, order)
!! reorder tensor index (no data)
TYPE(nd_to_2d_mapping), INTENT(IN) :: map_in
TYPE(nd_to_2d_mapping), INTENT(OUT) :: map_out
INTEGER, DIMENSION(ndims_mapping(map_in)), &
INTENT(IN) :: order
INTEGER :: ndim_nd
INTEGER, DIMENSION(ndims_mapping_row(map_in)) :: map1_2d, map1_2d_reorder
INTEGER, DIMENSION(ndims_mapping_column(map_in)) :: map2_2d, map2_2d_reorder
INTEGER, DIMENSION(ndims_mapping(map_in)) :: dims_nd, dims_reorder
CALL dbcsr_t_get_mapping_info(map_in, ndim_nd, dims_nd=dims_nd, map1_2d=map1_2d, map2_2d=map2_2d)
dims_reorder(order) = dims_nd
map1_2d_reorder(:) = order(map1_2d)
map2_2d_reorder(:) = order(map2_2d)
CALL create_nd_to_2d_mapping(map_out, dims_reorder, map1_2d_reorder, map2_2d_reorder)
END SUBROUTINE
END MODULE dbcsr_tensor_index