Skip to content

Commit 0b9b504

Browse files
committed
refac(data_partition_t):make bin array a component
1 parent 423f091 commit 0b9b504

File tree

3 files changed

+61
-40
lines changed

3 files changed

+61
-40
lines changed

src/sourcery/sourcery_data_partition_m.f90

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module sourcery_data_partition_m
22
!! distribute data identification numbers across images such that the number of
33
!! items differs by at most 1 between any two images.
44
use iso_fortran_env, only : real32, real64
5+
use sourcery_bin_m, only : bin_t
56
implicit none
67

78
private
@@ -10,31 +11,36 @@ module sourcery_data_partition_m
1011
type data_partition_t
1112
!! encapsulate a description of the data subset the executing image owns
1213
private
14+
type(bin_t), allocatable :: bin(:)
1315
contains
14-
procedure, nopass :: define_partitions
15-
procedure, nopass :: first
16-
procedure, nopass :: last
17-
procedure, nopass, private :: gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
16+
procedure :: define_partitions
17+
procedure :: first
18+
procedure :: last
19+
procedure, private :: gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
1820
generic :: gather => gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
1921
end type
2022

2123
interface
2224

23-
module subroutine define_partitions(cardinality)
25+
module subroutine define_partitions(self, cardinality)
2426
!! define the range of data identification numbers owned by the executing image
27+
implicit none
28+
class(data_partition_t), intent(out) :: self
2529
integer, intent(in) :: cardinality
2630
end subroutine
2731

28-
pure module function first(image_number) result(first_index)
32+
pure module function first(self, image_number) result(first_index)
2933
!! the result is the first identification number owned by the executing image
3034
implicit none
35+
class(data_partition_t), intent(in) :: self
3136
integer, intent(in), optional :: image_number
3237
integer first_index
3338
end function
3439

35-
pure module function last(image_number) result(last_index)
40+
pure module function last(self, image_number) result(last_index)
3641
!! the result is the last identification number owned by the executing image
3742
implicit none
43+
class(data_partition_t), intent(in) :: self
3844
integer, intent(in), optional :: image_number
3945
integer last_index
4046
end function
@@ -43,29 +49,37 @@ pure module function last(image_number) result(last_index)
4349
!! 1. Near the beginning/end of execution to amortize costs across an entire run or
4450
!! 2. Temporarily while developing/debugging code.
4551

46-
module subroutine gather_real32_1D_array( a, result_image, dim )
52+
module subroutine gather_real32_1D_array(self, a, result_image, dim )
4753
!! Gather the elements of an 1D array distributed along dimension dim onto result_image
54+
implicit none
55+
class(data_partition_t), intent(in) :: self
4856
real(real32), intent(inout) :: a(:)
4957
integer, intent(in), optional :: result_image
5058
integer, intent(in), optional :: dim
5159
end subroutine
5260

53-
module subroutine gather_real64_1D_array( a, result_image, dim )
61+
module subroutine gather_real64_1D_array(self, a, result_image, dim )
5462
!! Gather the elements of an 1D array distributed along dimension dim onto result_image
63+
implicit none
64+
class(data_partition_t), intent(in) :: self
5565
real(real64), intent(inout) :: a(:)
5666
integer, intent(in), optional :: result_image
5767
integer, intent(in), optional :: dim
5868
end subroutine
5969

60-
module subroutine gather_real32_2D_array( a, result_image, dim )
70+
module subroutine gather_real32_2D_array(self, a, result_image, dim )
6171
!! Gather the elements of an 2D array distributed along dimension dim onto result_image
72+
implicit none
73+
class(data_partition_t), intent(in) :: self
6274
real(real32), intent(inout) :: a(:,:)
6375
integer, intent(in), optional :: result_image
6476
integer, intent(in), optional :: dim
6577
end subroutine
6678

67-
module subroutine gather_real64_2D_array( a, result_image, dim )
79+
module subroutine gather_real64_2D_array(self, a, result_image, dim )
6880
!! Gather the elements of an 2D array distributed along dimension dim onto result_image
81+
implicit none
82+
class(data_partition_t), intent(in) :: self
6983
real(real64), intent(inout) :: a(:,:)
7084
integer, intent(in), optional :: result_image
7185
integer, intent(in), optional :: dim

