17 #ifndef _CARMA_UTILS_H_
18 #define _CARMA_UTILS_H_
25 #include <driver_types.h>
26 #include <vector_types.h>
34 #include <cuda_fp16.h>
35 #include <cuda_runtime_api.h>
38 #include <carma_indicators.hpp>
44 #define CARMA_PI 3.1415926535897932384626433832
60 std::ostringstream stm;
66 std::istringstream buffer(myString);
71 void inline split(std::vector<std::string> &tokens,
const std::string &text,
73 std::string::size_type start = 0, end = 0;
74 while ((end = text.find(sep, start)) != std::string::npos) {
75 tokens.push_back(text.substr(start, end - start));
78 tokens.push_back(text.substr(start));
84 #define DEBUG_TRACE(fmt, args...) \
85 fprintf(stderr, "[%s@%d]: " fmt "\n", __FILE__, __LINE__, ##args)
87 #define DEBUG_TRACE(fmt, args...) \
88 fprintf(stderr, "[%s@%d]: " fmt "\n", __FILE__, __LINE__, ##args)
91 #define CAST(type, new_var, var) type new_var = dynamic_cast<type>(var)
92 #define SCAST(type, new_var, var) type new_var = static_cast<type>(var)
98 #define carma_safe_call_no_sync(err) __carma_safe_call_no_sync(err, __FILE__, __LINE__)
99 #define carma_safe_call(err) __carma_safe_call((err), #err, __FILE__, __LINE__)
100 #define carma_safe_device_synchronize() \
101 __carma_safe_device_synchronize(__FILE__, __LINE__)
102 #define carmafft_safe_call(err) __carmafft_safe_call(err, __FILE__, __LINE__)
103 #define carma_check_msg(msg) __carma_check_msg(msg, __FILE__, __LINE__)
104 #define carma_safe_malloc(mallocCall) \
105 __carma_safe_malloc((mallocCall), __FILE__, __LINE__)
107 #define carma_safe_call_no_sync(err) err
108 #define carma_safe_call(err) err
109 #define carma_safe_device_synchronize() cudaDeviceSynchronize()
110 #define carmafft_safe_call(err) err
111 #define cutil_check_error(err) err
112 #define carma_check_msg(msg)
113 #define cutil_safe_malloc(mallocCall) (mallocCall)
117 #define MIN(a, b) ((a < b) ? a : b)
120 #define MAX(a, b) ((a > b) ? a : b)
133 inline bool is_pow2(
unsigned int x) {
return ((x & (x - 1)) == 0); }
140 template <
class T_data>
141 int find_nnz(T_data *d_data,
int *tmp_colind,
int N,
int *d_nnz,
int &h_nnz,
143 template <
class T_data>
145 int *colind,
int *rowind,
int nnz,
CarmaDevice *device);
149 template <
typename T_data>
154 int copy_from_float_to_half(
const float *data, half *dest,
int N,
156 int copy_from_half_to_float(
const half *d_data,
float *h_dest,
int N,
158 half *float_to_half_array(
float *source,
int N,
CarmaDevice *device);
159 float *half_to_float_array(half *source,
int N,
CarmaDevice *device);
171 if (cudaSuccess != err) {
172 fprintf(stderr,
"(%s:%i) : carma_safe_call_no_sync() Runtime API error : %s.\n",
173 file, line, cudaGetErrorString(err));
175 throw cudaGetErrorString(err);
181 if (cudaSuccess != err) {
182 fprintf(stderr,
"[%s:%i] %s\n carma_safe_call() Runtime API error : %s.\n",
183 file, line, code, cudaGetErrorString(err));
185 throw cudaGetErrorString(err);
190 cudaError err = cudaDeviceSynchronize();
191 if (cudaSuccess != err) {
193 "(%s:%i) : cudaDeviceSynchronize() Driver API error : %s.\n", file,
194 line, cudaGetErrorString(err));
196 throw cudaGetErrorString(err);
202 if (CUFFT_SUCCESS != err) {
203 fprintf(stderr,
"(%s:%i) : carmafft_safe_call() CUFFT error.\n", file, line);
205 throw "carmafft_safe_call() CUFFT error";
211 cudaError_t err = cudaGetLastError();
212 if (cudaSuccess != err) {
213 fprintf(stderr,
"(%s:%i) : carma_check_msg() CUTIL CUDA error : %s : %s.\n",
214 file, line, error_message, cudaGetErrorString(err));
215 throw cudaGetErrorString(err);
218 err = cudaDeviceSynchronize();
219 if (cudaSuccess != err) {
221 "(%s:%i) : carma_check_msg cudaDeviceSynchronize error: %s : %s.\n",
222 file, line, error_message, cudaGetErrorString(err));
223 throw cudaGetErrorString(err);
229 fprintf(stderr,
"(%s:%i) : cutil_safe_malloc host malloc failure\n", file,
231 throw "cutil_safe_malloc() cutil_safe_malloc host malloc failure";
int fill_array_with_value(T_data *d_data, T_data value, int N, CarmaDevice *device)
int fill_sparse_vect(T_data *dense_data, int *colind_sorted, T_data *values, int *colind, int *rowind, int nnz, CarmaDevice *device)
void __carmafft_safe_call(cufftResult err, const char *file, const int line)
void __carma_safe_device_synchronize(const char *file, const int line)
void __carma_safe_malloc(void *pointer, const char *file, const int line)
unsigned int next_pow2(unsigned int x)
void get_num_blocks_and_threads(CarmaDevice *device, int n, int &blocks, int &threads)
int find_nnz(T_data *d_data, int *tmp_colind, int N, int *d_nnz, int &h_nnz, CarmaDevice *device)
void __carma_safe_call_no_sync(cudaError err, const char *file, const int line)
void __carma_check_msg(const char *error_message, const char *file, const int line)
void carma_start_profile()
int double_to_float(double *idata, float *odata, int N, CarmaDevice *device)
void __carma_safe_call(cudaError err, const char *code, const char *file, const int line)
int float_to_double(float *idata, double *odata, int N, CarmaDevice *device)
bool is_pow2(unsigned int x)
void sum_get_num_blocks_and_threads(int n, CarmaDevice *device, int &blocks, int &threads)
void carma_stop_profile()
void split(std::vector< std::string > &tokens, const std::string &text, char sep)
std::string to_string(const T &n)
T from_string(const std::string &myString)