@@ -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
4554end block
4655
47- ! Example using mpl_allocator_mod
56+ ! Example using mpl_allocator_mod, encapsulating pluto completely
4857block
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)
5372end 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
5680block
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()
62100end 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)
73117end block
74118
75119! Example independent of mpl_allocator_mod, using the resource name directly.
76120block
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+
81132end 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
95152end 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
110171end block
0 commit comments