|
| 1 | +! Copyright (c) 2018-2026, 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 | +! |
| 33 | +! Defines a vector list |
| 34 | +module vector_list |
| 35 | + use, intrinsic :: iso_fortran_env, only : error_unit |
| 36 | + use vector, only : vector_ptr_t, vector_t |
| 37 | + use iso_c_binding, only : c_ptr |
| 38 | + use num_types, only : rp |
| 39 | + use utils, only : neko_error |
| 40 | + use comm, only : pe_rank |
| 41 | + implicit none |
| 42 | + private |
| 43 | + |
| 44 | + !> vector_list_t, To be able to group vectors together |
| 45 | + type, public :: vector_list_t |
| 46 | + type(vector_ptr_t), allocatable :: items(:) |
| 47 | + contains |
| 48 | + !> Constructor. Allocates array and pointers. |
| 49 | + procedure, pass(this) :: init => vector_list_init |
| 50 | + !> Destructor. |
| 51 | + procedure, pass(this) :: free => vector_list_free |
| 52 | + !> Append a vector to the list. |
| 53 | + procedure, pass(this) :: append => vector_list_append |
| 54 | + generic :: get => get_by_index, get_by_name |
| 55 | + !> Get an item pointer by array index |
| 56 | + procedure, pass(this) :: get_by_index => vector_list_get_by_index |
| 57 | + !> Get an item pointer by vector name |
| 58 | + procedure, pass(this) :: get_by_name => vector_list_get_by_name |
| 59 | + !> Point item at given index. |
| 60 | + generic :: assign => assign_to_ptr, assign_to_vector_ptr |
| 61 | + procedure, pass(this) :: assign_to_ptr => vector_list_assign_to_ptr |
| 62 | + procedure, pass(this) :: assign_to_vector_ptr => & |
| 63 | + vector_list_assign_to_vector_ptr |
| 64 | + procedure, pass(this) :: assign_to_vector => vector_list_assign_to_vector |
| 65 | + |
| 66 | + !> Get device pointer for a given index. |
| 67 | + procedure, pass(this) :: x_d => vector_list_x_d |
| 68 | + !> Get pointer to the raw data array for a given index. |
| 69 | + procedure, pass(this) :: x => vector_list_x |
| 70 | + !> Get number of items in the list. |
| 71 | + procedure, pass(this) :: size => vector_list_size |
| 72 | + !> Get the size of an item in the list. |
| 73 | + procedure, pass(this) :: item_size => vector_list_item_size |
| 74 | + !> Get the name for an item in the list. |
| 75 | + procedure, pass(this) :: name => vector_list_name |
| 76 | + end type vector_list_t |
| 77 | + |
| 78 | +contains |
| 79 | + !> Constructor. Just allocates the array. |
| 80 | + !! @param size The size of the list to preallocate |
| 81 | + subroutine vector_list_init(this, size) |
| 82 | + class(vector_list_t), intent(inout) :: this |
| 83 | + integer, intent(in) :: size |
| 84 | + |
| 85 | + call this%free() |
| 86 | + |
| 87 | + allocate(this%items(size)) |
| 88 | + end subroutine vector_list_init |
| 89 | + |
| 90 | + !> Get number of items in the list. |
| 91 | + pure function vector_list_size(this) result(n) |
| 92 | + class(vector_list_t), intent(in) :: this |
| 93 | + integer :: n |
| 94 | + n = size(this%items) |
| 95 | + end function vector_list_size |
| 96 | + |
| 97 | + !> Get an item pointer by array index |
| 98 | + !! @param i The index of the item. |
| 99 | + function vector_list_get_by_index(this, i) result(f) |
| 100 | + class(vector_list_t), target, intent(inout) :: this |
| 101 | + type(vector_t), pointer :: f |
| 102 | + integer, intent(in) :: i |
| 103 | + f => this%items(i)%ptr |
| 104 | + end function vector_list_get_by_index |
| 105 | + |
| 106 | + !> Get an item pointer by array index |
| 107 | + !! @param name The name of the item. |
| 108 | + function vector_list_get_by_name(this, name) result(f) |
| 109 | + class(vector_list_t), target, intent(inout) :: this |
| 110 | + type(vector_t), pointer :: f |
| 111 | + character(len=*), intent(in) :: name |
| 112 | + integer :: i |
| 113 | + |
| 114 | + nullify(f) |
| 115 | + |
| 116 | + do i = 1, this%size() |
| 117 | + if (this%name(i) .eq. trim(name)) then |
| 118 | + f => this%items(i)%ptr |
| 119 | + return |
| 120 | + end if |
| 121 | + end do |
| 122 | + |
| 123 | + if (pe_rank .eq. 0) then |
| 124 | + write(error_unit,*) "Current vector list contents:" |
| 125 | + |
| 126 | + do i = 1, this%size() |
| 127 | + write(error_unit,*) "- ", this%name(i) |
| 128 | + end do |
| 129 | + end if |
| 130 | + |
| 131 | + call neko_error("No vector with name " // trim(name) // " found in list") |
| 132 | + end function vector_list_get_by_name |
| 133 | + |
| 134 | + !> Append a vector to the list. |
| 135 | + !! @param f The vector to append. |
| 136 | + subroutine vector_list_append(this, f) |
| 137 | + class(vector_list_t), intent(inout) :: this |
| 138 | + class(vector_t), intent(in), target :: f |
| 139 | + type(vector_ptr_t), allocatable :: tmp(:) |
| 140 | + integer :: len |
| 141 | + |
| 142 | + if (.not. allocated(this%items)) then |
| 143 | + allocate(this%items(1)) |
| 144 | + call this%items(1)%init(f) |
| 145 | + return |
| 146 | + end if |
| 147 | + |
| 148 | + len = size(this%items) |
| 149 | + |
| 150 | + allocate(tmp(len+1)) |
| 151 | + tmp(1:len) = this%items |
| 152 | + call move_alloc(tmp, this%items) |
| 153 | + call this%items(len+1)%init(f) |
| 154 | + |
| 155 | + end subroutine vector_list_append |
| 156 | + |
| 157 | + !> Destructor. |
| 158 | + subroutine vector_list_free(this) |
| 159 | + class(vector_list_t), intent(inout) :: this |
| 160 | + integer :: i, n_fields |
| 161 | + |
| 162 | + if (allocated(this%items)) then |
| 163 | + n_fields = this%size() |
| 164 | + do i = 1, n_fields |
| 165 | + call this%items(i)%free() |
| 166 | + end do |
| 167 | + deallocate(this%items) |
| 168 | + end if |
| 169 | + |
| 170 | + end subroutine vector_list_free |
| 171 | + |
| 172 | + !> Get device pointer for a given index |
| 173 | + !! @param i The index of the item. |
| 174 | + function vector_list_x_d(this, i) result(ptr) |
| 175 | + class(vector_list_t), intent(in) :: this |
| 176 | + integer, intent(in) :: i |
| 177 | + type(c_ptr) :: ptr |
| 178 | + |
| 179 | + ptr = this%items(i)%ptr%x_d |
| 180 | + end function vector_list_x_d |
| 181 | + |
| 182 | + function vector_list_x(this, i) result(x) |
| 183 | + class(vector_list_t), target, intent(in) :: this |
| 184 | + real(kind=rp), pointer, contiguous :: x(:) |
| 185 | + integer, intent(in) :: i |
| 186 | + x => this%items(i)%ptr%x |
| 187 | + end function vector_list_x |
| 188 | + |
| 189 | + !> Get the size of item `i`. |
| 190 | + !! @param i The index of the item. |
| 191 | + function vector_list_item_size(this, i) result(size) |
| 192 | + class(vector_list_t), target, intent(in) :: this |
| 193 | + integer, intent(in) :: i |
| 194 | + integer :: size |
| 195 | + |
| 196 | + size = this%items(i)%ptr%size() |
| 197 | + |
| 198 | + end function vector_list_item_size |
| 199 | + |
| 200 | + !> Point item at a given index. |
| 201 | + !! @param i The index of the item. |
| 202 | + !! @param ptr A vector pointer to point the item to. |
| 203 | + subroutine vector_list_assign_to_ptr(this, i, ptr) |
| 204 | + class(vector_list_t), intent(inout) :: this |
| 205 | + integer, intent(in) :: i |
| 206 | + type(vector_t), pointer, intent(in) :: ptr |
| 207 | + |
| 208 | + call this%items(i)%init(ptr) |
| 209 | + |
| 210 | + end subroutine vector_list_assign_to_ptr |
| 211 | + |
| 212 | + !> Point item at a given index. |
| 213 | + !! @param i The index of the item. |
| 214 | + !! @param ptr An encapsulated vector pointer to point the item to. |
| 215 | + subroutine vector_list_assign_to_vector_ptr(this, i, ptr) |
| 216 | + class(vector_list_t), intent(inout) :: this |
| 217 | + integer, intent(in) :: i |
| 218 | + type(vector_ptr_t), target, intent(in) :: ptr |
| 219 | + |
| 220 | + call this%items(i)%init(ptr%ptr) |
| 221 | + end subroutine vector_list_assign_to_vector_ptr |
| 222 | + |
| 223 | + !> Point item at a given index. |
| 224 | + !! @param i The index of the item. |
| 225 | + !! @param vec A vector to point the item to. |
| 226 | + subroutine vector_list_assign_to_vector(this, i, vec) |
| 227 | + class(vector_list_t), intent(inout) :: this |
| 228 | + integer, intent(in) :: i |
| 229 | + type(vector_t), target, intent(in) :: vec |
| 230 | + |
| 231 | + call this%items(i)%init(vec) |
| 232 | + end subroutine vector_list_assign_to_vector |
| 233 | + |
| 234 | + !> Get the name for an item in the list. |
| 235 | + !! @param i The index of the item. |
| 236 | + function vector_list_name(this, i) result(result) |
| 237 | + class(vector_list_t), target, intent(in) :: this |
| 238 | + integer, intent(in) :: i |
| 239 | + character(len=80) :: result |
| 240 | + |
| 241 | + result = this%items(i)%ptr%name |
| 242 | + end function vector_list_name |
| 243 | + |
| 244 | + |
| 245 | +end module vector_list |
0 commit comments