Neko 1.99.3
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
device_array.f90
Go to the documentation of this file.
1! Copyright (c) 2026, 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!
35 use num_types, only : rp, sp, dp
39 use utils, only : neko_error
40 use, intrinsic :: iso_c_binding
41
42 implicit none
43 private
44
46 type, public :: device_array_t
48 type(c_ptr) :: x_d = c_null_ptr
50 integer, private :: n = 0
51 contains
53 procedure, pass(this) :: init => device_array_init
55 procedure, pass(this) :: free => device_array_free
57 procedure, private, pass(this) :: allocate => device_array_allocate
59 procedure, pass(this) :: size => device_array_size
61 procedure, pass(this) :: is_allocated => device_array_is_allocated
63 procedure, pass(this) :: assign => device_array_assign
64
66 generic :: assignment(=) => assign
67 end type device_array_t
68
69contains
70
74 subroutine device_array_init(this, size)
75 class(device_array_t), intent(inout) :: this
76 integer, intent(in) :: size
77
78 if (neko_bcknd_device .ne. 1) then
79 call neko_error('Device array cannot be initialized when ' // &
80 'NEKO_BCKND_DEVICE is not set to 1')
81 end if
82
83 call this%free()
84 call this%allocate(size)
85 call device_rzero(this%x_d, this%n)
86
87 end subroutine device_array_init
88
91 subroutine device_array_free(this)
92 class(device_array_t), intent(inout) :: this
93
94 this%n = 0
95 if (c_associated(this%x_d)) call device_free(this%x_d)
96
97 end subroutine device_array_free
98
102 subroutine device_array_allocate(this, size)
103 class(device_array_t), intent(inout) :: this
104 integer, intent(in) :: size
105 integer(c_size_t) :: c_size
106
107 this%n = size
108 select case (rp)
109 case (sp)
110 c_size = size * int(4, c_size_t)
111 case (dp)
112 c_size = size * int(8, c_size_t)
113 case default
114 call neko_error('Unknown Fortran type')
115 end select
116 call device_alloc(this%x_d, c_size)
117
118 end subroutine device_array_allocate
119
123 subroutine device_array_assign(this, source)
124 class(device_array_t), intent(inout) :: this
125 type(device_array_t), intent(in) :: source
126
127 if (.not. source%is_allocated()) then
128 call this%free()
129 return
130 else if (source%size() .ne. this%size()) then
131 call this%free()
132 call this%allocate(source%size())
133 end if
134
135 call device_copy(this%x_d, source%x_d, this%n)
136
137 end subroutine device_array_assign
138
141 pure function device_array_size(this) result(n)
142 class(device_array_t), intent(in) :: this
143 integer :: n
144 n = this%n
145 end function device_array_size
146
149 pure function device_array_is_allocated(this) result(is_alloc)
150 class(device_array_t), intent(in) :: this
151 logical :: is_alloc
152
153 is_alloc = c_associated(this%x_d)
154
155 end function device_array_is_allocated
156
157end module device_array
Module containing device only array type.
pure integer function device_array_size(this)
Return the size of the device array.
subroutine device_array_assign(this, source)
Assignment with deep-copy ownership semantics.
subroutine device_array_init(this, size)
Initialize a device array of size size.
subroutine device_array_free(this)
Free a device array.
subroutine device_array_allocate(this, size)
Allocate a device array (used by init and assignment).
pure logical function device_array_is_allocated(this)
Check whether the device array is allocated.
subroutine, public device_rzero(a_d, n, strm)
Zero a real vector.
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
Device abstraction, common interface for various accelerators.
Definition device.F90:34
subroutine, public device_free(x_d)
Deallocate memory on the device.
Definition device.F90:225
subroutine, public device_alloc(x_d, s)
Allocate memory on the device.
Definition device.F90:198
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public dp
Definition num_types.f90:9
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Utilities.
Definition utils.f90:35
Device-only temporary array.