Neko 1.99.1
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
68
70 generic :: assignment(=) => vector_assign_vector, &
72
73 ! Private interfaces
74 procedure, pass(a), private :: alloc => vector_allocate
75
76 end type vector_t
77
78 type, public :: vector_ptr_t
79 type(vector_t), pointer :: ptr
80 end type vector_ptr_t
81
82contains
83
85 subroutine vector_init(v, n)
86 class(vector_t), intent(inout) :: v
87 integer, intent(in) :: n
88
89 call v%alloc(n)
90 call cfill(v%x, 0.0_rp, n)
91 if (neko_bcknd_device .eq. 1) then
92 call device_cfill(v%x_d, 0.0_rp, n)
93 call device_sync()
94 end if
95
96 end subroutine vector_init
97
99 subroutine vector_allocate(a, n)
100 class(vector_t), intent(inout) :: a
101 integer, intent(in) :: n
102
103
104 if (a%n .eq. n) return
105 call a%free()
106
107 a%n = n
108 allocate(a%x(n))
109 if (neko_bcknd_device .eq. 1) then
110 call device_map(a%x, a%x_d, n)
111 end if
112
113 end subroutine vector_allocate
114
116 subroutine vector_free(v)
117 class(vector_t), intent(inout) :: v
118
119 if (allocated(v%x)) then
120 deallocate(v%x)
121 end if
122
123 if (c_associated(v%x_d)) then
124 call device_deassociate(v%x)
125 call device_free(v%x_d)
126 end if
127
128 v%n = 0
129
130 end subroutine vector_free
131
133 pure function vector_size(v) result(s)
134 class(vector_t), intent(in) :: v
135 integer :: s
136 s = v%n
137 end function vector_size
138
143 subroutine vector_copy_from(v, memdir, sync)
144 class(vector_t), intent(inout) :: v
145 integer, intent(in) :: memdir
146 logical, intent(in) :: sync
147
148 if (neko_bcknd_device .eq. 1) then
149 call device_memcpy(v%x, v%x_d, v%n, memdir, sync)
150 end if
151
152 end subroutine vector_copy_from
153
154
156 subroutine vector_assign_vector(v, w)
157 class(vector_t), intent(inout) :: v
158 type(vector_t), intent(in) :: w
159
160 call v%alloc(w%n)
161 if (neko_bcknd_device .eq. 1) then
162 call device_copy(v%x_d, w%x_d, v%n)
163 else
164 call copy(v%x, w%x, v%n)
165 end if
166
167 end subroutine vector_assign_vector
168
170 subroutine vector_assign_scalar(v, s)
171 class(vector_t), intent(inout) :: v
172 real(kind=rp), intent(in) :: s
173
174 if (neko_bcknd_device .eq. 1) then
175 call device_cfill(v%x_d, s, v%n)
176 else
177 call cfill(v%x, s, v%n)
178 end if
179
180 end subroutine vector_assign_scalar
181
182end 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
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:86
subroutine vector_assign_scalar(v, s)
Assignment .
Definition vector.f90:171
subroutine vector_free(v)
Deallocate a vector.
Definition vector.f90:117
pure integer function vector_size(v)
Return the number of entries in the vector.
Definition vector.f90:134
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:100
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:157
subroutine vector_copy_from(v, memdir, sync)
Easy way to copy between host and device.
Definition vector.f90:144