Neko 1.99.3
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
39 use math, only : cfill, copy
43 use utils, only : neko_error
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=80) :: 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 call cfill(v%x, 0.0_rp, n)
100 if (neko_bcknd_device .eq. 1) then
101 call device_cfill(v%x_d, 0.0_rp, n)
102 call device_sync()
103 end if
104
105 if (present(name)) then
106 v%name = name
107 end if
108
109 end subroutine vector_init
110
112 subroutine vector_allocate(a, n)
113 class(vector_t), intent(inout) :: a
114 integer, intent(in) :: n
115
116
117 if (a%n .eq. n) return
118 call a%free()
119
120 a%n = n
121 allocate(a%x(n))
122 if (neko_bcknd_device .eq. 1) then
123 call device_map(a%x, a%x_d, n)
124 end if
125
126 end subroutine vector_allocate
127
129 subroutine vector_free(v)
130 class(vector_t), intent(inout) :: v
131
132 if (allocated(v%x)) then
133 deallocate(v%x)
134 end if
135
136 if (c_associated(v%x_d)) then
137 call device_deassociate(v%x)
138 call device_free(v%x_d)
139 end if
140
141 v%n = 0
142 v%name = ""
143
144 end subroutine vector_free
145
147 pure function vector_size(v) result(s)
148 class(vector_t), intent(in) :: v
149 integer :: s
150 s = v%n
151 end function vector_size
152
157 subroutine vector_copy_from(v, memdir, sync)
158 class(vector_t), intent(inout) :: v
159 integer, intent(in) :: memdir
160 logical, intent(in) :: sync
161
162 if (neko_bcknd_device .eq. 1) then
163 call device_memcpy(v%x, v%x_d, v%n, memdir, sync)
164 end if
165
166 end subroutine vector_copy_from
167
168
170 subroutine vector_assign_vector(v, w)
171 class(vector_t), intent(inout) :: v
172 type(vector_t), intent(in) :: w
173
174 call v%alloc(w%n)
175 if (neko_bcknd_device .eq. 1) then
176 call device_copy(v%x_d, w%x_d, v%n)
177 else
178 call copy(v%x, w%x, v%n)
179 end if
180
181 v%name = w%name
182
183 end subroutine vector_assign_vector
184
186 subroutine vector_assign_scalar(v, s)
187 class(vector_t), intent(inout) :: v
188 real(kind=rp), intent(in) :: s
189
190 if (neko_bcknd_device .eq. 1) then
191 call device_cfill(v%x_d, s, v%n)
192 else
193 call cfill(v%x, s, v%n)
194 end if
195
196 end subroutine vector_assign_scalar
197
199 subroutine vector_assign_array(v, array)
200 class(vector_t), intent(inout) :: v
201 real(kind=rp), intent(in) :: array(:)
202
203 call v%alloc(size(array))
204 v%x = array
205
206 if (neko_bcknd_device .eq. 1) then
207 call v%copy_from(host_to_device, .true.)
208 end if
209
210 end subroutine vector_assign_array
211
212 ! ========================================================================== !
213 ! vector pointer type subroutines
214
215 subroutine vector_ptr_init(this, ptr)
216 class(vector_ptr_t), intent(inout) :: this
217 type(vector_t), target, intent(in) :: ptr
218
219 call this%free()
220 this%ptr => ptr
221 end subroutine vector_ptr_init
222
223 subroutine vector_ptr_free(this)
224 class(vector_ptr_t), intent(inout) :: this
225
226 if (associated(this%ptr)) then
227 nullify(this%ptr)
228 end if
229
230 end subroutine vector_ptr_free
231
232end module vector
Deassociate a Fortran array from a device pointer.
Definition device.F90:95
Map a Fortran array to a device (allocate and associate)
Definition device.F90:77
Copy data between host and device (or device and device)
Definition device.F90:71
Synchronize a device or stream.
Definition device.F90:107
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:47
subroutine, public device_free(x_d)
Deallocate memory on the device.
Definition device.F90:219
Definition math.f90:60
subroutine, public cfill(a, c, n)
Set all elements to a constant c .
Definition math.f90:487
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.f90:249
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
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:187
subroutine vector_free(v)
Deallocate a vector.
Definition vector.f90:130
pure integer function vector_size(v)
Return the number of entries in the vector.
Definition vector.f90:148
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:113
subroutine vector_ptr_init(this, ptr)
Definition vector.f90:216
subroutine vector_assign_array(v, array)
Assignment .
Definition vector.f90:200
subroutine vector_ptr_free(this)
Definition vector.f90:224
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:171
subroutine vector_copy_from(v, memdir, sync)
Easy way to copy between host and device.
Definition vector.f90:158