Skip to content

Commit 4c959f7

Browse files
committed
more
1 parent b751cf2 commit 4c959f7

File tree

8 files changed

+1127
-234
lines changed

8 files changed

+1127
-234
lines changed

pluto/examples/mpl_pinned_allocator.F90

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ subroutine mpl_allocator_init(bytes)
1414
real(8), parameter :: GB = 1024**3 ! 1 GB in bytes
1515
call pluto%trace%enable() ! Just for this example to show the trace output of the resource,
1616
! can also be enabled by setting the environment variable PLUTO_TRACE=1
17+
1718
mpl_resource = pluto%pinned_pool_resource()
19+
! This resource will manage a pool of pinned host memory, which is useful for efficient data transfer between host and device.
20+
! The resource will grow the pool as needed when allocations are made, but we can also reserve a certain amount of memory upfront
21+
! to ensure that it is available when needed and to potentially improve performance by reducing fragmentation.
22+
! NB: another implementation of a resource could easily be conceived, and registered by name, without having to change the rest of the code that uses it.
23+
! For example:
24+
! mpl_resource = pluto%get_registered_resource("my_custom_resource_using_buddy_allocator")
25+
1826
mpl_allocator = pluto%make_allocator(mpl_resource)
27+
1928
call mpl_allocator_print()
2029
call mpl_resource%reserve(bytes) ! reserve memory if the resource supports it, otherwise this is a no-op
2130
call mpl_allocator_print()
@@ -44,21 +53,50 @@ program main
4453
call mpl_allocator_init(1024**3) ! 1 GB
4554
end block
4655

47-
! Example using mpl_allocator_mod
56+
! Example using mpl_allocator_mod, encapsulating pluto completely
4857
block
4958
use mpl_allocator_mod, only : mpl_allocator
5059
real(8), pointer :: array(:,:)
60+
61+
! Allocation using shape, anonymous array
62+
call mpl_allocator%allocate(array, shape=[10, 8])
63+
call mpl_allocator%deallocate(array)
64+
65+
! Allocation using bounds, anonymous array
5166
call mpl_allocator%allocate(array, lbounds=[0,0], ubounds=[10, 8])
5267
call mpl_allocator%deallocate(array)
68+
69+
! label array for tracing
70+
call mpl_allocator%allocate("mpl_array", array, lbounds=[0,0], ubounds=[10, 8])
71+
call mpl_allocator%deallocate("mpl_array", array)
5372
end block
5473

74+
! ------------------------------------------------------------------------------------------------------------------------
75+
! Further examples that also use the pluto_module. This indicates that we will be able to use the same allocators
76+
! in other contexts independent of MPL, and that the resource is registered globally so that it can be used in other
77+
! translation units without having to pass it around.
78+
5579
! Example using mpl_resource using pluto%allocate / pluto%deallocate
5680
block
5781
use pluto_module, only : pluto
5882
use mpl_allocator_mod, only : mpl_resource
5983
real(8), pointer :: array(:,:)
84+
85+
! Allocation using shape, anonymous array
86+
call pluto%allocate(array, shape=[10, 8], resource=mpl_resource)
87+
call pluto%deallocate(array, resource=mpl_resource) ! IMPORTANT, must match the resource used for allocation
88+
89+
! Allocation using bounds, anonymous array
6090
call pluto%allocate(array, lbounds=[0,0], ubounds=[10, 8], resource=mpl_resource)
6191
call pluto%deallocate(array, resource=mpl_resource) ! IMPORTANT, must match the resource used for allocation
92+
93+
! label array for tracing
94+
call pluto%allocate("mpl_array", array, lbounds=[0,0], ubounds=[10, 8], resource=mpl_resource)
95+
call pluto%deallocate("mpl_array", array, resource=mpl_resource)
96+
97+
! Using label externally
98+
call pluto%set_label("mpl_array_2"); call pluto%allocate(array, lbounds=[0,0], ubounds=[10, 8], resource=mpl_resource); call pluto%unset_label()
99+
call pluto%set_label("mpl_array_2"); call pluto%deallocate(array, resource=mpl_resource); call pluto%unset_label()
62100
end block
63101

