Skip to content

Commit 2b34ed0

Browse files
authored
Merge pull request #49 from sourceryinstitute/add-bins
refac(data_partition_t): simplify by using bin_t
2 parents 1dd6f33 + c682440 commit 2b34ed0

File tree

4 files changed

+12
-41
lines changed

4 files changed

+12
-41
lines changed

src/sourcery/bin_m.f90

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,17 @@ elemental module function construct(num_items, num_bins, bin_number) result(bin)
2626

2727
interface
2828

29-
elemental module function first(self, bin_number) result(first_item_number)
29+
elemental module function first(self) result(first_item_number)
3030
!! the result is the first item number associated with the given bin
3131
implicit none
3232
class(bin_t), intent(in) :: self
33-
integer, intent(in) :: bin_number
3433
integer first_item_number
3534
end function
3635

37-
elemental module function last(self, bin_number) result(last_item_number)
36+
elemental module function last(self) result(last_item_number)
3837
!! the result is the last item number associated with the given bin
3938
implicit none
4039
class(bin_t), intent(in) :: self
41-
integer, intent(in) :: bin_number
4240
integer last_item_number
4341
end function
4442

src/sourcery/data_partition_m.f90

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ module data_partition_m
1818
generic :: gather => gather_real32_2D_array, gather_real64_2D_array, gather_real32_1D_array, gather_real64_1D_array
1919
end type
2020

21-
integer, allocatable :: first_datum(:), last_datum(:)
22-
2321
interface
2422

2523
module subroutine define_partitions(cardinality)

src/sourcery/data_partition_s.f90

+8-33
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,26 @@
11
submodule(data_partition_m) data_partition_s
22
use assert_m, only : assert
3+
use bin_m, only : bin_t
34
implicit none
45

56
logical, parameter :: verbose=.false.
7+
type(bin_t), allocatable :: bin(:)
68

79
contains
810

911
module procedure define_partitions
10-
11-
if (allocated(first_datum)) deallocate(first_datum)
12-
if (allocated(last_datum)) deallocate(last_datum)
13-
14-
associate( ni => num_images() )
15-
16-
call assert( ni<=cardinality, "sufficient data for distribution across images", cardinality)
17-
18-
allocate(first_datum(ni), last_datum(ni))
19-
20-
block
21-
integer i, image
22-
do image=1,ni
23-
associate( remainder => mod(cardinality, ni), quotient => cardinality/ni )
24-
first_datum(image) = sum([(quotient+overflow(i, remainder), i=1, image-1)]) + 1
25-
last_datum(image) = first_datum(image) + quotient + overflow(image, remainder) - 1
26-
end associate
27-
end do
28-
end block
29-
end associate
30-
31-
contains
32-
33-
pure function overflow(im, excess) result(extra_datum)
34-
integer, intent(in) :: im, excess
35-
integer extra_datum
36-
extra_datum= merge(1,0,im<=excess)
37-
end function
38-
12+
integer image
13+
bin = [( bin_t(num_items=cardinality, num_bins=num_images(), bin_number=image), image=1,num_images() )]
3914
end procedure
4015

4116
module procedure first
42-
call assert( allocated(first_datum), "allocated(first_datum)")
43-
first_index= first_datum( image_number )
17+
call assert( allocated(bin), "data_partition_s(first): allocated(bin)")
18+
first_index = bin(image_number)%first()
4419
end procedure
4520

4621
module procedure last
47-
call assert( allocated(last_datum), "allocated(last_datum)")
48-
last_index = last_datum( image_number )
22+
call assert( allocated(bin), "data_partition_s(last): allocated(bin)")
23+
last_index = bin(image_number)%last()
4924
end procedure
5025

5126
module procedure gather_real32_1D_array

test/bin_test.f90

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function verify_block_partitioning() result(test_passes)
5151
integer b
5252

5353
bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )]
54-
associate(in_bin => [(bins(b)%last(b) - bins(b)%first(b) + 1, b = 1, n_bins)])
54+
associate(in_bin => [(bins(b)%last() - bins(b)%first() + 1, b = 1, n_bins)])
5555
associate(remainder => mod(n_items, n_bins), items_per_bin => n_items/n_bins)
5656
test_passes = all([(in_bin(1:remainder) == items_per_bin + 1)]) .and. all([(in_bin(remainder+1:) == items_per_bin)])
5757
end associate
@@ -69,7 +69,7 @@ function verify_all_items_partitioned() result(test_passes)
6969
integer b
7070

7171
bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )]
72-
test_passes = sum([(bins(b)%last(b) - bins(b)%first(b) + 1, b = 1, n_bins)]) == n_items
72+
test_passes = sum([(bins(b)%last() - bins(b)%first() + 1, b = 1, n_bins)]) == n_items
7373

7474
end function
7575

0 commit comments

Comments
 (0)