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, 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_free
38 use math, only: cfill, copy
40 use utils, only: neko_error
41 use, intrinsic :: iso_c_binding
42 implicit none
43 private
44
45 type, public :: vector_t
47 real(kind=rp), allocatable :: x(:)
49 type(c_ptr) :: x_d = c_null_ptr
51 integer, private :: n = 0
52 contains
54 procedure, pass(v) :: init => vector_init
56 procedure, pass(v) :: free => vector_free
58 procedure, pass(v) :: size => vector_size
60 procedure, pass(v) :: vector_assign_vector
62 procedure, pass(v) :: vector_assign_scalar
63
65 generic :: assignment(=) => vector_assign_vector, &
67
68 ! Private interfaces
69 procedure, pass(a), private :: alloc => vector_allocate
70
71 end type vector_t
72
73 type, public :: vector_ptr_t
74 type(vector_t), pointer :: ptr
75 end type vector_ptr_t
76
77contains
78
80 subroutine vector_init(v, n)
81 class(vector_t), intent(inout) :: v
82 integer, intent(in) :: n
83
84 call v%alloc(n)
85 call cfill(v%x, 0.0_rp, n)
86 if (neko_bcknd_device .eq. 1) then
87 call device_cfill(v%x_d, 0.0_rp, n)
88 end if
89
90 end subroutine vector_init
91
93 subroutine vector_allocate(a, n)
94 class(vector_t), intent(inout) :: a
95 integer, intent(in) :: n
96
97
98 if (a%n .eq. n) return
99 call a%free()
100
101 a%n = n
102 allocate(a%x(n))
103 if (neko_bcknd_device .eq. 1) then
104 call device_map(a%x, a%x_d, n)
105 end if
106
107 end subroutine vector_allocate
108
110 subroutine vector_free(v)
111 class(vector_t), intent(inout) :: v
112
113 if (allocated(v%x)) then
114 deallocate(v%x)
115 end if
116
117 if (c_associated(v%x_d)) then
118 call device_free(v%x_d)
119 end if
120
121 v%n = 0
122
123 end subroutine vector_free
124
126 pure function vector_size(v) result(s)
127 class(vector_t), intent(in) :: v
128 integer :: s
129 s = v%n
130 end function vector_size
131
133 subroutine vector_assign_vector(v, w)
134 class(vector_t), intent(inout) :: v
135 type(vector_t), intent(in) :: w
136
137 call v%alloc(w%n)
138 if (neko_bcknd_device .eq. 1) then
139 call device_copy(v%x_d, w%x_d, v%n)
140 else
141 call copy(v%x, w%x, v%n)
142 end if
143
144 end subroutine vector_assign_vector
145
147 subroutine vector_assign_scalar(v, s)
148 class(vector_t), intent(inout) :: v
149 real(kind=rp), intent(in) :: s
150
151 if (neko_bcknd_device .eq. 1) then
152 call device_cfill(v%x_d, s, v%n)
153 else
154 call cfill(v%x, s, v%n)
155 end if
156
157 end subroutine vector_assign_scalar
158
159end module vector
Map a Fortran array to a device (allocate and associate)
Definition device.F90:72
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
subroutine, public device_cfill(a_d, c, n, strm)
Set all elements to a constant c .
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:214
Definition math.f90:60
subroutine, public cfill(a, c, n)
Set all elements to a constant c .
Definition math.f90:493
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.f90:255
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:81
subroutine vector_assign_scalar(v, s)
Assignment .
Definition vector.f90:148
subroutine vector_free(v)
Deallocate a vector.
Definition vector.f90:111
pure integer function vector_size(v)
Return the number of entries in the vector.
Definition vector.f90:127
subroutine vector_allocate(a, n)
Vector allocation without initialisation.
Definition vector.f90:94
subroutine vector_assign_vector(v, w)
Assignment .
Definition vector.f90:134