35 use,
intrinsic :: iso_c_binding, only : c_ptr, c_null_ptr, &
36 c_size_t, c_associated
53 integer :: n_elements = 0
55 integer,
allocatable ::
mask(:)
61 type(c_ptr) :: mask_d = c_null_ptr
62 logical :: is_set_ = .false.
99 class(
mask_t),
intent(inout) :: this
100 integer,
intent(in) :: n_elements
102 this%is_set_ = .false.
103 if (n_elements .eq. this%n_elements)
return
106 allocate(this%mask(n_elements))
109 call device_alloc(this%mask_d, int(n_elements, c_size_t) * 4_c_size_t)
112 this%n_elements = n_elements
117 class(
mask_t),
intent(inout) :: this
119 if (
allocated(this%mask))
then
120 if (c_associated(this%mask_d))
then
123 deallocate(this%mask)
127 this%mask_d = c_null_ptr
128 this%is_set_ = .false.
133 class(
mask_t),
intent(inout) :: this
134 integer,
intent(in) :: n_elements
135 integer,
intent(in) :: mask_array(n_elements)
137 call this%allocate(n_elements)
139 this%mask = mask_array
141 call device_memcpy(this%mask, this%mask_d, this%n_elements, &
146 this%is_set_ = .true.
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
156 size_c = n_elements * int(4, c_size_t)
158 call this%allocate(n_elements)
163 this%mask = this%mask + 1
165 this%is_set_ = .true.
170 class(
mask_t),
intent(inout) :: this
171 class(
mask_t),
intent(inout) :: other
172 integer(kind=c_size_t) :: size_c
174 call this%allocate(other%n_elements)
176 size_c = other%n_elements * int(4, c_size_t)
178 this%mask = other%mask
184 this%n_elements = other%n_elements
185 this%is_set_ = other%is_set_
190 class(
mask_t),
intent(inout) :: this
191 class(
mask_t),
intent(in) :: other
192 integer,
intent(in) :: total_elements
194 logical,
allocatable :: found(:)
195 integer,
allocatable :: new_mask(:)
196 integer :: i, j, k, v, new_size
198 allocate(found(total_elements))
202 do i = 1, other%size()
204 if (v >= 1 .and. v <= total_elements) found(v) = .true.
209 do j = 1, total_elements
210 if (.not. found(j)) new_size = new_size + 1
213 allocate(new_mask(new_size))
217 do j = 1, total_elements
218 if (.not. found(j))
then
224 call this%init_from_array(new_mask, new_size)
229 class(
mask_t),
intent(in) :: this
230 integer :: n_elements
232 n_elements = this%n_elements
237 class(
mask_t),
intent(in) :: this
240 is_set = this%is_set_
245 class(
mask_t),
intent(in),
target :: this
246 integer,
pointer :: mask_array(:)
248 if (.not. this%is_set())
call neko_error(
"Mask is not set.")
250 mask_array => this%mask
255 class(
mask_t),
intent(in),
target :: this
256 integer,
intent(in) :: index
257 integer :: mask_value
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")
264 mask_value = this%mask(index)
269 class(
mask_t),
intent(in) :: this
270 type(c_ptr) :: mask_array_d
272 if (.not. this%is_set())
call neko_error(
"Mask is not set.")
274 mask_array_d = this%mask_d
279 class(
mask_t),
intent(inout) :: this
280 integer,
intent(in) :: n_elements
281 integer,
intent(in) :: mask_array(n_elements)
283 call this%allocate(n_elements)
285 this%mask = mask_array
292 this%is_set_ = .true.
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
302 call this%allocate(n_elements)
303 size_c = n_elements * int(4, c_size_t)
309 this%mask = this%mask + 1
311 this%is_set_ = .true.
Copy data between host and device (or device and device).
Device abstraction, common interface for various accelerators.
integer, parameter, public device_to_device
integer, parameter, public host_to_device
subroutine, public device_free(x_d)
Deallocate memory on the device.
integer, parameter, public device_to_host
subroutine, public device_alloc(x_d, s)
Allocate memory on the device.
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...