64102
! Example independent of mpl_allocator_mod, using the resource name directly.
@@ -68,16 +106,29 @@ program main
68106
type(pluto_allocator) :: allocator
69107
real(8), pointer :: array(:,:)
70108
allocator = pluto%make_allocator("MPL") ! This has been registered by name, now equivalent to using mpl_allocator
109+
110+
! anonymous array
71111
call allocator%allocate(array, lbounds=[0,0], ubounds=[10, 8])
72112
call allocator%deallocate(array)
113+
114+
! label array for tracing
115+
call allocator%allocate("mpl_array", array, lbounds=[0,0], ubounds=[10, 8])
116+
call allocator%deallocate("mpl_array", array)
73117
end block
74118

75119
! Example independent of mpl_allocator_mod, using the resource name directly.
76120
block
77121
use pluto_module, only : pluto
78122
real(8), pointer :: array(:,:)
123+
124+
! anonymous array
79125
call pluto%allocate(array, lbounds=[0,0], ubounds=[10, 8], resource="MPL")
80126
call pluto%deallocate(array, resource="MPL") ! IMPORTANT, must match the resource used for allocation
127+
128+
! label array for tracing
129+
call pluto%allocate("mpl_array", array, lbounds=[0,0], ubounds=[10, 8], resource="MPL")
130+
call pluto%deallocate("mpl_array", array, resource="MPL") ! IMPORTANT, must match the resource used for allocation
131+
81132
end block
82133

83134
! Example independent of mpl_allocator_mod, modifying the default host allocator in a scope.
@@ -88,8 +139,14 @@ program main
88139
call pluto%host%set_default_resource("MPL") ! set the default host resource for this scope to "MPL"
89140
block
90141
real(8), pointer :: array(:,:)
142+
143+
! anonymous array
91144
call pluto%host%allocate(array, lbounds=[0,0], ubounds=[10, 8]) ! will use the default resource for the host, which is now "MPL"
92-
call pluto%host%deallocate(array)
145+
call pluto%host%deallocate(array) ! will use the default resource for the host, which is now "MPL"
146+
147+
! label array for tracing
148+
call pluto%host%allocate("mpl_array", array, lbounds=[0,0], ubounds=[10, 8]) ! will use the default resource for the host, which is now "MPL"
149+
call pluto%host%deallocate("mpl_array", array) ! will use the default resource for the host, which is now "MPL"
93150
end block
94151
call pluto%scope%pop() ! restore the previous default resource for the host
95152
end block
@@ -101,10 +158,14 @@ program main
101158
use mpl_allocator_mod, only : mpl_resource
102159
call pluto%scope%push()
103160
call pluto%host%set_default_resource(mpl_resource) ! set the default host resource for this scope to mpl_resource
104-
block
161+
block
105162
real(8), pointer :: array(:,:)
106163
call pluto%host%allocate(array, shape=[10, 8]) ! will use the default resource for the host, which is now mpl_resource
107-
call pluto%host%deallocate(array)
164+
call pluto%host%deallocate(array) ! will use the default resource for the host, which is now mpl_resource
165+
166+
! label array for tracing
167+
call pluto%host%allocate("mpl_array", array, shape=[10, 8]) ! will use the default resource for the host, which is now "MPL"
168+
call pluto%host%deallocate("mpl_array", array) ! will use the default resource for the host, which is now "MPL"
108169
end block
109170
call pluto%scope%pop() ! restore the previous default resource for the host
110171
end block

pluto/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ if (HAVE_FORTRAN)
125125
pluto_f/pluto_module_allocator.F90
126126
pluto_f/pluto_module_scope.F90
127127
pluto_f/pluto_module_trace.F90
128+
pluto_f/pluto_module_host.F90
129+
pluto_f/pluto_module_device.F90
128130
pluto_f/pluto_module.F90
129131
PRIVATE_LIBS pluto
130132
PUBLIC_INCLUDES $<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}>

0 commit comments

Comments
 (0)