Skip to content

Commit 262df7a

Browse files
author
Damian Rouson
committed
feat(data_partition): support real{32,64} gathers
1 parent e20b6cc commit 262df7a

File tree

2 files changed

+123
-12
lines changed

2 files changed

+123
-12
lines changed

src/data-partition-implementation.F90

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
associate( ni => num_images() )
1515

16-
call assert( ni<=cardinality, "sufficient data for distribution across images")
16+
call assert( ni<=cardinality, "sufficient data for distribution across images", cardinality)
1717

1818
allocate(first_datum(ni), last_datum(ni))
1919

@@ -54,7 +54,39 @@ pure function overflow(im, excess) result(extra_datum)
5454
last_index = last_datum( image_number )
5555
end procedure
5656

57-
module procedure gather_real_1D_array
57+
module procedure gather_real32_1D_array
58+
59+
if (present(dim)) call assert (dim==1, "dimensioned partitioned == 1")
60+
61+
associate( me => this_image() )
62+
if (verbose) then
63+
write(6,*) 'gather_real_1D_array(): executing on image', me
64+
flush(6)
65+
end if
66+
associate( first=>first(me), last=>last(me) )
67+
if (.not. present(result_image)) then
68+
a(1:first-1) = 0.
69+
a(last+1:) = 0.
70+
call co_sum(a)
71+
else
72+
block
73+
real(real32), allocatable, dimension(:) :: a_lower, a_upper
74+
a_lower = a(1:first-1)
75+
a_upper = a(last+1:)
76+
a(1:first-1) = 0.
77+
a(last+1:) = 0.
78+
call co_sum(a, result_image=result_image)
79+
if (result_image /= me) then
80+
a(1:first-1) = a_lower
81+
a(last+1:) = a_upper
82+
end if
83+
end block
84+
end if
85+
end associate
86+
end associate
87+
end procedure
88+
89+
module procedure gather_real64_1D_array
5890

5991
if (present(dim)) call assert (dim==1, "dimensioned partitioned == 1")
6092

@@ -86,7 +118,72 @@ pure function overflow(im, excess) result(extra_datum)
86118
end associate
87119
end procedure
88120

89-
module procedure gather_real_2D_array
121+
module procedure gather_real32_2D_array
122+
123+
integer dim_
124+
if (present(dim)) then
125+
dim_ = dim
126+
else
127+
dim_ = 2
128+
end if
129+
130+
associate( me => this_image() )
131+
if (verbose) then
132+
write(6,*) 'gather_real32_2D_array(): executing on image', me
133+
flush(6)
134+
end if
135+
associate( first => first(me), last => last(me) )
136+
if (.not. present(result_image)) then
137+
select case(dim_)
138+
case(1)
139+
a(1:first-1, :) = 0.
140+
a(last+1:, :) = 0.
141+
case(2)
142+
a(:, 1:first-1) = 0.
143+
a(:, last+1:) = 0.
144+
case default
145+
error stop "gather_real32_2D_array: invalid dim argument"
146+
end select
147+
call co_sum(a)
148+
else
149+
block
150+
real(real32), allocatable, dimension(:,:) :: a_lower, a_upper
151+
select case(dim_)
152+
case(1)
153+
a_lower = a(1:first-1, :)
154+
a_upper = a(last+1:, :)
155+
a(1:first-1, :) = 0.
156+
a(last+1:, :) = 0.
157+
case(2)
158+
a_lower = a(:, 1:first-1)
159+
a_upper = a(:, last+1:)
160+
a(:, 1:first-1) = 0.
161+
a(:, last+1:) = 0.
162+
case default
163+
error stop "gather_real32_2D_array: invalid dim argument"
164+
end select
165+
166+
call co_sum(a, result_image=result_image)
167+
168+
if (result_image /= me) then
169+
select case(dim_)
170+
case(1)
171+
a(1:first-1, :) = a_lower
172+
a(last+1:, :) = a_upper
173+
case(2)
174+
a(:, 1:first-1) = a_lower
175+
a(:, last+1:) = a_upper
176+
case default
177+
error stop "gather_real32_2D_array: invalid dim argument"
178+
end select
179+
end if
180+
end block
181+
end if
182+
end associate
183+
end associate
184+
end procedure
185+
186+
module procedure gather_real64_2D_array
90187

