 |
COMPASS
5.0.0
End-to-end AO simulation tool using GPU acceleration
|
Go to the documentation of this file.
42 #ifndef _CARMA_UTILS_H_
43 #define _CARMA_UTILS_H_
50 #include <driver_types.h>
51 #include <vector_types.h>
59 #include <cuda_fp16.h>
60 #include <cuda_runtime_api.h>
67 #define CARMA_PI 3.1415926535897932384626433832
83 std::ostringstream stm;
89 std::istringstream buffer(myString);
94 void inline split(std::vector<std::string> &tokens,
const std::string &text,
96 std::string::size_type start = 0, end = 0;
97 while ((end = text.find(sep, start)) != std::string::npos) {
98 tokens.push_back(text.substr(start, end - start));
101 tokens.push_back(text.substr(start));
105 int prev = 0, count = 0, max;
110 std::chrono::system_clock::time_point start;
121 #define DEBUG_TRACE(fmt, args...) \
122 fprintf(stderr, "[%s@%d]: " fmt "\n", __FILE__, __LINE__, ##args)
124 #define DEBUG_TRACE(fmt, args...) \
125 fprintf(stderr, "[%s@%d]: " fmt "\n", __FILE__, __LINE__, ##args)
128 #define CAST(type, new_var, var) type new_var = dynamic_cast<type>(var)
129 #define SCAST(type, new_var, var) type new_var = static_cast<type>(var)
135 #define carma_safe_call_no_sync(err) __carma_safe_call_no_sync(err, __FILE__, __LINE__)
136 #define carma_safe_call(err) __carma_safe_call((err), #err, __FILE__, __LINE__)
137 #define carma_safe_device_synchronize() \
138 __carma_safe_device_synchronize(__FILE__, __LINE__)
139 #define carmafft_safe_call(err) __carmafft_safe_call(err, __FILE__, __LINE__)
140 #define carma_check_msg(msg) __carma_check_msg(msg, __FILE__, __LINE__)
141 #define carma_safe_malloc(mallocCall) \
142 __carma_safe_malloc((mallocCall), __FILE__, __LINE__)
144 #define carma_safe_call_no_sync(err) err
145 #define carma_safe_call(err) err
146 #define carma_safe_device_synchronize() cudaDeviceSynchronize()
147 #define carmafft_safe_call(err) err
148 #define cutil_check_error(err) err
149 #define carma_check_msg(msg)
150 #define cutil_safe_malloc(mallocCall) (mallocCall)
154 #define MIN(a, b) ((a < b) ? a : b)
157 #define MAX(a, b) ((a > b) ? a : b)
170 inline bool is_pow2(
unsigned int x) {
return ((x & (x - 1)) == 0); }
177 template <
class T_data>
178 int find_nnz(T_data *d_data,
int *tmp_colind,
int N,
int *d_nnz,
int &h_nnz,
180 template <
class T_data>
182 int *colind,
int *rowind,
int nnz,
CarmaDevice *device);
186 template <
typename T_data>
191 int copy_from_float_to_half(
const float *data, half *dest,
int N,
193 int copy_from_half_to_float(
const half *d_data,
float *h_dest,
int N,
195 half *float_to_half_array(
float *source,
int N,
CarmaDevice *device);
196 float *half_to_float_array(half *source,
int N,
CarmaDevice *device);
208 if (cudaSuccess != err) {
209 fprintf(stderr,
"(%s:%i) : carma_safe_call_no_sync() Runtime API error : %s.\n",
210 file, line, cudaGetErrorString(err));
212 throw cudaGetErrorString(err);
218 if (cudaSuccess != err) {
219 fprintf(stderr,
"[%s:%i] %s\n carma_safe_call() Runtime API error : %s.\n",
220 file, line, code, cudaGetErrorString(err));
222 throw cudaGetErrorString(err);
227 cudaError err = cudaDeviceSynchronize();
228 if (cudaSuccess != err) {
230 "(%s:%i) : cudaDeviceSynchronize() Driver API error : %s.\n", file,
231 line, cudaGetErrorString(err));
233 throw cudaGetErrorString(err);
239 if (CUFFT_SUCCESS != err) {
240 fprintf(stderr,
"(%s:%i) : carmafft_safe_call() CUFFT error.\n", file, line);
242 throw "carmafft_safe_call() CUFFT error";
248 cudaError_t err = cudaGetLastError();
249 if (cudaSuccess != err) {
250 fprintf(stderr,
"(%s:%i) : carma_check_msg() CUTIL CUDA error : %s : %s.\n",
251 file, line, error_message, cudaGetErrorString(err));
252 throw cudaGetErrorString(err);
255 err = cudaDeviceSynchronize();
256 if (cudaSuccess != err) {
258 "(%s:%i) : carma_check_msg cudaDeviceSynchronize error: %s : %s.\n",
259 file, line, error_message, cudaGetErrorString(err));
260 throw cudaGetErrorString(err);
266 fprintf(stderr,
"(%s:%i) : cutil_safe_malloc host malloc failure\n", file,
268 throw "cutil_safe_malloc() cutil_safe_malloc host malloc failure";
272 #endif // _CARMA_UTILS_H_
int double_to_float(double *idata, float *odata, int N, CarmaDevice *device)
unsigned int next_pow2(unsigned int x)
void get_num_blocks_and_threads(CarmaDevice *device, int n, int &blocks, int &threads)
T from_string(const std::string &myString)
void sum_get_num_blocks_and_threads(int n, CarmaDevice *device, int &blocks, int &threads)
void __carma_safe_call_no_sync(cudaError err, const char *file, const int line)
void carma_start_profile()
std::string to_string(const T &n)
int float_to_double(float *idata, double *odata, int N, CarmaDevice *device)
void __carma_check_msg(const char *error_message, const char *file, const int line)
bool is_pow2(unsigned int x)
ProgressBar(int i, const std::string &desc="")
void __carma_safe_device_synchronize(const char *file, const int line)
void split(std::vector< std::string > &tokens, const std::string &text, char sep)
void __carma_safe_call(cudaError err, const char *code, const char *file, const int line)
void carma_stop_profile()
void __carma_safe_malloc(void *pointer, const char *file, const int line)
int fill_sparse_vect(T_data *dense_data, int *colind_sorted, T_data *values, int *colind, int *rowind, int nnz, CarmaDevice *device)
int find_nnz(T_data *d_data, int *tmp_colind, int N, int *d_nnz, int &h_nnz, CarmaDevice *device)
void __carmafft_safe_call(cufftResult err, const char *file, const int line)
int fill_array_with_value(T_data *d_data, T_data value, int N, CarmaDevice *device)