src/sourcery/sourcery_data_partition_s.f90

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
submodule(sourcery_data_partition_m) sourcery_data_partition_s
22
use assert_m, only : assert
3-
use sourcery_bin_m, only : bin_t
43
implicit none
54

65
logical, parameter :: verbose=.false.
7-
type(bin_t), allocatable :: bin(:)
86

97
contains
108

119
module procedure define_partitions
1210
integer image
13-
bin = [( bin_t(num_items=cardinality, num_bins=num_images(), bin_number=image), image=1,num_images() )]
11+
self%bin = [( bin_t(num_items=cardinality, num_bins=num_images(), bin_number=image), image=1,num_images() )]
1412
end procedure
1513

1614
module procedure first
1715
integer image
1816

19-
call assert( allocated(bin), "data_partition_s(first): allocated(bin)")
17+
call assert( allocated(self%bin), "data_partition_s(first): allocated(self%bin)")
2018

2119
if (present(image_number)) then
2220
image = image_number
2321
else
2422
image = this_image()
2523
end if
26-
first_index = bin(image)%first()
24+
first_index = self%bin(image)%first()
2725
end procedure
2826

2927
module procedure last
3028
integer image
3129

32-
call assert( allocated(bin), "data_partition_s(last): allocated(bin)")
30+
call assert( allocated(self%bin), "data_partition_s(last): allocated(self%in)")
3331

3432
if (present(image_number)) then
3533
image = image_number
3634
else
3735
image = this_image()
3836
end if
39-
last_index = bin(image)%last()
37+
last_index = self%bin(image)%last()
4038
end procedure
4139

4240
module procedure gather_real32_1D_array
@@ -48,7 +46,7 @@
4846
write(6,*) 'gather_real_1D_array(): executing on image', me
4947
flush(6)
5048
end if
51-
associate( first=>first(me), last=>last(me) )
49+
associate(first=>self%first(me), last=>self%last(me))
5250
if (.not. present(result_image)) then
5351
a(1:first-1) = 0.
5452
a(last+1:) = 0.
@@ -80,7 +78,7 @@
8078
write(6,*) 'gather_real_1D_array(): executing on image', me
8179
flush(6)
8280
end if
83-
associate( first=>first(me), last=>last(me) )
81+
associate(first=>self%first(me), last=>self%last(me))
8482
if (.not. present(result_image)) then
8583
a(1:first-1) = 0.
8684
a(last+1:) = 0.
@@ -117,7 +115,7 @@
117115
write(6,*) 'gather_real32_2D_array(): executing on image', me
118116
flush(6)
119117
end if
120-
associate( first => first(me), last => last(me) )
118+
associate(first=>self%first(me), last=>self%last(me))
121119
if (.not. present(result_image)) then
122120
select case(dim_)
123121
case(1)
@@ -182,7 +180,7 @@
182180
write(6,*) 'gather_real64_2D_array(): executing on image', me
183181
flush(6)
184182
end if
185-
associate( first => first(me), last => last(me) )
183+
associate(first => self%first(me), last => self%last(me))
186184
if (.not. present(result_image)) then
187185
select case(dim_)
188186
case(1)

test/data_partition_test.f90

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,18 @@ pure function subject() result(specimen)
2626
function results() result(test_results)
2727
type(test_result_t), allocatable :: test_results(:)
2828

