Triumvirate C++ API 0.5.0.post1.dev301+g026f21751
Three-point clustering measurements in large-scale structure analyses.
Loading...
Searching...
No Matches
arrayops.hpp
Go to the documentation of this file.
1// Triumvirate: Three-Point Clustering Measurements in LSS
2//
3// Copyright (C) 2023 Mike S Wang & Naonori S Sugiyama [GPL-3.0-or-later]
4//
5// This file is part of the Triumvirate program. See the COPYRIGHT
6// and LICENCE files at the top-level directory of this distribution
7// for details of copyright and licensing.
8//
9// This program is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17// See the GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program. If not, see <https://www.gnu.org/licenses/>.
21
32
33#ifndef TRIUMVIRATE_INCLUDE_ARRAYOPS_HPP_INCLUDED_
34#define TRIUMVIRATE_INCLUDE_ARRAYOPS_HPP_INCLUDED_
35
36#if defined(TRV_USE_HIP)
37#include <hipfft/hipfftXt.h>
38#elif defined(TRV_USE_CUDA) // !TRV_USE_HIP && TRV_USE_CUDA
39#include <cufftXt.h>
40#endif // TRV_USE_HIP
41#include <fftw3.h>
42
43#include <algorithm>
44#include <cfenv>
45#include <cmath>
46#include <cstdarg>
47#include <stdexcept>
48#include <vector>
49
50#include "monitor.hpp"
51
52#if defined(TRV_USE_HIP)
53using fft_double_complex = hipfftDoubleComplex;
54using fftHandle = hipfftHandle;
55using libXtDesc = hipLibXtDesc;
56#elif defined(TRV_USE_CUDA) // !TRV_USE_HIP && TRV_USE_CUDA
57using fft_double_complex = cufftDoubleComplex;
58using fftHandle = cufftHandle;
59using libXtDesc = cudaLibXtDesc;
60#endif // TRV_USE_HIP
61
62namespace trv {
63
64// ***********************************************************************
65// Extrapolation
66// ***********************************************************************
67
68namespace sys {
69
74class ExtrapError: public std::runtime_error {
75 public:
76 std::string err_mesg;
77
84 ExtrapError(const char* fmt_string, ...);
85
91 virtual const char* what() const noexcept;
92};
93
94} // namespace trv::sys
95
96namespace array {
97
103 NONE = 0,
104 ZERO = 1,
105 PAD = 2,
106 LIN = 3,
107 LOGLIN = 4,
108};
109
120template <typename T>
122 const std::vector<T>& arr, const T val,
123 T atol = 1.e-8, T rtol = 1.e-5
124) {
125 for (T elem : arr) {
126 if (std::abs(elem - val) > atol + rtol * std::abs(val)) {return false;}
127 }
128 return true;
129}
130
142int check_1d_array(
143 std::vector<double>& a, bool check_lin, bool check_loglin, bool check_sign
144);
145
153void extrap_lin(std::vector<double>& a, int N_ext, std::vector<double>& a_ext);
154
162void extrap_loglin(
163 std::vector<double>& a, int N_ext, std::vector<double>& a_ext
164);
165
175void extrap_pad(
176 std::vector<double>& a,
177 int N_ext, double c_lower, double c_upper,
178 std::vector<double>& a_ext
179);
180
191void extrap2d_lin(
192 std::vector< std::vector<double> >& a,
193 int N_row_ext, int N_col_ext,
194 std::vector< std::vector<double> >& a_ext
195);
196
207void extrap2d_loglin(
208 std::vector< std::vector<double> >& a,
209 int N_row_ext, int N_col_ext,
210 std::vector< std::vector<double> >& a_ext
211);
212
227void extrap2d_pad(
228 std::vector< std::vector<double> >& a,
229 int N_row_ext, int N_col_ext,
230 double c_row_lower, double c_row_upper,
231 double c_col_lower, double c_col_upper,
232 std::vector< std::vector<double> >& a_ext
233);
234
235
236// ***********************************************************************
237// Sorting
238// ***********************************************************************
239
246std::vector<int> get_sorted_indices(std::vector<int> sorting_vector);
247
248
249// ***********************************************************************
250// Memory management
251// ***********************************************************************
252
253#if defined(TRV_USE_HIP) || defined(TRV_USE_CUDA)
262void copy_complex_array_dtoh(
263 fft_double_complex* d_arr, fftw_complex* arr, std::size_t length
264);
265
274void copy_complex_array_htod(
275 fftw_complex* arr, fft_double_complex* d_arr, std::size_t length
276);
277
286void copy_complex_array_dtoh_mgpu(
287 fftHandle plan, libXtDesc* libxt_desc, fftw_complex* arr
288);
289
298void copy_complex_array_htod_mgpu(
299 fftHandle plan, libXtDesc* libxt_desc, fftw_complex* arr
300);
301#endif // TRV_USE_HIP || TRV_USE_CUDA
302} // namespace trv::array
303
304} // namespace trv
305
306#endif // !TRIUMVIRATE_INCLUDE_ARRAYOPS_HPP_INCLUDED_
ExtrapError(const char *fmt_string,...)
Construct an trv::sys::ExtrapError exception.
Definition arrayops.cpp:41
std::string err_mesg
error message
Definition arrayops.hpp:76
virtual const char * what() const noexcept
Exception string representation.
Definition arrayops.cpp:54
Provide tracking of program resources and exceptions.
ExtrapOption
Extrapolation scheme.
Definition arrayops.hpp:102
@ NONE
0: none
Definition arrayops.hpp:103
@ LOGLIN
4: log-linear
Definition arrayops.hpp:107
@ PAD
2: pad
Definition arrayops.hpp:105
@ ZERO
1: zero
Definition arrayops.hpp:104
@ LIN
3: linear
Definition arrayops.hpp:106
bool check_isclose(const std::vector< T > &arr, const T val, T atol=1.e-8, T rtol=1.e-5)
Check if all elements of a 1-d array are close to a given value.
Definition arrayops.hpp:121