Neko 1.99.9
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, only : c_ptr, c_null_ptr, c_size_t, &
41 c_associated
42
43 implicit none
44 private
45
47 type, public :: device_array_t
49 type(c_ptr) :: x_d = c_null_ptr
51 integer, private :: n = 0
52 contains
54 procedure, pass(this) :: init => device_array_init
56 procedure, pass(this) :: free => device_array_free
58 procedure, private, pass(this) :: allocate => device_array_allocate
60 procedure, pass(this) :: size => device_array_size
62 procedure, pass(this) :: is_allocated => device_array_is_allocated
64 procedure, pass(this) :: assign => device_array_assign
65
67 generic :: assignment(=) => assign
68 end type device_array_t
69
70contains
71
75 subroutine device_array_init(this, size)
76 class(device_array_t), intent(inout) :: this
77 integer, intent(in) :: size
78
79 if (neko_bcknd_device .ne. 1) then
80 call neko_error('Device array cannot be initialized when ' // &
81 'NEKO_BCKND_DEVICE is not set to 1')
82 end if
83
84 call this%free()
85 call this%allocate(size)
86 call device_rzero(this%x_d, this%n)
87
88 end subroutine device_array_init
89
92 subroutine device_array_free(this)
93 class(device_array_t), intent(inout) :: this
94
95 this%n = 0
96 if (c_associated(this%x_d)) call device_free(this%x_d)
97
98 end subroutine device_array_free
99
103 subroutine device_array_allocate(this, size)
104 class(device_array_t), intent(inout) :: this
105 integer, intent(in) :: size
106 integer(c_size_t) :: c_size
107
108 this%n = size
109 select case (rp)
110 case (sp)
111 c_size = size * int(4, c_size_t)
112 case (dp)
113 c_size = size * int(8, c_size_t)
114 case default
115 call neko_error('Unknown Fortran type')
116 end select
117 call device_alloc(this%x_d, c_size)
118
119 end subroutine device_array_allocate
120
124 subroutine device_array_assign(this, source)
125 class(device_array_t), intent(inout) :: this
126 type(device_array_t), intent(in) :: source
127
128 if (.not. source%is_allocated()) then
129 call this%free()
130 return
131 else if (source%size() .ne. this%size()) then
132 call this%free()
133 call this%allocate(source%size())
134 end if
135
136 call device_copy(this%x_d, source%x_d, this%n)
137
138 end subroutine device_array_assign
139
142 pure function device_array_size(this) result(n)
143 class(device_array_t), intent(in) :: this
144 integer :: n
145 n = this%n
146 end function device_array_size
147
150 pure function device_array_is_allocated(this) result(is_alloc)
151 class(device_array_t), intent(in) :: this
152 logical :: is_alloc
153
154 is_alloc = c_associated(this%x_d)
155
156 end function device_array_is_allocated
157
158end 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:243
subroutine, public device_alloc(x_d, s)
Allocate memory on the device.
Definition device.F90:212
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Utilities.
Definition utils.f90:35
Device-only temporary array.