Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
mask.f90
Go to the documentation of this file.
1! Copyright (c) 2020-2025, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33!> Object for handling masks in Neko.
34module mask
35 use, intrinsic :: iso_c_binding, only : c_ptr, c_null_ptr, &
36 c_size_t, c_associated
40 use device_math, only : device_cadd
41 use utils, only : neko_error
42
43 implicit none
44 private
45
46 !> Type for consistently handling masks in Neko.
47 !! This type encapsulates the mask array and its associated device pointer.
48 !!
49 !! @note The mask is platform-agnostic, meaning it can be used on both CPU and
50 !! GPU, without adjusting for 1-based or 0-based indexing.
51 type, public :: mask_t
52 private
53 integer :: n_elements = 0 ! Number of elements in the mask
54 !> The mask array, 1-based (Fortran) indices
55 integer, allocatable :: mask(:)
56 !> Device copy of the mask, 0-based indices. It is a separate
57 !! allocation and never a mapping of @a mask: the two copies carry
58 !! different conventions, and on a unified-memory backend a mapped
59 !! array aliases its host array, so the in-place shift to 0-based
60 !! would corrupt the host copy that later merges read.
61 type(c_ptr) :: mask_d = c_null_ptr
62 logical :: is_set_ = .false. ! Flag to indicate if the mask is set
63
64 contains
65 ! Public interface for the mask type
66 generic, public :: init => init_from_array, init_from_array_device, &
68 procedure, public, pass(this) :: free => mask_free
69
70 procedure, public, pass(this) :: invert_from => invert_mask
71 procedure, public, pass(this) :: size => mask_size
72 procedure, public, pass(this) :: is_set => mask_is_set
73 procedure, public, pass(this) :: get_d => mask_get_d
74
75 generic, public :: set => mask_set, mask_set_d
76 generic, public :: get => mask_get, mask_get_i
77
78 ! Private procedures
79 procedure, pass(this) :: allocate => mask_allocate
80 procedure, pass(this) :: init_from_array
81 procedure, pass(this) :: init_from_array_device
82 procedure, pass(this) :: init_from_mask
83 procedure, pass(this) :: invert_mask
84
85 ! Setters
86 procedure, pass(this) :: mask_set
87 procedure, pass(this) :: mask_set_d
88
89 ! Getters
90 procedure, pass(this) :: mask_get
91 procedure, pass(this) :: mask_get_i
92
93 end type mask_t
94
95contains
96
97 !> Allocate the mask object.
98 subroutine mask_allocate(this, n_elements)
99 class(mask_t), intent(inout) :: this
100 integer, intent(in) :: n_elements
101
102 this%is_set_ = .false.
103 if (n_elements .eq. this%n_elements) return
104 call this%free()
105
106 allocate(this%mask(n_elements))
107
108 if (neko_bcknd_device .eq. 1) then
109 call device_alloc(this%mask_d, int(n_elements, c_size_t) * 4_c_size_t)
110 end if
111
112 this%n_elements = n_elements
113 end subroutine mask_allocate
114
115 !> Free the mask object.
116 subroutine mask_free(this)
117 class(mask_t), intent(inout) :: this
118
119 if (allocated(this%mask)) then
120 if (c_associated(this%mask_d)) then
121 call device_free(this%mask_d)
122 end if
123 deallocate(this%mask)
124 end if
125
126 this%n_elements = 0
127 this%mask_d = c_null_ptr
128 this%is_set_ = .false.
129 end subroutine mask_free
130
131 !> Initialize the mask from a 1-indexed host array.
132 subroutine init_from_array(this, mask_array, n_elements)
133 class(mask_t), intent(inout) :: this
134 integer, intent(in) :: n_elements
135 integer, intent(in) :: mask_array(n_elements)
136
137 call this%allocate(n_elements)
138
139 this%mask = mask_array
140 if (neko_bcknd_device .eq. 1) then
141 call device_memcpy(this%mask, this%mask_d, this%n_elements, &
142 host_to_device, sync = .true.)
143 call device_cadd(this%mask_d, -1, this%n_elements)
144 end if
145
146 this%is_set_ = .true.
147 end subroutine init_from_array
148
149 !> Initialize the mask from a 0-indexed device array.
150 subroutine init_from_array_device(this, mask_array_d, n_elements)
151 class(mask_t), intent(inout) :: this
152 integer, intent(in) :: n_elements
153 type(c_ptr), intent(inout):: mask_array_d
154 integer(kind=c_size_t) :: size_c
155
156 size_c = n_elements * int(4, c_size_t)
157
158 call this%allocate(n_elements)
159 call device_memcpy(this%mask_d, mask_array_d, size_c, &
160 device_to_device, sync = .false.)
161 call device_memcpy(this%mask, mask_array_d, n_elements, &
162 device_to_host, sync = .true.)
163 this%mask = this%mask + 1 ! device copy is 0-based, host is 1-based
164
165 this%is_set_ = .true.
166 end subroutine init_from_array_device
167
168 !> Initialize the mask from another mask object.
169 subroutine init_from_mask(this, other)
170 class(mask_t), intent(inout) :: this
171 class(mask_t), intent(inout) :: other
172 integer(kind=c_size_t) :: size_c
173
174 call this%allocate(other%n_elements)
175
176 size_c = other%n_elements * int(4, c_size_t)
177
178 this%mask = other%mask
179 if (neko_bcknd_device .eq. 1) then
180 call device_memcpy(this%mask_d, other%mask_d, size_c, &
181 device_to_device, sync = .true.)
182 end if
183
184 this%n_elements = other%n_elements
185 this%is_set_ = other%is_set_
186 end subroutine init_from_mask
187
188 !> Invert the contents of another mask object.
189 subroutine invert_mask(this, other, total_elements)
190 class(mask_t), intent(inout) :: this
191 class(mask_t), intent(in) :: other
192 integer, intent(in) :: total_elements
193
194 logical, allocatable :: found(:)
195 integer, allocatable :: new_mask(:)
196 integer :: i, j, k, v, new_size
197
198 allocate(found(total_elements))
199 found = .false.
200
201 ! mark present
202 do i = 1, other%size()
203 v = other%mask(i)
204 if (v >= 1 .and. v <= total_elements) found(v) = .true.
205 end do
206
207 ! count complement
208 new_size = 0
209 do j = 1, total_elements
210 if (.not. found(j)) new_size = new_size + 1
211 end do
212
213 allocate(new_mask(new_size))
214
215 ! fill complement
216 k = 1
217 do j = 1, total_elements
218 if (.not. found(j)) then
219 new_mask(k) = j
220 k = k + 1
221 end if
222 end do
223
224 call this%init_from_array(new_mask, new_size)
225 end subroutine invert_mask
226
227 !> Get the size of the mask.
228 pure function mask_size(this) result(n_elements)
229 class(mask_t), intent(in) :: this
230 integer :: n_elements
231
232 n_elements = this%n_elements
233 end function mask_size
234
235 !> Check if the mask is set.
236 pure function mask_is_set(this) result(is_set)
237 class(mask_t), intent(in) :: this
238 logical :: is_set
239
240 is_set = this%is_set_
241 end function mask_is_set
242
243 !> Get the mask array.
244 function mask_get(this) result(mask_array)
245 class(mask_t), intent(in), target :: this
246 integer, pointer :: mask_array(:)
247
248 if (.not. this%is_set()) call neko_error("Mask is not set.")
249
250 mask_array => this%mask
251 end function mask_get
252
253 !> Get the mask array.
254 function mask_get_i(this, index) result(mask_value)
255 class(mask_t), intent(in), target :: this
256 integer, intent(in) :: index
257 integer :: mask_value
258
259 if (.not. this%is_set()) call neko_error("Mask is not set.")
260 if (index < 1 .or. index > this%n_elements) then
261 call neko_error("Index out of bounds in mask_get_i")
262 end if
263
264 mask_value = this%mask(index)
265 end function mask_get_i
266
267 !> Get the device pointer to the mask array.
268 function mask_get_d(this) result(mask_array_d)
269 class(mask_t), intent(in) :: this
270 type(c_ptr) :: mask_array_d
271
272 if (.not. this%is_set()) call neko_error("Mask is not set.")
273
274 mask_array_d = this%mask_d
275 end function mask_get_d
276
277 !> Set the mask from a 1-indexed host array.
278 subroutine mask_set(this, mask_array, n_elements)
279 class(mask_t), intent(inout) :: this
280 integer, intent(in) :: n_elements
281 integer, intent(in) :: mask_array(n_elements)
282
283 call this%allocate(n_elements)
284
285 this%mask = mask_array
286 if (neko_bcknd_device .eq. 1) then
287 call device_memcpy(this%mask, this%mask_d, n_elements, &
288 host_to_device, sync = .true.)
289 call device_cadd(this%mask_d, -1, n_elements)
290 end if
291
292 this%is_set_ = .true.
293 end subroutine mask_set
294
295 !> Set the mask from a 0-indexed device array.
296 subroutine mask_set_d(this, mask_array_d, n_elements)
297 class(mask_t), intent(inout) :: this
298 integer, intent(in) :: n_elements
299 type(c_ptr), intent(inout) :: mask_array_d
300 integer(kind=c_size_t) :: size_c
301
302 call this%allocate(n_elements)
303 size_c = n_elements * int(4, c_size_t)
304
305 call device_memcpy(this%mask_d, mask_array_d, size_c, &
306 device_to_device, sync = .false.)
307 call device_memcpy(this%mask, mask_array_d, n_elements, &
308 device_to_host, sync = .true.)
309 this%mask = this%mask + 1 ! device copy is 0-based, host is 1-based
310
311 this%is_set_ = .true.
312 end subroutine mask_set_d
313
314
315end module mask
Copy data between host and device (or device and device).
Definition device.F90:72
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public device_to_device
Definition device.F90:48
integer, parameter, public host_to_device
Definition device.F90:48
subroutine, public device_free(x_d)
Deallocate memory on the device.
Definition device.F90:281
integer, parameter, public device_to_host
Definition device.F90:48
subroutine, public device_alloc(x_d, s)
Allocate memory on the device.
Definition device.F90:250
Object for handling masks in Neko.
Definition mask.f90:34
subroutine invert_mask(this, other, total_elements)
Invert the contents of another mask object.
Definition mask.f90:190
pure logical function mask_is_set(this)
Check if the mask is set.
Definition mask.f90:237
integer function, dimension(:), pointer mask_get(this)
Get the mask array.
Definition mask.f90:245
subroutine init_from_array(this, mask_array, n_elements)
Initialize the mask from a 1-indexed host array.
Definition mask.f90:133
subroutine init_from_mask(this, other)
Initialize the mask from another mask object.
Definition mask.f90:170
pure integer function mask_size(this)
Get the size of the mask.
Definition mask.f90:229
subroutine mask_allocate(this, n_elements)
Allocate the mask object.
Definition mask.f90:99
integer function mask_get_i(this, index)
Get the mask array.
Definition mask.f90:255
subroutine mask_set_d(this, mask_array_d, n_elements)
Set the mask from a 0-indexed device array.
Definition mask.f90:297
subroutine mask_free(this)
Free the mask object.
Definition mask.f90:117
subroutine mask_set(this, mask_array, n_elements)
Set the mask from a 1-indexed host array.
Definition mask.f90:279
type(c_ptr) function mask_get_d(this)
Get the device pointer to the mask array.
Definition mask.f90:269
subroutine init_from_array_device(this, mask_array_d, n_elements)
Initialize the mask from a 0-indexed device array.
Definition mask.f90:151
Build configurations.
integer, parameter neko_bcknd_device
Utilities.
Definition utils.f90:35
Type for consistently handling masks in Neko. This type encapsulates the mask array and its associate...
Definition mask.f90:51