(git:e90df42)
Loading...
Searching...
No Matches
dbt_unittest.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Block tensor unit test
10!> \author Patrick Seewald
11! **************************************************************************************************
13 USE cp_dbcsr_api, ONLY: dbcsr_finalize_lib,&
14 dbcsr_init_lib
15 USE dbm_api, ONLY: dbm_library_finalize,&
18 USE dbt_api, ONLY: dbt_library_finalize,&
19 dbt_library_init,&
20 dbt_library_print_stats
21 USE dbt_test, ONLY: dbt_contract_test,&
25 USE dbt_types, ONLY: &
29 USE kinds, ONLY: dp
31 USE message_passing, ONLY: mp_comm_type,&
36#include "../base/base_uses.f90"
37
38 IMPLICIT NONE
39
40 TYPE(mp_comm_type) :: mp_comm
41 INTEGER :: mynode, io_unit
42 INTEGER :: ndims, nblks_alloc, nblks_1, nblks_2, nblks_3, nblks_4, nblks_5, &
43 nblks_alloc_1, nblks_alloc_2, nblks_alloc_3, nblks_alloc_4, nblks_alloc_5
44 INTEGER, DIMENSION(:), ALLOCATABLE :: size_1, size_2, size_3, size_4, size_5, dist1_1, dist1_2, dist1_3, &
45 dist2_1, dist2_2, dist3_1, dist3_2, dist3_3, dist4_1, dist4_2, &
46 dist4_3, dist4_4, dist5_1, dist5_2, dist5_3
47 INTEGER, DIMENSION(:), ALLOCATABLE :: blk_ind_1, blk_ind_2, blk_ind_3, blk_ind_4, blk_ind_1_1, blk_ind_2_1, &
48 blk_ind_3_1, blk_ind_3_2, blk_ind_4_2, blk_ind_1_3, blk_ind_2_3, &
49 blk_ind_4_3, blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4, &
50 blk_ind_3_5, blk_ind_4_5, blk_ind_5_5
51 INTEGER, DIMENSION(:), ALLOCATABLE :: map11, map31, map12, map32, map21, map22
52
53 LOGICAL, PARAMETER :: verbose = .false.
54 TYPE(dbt_distribution_type) :: dist1, dist2, dist3
55 TYPE(dbt_type) :: tensor_a, tensor_b, tensor_c
56
57 LOGICAL, PARAMETER :: test_format = .true.
58 LOGICAL, PARAMETER :: test_contraction = .true.
59 INTEGER, DIMENSION(4) :: pdims_4d
60 INTEGER, DIMENSION(3) :: pdims_3d
61 INTEGER, DIMENSION(2) :: pdims_2d
62 TYPE(dbt_pgrid_type) :: pgrid_2d, pgrid_3d, pgrid_4d
63 INTEGER, DIMENSION(:), ALLOCATABLE :: bounds_t
64 INTEGER, DIMENSION(:, :), ALLOCATABLE :: bounds, bounds_1, bounds_2
65
66 CALL mp_world_init(mp_comm)
67 mynode = mp_comm%mepos
68
69 ! Select active offload device when available.
70 IF (offload_get_device_count() > 0) THEN
72 END IF
73
74 ! set standard output parameters
75 io_unit = -1
76 IF (mynode == 0) io_unit = default_output_unit
77
78 CALL dbcsr_init_lib(mp_comm%get_handle(), io_unit) ! Needed for DBM_VALIDATE_AGAINST_DBCSR.
79 CALL dbm_library_init()
80 CALL dbt_library_init()
81
83
84 ! Process grid
85
86 IF (test_format) THEN
87!--------------------------------------------------------------------------------------------------!
88! Test 1: Testing matrix representations of tensor rank 2 !
89!--------------------------------------------------------------------------------------------------!
90 ndims = 2
91
92 ! Number of blocks in each dimension
93 nblks_1 = 14
94 nblks_2 = 21
95
96 ! Block sizes in each dimension
97 ALLOCATE (size_1(nblks_1), size_2(nblks_2))
98
99 size_1(:) = [3, 5, 1, 23, 2, 3, 1, 6, 3, 8, 2, 3, 5, 1]
100 size_2(:) = [4, 2, 5, 3, 1, 5, 13, 5, 2, 4, 5, 6, 7, 2, 3, 1, 2, 6, 9, 12, 21]
101
102 ! Number of non-zero blocks
103 nblks_alloc = 12
104 ALLOCATE (blk_ind_1(nblks_alloc), blk_ind_2(nblks_alloc))
105
106 ! Indices of non-zero blocks (s.t. index of ith block is [blk_ind_1(i), blk_ind_2(i), ...])
107 blk_ind_1(:) = [1, 1, 1, 2, 4, 4, 7, 10, 10, 10, 10, 13] !&
108 blk_ind_2(:) = [1, 3, 11, 15, 4, 17, 21, 6, 9, 13, 19, 7] !&
109
110 ! Test tensor formats
111 CALL dbt_test_formats(ndims, mp_comm, io_unit, verbose, &
112 blk_size_1=size_1, blk_size_2=size_2, &
113 blk_ind_1=blk_ind_1, blk_ind_2=blk_ind_2)
114
115 DEALLOCATE (size_1, size_2)
116 DEALLOCATE (blk_ind_1, blk_ind_2)
117
118!--------------------------------------------------------------------------------------------------!
119! Test 2: Testing matrix representations of tensor rank 3 !
120!--------------------------------------------------------------------------------------------------!
121 ndims = 3
122
123 ! Number of blocks in each dimension
124 nblks_1 = 4
125 nblks_2 = 6
126 nblks_3 = 3
127
128 ! Block sizes in each dimension
129 ALLOCATE (size_1(nblks_1), size_2(nblks_2), size_3(nblks_3))
130
131 size_1(:) = [3, 1, 5, 2]
132 size_2(:) = [1, 2, 5, 3, 2, 4]
133 size_3(:) = [4, 2, 10]
134
135 ! Number of non-zero blocks
136 nblks_alloc = 6
137 ALLOCATE (blk_ind_1(nblks_alloc), blk_ind_2(nblks_alloc), blk_ind_3(nblks_alloc))
138
139 ! Indices of non-zero blocks (s.t. index of ith block is [blk_ind_1(i), blk_ind_2(i), ...])
140 blk_ind_1(:) = [1, 1, 1, 2, 2, 2] !&
141 blk_ind_2(:) = [2, 2, 4, 1, 1, 2] !&
142 blk_ind_3(:) = [1, 3, 3, 2, 3, 2] !&
143
144 ! Test tensor formats
145 CALL dbt_test_formats(ndims, mp_comm, io_unit, verbose, &
146 blk_size_1=size_1, blk_size_2=size_2, blk_size_3=size_3, &
147 blk_ind_1=blk_ind_1, blk_ind_2=blk_ind_2, blk_ind_3=blk_ind_3)
148
149 DEALLOCATE (size_1, size_2, size_3)
150 DEALLOCATE (blk_ind_1, blk_ind_2, blk_ind_3)
151
152!--------------------------------------------------------------------------------------------------!
153! Test 3: Testing matrix representations of tensor rank 4 !
154!--------------------------------------------------------------------------------------------------!
155 ndims = 4
156
157 ! Number of blocks in each dimension
158 nblks_1 = 2
159 nblks_2 = 13
160 nblks_3 = 7
161 nblks_4 = 3
162
163 ! Block sizes in each dimension
164 ALLOCATE (size_1(nblks_1), size_2(nblks_2), size_3(nblks_3), size_4(nblks_4))
165
166 size_1(:) = [5, 9]
167 size_2(:) = [6, 2, 5, 12, 3, 1, 7, 2, 5, 17, 9, 3, 4]
168 size_3(:) = [2, 7, 3, 8, 5, 15, 1]
169 size_4(:) = [12, 5, 3]
170
171 ! Number of non-zero blocks
172 nblks_alloc = 19
173 ALLOCATE (blk_ind_1(nblks_alloc), blk_ind_2(nblks_alloc), blk_ind_3(nblks_alloc), blk_ind_4(nblks_alloc))
174
175 ! Indices of non-zero blocks (s.t. index of ith block is [blk_ind_1(i), blk_ind_2(i), ...])
176 blk_ind_1(:) = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2] !&
177 blk_ind_2(:) = [2, 2, 3, 4, 7, 7, 10, 11, 11, 12, 12, 1, 1, 3, 5, 6, 6, 9, 12] !&
178 blk_ind_3(:) = [1, 4, 6, 3, 1, 4, 2, 5, 7, 3, 3, 1, 4, 7, 6, 4, 5, 2, 3] !&
179 blk_ind_4(:) = [3, 2, 3, 1, 1, 2, 1, 3, 2, 2, 3, 1, 3, 2, 1, 1, 3, 2, 2] !&
180
181 ! Test tensor formats
182 CALL dbt_test_formats(ndims, mp_comm, io_unit, verbose, &
183 blk_size_1=size_1, blk_size_2=size_2, blk_size_3=size_3, blk_size_4=size_4, &
184 blk_ind_1=blk_ind_1, blk_ind_2=blk_ind_2, blk_ind_3=blk_ind_3, blk_ind_4=blk_ind_4)
185
186 DEALLOCATE (size_1, size_2, size_3, size_4)
187 DEALLOCATE (blk_ind_1, blk_ind_2, blk_ind_3, blk_ind_4)
188
189 END IF
190 IF (test_contraction) THEN
191
192!--------------------------------------------------------------------------------------------------!
193! Preparations for tensor contraction tests !
194!--------------------------------------------------------------------------------------------------!
195
196 nblks_1 = 4
197 nblks_2 = 11
198 nblks_3 = 9
199 nblks_4 = 5
200 nblks_5 = 3
201
202 ! Block sizes in each dimension
203 ALLOCATE (size_1(nblks_1), size_2(nblks_2), size_3(nblks_3), size_4(nblks_4), size_5(nblks_5))
204
205 size_1(:) = [3, 9, 12, 1]
206 size_2(:) = [4, 2, 3, 1, 9, 2, 32, 10, 5, 8, 7]
207 size_3(:) = [7, 3, 8, 7, 9, 5, 10, 23, 2]
208 size_4(:) = [8, 1, 4, 13, 6]
209 size_5(:) = [4, 2, 22]
210
211 nblks_alloc_1 = 32
212 ALLOCATE (blk_ind_1_1(nblks_alloc_1), blk_ind_2_1(nblks_alloc_1), blk_ind_3_1(nblks_alloc_1))
213
214 blk_ind_1_1(:) = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, & !&
215 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, & !&
216 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, & !&
217 4, 4] !&
218
219 blk_ind_2_1(:) = [ 3, 5, 5, 5, 6, 6, 7, 8, 10, 11, & !&
220 11, 1, 1, 4, 7, 7, 9, 10 , 2, 2, & !&
221 5, 6, 8, 8, 9, 11, 11, 2 , 4, 5, & !&
222 5, 8] !&
223
224 blk_ind_3_1(:) = [7, 3, 5, 9, 6, 8, 2, 8, 3, 2, & !&
225 3, 1, 4, 6, 2, 7, 5, 8, 3, 7, & !&
226 1, 4, 3, 7, 8, 5, 8, 9, 6, 1, & !&
227 2, 7] !&
228
229 nblks_alloc_2 = 12
230 ALLOCATE (blk_ind_3_2(nblks_alloc_2), blk_ind_4_2(nblks_alloc_2))
231
232 blk_ind_3_2(:) = [1, 1, 2, 2, 2, 4, 4, 5, 5, 6, & !&
233 8, 8] !&
234 blk_ind_4_2(:) = [2, 3, 2, 4, 5, 3, 5, 1, 3, 3, & !&
235 1, 4] !&
236
237 nblks_alloc_3 = 5
238 ALLOCATE (blk_ind_1_3(nblks_alloc_3), blk_ind_2_3(nblks_alloc_3), blk_ind_4_3(nblks_alloc_3))
239
240 blk_ind_1_3(:) = [1, 1, 2, 4, 4]
241 blk_ind_2_3(:) = [2, 6, 6, 7, 9]
242 blk_ind_4_3(:) = [1, 3, 4, 4, 5]
243
244 nblks_alloc_4 = 36
245 ALLOCATE (blk_ind_1_4(nblks_alloc_4))
246 ALLOCATE (blk_ind_2_4(nblks_alloc_4))
247 ALLOCATE (blk_ind_4_4(nblks_alloc_4))
248 ALLOCATE (blk_ind_5_4(nblks_alloc_4))
249
250 blk_ind_1_4(:) = [ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, & !&
251 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, & !&
252 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, & !&
253 4, 4, 4, 4, 4, 4] !&
254
255 blk_ind_2_4(:) = [ 1, 3, 4, 6, 10, 2, 2, 4, 5, 5, & !&
256 6, 6, 6, 7, 7, 9, 9, 9, 10, 11, & !&
257 1, 3, 3, 4, 5, 6, 8, 9, 11, 11, & !&
258 1, 3, 4, 6, 10, 11] !&
259
260 blk_ind_4_4(:) = [ 3, 5, 2, 3, 2, 3, 5, 1, 1, 4, & !&
261 2, 3, 4, 1, 4, 3, 4, 4, 2, 1, & !&
262 3, 1, 1, 3, 4, 3, 4, 2, 2, 3, & !&
263 1, 1, 3, 2, 5, 5] !&
264
265 blk_ind_5_4(:) = [ 1, 3, 2, 1, 1, 2, 3, 1, 3, 1, & !&
266 2, 3, 2, 1, 3, 2, 3, 2, 1, 2, & !&
267 3, 1, 2, 3, 2, 2, 2, 3, 1, 2, & !&
268 1, 3, 2, 1, 3, 2] !&
269
270 nblks_alloc_5 = 8
271
272 ALLOCATE (blk_ind_3_5(nblks_alloc_5), blk_ind_4_5(nblks_alloc_5), blk_ind_5_5(nblks_alloc_5))
273
274 blk_ind_3_5(:) = [2, 4, 5, 5, 5, 6, 6, 8]
275 blk_ind_4_5(:) = [3, 2, 1, 1, 3, 2, 4, 5]
276 blk_ind_5_5(:) = [3, 2, 1, 2, 3, 2, 1, 1]
277
278 pdims_4d(:) = 0; pdims_3d(:) = 0; pdims_2d(:) = 0
279 CALL dbt_pgrid_create(mp_comm, pdims_4d, pgrid_4d)
280 CALL dbt_pgrid_create(mp_comm, pdims_3d, pgrid_3d)
281 CALL dbt_pgrid_create(mp_comm, pdims_2d, pgrid_2d)
282
283 ALLOCATE (dist1_1(nblks_1))
284 CALL dbt_default_distvec(nblks_1, pdims_3d(1), size_1, dist1_1)
285 ALLOCATE (dist1_2(nblks_2))
286 CALL dbt_default_distvec(nblks_2, pdims_3d(2), size_2, dist1_2)
287 ALLOCATE (dist1_3(nblks_3))
288 CALL dbt_default_distvec(nblks_3, pdims_3d(3), size_3, dist1_3)
289
290 ALLOCATE (dist2_1(nblks_3))
291 CALL dbt_default_distvec(nblks_3, pdims_2d(1), size_3, dist2_1)
292 ALLOCATE (dist2_2(nblks_4))
293 CALL dbt_default_distvec(nblks_4, pdims_2d(2), size_4, dist2_2)
294
295 ALLOCATE (dist3_1(nblks_1))
296 CALL dbt_default_distvec(nblks_1, pdims_3d(1), size_1, dist3_1)
297 ALLOCATE (dist3_2(nblks_2))
298 CALL dbt_default_distvec(nblks_2, pdims_3d(2), size_2, dist3_2)
299 ALLOCATE (dist3_3(nblks_4))
300 CALL dbt_default_distvec(nblks_4, pdims_3d(3), size_4, dist3_3)
301
302 ALLOCATE (dist4_1(nblks_1))
303 CALL dbt_default_distvec(nblks_1, pdims_4d(1), size_1, dist4_1)
304 ALLOCATE (dist4_2(nblks_2))
305 CALL dbt_default_distvec(nblks_2, pdims_4d(2), size_2, dist4_2)
306 ALLOCATE (dist4_3(nblks_4))
307 CALL dbt_default_distvec(nblks_4, pdims_4d(3), size_4, dist4_3)
308 ALLOCATE (dist4_4(nblks_5))
309 CALL dbt_default_distvec(nblks_5, pdims_4d(4), size_5, dist4_4)
310
311 ALLOCATE (dist5_1(nblks_3))
312 CALL dbt_default_distvec(nblks_3, pdims_3d(1), size_3, dist5_1)
313 ALLOCATE (dist5_2(nblks_4))
314 CALL dbt_default_distvec(nblks_4, pdims_3d(2), size_4, dist5_2)
315 ALLOCATE (dist5_3(nblks_5))
316 CALL dbt_default_distvec(nblks_5, pdims_3d(3), size_5, dist5_3)
317
318!--------------------------------------------------------------------------------------------------!
319! Test 4: Testing tensor contraction (12|3)x(3|4)=(12|4) !
320!--------------------------------------------------------------------------------------------------!
321
322 ALLOCATE (map11(2), map12(1), map21(1), map22(1), map31(2), map32(1))
323 map11(:) = [1, 2]
324 map12(:) = [3]
325 map21(:) = [1]
326 map22(:) = [2]
327 map31(:) = [1, 2]
328 map32(:) = [3]
329
330 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
331 CALL dbt_distribution_new(dist2, pgrid_2d, dist2_1, dist2_2)
332 CALL dbt_distribution_new(dist3, pgrid_3d, dist3_1, dist3_2, dist3_3)
333
334 CALL dbt_create(tensor_a, "(12|3)", dist1, map11, map12, size_1, size_2, size_3)
335 CALL dbt_create(tensor_b, "(3|4)", dist2, map21, map22, size_3, size_4)
336 CALL dbt_create(tensor_c, "(12|4)", dist3, map31, map32, size_1, size_2, size_4)
337
338 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
339 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_3_2, blk_ind_4_2)
340
341 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_1_3, blk_ind_2_3, blk_ind_4_3)
342
343 CALL dbt_contract_test(0.9_dp, tensor_a, tensor_b, 0.1_dp, tensor_c, &
344 [3], [2, 1], &
345 [1], [2], &
346 [2, 1], [3], &
347 io_unit, &
348 log_verbose=verbose, &
349 write_int=.true.)
350
351 DEALLOCATE (map11, map12, map21, map22, map31, map32)
352
353 CALL dbt_destroy(tensor_a)
354 CALL dbt_destroy(tensor_b)
355 CALL dbt_destroy(tensor_c)
356 CALL dbt_distribution_destroy(dist1)
357 CALL dbt_distribution_destroy(dist2)
358 CALL dbt_distribution_destroy(dist3)
359
360!--------------------------------------------------------------------------------------------------!
361! Test 5: Testing tensor contraction (2|31)x(4|3)=(24|1) !
362!--------------------------------------------------------------------------------------------------!
363
364 ALLOCATE (map11(1), map12(2), map21(1), map22(1), map31(2), map32(1))
365 map11(:) = [2]
366 map12(:) = [3, 1]
367 map21(:) = [2]
368 map22(:) = [1]
369 map31(:) = [2, 3]
370 map32(:) = [1]
371
372 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
373 CALL dbt_distribution_new(dist2, pgrid_2d, dist2_1, dist2_2)
374 CALL dbt_distribution_new(dist3, pgrid_3d, dist3_1, dist3_2, dist3_3)
375
376 CALL dbt_create(tensor_a, "(2|31)", dist1, map11, map12, size_1, size_2, size_3)
377 CALL dbt_create(tensor_b, "(4|3)", dist2, map21, map22, size_3, size_4)
378 CALL dbt_create(tensor_c, "(24|1)", dist3, map31, map32, size_1, size_2, size_4)
379
380 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
381 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_3_2, blk_ind_4_2)
382 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_1_3, blk_ind_2_3, blk_ind_4_3)
383
384 CALL dbt_contract_test(0.9_dp, tensor_a, tensor_b, 0.1_dp, tensor_c, &
385 [3], [1, 2], &
386 [1], [2], &
387 [1, 2], [3], &
388 io_unit, &
389 log_verbose=verbose, &
390 write_int=.true.)
391
392 DEALLOCATE (map11, map12, map21, map22, map31, map32)
393
394 CALL dbt_destroy(tensor_a)
395 CALL dbt_destroy(tensor_b)
396 CALL dbt_destroy(tensor_c)
397 CALL dbt_distribution_destroy(dist1)
398 CALL dbt_distribution_destroy(dist2)
399 CALL dbt_distribution_destroy(dist3)
400
401!-------------------------------------------------------------------------------------------------!
402! Test 6: Testing tensor contraction (4|3)x(1|32)=(24|1) !
403!-------------------------------------------------------------------------------------------------!
404
405 ALLOCATE (map11(1), map12(2), map21(1), map22(1), map31(2), map32(1))
406 map11(:) = [1]
407 map12(:) = [3, 2]
408 map21(:) = [2]
409 map22(:) = [1]
410 map31(:) = [2, 3]
411 map32(:) = [1]
412
413 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
414 CALL dbt_distribution_new(dist2, pgrid_2d, dist2_1, dist2_2)
415 CALL dbt_distribution_new(dist3, pgrid_3d, dist3_1, dist3_2, dist3_3)
416
417 CALL dbt_create(tensor_a, "(1|32)", dist1, map11, map12, size_1, size_2, size_3)
418 CALL dbt_create(tensor_b, "(4|3)", dist2, map21, map22, size_3, size_4)
419 CALL dbt_create(tensor_c, "(24|1)", dist3, map31, map32, size_1, size_2, size_4)
420
421 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
422 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_3_2, blk_ind_4_2)
423 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_1_3, blk_ind_2_3, blk_ind_4_3)
424
425 ALLOCATE (bounds_t(ndims_tensor(tensor_b)))
426 CALL dbt_get_info(tensor_b, nfull_total=bounds_t)
427
428 ALLOCATE (bounds(2, 1))
429 bounds(1, 1) = 1
430 bounds(2, 1) = bounds_t(1) - 21
431
432 CALL dbt_contract_test(0.9_dp, tensor_b, tensor_a, 0.1_dp, tensor_c, &
433 [1], [2], &
434 [3], [1, 2], &
435 [3], [1, 2], &
436 io_unit, &
437 bounds_1=bounds, &
438 log_verbose=verbose, &
439 write_int=.true.)
440
441 DEALLOCATE (map11, map12, map21, map22, map31, map32, bounds_t, bounds)
442
443 CALL dbt_destroy(tensor_a)
444 CALL dbt_destroy(tensor_b)
445 CALL dbt_destroy(tensor_c)
446 CALL dbt_distribution_destroy(dist1)
447 CALL dbt_distribution_destroy(dist2)
448 CALL dbt_distribution_destroy(dist3)
449
450!-------------------------------------------------------------------------------------------------!
451! Test 7: Testing tensor contraction (1|24)x(3|4)=(21|3) !
452!-------------------------------------------------------------------------------------------------!
453
454 ALLOCATE (map11(2), map12(1), map21(1), map22(1), map31(1), map32(2))
455 map11(:) = [2, 1]
456 map12(:) = [3]
457 map21(:) = [1]
458 map22(:) = [2]
459 map31(:) = [1]
460 map32(:) = [2, 3]
461
462 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
463 CALL dbt_distribution_new(dist2, pgrid_2d, dist2_1, dist2_2)
464 CALL dbt_distribution_new(dist3, pgrid_3d, dist3_1, dist3_2, dist3_3)
465
466 CALL dbt_create(tensor_a, "(21|3)", dist1, map11, map12, size_1, size_2, size_3)
467 CALL dbt_create(tensor_b, "(3|4)", dist2, map21, map22, size_3, size_4)
468 CALL dbt_create(tensor_c, "(1|24)", dist3, map31, map32, size_1, size_2, size_4)
469
470 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
471 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_3_2, blk_ind_4_2)
472 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_1_3, blk_ind_2_3, blk_ind_4_3)
473
474 ALLOCATE (bounds_t(ndims_tensor(tensor_c)))
475 CALL dbt_get_info(tensor_c, nfull_total=bounds_t)
476
477 ALLOCATE (bounds(2, 2))
478 bounds(1, 1) = 4
479 bounds(2, 1) = bounds_t(1)
480 bounds(1, 2) = 13
481 bounds(2, 2) = bounds_t(2) - 10
482 DEALLOCATE (bounds_t)
483
484 CALL dbt_contract_test(0.2_dp, tensor_c, tensor_b, 0.8_dp, tensor_a, &
485 [3], [1, 2], &
486 [2], [1], &
487 [1, 2], [3], &
488 io_unit, &
489 bounds_2=bounds, &
490 log_verbose=verbose, &
491 write_int=.true.)
492
493 DEALLOCATE (map11, map12, map21, map22, map31, map32, bounds)
494
495 CALL dbt_destroy(tensor_a)
496 CALL dbt_destroy(tensor_b)
497 CALL dbt_destroy(tensor_c)
498 CALL dbt_distribution_destroy(dist1)
499 CALL dbt_distribution_destroy(dist2)
500 CALL dbt_distribution_destroy(dist3)
501
502!-------------------------------------------------------------------------------------------------!
503! Test 8: Testing tensor contraction (12|3)x(12|45)=(3|45)
504!-------------------------------------------------------------------------------------------------!
505
506 ALLOCATE (map11(2), map12(1), map21(2), map22(2), map31(1), map32(2))
507 map11(:) = [1, 2]
508 map12(:) = [3]
509 map21(:) = [1, 2]
510 map22(:) = [3, 4]
511 map31(:) = [1]
512 map32(:) = [2, 3]
513
514 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
515 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
516 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
517
518 CALL dbt_create(tensor_a, "(12|3)", dist1, map11, map12, size_1, size_2, size_3)
519 CALL dbt_create(tensor_b, "(12|45)", dist2, map21, map22, size_1, size_2, size_4, size_5)
520 CALL dbt_create(tensor_c, "(3|45)", dist3, map31, map32, size_3, size_4, size_5)
521
522 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
523 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
524 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
525
526 ALLOCATE (bounds_t(ndims_tensor(tensor_a)))
527 CALL dbt_get_info(tensor_a, nfull_total=bounds_t)
528 ALLOCATE (bounds_1(2, 2))
529 bounds_1(1, 1) = 7
530 bounds_1(2, 1) = bounds_t(2) - 17
531 bounds_1(1, 2) = 8
532 bounds_1(2, 2) = bounds_t(1)
533 DEALLOCATE (bounds_t)
534
535 ALLOCATE (bounds_t(ndims_tensor(tensor_b)))
536 CALL dbt_get_info(tensor_b, nfull_total=bounds_t)
537 ALLOCATE (bounds_2(2, 2))
538 bounds_2(1, 1) = 1
539 bounds_2(2, 1) = bounds_t(3)
540 bounds_2(1, 2) = 1
541 bounds_2(2, 2) = bounds_t(4) - 18
542 DEALLOCATE (bounds_t)
543
544 CALL dbt_contract_test(0.2_dp, tensor_a, tensor_b, 0.8_dp, tensor_c, &
545 [2, 1], [3], &
546 [2, 1], [3, 4], &
547 [1], [2, 3], &
548 io_unit, &
549 bounds_1=bounds_1, &
550 bounds_3=bounds_2, &
551 log_verbose=verbose, &
552 write_int=.true.)
553
554 DEALLOCATE (map11, map12, map21, map22, map31, map32, bounds_1, bounds_2)
555
556 CALL dbt_destroy(tensor_a)
557 CALL dbt_destroy(tensor_b)
558 CALL dbt_destroy(tensor_c)
559 CALL dbt_distribution_destroy(dist1)
560 CALL dbt_distribution_destroy(dist2)
561 CALL dbt_distribution_destroy(dist3)
562
563!-------------------------------------------------------------------------------------------------!
564! Test 9: Testing tensor contraction (3|21)x(12|45)=(3|45)
565!-------------------------------------------------------------------------------------------------!
566
567 ALLOCATE (map11(1), map12(2), map21(2), map22(2), map31(1), map32(2))
568 map11(:) = [3]
569 map12(:) = [2, 1]
570 map21(:) = [1, 2]
571 map22(:) = [3, 4]
572 map31(:) = [1]
573 map32(:) = [2, 3]
574
575 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
576 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
577 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
578
579 CALL dbt_create(tensor_a, "(3|21)", dist1, map11, map12, size_1, size_2, size_3)
580 CALL dbt_create(tensor_b, "(12|45)", dist2, map21, map22, size_1, size_2, size_4, size_5)
581 CALL dbt_create(tensor_c, "(3|45)", dist3, map31, map32, size_3, size_4, size_5)
582
583 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
584 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
585 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
586
587 CALL dbt_contract_test(0.2_dp, tensor_a, tensor_b, 0.8_dp, tensor_c, &
588 [2, 1], [3], &
589 [2, 1], [3, 4], &
590 [1], [2, 3], &
591 io_unit, &
592 log_verbose=verbose, &
593 write_int=.true.)
594
595 DEALLOCATE (map11, map12, map21, map22, map31, map32)
596
597 CALL dbt_destroy(tensor_a)
598 CALL dbt_destroy(tensor_b)
599 CALL dbt_destroy(tensor_c)
600 CALL dbt_distribution_destroy(dist1)
601 CALL dbt_distribution_destroy(dist2)
602 CALL dbt_distribution_destroy(dist3)
603
604!-------------------------------------------------------------------------------------------------!
605! Test 10: Testing tensor contraction (13|2)x(54|21)=(3|45)
606!-------------------------------------------------------------------------------------------------!
607
608 ALLOCATE (map11(2), map12(1), map21(2), map22(2), map31(1), map32(2))
609 map11(:) = [1, 3]
610 map12(:) = [2]
611 map21(:) = [4, 3]
612 map22(:) = [2, 1]
613 map31(:) = [1]
614 map32(:) = [2, 3]
615
616 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
617 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
618 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
619
620 CALL dbt_create(tensor_a, "(13|2)", dist1, map11, map12, size_1, size_2, size_3)
621 CALL dbt_create(tensor_b, "(54|21)", dist2, map21, map22, size_1, size_2, size_4, size_5)
622 CALL dbt_create(tensor_c, "(3|45)", dist3, map31, map32, size_3, size_4, size_5)
623
624 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
625 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
626 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
627
628 CALL dbt_contract_test(0.2_dp, tensor_a, tensor_b, 0.8_dp, tensor_c, &
629 [1, 2], [3], &
630 [1, 2], [3, 4], &
631 [1], [2, 3], &
632 io_unit, &
633 log_verbose=verbose, &
634 write_int=.true.)
635
636 DEALLOCATE (map11, map12, map21, map22, map31, map32)
637
638 CALL dbt_destroy(tensor_a)
639 CALL dbt_destroy(tensor_b)
640 CALL dbt_destroy(tensor_c)
641 CALL dbt_distribution_destroy(dist1)
642 CALL dbt_distribution_destroy(dist2)
643 CALL dbt_distribution_destroy(dist3)
644
645!-------------------------------------------------------------------------------------------------!
646! Test 10: Testing tensor contraction (54|21)x(2|31)=(43|5)
647!-------------------------------------------------------------------------------------------------!
648
649 ALLOCATE (map11(1), map12(2), map21(2), map22(2), map31(2), map32(1))
650 map11(:) = [2]
651 map12(:) = [3, 1]
652 map21(:) = [4, 3]
653 map22(:) = [2, 1]
654 map31(:) = [2, 1]
655 map32(:) = [3]
656
657 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
658 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
659 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
660
661 CALL dbt_create(tensor_a, "(2|31)", dist1, map11, map12, size_1, size_2, size_3)
662 CALL dbt_create(tensor_b, "(54|21)", dist2, map21, map22, size_1, size_2, size_4, size_5)
663 CALL dbt_create(tensor_c, "(43|5)", dist3, map31, map32, size_3, size_4, size_5)
664
665 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
666 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
667 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
668
669 CALL dbt_contract_test(0.2_dp, tensor_b, tensor_a, 0.8_dp, tensor_c, &
670 [2, 1], [4, 3], &
671 [2, 1], [3], &
672 [3, 2], [1], &
673 io_unit, &
674 log_verbose=verbose, &
675 write_int=.true.)
676
677 DEALLOCATE (map11, map12, map21, map22, map31, map32)
678
679 CALL dbt_destroy(tensor_a)
680 CALL dbt_destroy(tensor_b)
681 CALL dbt_destroy(tensor_c)
682 CALL dbt_distribution_destroy(dist1)
683 CALL dbt_distribution_destroy(dist2)
684 CALL dbt_distribution_destroy(dist3)
685
686!-------------------------------------------------------------------------------------------------!
687! Test 11: Testing tensor contraction (241|5)x(31|2)=(5|43)
688!-------------------------------------------------------------------------------------------------!
689
690 ALLOCATE (map11(2), map12(1), map21(3), map22(1), map31(1), map32(2))
691 map11(:) = [3, 1]
692 map12(:) = [2]
693 map21(:) = [2, 3, 1]
694 map22(:) = [4]
695 map31(:) = [3]
696 map32(:) = [2, 1]
697
698 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
699 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
700 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
701
702 CALL dbt_create(tensor_a, "(31|2)", dist1, map11, map12, size_1, size_2, size_3)
703 CALL dbt_create(tensor_b, "(241|5)", dist2, map21, map22, size_1, size_2, size_4, size_5)
704 CALL dbt_create(tensor_c, "(5|43)", dist3, map31, map32, size_3, size_4, size_5)
705
706 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
707 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
708 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
709
710 CALL dbt_contract_test(0.6_dp, tensor_b, tensor_a, 0.4_dp, tensor_c, &
711 [2, 1], [3, 4], &
712 [2, 1], [3], &
713 [2, 3], [1], &
714 io_unit, &
715 log_verbose=verbose, &
716 write_int=.true.)
717
718 DEALLOCATE (map11, map12, map21, map22, map31, map32)
719
720 CALL dbt_destroy(tensor_a)
721 CALL dbt_destroy(tensor_b)
722 CALL dbt_destroy(tensor_c)
723 CALL dbt_distribution_destroy(dist1)
724 CALL dbt_distribution_destroy(dist2)
725 CALL dbt_distribution_destroy(dist3)
726
727!-------------------------------------------------------------------------------------------------!
728! Test 12: Testing tensor contraction (34|5)x(12|3)=(14|25)
729!-------------------------------------------------------------------------------------------------!
730
731 ALLOCATE (map11(2), map12(1), map21(2), map22(2), map31(2), map32(1))
732 map11(:) = [1, 2]
733 map12(:) = [3]
734 map21(:) = [1, 3]
735 map22(:) = [2, 4]
736 map31(:) = [1, 2]
737 map32(:) = [3]
738
739 CALL dbt_distribution_new(dist1, pgrid_3d, dist1_1, dist1_2, dist1_3)
740 CALL dbt_distribution_new(dist2, pgrid_4d, dist4_1, dist4_2, dist4_3, dist4_4)
741 CALL dbt_distribution_new(dist3, pgrid_3d, dist5_1, dist5_2, dist5_3)
742
743 CALL dbt_create(tensor_a, "(12|3)", dist1, map11, map12, size_1, size_2, size_3)
744 CALL dbt_create(tensor_b, "(14|25)", dist2, map21, map22, size_1, size_2, size_4, size_5)
745 CALL dbt_create(tensor_c, "(34|5)", dist3, map31, map32, size_3, size_4, size_5)
746
747 CALL dbt_setup_test_tensor(tensor_a, mp_comm, .false., blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
748 CALL dbt_setup_test_tensor(tensor_b, mp_comm, .false., blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
749 CALL dbt_setup_test_tensor(tensor_c, mp_comm, .false., blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
750
751 CALL dbt_contract_test(0.2_dp, tensor_c, tensor_a, 0.8_dp, tensor_b, &
752 [1], [2, 3], &
753 [3], [1, 2], &
754 [3, 4], [1, 2], &
755 io_unit, &
756 log_verbose=verbose, &
757 write_int=.true.)
758
759 DEALLOCATE (map11, map12, map21, map22, map31, map32)
760
761 CALL dbt_destroy(tensor_a)
762 CALL dbt_destroy(tensor_b)
763 CALL dbt_destroy(tensor_c)
764 CALL dbt_distribution_destroy(dist1)
765 CALL dbt_distribution_destroy(dist2)
766 CALL dbt_distribution_destroy(dist3)
767
768!--------------------------------------------------------------------------------------------------!
769! Cleanup for tensor contraction tests !
770!--------------------------------------------------------------------------------------------------!
771
772 DEALLOCATE (blk_ind_1_1, blk_ind_2_1, blk_ind_3_1)
773 DEALLOCATE (blk_ind_3_2, blk_ind_4_2)
774 DEALLOCATE (blk_ind_1_3, blk_ind_2_3, blk_ind_4_3)
775 DEALLOCATE (blk_ind_1_4, blk_ind_2_4, blk_ind_4_4, blk_ind_5_4)
776 DEALLOCATE (blk_ind_3_5, blk_ind_4_5, blk_ind_5_5)
777 DEALLOCATE (size_1, size_2, size_3, size_4, size_5, dist1_1, dist1_2, dist1_3, &
778 dist2_1, dist2_2, dist3_1, dist3_2, dist3_3, dist4_1, dist4_2, &
779 dist4_3, dist4_4, dist5_1, dist5_2, dist5_3)
780 CALL dbt_pgrid_destroy(pgrid_3d)
781 CALL dbt_pgrid_destroy(pgrid_2d)
782 CALL dbt_pgrid_destroy(pgrid_4d)
783
784 END IF
785
786!--------------------------------------------------------------------------------------------------!
787! End tests !
788!--------------------------------------------------------------------------------------------------!
789
790 CALL dbm_library_print_stats(mp_comm, io_unit)
791 CALL dbt_library_print_stats(mp_comm, io_unit)
792 CALL dbt_library_finalize()
794 CALL dbcsr_finalize_lib() ! Needed for DBM_VALIDATE_AGAINST_DBCSR.
795
796 ! finalize mpi
797 CALL mp_world_finalize()
798
799END PROGRAM dbt_unittest
program dbt_unittest
Block tensor unit test.
subroutine, public dbm_library_init()
Initialize DBM library.
Definition dbm_api.F:1483
subroutine, public dbm_library_finalize()
Finalize DBM library.
Definition dbm_api.F:1497
subroutine, public dbm_library_print_stats(mpi_comm, output_unit)
Print DBM library statistics.
Definition dbm_api.F:1513
This is the start of a dbt_api, all publically needed functions are exported here....
Definition dbt_api.F:17
General methods for testing DBT tensors.
Definition dbt_test.F:12
subroutine, public dbt_reset_randmat_seed()
Reset the seed used for generating random matrices to default value.
Definition dbt_test.F:1195
subroutine, public dbt_test_formats(ndims, mp_comm, unit_nr, verbose, blk_size_1, blk_size_2, blk_size_3, blk_size_4, blk_ind_1, blk_ind_2, blk_ind_3, blk_ind_4)
Test equivalence of all tensor formats, using a random distribution.
Definition dbt_test.F:176
subroutine, public dbt_contract_test(alpha, tensor_1, tensor_2, beta, tensor_3, contract_1, notcontract_1, contract_2, notcontract_2, map_1, map_2, unit_nr, bounds_1, bounds_2, bounds_3, log_verbose, write_int)
test tensor contraction
Definition dbt_test.F:833
subroutine, public dbt_setup_test_tensor(tensor, mp_comm, enumerate, blk_ind_1, blk_ind_2, blk_ind_3, blk_ind_4)
Allocate and fill test tensor - entries are enumerated by their index s.t. they only depend on global...
Definition dbt_test.F:476
DBT tensor framework for block-sparse tensor contraction: Types and create/destroy routines.
Definition dbt_types.F:12
subroutine, public dbt_pgrid_destroy(pgrid, keep_comm)
destroy process grid
Definition dbt_types.F:894
subroutine, public dbt_distribution_new(dist, pgrid, nd_dist_1, nd_dist_2, nd_dist_3, nd_dist_4)
Create a tensor distribution.
Definition dbt_types.F:875
subroutine, public dbt_destroy(tensor)
Destroy a tensor.
Definition dbt_types.F:1399
subroutine, public dbt_get_info(tensor, nblks_total, nfull_total, nblks_local, nfull_local, pdims, my_ploc, blks_local_1, blks_local_2, blks_local_3, blks_local_4, proc_dist_1, proc_dist_2, proc_dist_3, proc_dist_4, blk_size_1, blk_size_2, blk_size_3, blk_size_4, blk_offset_1, blk_offset_2, blk_offset_3, blk_offset_4, distribution, name)
As block_get_info but for tensors.
Definition dbt_types.F:1645
pure integer function, public ndims_tensor(tensor)
tensor rank
Definition dbt_types.F:1216
subroutine, public dbt_default_distvec(nblk, nproc, blk_size, dist)
get a load-balanced and randomized distribution along one tensor dimension
Definition dbt_types.F:1865
subroutine, public dbt_pgrid_create(mp_comm, dims, pgrid, tensor_dims)
Definition dbt_types.F:1514
subroutine, public dbt_distribution_destroy(dist)
Destroy tensor distribution.
Definition dbt_types.F:915
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
integer, parameter, public default_output_unit
Definition machine.F:48
Interface to the message passing library MPI.
subroutine, public mp_world_init(mp_comm)
initializes the system default communicator
subroutine, public mp_world_finalize()
Finalize the system default communicator and MPI when CP2K owns MPI.
Fortran API for the offload package, which is written in C.
Definition offload_api.F:12
subroutine, public offload_set_chosen_device(device_id)
Selects the chosen device to be used.
integer function, public offload_get_device_count()
Returns the number of available devices.