91188
integer dim_
92189
if (present(dim)) then
@@ -97,7 +194,7 @@ pure function overflow(im, excess) result(extra_datum)
97194

98195
associate( me => this_image() )
99196
if (verbose) then
100-
write(6,*) 'gather_real_2D_array(): executing on image', me
197+
write(6,*) 'gather_real64_2D_array(): executing on image', me
101198
flush(6)
102199
end if
103200
associate( first => first(me), last => last(me) )
@@ -110,7 +207,7 @@ pure function overflow(im, excess) result(extra_datum)
110207
a(:, 1:first-1) = 0.
111208
a(:, last+1:) = 0.
112209
case default
113-
error stop "gather_real_2D_array: invalid dim argument"
210+
error stop "gather_real64_2D_array: invalid dim argument"
114211
end select
115212
call co_sum(a)
116213
else
@@ -128,7 +225,7 @@ pure function overflow(im, excess) result(extra_datum)
128225
a(:, 1:first-1) = 0.
129226
a(:, last+1:) = 0.
130227
case default
131-
error stop "gather_real_2D_array: invalid dim argument"
228+
error stop "gather_real64_2D_array: invalid dim argument"
132229
end select
133230

134231
call co_sum(a, result_image=result_image)
@@ -142,7 +239,7 @@ pure function overflow(im, excess) result(extra_datum)
142239
a(:, 1:first-1) = a_lower
143240
a(:, last+1:) = a_upper
144241
case default
145-
error stop "gather_real_2D_array: invalid dim argument"
242+
error stop "gather_real64_2D_array: invalid dim argument"
146243
end select
147244
end if
148245
end block

src/data-partition-interface.f90

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module data_partition_interface
22
!! distribute data identification numbers across images such that the number of
33
!! items differs by at most 1 between any two images.
4-
use iso_fortran_env, only : real64
4+
use iso_fortran_env, only : real32, real64
55
implicit none
66

77
private
@@ -14,8 +14,8 @@ module data_partition_interface
1414
procedure, nopass :: define_partitions
1515
procedure, nopass :: first
1616
procedure, nopass :: last
17-
procedure, nopass, private :: gather_real_2D_array, gather_real_1D_array
18-
generic :: gather => gather_real_2D_array, gather_real_1D_array
17+
procedure, nopass, private :: gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
18+
generic :: gather => gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
1919
end type
2020

2121
integer, allocatable :: first_datum(:), last_datum(:)
@@ -45,14 +45,28 @@ pure module function last(image_number) result(last_index)
4545
!! 1. Near the beginning/end of execution to amortize costs across an entire run or
4646
!! 2. Temporarily while developing/debugging code.
4747

48-
module subroutine gather_real_1D_array( a, result_image, dim )
48+
module subroutine gather_real32_1D_array( a, result_image, dim )
49+
!! Gather the elements of an 1D array distributed along dimension dim onto result_image
50+
real(real32), intent(inout) :: a(:)
51+
integer, intent(in), optional :: result_image
52+
integer, intent(in), optional :: dim
53+
end subroutine
54+
55+
module subroutine gather_real64_1D_array( a, result_image, dim )
4956
!! Gather the elements of an 1D array distributed along dimension dim onto result_image
5057
real(real64), intent(inout) :: a(:)
5158
integer, intent(in), optional :: result_image
5259
integer, intent(in), optional :: dim
5360
end subroutine
5461

55-
module subroutine gather_real_2D_array( a, result_image, dim )
62+
module subroutine gather_real32_2D_array( a, result_image, dim )
63+
!! Gather the elements of an 2D array distributed along dimension dim onto result_image
64+
real(real32), intent(inout) :: a(:,:)
65+
integer, intent(in), optional :: result_image
66+
integer, intent(in), optional :: dim
67+
end subroutine
68+
69+
module subroutine gather_real64_2D_array( a, result_image, dim )
5670
!! Gather the elements of an 2D array distributed along dimension dim onto result_image
5771
real(real64), intent(inout) :: a(:,:)
5872
integer, intent(in), optional :: result_image

0 commit comments

Comments
 (0)