Neko 1.99.2
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(:)
52 type(c_ptr) :: x_d = c_null_ptr
54 integer, private :: n = 0
55 contains
57 procedure, pass(v) :: init => vector_init
59 procedure, pass(v) :: free => vector_free
61 procedure, pass(v) :: copy_from => vector_copy_from
63 procedure, pass(v) :: size => vector_size
65 procedure, pass(v) :: vector_assign_vector
67 procedure, pass(v) :: vector_assign_scalar
69 procedure, pass(v) :: vector_assign_array
70
72 generic :: assignment(=) => vector_assign_vector, &
74
75 ! Private interfaces
76 procedure, pass(a), private :: alloc => vector_allocate
77
78 end type vector_t
79
80 type, public :: vector_ptr_t
81 type(vector_t), pointer :: ptr
82 end type vector_ptr_t
83
84contains
85
87 subroutine vector_init(v, n)
88 class(vector_t), intent(inout) :: v
89 integer, intent(in) :: n
90
91 call v%alloc(n)
92 call cfill(v%x, 0.0_rp, n)
93 if (neko_bcknd_device .eq. 1) then
94 call device_cfill(v%x_d, 0.0_rp, n)
95 call device_sync()
96 end if
97
98 end subroutine vector_init
99
101 subroutine vector_allocate(a, n)
102 class(vector_t), intent(inout) :: a
103 integer, intent(in) :: n
104
105
106 if (a%n .eq. n) return
107 call a%free()
108
109 a%n = n
110 allocate(a%x(n))
111 if (neko_bcknd_device .eq. 1) then
112 call device_map(a%x, a%x_d, n)
113 end if
114
115 end subroutine vector_allocate
116
118 subroutine vector_free(v)
119 class(vector_t), intent(inout) :: v
120
121 if (allocated(v%x)) then
122 deallocate(v%x)
123 end if
124
125 if (c_associated(v%x_d)) then
126 call device_deassociate(v%x)
127 call device_free(v%x_d)
128 end if
129
130 v%n = 0
131
132 end subroutine vector_free
133
135 pure function vector_size(v) result(s)
136 class(vector_t), intent(in) :: v
137 integer :: s
138 s = v%n
139 end function vector_size
140
145 subroutine vector_copy_from(v, memdir, sync)
146 class(vector_t), intent(inout) :: v
147 integer, intent(in) :: memdir
148 logical, intent(in) :: sync
149
150 if (neko_bcknd_device .eq. 1) then
151 call device_memcpy(v%x, v%x_d, v%n, memdir, sync)
152 end if
153
154 end subroutine vector_copy_from
155
156
158 subroutine vector_assign_vector(v, w)
159 class(vector_t), intent(inout) :: v
160 type(vector_t), intent(in) :: w
161
162 call v%alloc(w%n)
163 if (neko_bcknd_device .eq. 1) then
164 call device_copy(v%x_d, w%x_d, v%n)
165 else
166 call copy(v%x, w%x, v%n)
167 end if
168
169 end subroutine vector_assign_vector
170
172 subroutine vector_assign_scalar(v, s)
173 class(vector_t), intent(inout) :: v
174 real(kind=rp), intent(in) :: s
175
176 if (neko_bcknd_device .eq. 1) then
177 call device_cfill(v%x_d, s, v%n)
178 else
179 call cfill(v%x, s, v%n)
180 end if
181
182 end subroutine vector_assign_scalar
183
185 subroutine vector_assign_array(v, array)
186 class(vector_t), intent(inout) :: v
187 real(kind=rp), intent(in) :: array(:)
188
189 call v%alloc(size(array))
190 v%x = array
191
192 if (neko_bcknd_device .eq. 1) then
193 call v%copy_from(host_to_device, .true.)
194 end if
195
196 end subroutine vector_assign_array
197
198end 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)
Initialise a vector of size n.
Definition vector.f90:88
subroutine vector_assign_scalar(v, s)
Assignment .
Definition vector.f90:173
subroutine vector_free(v)
Deallocate a vector.
Definition vector.f90:119
pure integer function vector_size(v)
Return the number of entries in the vector.
Definition vector.f90:136
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:102
subroutine vector_assign_array(v, array)
Assignment .
Definition vector.f90:186
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:159
subroutine vector_copy_from(v, memdir, sync)
Easy way to copy between host and device.
Definition vector.f90:146