Skip to content

Commit 03aacbb

Browse files
authored
Merge pull request #296 from BerkeleyLab/refactor-occupancy_t
Refactor occupancy_t type to reduce memory allocations
2 parents 7852528 + 2725b43 commit 03aacbb

7 files changed

Lines changed: 259 additions & 350 deletions

File tree

‎demo/app/train-cloud-microphysics.F90‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ program train_cloud_microphysics
1717
tensor_map_t, training_configuration_t, training_data_files_t, shuffle
1818

1919
!! Internal dependencies:
20-
use phase_space_bin_m, only : phase_space_bin_t
2120
use NetCDF_file_m, only: NetCDF_file_t
2221
use NetCDF_variable_m, only: NetCDF_variable_t, tensors, time_derivative_t
23-
use occupancy_m, only : occupancy_t
22+
use occupancy_m, only : occupancy_t, phase_space_bin_t
2423
use default_m, only: default_or_internal_read
2524
use time_data_m, only: time_data_t
2625

@@ -316,15 +315,15 @@ subroutine read_train_write(training_configuration, training_data_files, args, p
316315
! Determine the phase-space bin that holds each output tensor
317316
associate(output_minima => output_map%minima(), output_maxima => output_map%maxima())
318317
bin = [(phase_space_bin_t(output_tensors(i), output_minima, output_maxima, args%num_bins), i = 1, size(output_tensors))]
318+
occupancy = occupancy_t(minima = output_minima, maxima = output_maxima, bins_per_dimension = args%num_bins)
319319
end associate
320320

321-
call occupancy%vacate( dims = [( args%num_bins, i = 1, size(derivative,1))] )
322-
323321
print *, "Populate bins"
324322
do i = 1, size(output_tensors)
325-
if (occupancy%occupied(bin(i)%loc)) cycle
326-
call occupancy%occupy(bin(i)%loc)
327-
keepers(i) = .true.
323+
if (.not. occupancy%occupied(bin(i))) then
324+
call occupancy%add(bin(i))
325+
keepers(i) = .true.
326+
end if
328327
end do
329328

330329
print *, "Pack remaining input/output tensor pairs"

‎demo/fpm.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Fiats-Demonstration-Applications"
22

33
[dependencies]
4-
julienne = {git = "https://github.com/berkeleylab/julienne", tag = "3.6.1"}
4+
julienne = {git = "https://github.com/berkeleylab/julienne", tag = "4.1.0"}
55
fiats = {path = "../"}
66
netcdf-interfaces = {git = "https://github.com/berkeleylab/netcdf-interfaces.git", rev = "d2bbb71ac52b4e346b62572b1ca1620134481096"}

‎demo/src/occupancy_m.f90‎

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,88 @@
11
! Copyright (c), The Regents of the University of California
22
! Terms of use are as specified in LICENSE.txt
3+
34
module occupancy_m
45
use iso_fortran_env, only : int64
6+
use tensor_m, only : tensor_t
57
implicit none
68

79
private
810
public :: occupancy_t
11+
public :: phase_space_bin_t
12+
13+
integer, parameter :: default_kind = kind(1)
14+
15+
type phase_space_bin_t
16+
integer, allocatable :: loc(:)
17+
end type
18+
19+
interface phase_space_bin_t
20+
21+
pure module function construct_phase_space_bin(tensor, minima, maxima, bins_per_dimension) result(phase_space_bin)
22+
implicit none
23+
type(tensor_t), intent(in) :: tensor
24+
real, intent(in) :: minima(:), maxima(:)
25+
integer, intent(in) :: bins_per_dimension
26+
type(phase_space_bin_t) phase_space_bin
27+
end function
28+
29+
end interface
30+
31+
type occupancy_t
32+
!! Encapsulate the occupancy status of phase-space bins
33+
private
34+
type(phase_space_bin_t), allocatable :: phase_space_bin_(:)
35+
real, allocatable :: minima_(:), maxima_(:)
36+
integer bins_per_dimension_, num_occupied_
37+
contains
38+
procedure, non_overridable :: occupied
39+
procedure, non_overridable :: add
40+
procedure, non_overridable :: num_occupied
41+
procedure, non_overridable :: num_bins
42+
end type
43+
44+
interface occupancy_t
45+
46+
module pure function construct_occupancy(minima, maxima, bins_per_dimension) result(occupancy)
47+
implicit none
48+
real, intent(in) :: minima(:), maxima(:)
49+
integer, intent(in) :: bins_per_dimension
50+
type(occupancy_t) occupancy
51+
end function
52+
53+
end interface
54+
55+
interface
56+
57+
module subroutine add(self, bin)
58+
!! Add bin to list of occupied bins
59+
implicit none
60+
class(occupancy_t), intent(inout) :: self
61+
type(phase_space_bin_t), intent(in) :: bin
62+
end subroutine
63+
64+
pure module function occupied(self, bin) result(bin_occupied)
65+
!! Result is true if the provided bin location has been occupied
66+
implicit none
67+
class(occupancy_t), intent(in) :: self
68+
type(phase_space_bin_t), intent(in) :: bin
69+
logical bin_occupied
70+
end function
71+
72+
pure module function num_occupied(self) result(occupied)
73+
!! Result is the number of occupied bins
74+
implicit none
75+
class(occupancy_t), intent(in) :: self
76+
integer occupied
77+
end function
78+
79+
pure module function num_bins(self) result(bins)
80+
!! Result is the total number of bins
81+
implicit none
82+
class(occupancy_t), intent(in) :: self
83+
integer(int64) bins
84+
end function
985

10-
type occupancy_t
11-
private
12-
logical, allocatable :: occupied_1D_(:)
13-
logical, allocatable :: occupied_2D_(:,:)
14-
logical, allocatable :: occupied_3D_(:,:,:)
15-
logical, allocatable :: occupied_4D_(:,:,:,:)
16-
logical, allocatable :: occupied_5D_(:,:,:,:,:)
17-
logical, allocatable :: occupied_6D_(:,:,:,:,:,:)
18-
logical, allocatable :: occupied_7D_(:,:,:,:,:,:,:)
19-
logical, allocatable :: occupied_8D_(:,:,:,:,:,:,:,:)
20-
logical, allocatable :: occupied_9D_(:,:,:,:,:,:,:,:,:)
21-
logical, allocatable :: occupied_10D_(:,:,:,:,:,:,:,:,:,:)
22-
logical, allocatable :: occupied_11D_(:,:,:,:,:,:,:,:,:,:,:)
23-
logical, allocatable :: occupied_12D_(:,:,:,:,:,:,:,:,:,:,:,:)
24-
logical, allocatable :: occupied_13D_(:,:,:,:,:,:,:,:,:,:,:,:,:)
25-
logical, allocatable :: occupied_14D_(:,:,:,:,:,:,:,:,:,:,:,:,:,:)
26-
logical, allocatable :: occupied_15D_(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:)
27-
contains
28-
procedure, non_overridable :: vacate
29-
procedure, non_overridable :: occupy
30-
procedure, non_overridable :: occupied
31-
procedure, non_overridable :: num_occupied
32-
procedure, non_overridable :: num_bins
33-
procedure, non_overridable :: allocated_dim
34-
end type
35-
36-
interface
37-
38-
pure module subroutine vacate(self, dims)
39-
implicit none
40-
class(occupancy_t), intent(inout) :: self
41-
integer, intent(in) :: dims(:)
42-
end subroutine
43-
44-
pure module subroutine occupy(self, loc)
45-
implicit none
46-
class(occupancy_t), intent(inout) :: self
47-
integer, intent(in) :: loc(:)
48-
end subroutine
49-
50-
pure module function occupied(self, loc) result(bin_occupied)
51-
implicit none
52-
class(occupancy_t), intent(in) :: self
53-
integer, intent(in) :: loc(:)
54-
logical bin_occupied
55-
end function
56-
57-
pure module function num_occupied(self) result(bins_occupied)
58-
implicit none
59-
class(occupancy_t), intent(in) :: self
60-
integer(int64) bins_occupied
61-
end function
62-
63-
pure module function num_bins(self) result(bins_total)
64-
implicit none
65-
class(occupancy_t), intent(in) :: self
66-
integer(int64) bins_total
67-
end function
68-
69-
pure module function allocated_dim(self) result(my_dim)
70-
implicit none
71-
class(occupancy_t), intent(in) :: self
72-
integer my_dim
73-
end function
74-
75-
end interface
86+
end interface
7687

7788
end module occupancy_m

0 commit comments

Comments
 (0)