35 use,
intrinsic :: iso_c_binding, only : c_ptr, c_null_ptr, &
53 integer :: n_elements = 0
54 integer,
allocatable ::
mask(:)
55 type(c_ptr) :: mask_d = c_null_ptr
56 logical :: is_set_ = .false.
93 class(
mask_t),
intent(inout) :: this
94 integer,
intent(in) :: n_elements
96 this%is_set_ = .false.
97 if (n_elements .eq. this%n_elements)
return
100 allocate(this%mask(n_elements))
103 call device_map(this%mask, this%mask_d, n_elements)
106 this%n_elements = n_elements
111 class(
mask_t),
intent(inout) :: this
113 if (
allocated(this%mask))
then
117 deallocate(this%mask)
121 this%mask_d = c_null_ptr
122 this%is_set_ = .false.
127 class(
mask_t),
intent(inout) :: this
128 integer,
intent(in) :: n_elements
129 integer,
intent(in) :: mask_array(n_elements)
131 call this%allocate(n_elements)
133 this%mask = mask_array
135 call device_memcpy(this%mask, this%mask_d, this%n_elements, &
140 this%is_set_ = .true.
145 class(
mask_t),
intent(inout) :: this
146 integer,
intent(in) :: n_elements
147 type(c_ptr),
intent(inout):: mask_array_d
148 integer(kind=c_size_t) :: size_c
150 size_c = n_elements * int(4, c_size_t)
152 call this%allocate(n_elements)
157 this%mask = this%mask - 1
159 this%is_set_ = .true.
164 class(
mask_t),
intent(inout) :: this
165 class(
mask_t),
intent(inout) :: other
166 integer(kind=c_size_t) :: size_c
168 call this%allocate(other%n_elements)
170 size_c = other%n_elements * int(4, c_size_t)
172 this%mask = other%mask
178 this%n_elements = other%n_elements
179 this%is_set_ = other%is_set_
184 class(
mask_t),
intent(inout) :: this
185 class(
mask_t),
intent(in) :: other
186 integer,
intent(in) :: total_elements
188 logical,
allocatable :: found(:)
189 integer,
allocatable :: new_mask(:)
190 integer :: i, j, k, v, new_size
192 allocate(found(total_elements))
196 do i = 1, other%size()
198 if (v >= 1 .and. v <= total_elements) found(v) = .true.
203 do j = 1, total_elements
204 if (.not. found(j)) new_size = new_size + 1
207 allocate(new_mask(new_size))
211 do j = 1, total_elements
212 if (.not. found(j))
then
218 call this%init_from_array(new_mask, new_size)
223 class(
mask_t),
intent(in) :: this
224 integer :: n_elements
226 n_elements = this%n_elements
231 class(
mask_t),
intent(in) :: this
234 is_set = this%is_set_
239 class(
mask_t),
intent(in),
target :: this
240 integer,
pointer :: mask_array(:)
242 if (.not. this%is_set())
call neko_error(
"Mask is not set.")
244 mask_array => this%mask
249 class(
mask_t),
intent(in),
target :: this
250 integer,
intent(in) :: index
251 integer :: mask_value
253 if (.not. this%is_set())
call neko_error(
"Mask is not set.")
254 if (index < 1 .or. index > this%n_elements)
then
255 call neko_error(
"Index out of bounds in mask_get_i")
258 mask_value = this%mask(index)
263 class(
mask_t),
intent(in) :: this
264 type(c_ptr) :: mask_array_d
266 if (.not. this%is_set())
call neko_error(
"Mask is not set.")
268 mask_array_d = this%mask_d
273 class(
mask_t),
intent(inout) :: this
274 integer,
intent(in) :: n_elements
275 integer,
intent(in) :: mask_array(n_elements)
277 call this%allocate(n_elements)
279 this%mask = mask_array
286 this%is_set_ = .true.
291 class(
mask_t),
intent(inout) :: this
292 integer,
intent(in) :: n_elements
293 type(c_ptr),
intent(inout) :: mask_array_d
294 integer(kind=c_size_t) :: size_c
296 call this%allocate(n_elements)
297 size_c = n_elements * int(4, c_size_t)
303 this%mask = this%mask - 1
305 this%is_set_ = .true.
Map a Fortran array to a device (allocate and associate)
Copy data between host and device (or device and device)
Unmap a Fortran array from a device (deassociate and free)
Device abstraction, common interface for various accelerators.
integer, parameter, public device_to_device
integer, parameter, public host_to_device
integer, parameter, public device_to_host
Object for handling masks in Neko.
subroutine invert_mask(this, other, total_elements)
Invert the contents of another mask object.
pure logical function mask_is_set(this)
Check if the mask is set.
integer function, dimension(:), pointer mask_get(this)
Get the mask array.
subroutine init_from_array(this, mask_array, n_elements)
Initialize the mask from a 1-indexed host array.
subroutine init_from_mask(this, other)
Initialize the mask from another mask object.
pure integer function mask_size(this)
Get the size of the mask.
subroutine mask_allocate(this, n_elements)
Allocate the mask object.
integer function mask_get_i(this, index)
Get the mask array.
subroutine mask_set_d(this, mask_array_d, n_elements)
Set the mask from a 0-indexed device array.
subroutine mask_free(this)
Free the mask object.
subroutine mask_set(this, mask_array, n_elements)
Set the mask from a 1-indexed host array.
type(c_ptr) function mask_get_d(this)
Get the device pointer to the mask array.
subroutine init_from_array_device(this, mask_array_d, n_elements)
Initialize the mask from a 0-indexed device array.
integer, parameter neko_bcknd_device
Type for consistently handling masks in Neko. This type encapsulates the mask array and its associate...