|
| 1 | +submodule(data_partition_interface) data_partition_implementation |
| 2 | + use assertions_interface, only : assert, assertions |
| 3 | + implicit none |
| 4 | + |
| 5 | +contains |
| 6 | + |
| 7 | + module procedure define_partitions |
| 8 | + |
| 9 | + if (allocated(first_datum)) deallocate(first_datum) |
| 10 | + if (allocated(last_datum)) deallocate(last_datum) |
| 11 | + |
| 12 | + associate( ni => num_images() ) |
| 13 | + |
| 14 | + call assert( ni<=cardinality, "sufficient data for distribution across images") |
| 15 | + |
| 16 | + allocate(first_datum(ni), last_datum(ni)) |
| 17 | + |
| 18 | + block |
| 19 | + integer i, image |
| 20 | + do image=1,ni |
| 21 | + associate( remainder => mod(cardinality, ni), quotient => cardinality/ni ) |
| 22 | + first_datum(image) = sum([(quotient+overflow(i, remainder), i=1, image-1)]) + 1 |
| 23 | + last_datum(image) = first_datum(image) + quotient + overflow(image, remainder) - 1 |
| 24 | + end associate |
| 25 | + end do |
| 26 | + end block |
| 27 | + end associate |
| 28 | + |
| 29 | +#ifdef FORD |
| 30 | + end procedure |
| 31 | +#else |
| 32 | + contains |
| 33 | +#endif |
| 34 | + |
| 35 | + pure function overflow(im, excess) result(extra_datum) |
| 36 | + integer, intent(in) :: im, excess |
| 37 | + integer extra_datum |
| 38 | + extra_datum= merge(1,0,im<=excess) |
| 39 | + end function |
| 40 | + |
| 41 | +#ifndef FORD |
| 42 | + end procedure |
| 43 | +#endif |
| 44 | + |
| 45 | + module procedure first |
| 46 | + if (assertions) call assert( allocated(first_datum), "allocated(first_datum)") |
| 47 | + first_index= first_datum( image_number ) |
| 48 | + end procedure |
| 49 | + |
| 50 | + module procedure last |
| 51 | + if (assertions) call assert( allocated(last_datum), "allocated(last_datum)") |
| 52 | + last_index = last_datum( image_number ) |
| 53 | + end procedure |
| 54 | + |
| 55 | + module procedure gather_real_1D_array |
| 56 | + |
| 57 | + if (present(dim)) call assert (dim==1, "dimensioned partitioned == 1") |
| 58 | + |
| 59 | + associate( me => this_image() ) |
| 60 | + write(6,*) 'gather_real_1D_array(): executing on image', me |
| 61 | + flush(6) |
| 62 | + associate( first=>first(me), last=>last(me) ) |
| 63 | + if (.not. present(result_image)) then |
| 64 | + a(1:first-1) = 0. |
| 65 | + a(last+1:) = 0. |
| 66 | + call co_sum(a) |
| 67 | + else |
| 68 | + block |
| 69 | + real(real64), allocatable, dimension(:) :: a_lower, a_upper |
| 70 | + a_lower = a(1:first-1) |
| 71 | + a_upper = a(last+1:) |
| 72 | + a(1:first-1) = 0. |
| 73 | + a(last+1:) = 0. |
| 74 | + call co_sum(a, result_image=result_image) |
| 75 | + if (result_image /= me) then |
| 76 | + a(1:first-1) = a_lower |
| 77 | + a(last+1:) = a_upper |
| 78 | + end if |
| 79 | + end block |
| 80 | + end if |
| 81 | + end associate |
| 82 | + end associate |
| 83 | + end procedure |
| 84 | + |
| 85 | + module procedure gather_real_2D_array |
| 86 | + |
| 87 | + integer dim_ |
| 88 | + if (present(dim)) then |
| 89 | + dim_ = dim |
| 90 | + else |
| 91 | + dim_ = 2 |
| 92 | + end if |
| 93 | + |
| 94 | + associate( me => this_image() ) |
| 95 | + write(6,*) 'gather_real_2D_array(): executing on image', me |
| 96 | + flush(6) |
| 97 | + associate( first => first(me), last => last(me) ) |
| 98 | + if (.not. present(result_image)) then |
| 99 | + select case(dim_) |
| 100 | + case(1) |
| 101 | + a(1:first-1, :) = 0. |
| 102 | + a(last+1:, :) = 0. |
| 103 | + case(2) |
| 104 | + a(:, 1:first-1) = 0. |
| 105 | + a(:, last+1:) = 0. |
| 106 | + case default |
| 107 | + error stop "gather_real_2D_array: invalid dim argument" |
| 108 | + end select |
| 109 | + call co_sum(a) |
| 110 | + else |
| 111 | + block |
| 112 | + real(real64), allocatable, dimension(:,:) :: a_lower, a_upper |
| 113 | + select case(dim_) |
| 114 | + case(1) |
| 115 | + a_lower = a(1:first-1, :) |
| 116 | + a_upper = a(last+1:, :) |
| 117 | + a(1:first-1, :) = 0. |
| 118 | + a(last+1:, :) = 0. |
| 119 | + case(2) |
| 120 | + a_lower = a(:, 1:first-1) |
| 121 | + a_upper = a(:, last+1:) |
| 122 | + a(:, 1:first-1) = 0. |
| 123 | + a(:, last+1:) = 0. |
| 124 | + case default |
| 125 | + error stop "gather_real_2D_array: invalid dim argument" |
| 126 | + end select |
| 127 | + |
| 128 | + call co_sum(a, result_image=result_image) |
| 129 | + |
| 130 | + if (result_image /= me) then |
| 131 | + select case(dim_) |
| 132 | + case(1) |
| 133 | + a(1:first-1, :) = a_lower |
| 134 | + a(last+1:, :) = a_upper |
| 135 | + case(2) |
| 136 | + a(:, 1:first-1) = a_lower |
| 137 | + a(:, last+1:) = a_upper |
| 138 | + case default |
| 139 | + error stop "gather_real_2D_array: invalid dim argument" |
| 140 | + end select |
| 141 | + end if |
| 142 | + end block |
| 143 | + end if |
| 144 | + end associate |
| 145 | + end associate |
| 146 | + end procedure |
| 147 | + |
| 148 | +end submodule data_partition_implementation |
0 commit comments