29-
call partition%define_partitions(cardinality=num_particles)
30-
31-
associate( me=>this_image() )
32-
associate( my_first=>partition%first(me), my_last=>partition%last(me) )
33-
test_results = [ &
34-
test_result_t("partitioning data in nearly even blocks", verify_block_partitioning()), &
35-
test_result_t("default image_number is this_image()", verify_default_image_number()), &
36-
test_result_t("partitioning all data across all images without data loss", verify_all_particles_partitioned()), &
37-
test_result_t("gathering a 1D real array onto all images", verify_all_gather_1D_real_array()), &
38-
test_result_t("gathering dimension 1 of 2D real array onto all images witout dim argument", &
39-
verify_all_gather_2D_real_array()), &
40-
test_result_t("gathering dimension 1 of 2D real array onton all images with dim argument", &
41-
verify_all_gather_2D_real_array_dim1()), &
42-
test_result_t("gathering dimension 1 of 2D real array onto result_image with dim argument", &
43-
verify_gather_2D_real_array_dim1()) &
44-
]
45-
end associate
46-
end associate
29+
test_results = [ &
30+
test_result_t("partitioning data in nearly even blocks", verify_block_partitioning()), &
31+
test_result_t("default image_number is this_image()", verify_default_image_number()), &
32+
test_result_t("partitioning all data across all images without data loss", verify_all_particles_partitioned()), &
33+
test_result_t("gathering a 1D real array onto all images", verify_all_gather_1D_real_array()), &
34+
test_result_t("gathering dimension 1 of 2D real array onto all images witout dim argument", &
35+
verify_all_gather_2D_real_array()), &
36+
test_result_t("gathering dimension 1 of 2D real array onton all images with dim argument", &
37+
verify_all_gather_2D_real_array_dim1()), &
38+
test_result_t("gathering dimension 1 of 2D real array onto result_image with dim argument", &
39+
verify_gather_2D_real_array_dim1()) &
40+
]
4741
end function
4842

4943
function verify_block_partitioning() result(test_passes)
@@ -53,6 +47,8 @@ function verify_block_partitioning() result(test_passes)
5347
logical test_passes
5448
integer my_particles
5549

50+
call partition%define_partitions(cardinality=num_particles)
51+
5652
associate( me=>this_image() )
5753
associate( my_first=>partition%first(me), my_last=>partition%last(me) )
5854
my_particles = my_last - my_first + 1
@@ -63,13 +59,16 @@ function verify_block_partitioning() result(test_passes)
6359
end associate
6460
end associate
6561
end associate
62+
6663
end function
6764

6865
function verify_default_image_number() result(test_passes)
6966
!! Verify that the first and last functions assume image_number == this_image() if image_number is not present
7067
type(data_partition_t) partition
7168
logical test_passes
7269

70+
call partition%define_partitions(cardinality=num_particles)
71+
7372
associate( me=>this_image() )
7473
test_passes = partition%first() == partition%first(me) .and.partition%last() == partition%last(me)
7574
end associate
@@ -82,6 +81,8 @@ function verify_all_particles_partitioned() result(test_passes)
8281
logical test_passes
8382
integer particles
8483

84+
call partition%define_partitions(cardinality=num_particles)
85+
8586
associate(me => this_image())
8687
associate( my_first=>partition%first(me), my_last=>partition%last(me) )
8788
particles = my_last - my_first + 1
@@ -97,6 +98,8 @@ function verify_all_gather_1D_real_array() result(test_passes)
9798
real(real64) :: particle_scalar(num_particles)
9899
real(real64), parameter :: junk=-12345._real64, expected=1._real64
99100

101+
call partition%define_partitions(cardinality=num_particles)
102+
100103
associate(me => this_image())
101104
associate( first=>partition%first(me), last=>partition%last(me) )
102105
particle_scalar(first:last) = expected !! values to be gathered
@@ -115,6 +118,8 @@ function verify_all_gather_2D_real_array() result(test_passes)
115118
real(real64) particle_vector(vec_space_dim, num_particles)
116119
real(real64), parameter :: junk=-12345._real64, expected=1._real64
117120

121+
call partition%define_partitions(cardinality=num_particles)
122+
118123
associate(me => this_image())
119124
associate( first=>partition%first(me), last=>partition%last(me) )
120125

@@ -134,6 +139,8 @@ function verify_all_gather_2D_real_array_dim1() result(test_passes)
134139
real(real64) :: vector_transpose(num_particles, vec_space_dim)
135140
real(real64), parameter :: junk=-12345._real64, expected=1._real64
136141

142+
call partition%define_partitions(cardinality=num_particles)
143+
137144
associate(me => this_image())
138145
associate( first=>partition%first(me), last=>partition%last(me) )
139146

@@ -156,6 +163,8 @@ function verify_gather_2D_real_array_dim1() result(test_passes)
156163
real(real64) :: vector_transpose(num_particles, vec_space_dim)
157164
real(real64), parameter :: junk=-12345._real64, expected=1._real64
158165

166+
call partition%define_partitions(cardinality=num_particles)
167+
159168
associate(me => this_image())
160169
associate( first=>partition%first(me), last=>partition%last(me) )
161170

0 commit comments

Comments
 (0)