Neko 1.99.6
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
vector.f90
Go to the documentation of this file.
1! Copyright (c) 2022-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!
34module vector
36 use num_types, only : rp
37 use device, only : device_map, device_unmap, &
39 use math, only : cfill, copy
44 use, intrinsic :: iso_c_binding
45 implicit none
46 private
47
48 type, public :: vector_t
50 real(kind=rp), allocatable :: x(:)
51 character(len=NEKO_VARNAME_LEN) :: name = ""
53 type(c_ptr) :: x_d = c_null_ptr
55 integer, private :: n = 0
56 contains
58 procedure, pass(v) :: init => vector_init
60 procedure, pass(v) :: free => vector_free
62 procedure, pass(v) :: copy_from => vector_copy_from
64 procedure, pass(v) :: size => vector_size
66 procedure, pass(v) :: vector_assign_vector
68 procedure, pass(v) :: vector_assign_scalar
70 procedure, pass(v) :: vector_assign_array
71
73 generic :: assignment(=) => vector_assign_vector, &
75
76 ! Private interfaces
77 procedure, pass(a), private :: alloc => vector_allocate
78
79 end type vector_t
80
81 type, public :: vector_ptr_t
82 type(vector_t), pointer :: ptr => null()
83 contains
85 procedure, pass(this) :: init => vector_ptr_init
87 procedure, pass(this) :: free => vector_ptr_free
88 end type vector_ptr_t
89
90contains
91
93 subroutine vector_init(v, n, name)
94 class(vector_t), intent(inout) :: v
95 integer, intent(in) :: n
96 character(len=*), intent(in), optional :: name
97
98 call v%alloc(n)
99 if (neko_bcknd_device .eq. 1) then
100 ! Zero the device side first: under zero-copy the device then
101 ! faults the pages (device first touch), which gives contiguous
102 ! physical mappings and thus better GPU TLB utilisation
103 call device_cfill(v%x_d, 0.0_rp, n)
104 call device_sync()
105 end if
106 call cfill(v%x, 0.0_rp, n)
107
108 if (present(name)) then
109 v%name = name
110 end if
111
112 end subroutine vector_init
113
115 subroutine vector_allocate(a, n)
116 class(vector_t), intent(inout) :: a
117 integer, intent(in) :: n
118
119
120 if (a%n .eq. n) return
121 call a%free()
122
123 a%n = n
124 allocate(a%x(n))
125 if (neko_bcknd_device .eq. 1) then
126 call device_map(a%x, a%x_d, n)
127 end if
128
129 end subroutine vector_allocate
130
132 subroutine vector_free(v)
133 class(vector_t), intent(inout) :: v
134
135 if (allocated(v%x)) then
136 if (neko_bcknd_device .eq. 1) then
137 call device_unmap(v%x, v%x_d)
138 end if
139 deallocate(v%x)
140 end if
141
142 v%n = 0
143 v%name = ""
144
145 end subroutine vector_free
146
148 pure function vector_size(v) result(s)
149 class(vector_t), intent(in) :: v
150 integer :: s
151 s = v%n
152 end function vector_size
153
158 subroutine vector_copy_from(v, memdir, sync)
159 class(vector_t), intent(inout) :: v
160 integer, intent(in) :: memdir
161 logical, intent(in) :: sync
162
163 if (neko_bcknd_device .eq. 1) then
164 call device_memcpy(v%x, v%x_d, v%n, memdir, sync)
165 end if
166
167 end subroutine vector_copy_from
168
169
171 subroutine vector_assign_vector(v, w)
172 class(vector_t), intent(inout) :: v
173 type(vector_t), intent(in) :: w
174
175 call v%alloc(w%n)
176 if (neko_bcknd_device .eq. 1) then
177 call device_copy(v%x_d, w%x_d, v%n)
178 else
179 call copy(v%x, w%x, v%n)
180 end if
181
182 v%name = w%name
183
184 end subroutine vector_assign_vector
185
187 subroutine vector_assign_scalar(v, s)
188 class(vector_t), intent(inout) :: v
189 real(kind=rp), intent(in) :: s
190
191 if (neko_bcknd_device .eq. 1) then
192 call device_cfill(v%x_d, s, v%n)
193 else
194 call cfill(v%x, s, v%n)
195 end if
196
197 end subroutine vector_assign_scalar
198
200 subroutine vector_assign_array(v, array)
201 class(vector_t), intent(inout) :: v
202 real(kind=rp), intent(in) :: array(:)
203
204 call v%alloc(size(array))
205 v%x = array
206
207 if (neko_bcknd_device .eq. 1) then
208 call v%copy_from(host_to_device, .true.)
209 end if
210
211 end subroutine vector_assign_array
212
213 ! ========================================================================== !
214 ! vector pointer type subroutines
215
216 subroutine vector_ptr_init(this, ptr)
217 class(vector_ptr_t), intent(inout) :: this
218 type(vector_t), target, intent(in) :: ptr
219
220 call this%free()
221 this%ptr => ptr
222 end subroutine vector_ptr_init
223
224 subroutine vector_ptr_free(this)
225 class(vector_ptr_t), intent(inout) :: this
226
227 if (associated(this%ptr)) then
228 nullify(this%ptr)
229 end if
230
231 end subroutine vector_ptr_free
232
233end module vector
Map a Fortran array to a device (allocate and associate)
Definition device.F90:83
Copy data between host and device (or device and device)
Definition device.F90:72
Synchronize a device or stream.
Definition device.F90:119
Unmap a Fortran array from a device (deassociate and free)
Definition device.F90:89
subroutine, public device_sub3(a_d, b_d, c_d, n, strm)
Vector subtraction .
subroutine, public device_cmult(a_d, c, n, strm)
Multiplication by constant c .
subroutine, public device_cadd2(a_d, b_d, c, n, strm)
Add a scalar to vector .
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
subroutine, public device_invcol3(a_d, b_d, c_d, n, strm)
Vector division .
subroutine, public device_col2(a_d, b_d, n, strm)
Vector multiplication .
subroutine, public device_cdiv2(a_d, b_d, c, n, strm)
Division of constant c by array .
subroutine, public device_cmult2(a_d, b_d, c, n, strm)
Multiplication by constant c .
subroutine, public device_col3(a_d, b_d, c_d, n, strm)
Vector multiplication with 3 vectors .
subroutine, public device_cfill(a_d, c, n, strm)
Set all elements to a constant c .
subroutine, public device_add3(a_d, b_d, c_d, n, strm)
Vector addition .
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
Definition math.f90:60
subroutine, public cfill(a, c, n)
Set all elements to a constant c .
Definition math.f90:597
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.f90:291
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Utilities.
Definition utils.f90:35
integer, parameter, public neko_varname_len
Definition utils.f90:43
Defines a vector.
Definition vector.f90:34
subroutine vector_init(v, n, name)
Initialise a vector of size n.
Definition vector.f90:94
subroutine vector_assign_scalar(v, s)
Assignment .
Definition vector.f90:188
subroutine vector_free(v)
Deallocate a vector.
Definition vector.f90:133
pure integer function vector_size(v)
Return the number of entries in the vector.
Definition vector.f90:149
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:116
subroutine vector_ptr_init(this, ptr)
Definition vector.f90:217
subroutine vector_assign_array(v, array)
Assignment .
Definition vector.f90:201
subroutine vector_ptr_free(this)
Definition vector.f90:225
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:172
subroutine vector_copy_from(v, memdir, sync)
Easy way to copy between host and device.
Definition vector.f90:159