%-*- mode: Noweb; noweb-code-mode: c-mode -*- \index{utilities} \section{The files} \subsection{Header} <>= #ifndef __UTILITIES_H__ #define __UTILITIES_H__ #include #include #include #include #include #include #include #include "cublas_v2.h" #include "cusparse_v2.h" #include #define PI 3.141592653589793 #define RADIAN2ARCSEC (180*3600/PI) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define CUBLAS_VERBOSE 0 #define RAND_SEED 2013 #define N_THREAD 16 #define N_THREAD2 256 #define SQRT_DBL_MAX 1.3407807929942596e+154 //#define M_PI 3.14159265358979323846264338328 /* pi */ #define DBL_EPSILON 2.2204460492503131e-16 static void Error( const char *errMsg, const char *file, int line ) { fprintf(stderr,"\n\x1B[31m@(CEO)>ERROR: %s in %s at line %d\x1B[0m\n", errMsg, file, line ); exit( EXIT_FAILURE ); } #define ERROR( errMsg ) (Error( errMsg, __FILE__, __LINE__ )) #ifdef SILENT #define INFO( infoMsg, args... ); #else #define INFO( infoMsg , args...) fprintf(stdout,infoMsg, ##args); #endif static void HandleError( cudaError_t err, const char *file, int line ) { if (err != cudaSuccess) { fprintf(stderr,"\n\x1B[31m@(CEO)>ERROR: %s in %s at line %d\x1B[0m\n", cudaGetErrorString( err ), file, line ); exit( EXIT_FAILURE ); } } #define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ )) static void HandleErrorCUFFT( cufftResult_t err, const char *msg, const char *file, int line ) { if (err != CUFFT_SUCCESS) { fprintf(stderr,"\n\x1B[31m@(CEO)>CUFFT ERROR: %s in %s at line %d\x1B[0m\n", msg, file, line ); exit( EXIT_FAILURE ); } } #define HANDLE_ERROR_CUFFT( err, msg ) (HandleErrorCUFFT( err, msg, __FILE__, __LINE__ )) static void HandleErrorCUSPARSE( cusparseStatus_t err, const char *msg, const char *file, int line ) { if (err != CUSPARSE_STATUS_SUCCESS) { fprintf(stderr,"\n\x1B[31m@(CEO)>CUSPARSE ERROR: %s in %s at line %d\x1B[0m\n", msg, file, line ); exit( EXIT_FAILURE ); } } #define HANDLE_ERROR_CUSPARSE( err, msg ) (HandleErrorCUSPARSE( err, msg, __FILE__, __LINE__ )) static void HandleErrorCURAND( curandStatus_t err, const char *msg, const char *file, int line ) { if (err != CURAND_STATUS_SUCCESS) { fprintf(stderr,"\n\x1B[31m@(CEO)>CURAND ERROR: %s in %s at line %d\x1B[0m\n", msg, file, line ); exit( EXIT_FAILURE ); } } #define HANDLE_ERROR_CUSOLVER( err ) (HandleErrorCUSOLVER( err, __FILE__, __LINE__ )) static void HandleErrorCUSOLVER( cusolverStatus_t err, const char *file, int line ) { if (err != CUSOLVER_STATUS_SUCCESS) { if (err==CUSOLVER_STATUS_NOT_INITIALIZED) fprintf(stderr,"\n\x1B[31m@(CEO)>CUSOLVER ERROR: %s in %s at line %d:.\x1B[0m\n", "the library was not initialized", file, line ); else if (err==CUSOLVER_STATUS_INVALID_VALUE) fprintf(stderr,"\n\x1B[31m@(CEO)>CUSOLVER ERROR: %s in %s at line %d\x1B[0m\n", "invalid parameters were passed (m,n<0 or ldaCUSOLVER ERROR: %s in %s at line %d\x1B[0m\n", "the device only supports compute capability 2.0 and above", file, line ); else fprintf(stderr,"\n\x1B[31m@(CEO)>CURAND ERROR: %s in %s at line %d\x1B[0m\n", "an internal operation failed.", file, line ); exit( EXIT_FAILURE ); } } #define HANDLE_ERROR_CURAND( err, msg ) (HandleErrorCURAND( err, msg, __FILE__, __LINE__ )) inline float sign(float x) { return (float) ( (x > 0) - (x < 0) ); } int round_up_to_nhp2(int n); void set_device(int id); int get_device_count(); #define ARCSEC(a) (a*PI/180/3600) <> <> <> <> <> <> <> void CUBLAS_ERROR(cublasStatus_t status); <> <> <> __global__ void fill_ones_char(char *ones, int n_data) ; __global__ void circular_pupil(char *pupil, int N, float scale); __global__ void square_pupil(char *pupil, int N, int N_S, int I_S, int J_S); __global__ void rot_square_pupil(char *pupil, int N, float N_S, float I_0, float J_0, float theta, float I_S, float J_S); __global__ void GMT_pupil(char *pupil, int *piston_mask , const int N, const float S, float Dsh, const float c, const char v); //__global__ void valid_lenslet(char *mask, int N_pupil, float threshold); __global__ void fried_geometry(char *lenslet_mask, char *actuator_mask, int NL, int n, float threshold); __global__ void fried_geometry_vs_pupil(char *lenslet_mask, char *actuator_mask, const int NL, const int n, const float threshold, const char *pupil); __global__ void wavefront_finite_difference_kernel(float *sx, float *sy, int NL, float *phi, int n, float d); __global__ void wavefront_finite_difference_kernel_masked( float *sx, float *sy, int NL, float *phi, int n, float d, char *M); __global__ void wavefront_finite_difference_sparse_matrix_kernel( float *csrValH, int *csrColIndH, int *csrRowPtrH, const int NL, const int n, const float d); __global__ void set_gmt_piston_kernel(float *phase, float *p, char *m, int *piston_mask, const int N); __global__ void double2float_kern(float *d__single_data, double *d__double_data, const int N); char fried_geometry_setup(char *lenslet_mask, char *actuator_mask, int NL, int n, float threshold); char fried_geometry_setup_vs_pupil(char *lenslet_mask, char *actuator_mask, int NL, int n, float threshold, char *pupil); void dev2file(const char* filename, float* d__data, int n_data); void dev2file(const char* filename, float* d__data, int n_data, int n_data_page, int n_page); void dev2file(const char* filename, float* d__data, int n_data, int *n_data_page, int n_page); void dev2file(const char* filename, int* d__data, int n_data); void dev2file(const char* filename, float2* d__data, int n_data); void dev2file(const char* filename, char* d__data, int n_data); double atmosphere_refractive_index(float wavelength, float altitude, float temperature, float humidity); double atmospheric_dispersion(float wavelength, float delta_wavelength, float zenith, float altitude, float temperature, float humidity); void dev2host( float *host_data, float *dev_data, int N); void dev2host_int( int *host_data, int *dev_data, int N); void host2dev( float *dev_data, float *host_data, int N); void host2dev_char( char *dev_data, char *host_data, int N); void freedev( float **dev_data ); <