|
| 1 | +module data_partition_test |
| 2 | + !! author: Damian Rouson |
| 3 | + !! |
| 4 | + !! summary: verify data partitioning across images and data gathering |
| 5 | + use vegetables, only: result_t, test_item_t, describe, it, assert_equals, assert_that |
| 6 | + use data_partition_interface, only : data_partition |
| 7 | + use iso_fortran_env, only : real64 |
| 8 | + implicit none |
| 9 | + |
| 10 | + private |
| 11 | + public :: test_data_partition |
| 12 | + |
| 13 | + type(data_partition) partition |
| 14 | + integer, parameter :: num_particles=31, gatherer=1, num_steps=9 |
| 15 | + |
| 16 | +contains |
| 17 | + |
| 18 | + function test_data_partition() result(tests) |
| 19 | + type(test_item_t) tests |
| 20 | + |
| 21 | + integer iteration |
| 22 | + |
| 23 | + call partition%define_partitions( cardinality=num_particles) |
| 24 | + |
| 25 | + associate( me=>this_image() ) |
| 26 | + associate( my_first=>partition%first(me), my_last=>partition%last(me) ) |
| 27 | + !do iteration=1,num_steps |
| 28 | + tests = describe( & |
| 29 | + "data_partition class", & |
| 30 | + [it( & |
| 31 | + "partitions data in nearly even blocks", & |
| 32 | + verify_block_partitioning), & |
| 33 | + it( & |
| 34 | + "all data partitioned across all images without data loss", & |
| 35 | + verify_all_particles_partitioned), & |
| 36 | + it( & |
| 37 | + "1D real array gathered on all images", & |
| 38 | + verify_all_gather_1D_real_array), & |
| 39 | + it( & |
| 40 | + "dimension 1 of 2D real array gathered on all images witout dim argument", & |
| 41 | + verify_all_gather_2D_real_array), & |
| 42 | + it( & |
| 43 | + "dimension 1 of 2D real array gathered on all images with dim argument", & |
| 44 | + verify_all_gather_2D_real_array_dim1), & |
| 45 | + it( & |
| 46 | + "dimension 1 of 2D real array gathered onto result_image with dim argument", & |
| 47 | + verify_gather_2D_real_array_dim1), & |
| 48 | + it( & |
| 49 | + "distributes all particles without losing any", & |
| 50 | + verify_all_particles_partitioned)]) |
| 51 | + !end do |
| 52 | + end associate |
| 53 | + end associate |
| 54 | + end function |
| 55 | + |
| 56 | + function verify_block_partitioning() result(result_) |
| 57 | + !! Verify that the data is partitioned across images evenly to |
| 58 | + !! within a difference of one datum between any two images. |
| 59 | + type(data_partition) partition |
| 60 | + type(result_t) result_ |
| 61 | + integer my_particles |
| 62 | + |
| 63 | + associate( me=>this_image() ) |
| 64 | + associate( my_first=>partition%first(me), my_last=>partition%last(me) ) |
| 65 | + my_particles = my_last - my_first + 1 |
| 66 | + associate( ni=>num_images() ) |
| 67 | + associate( quotient=>num_particles/ni, remainder=>mod(num_particles,ni) ) |
| 68 | + result_ = assert_equals( quotient + merge(1, 0, me<=remainder), my_particles, "block distribution" ) |
| 69 | + end associate |
| 70 | + end associate |
| 71 | + end associate |
| 72 | + end associate |
| 73 | + end function |
| 74 | + |
| 75 | + function verify_all_particles_partitioned() result(result_) |
| 76 | + !! Verify that the number of particles on each image sums to the |
| 77 | + !! total number of particles distributed. |
| 78 | + type(data_partition) partition |
| 79 | + type(result_t) result_ |
| 80 | + integer particles |
| 81 | + |
| 82 | + associate(me => this_image()) |
| 83 | + associate( my_first=>partition%first(me), my_last=>partition%last(me) ) |
| 84 | + particles = my_last - my_first + 1 |
| 85 | + call co_sum(particles) |
| 86 | + result_ = assert_equals(num_particles, particles, "all particles distributed" ) |
| 87 | + end associate |
| 88 | + end associate |
| 89 | + end function |
| 90 | + |
| 91 | + function verify_all_gather_1D_real_array() result(result_) |
| 92 | + type(data_partition) partition |
| 93 | + type(result_t) result_ |
| 94 | + real(real64) :: particle_scalar(num_particles) |
| 95 | + real(real64), parameter :: junk=-12345._real64, expected=1._real64 |
| 96 | + |
| 97 | + associate(me => this_image()) |
| 98 | + associate( first=>partition%first(me), last=>partition%last(me) ) |
| 99 | + |
| 100 | + particle_scalar(first:last) = expected !! values to be gathered |
| 101 | + particle_scalar(1:first-1) = junk !! values to be overwritten by the gather |
| 102 | + particle_scalar(last+1:) = junk !! values to be overwritten by the gather |
| 103 | + |
| 104 | + call partition%gather(particle_scalar) |
| 105 | + |
| 106 | + result_ = assert_that( all(particle_scalar==expected), "real 1D array all-gathered" ) |
| 107 | + |
| 108 | + end associate |
| 109 | + end associate |
| 110 | + end function |
| 111 | + |
| 112 | + function verify_all_gather_2D_real_array() result(result_) |
| 113 | + type(data_partition) partition |
| 114 | + type(result_t) result_ |
| 115 | + integer, parameter :: vec_space_dim=3 |
| 116 | + real(real64) particle_vector(vec_space_dim, num_particles) |
| 117 | + real(real64), parameter :: junk=-12345._real64, expected=1._real64 |
| 118 | + |
| 119 | + associate(me => this_image()) |
| 120 | + associate( first=>partition%first(me), last=>partition%last(me) ) |
| 121 | + |
| 122 | + particle_vector(:, first:last) = expected !! values to be gathered |
| 123 | + particle_vector(:, 1:first-1) = junk !! values to be overwritten by the gather |
| 124 | + particle_vector(:, last+1:) = junk !! values to be overwritten by the gather |
| 125 | + |
| 126 | + call partition%gather(particle_vector) |
| 127 | + |
| 128 | + result_ = assert_that(all(particle_vector==expected), "real 2D array all-gathered implicitly along dimension 1" ) |
| 129 | + |
| 130 | + end associate |
| 131 | + end associate |
| 132 | + end function |
| 133 | + |
| 134 | + function verify_all_gather_2D_real_array_dim1() result(result_) |
| 135 | + type(data_partition) partition |
| 136 | + type(result_t) result_ |
| 137 | + integer, parameter :: vec_space_dim=3 |
| 138 | + real(real64) :: vector_transpose(num_particles, vec_space_dim) |
| 139 | + real(real64), parameter :: junk=-12345._real64, expected=1._real64 |
| 140 | + |
| 141 | + associate(me => this_image()) |
| 142 | + associate( first=>partition%first(me), last=>partition%last(me) ) |
| 143 | + |
| 144 | + vector_transpose(first:last, :) = expected !! values to be gathered |
| 145 | + vector_transpose(1:first-1, :) = junk !! values to be overwritten by the gather |
| 146 | + vector_transpose(last+1:, :) = junk !! values to be overwritten by the gather |
| 147 | + |
| 148 | + call partition%gather( vector_transpose, dim=1) |
| 149 | + |
| 150 | + result_ = assert_that(all(vector_transpose==expected), "vector_transpose gathered explicitly along dimension 1" ) |
| 151 | + |
| 152 | + end associate |
| 153 | + end associate |
| 154 | + end function |
| 155 | + |
| 156 | + function verify_gather_2D_real_array_dim1() result(result_) |
| 157 | + type(data_partition) partition |
| 158 | + type(result_t) result_ |
| 159 | + integer, parameter :: vec_space_dim=3 |
| 160 | + real(real64) :: vector_transpose(num_particles, vec_space_dim) |
| 161 | + real(real64), parameter :: junk=-12345._real64, expected=1._real64 |
| 162 | + |
| 163 | + associate(me => this_image()) |
| 164 | + associate( first=>partition%first(me), last=>partition%last(me) ) |
| 165 | + |
| 166 | + vector_transpose(first:last, :) = expected !! values to be gathered |
| 167 | + vector_transpose(1:first-1, :) = junk !! values to be overwritten by the gather |
| 168 | + vector_transpose(last+1:, :) = junk !! values to be overwritten by the gather |
| 169 | + |
| 170 | + call partition%gather( vector_transpose, result_image=gatherer, dim=1) |
| 171 | + |
| 172 | + if (me==gatherer) then |
| 173 | + result_ = assert_that(all(vector_transpose==expected), "all( particle_vector==expected)") |
| 174 | + else |
| 175 | + result_ = & |
| 176 | + assert_that(all(vector_transpose(1:first-1,:)==junk), "lower transpose data unchanged)") .and. & |
| 177 | + assert_that(all(vector_transpose(first:last,:)==expected), "expected transpose data gathered") .and. & |
| 178 | + assert_that(all(vector_transpose(last+1:,:)==junk), "upper transpose data unchanged)" ) |
| 179 | + end if |
| 180 | + |
| 181 | + end associate |
| 182 | + end associate |
| 183 | + end function |
| 184 | + |
| 185 | +end module data_partition_test |
0 commit comments