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
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 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 if (neko_bcknd_device .eq. 1) then
134 call device_unmap(v%x, v%x_d)
135 end if
136 deallocate(v%x)
137 end if
138
139 v%n = 0
140 v%name = ""
141
142 end subroutine vector_free
143
145 pure function vector_size(v) result(s)
146 class(vector_t), intent(in) :: v
147 integer :: s
148 s = v%n
149 end function vector_size
150
155 subroutine vector_copy_from(v, memdir, sync)
156 class(vector_t), intent(inout) :: v
157 integer, intent(in) :: memdir
158 logical, intent(in) :: sync
159
160 if (neko_bcknd_device .eq. 1) then
161 call device_memcpy(v%x, v%x_d, v%n, memdir, sync)
162 end if
163
164 end subroutine vector_copy_from
165
166
168 subroutine vector_assign_vector(v, w)
169 class(vector_t), intent(inout) :: v
170 type(vector_t), intent(in) :: w
171
172 call v%alloc(w%n)
173 if (neko_bcknd_device .eq. 1) then
174 call device_copy(v%x_d, w%x_d, v%n)
175 else
176 call copy(v%x, w%x, v%n)
177 end if
178
179 v%name = w%name
180
181 end subroutine vector_assign_vector
182
184 subroutine vector_assign_scalar(v, s)
185 class(vector_t), intent(inout) :: v
186 real(kind=rp), intent(in) :: s
187
188 if (neko_bcknd_device .eq. 1) then
189 call device_cfill(v%x_d, s, v%n)
190 else
191 call cfill(v%x, s, v%n)
192 end if
193
194 end subroutine vector_assign_scalar
195
197 subroutine vector_assign_array(v, array)
198 class(vector_t), intent(inout) :: v
199 real(kind=rp), intent(in) :: array(:)
200
201 call v%alloc(size(array))
202 v%x = array
203
204 if (neko_bcknd_device .eq. 1) then
205 call v%copy_from(host_to_device, .true.)
206 end if
207
208 end subroutine vector_assign_array
209
210 ! ========================================================================== !
211 ! vector pointer type subroutines
212
213 subroutine vector_ptr_init(this, ptr)
214 class(vector_ptr_t), intent(inout) :: this
215 type(vector_t), target, intent(in) :: ptr
216
217 call this%free()
218 this%ptr => ptr
219 end subroutine vector_ptr_init
220
221 subroutine vector_ptr_free(this)
222 class(vector_ptr_t), intent(inout) :: this
223
224 if (associated(this%ptr)) then
225 nullify(this%ptr)
226 end if
227
228 end subroutine vector_ptr_free
229
230end module vector
Map a Fortran array to a device (allocate and associate)
Definition device.F90:78
Copy data between host and device (or device and device)
Definition device.F90:72
Synchronize a device or stream.
Definition device.F90:114
Unmap a Fortran array from a device (deassociate and free)
Definition device.F90:84
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:595
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.f90:289
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:185
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:146
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:113
subroutine vector_ptr_init(this, ptr)
Definition vector.f90:214
subroutine vector_assign_array(v, array)
Assignment .
Definition vector.f90:198
subroutine vector_ptr_free(this)
Definition vector.f90:222
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:169
subroutine vector_copy_from(v, memdir, sync)
Easy way to copy between host and device.
Definition vector.f90:156