-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.h
269 lines (231 loc) · 6.08 KB
/
conversions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#ifndef CONVERSIONS_H
#define CONVERSIONS_H
// need to include runtime.h and cblas.h
#ifdef __cplusplus
#include <ccomplex>
#else
#include <complex.h>
#endif
#include "cblas.h"
#include "common.h"
#include "runtime.h"
#ifdef __cplusplus
extern "C" {
#endif
static inline CBLAS_TRANSPOSE c_trans(char c) {
switch (c) {
case 'N':
case 'n':
return CblasNoTrans;
case 'T':
case 't':
return CblasTrans;
case 'C':
case 'c':
return CblasConjTrans;
default:
writef(STDERR_FILENO, "Invalid transpose op '%c'\n", c);
abort();
break;
}
}
static inline CBLAS_SIDE c_side(char c) {
switch (c) {
case 'L':
case 'l':
return CblasLeft;
case 'R':
case 'r':
return CblasRight;
default:
writef(STDERR_FILENO, "Invalid side op '%c'\n", c);
abort();
break;
}
}
static inline CBLAS_UPLO c_uplo(char c) {
switch (c) {
case 'U':
case 'u':
return CblasUpper;
case 'L':
case 'l':
return CblasLower;
default:
writef(STDERR_FILENO, "Invalid uplo op '%c'\n", c);
abort();
break;
}
}
static inline CBLAS_DIAG c_diag(char c) {
switch (c) {
case 'U':
case 'u':
return CblasUnit;
case 'N':
case 'n':
return CblasNonUnit;
default:
writef(STDERR_FILENO, "Invalid diag op '%c'\n", c);
abort();
break;
}
}
#ifdef __cplusplus
}; // extern "C"
#endif
#ifdef __cplusplus
#if USE_CUDA
#include <cublas_api.h>
static inline cuComplex* cmplx_ptr(float _Complex *fptr) {
return (cuComplex*) fptr;
}
static inline cuDoubleComplex* cmplx_ptr(double _Complex *dptr) {
return (cuDoubleComplex*) dptr;
}
static inline cuComplex cu(float _Complex f) {
return (cuComplex) { .x = crealf(f), .y = cimagf(f) };
}
static inline cuComplex cu(float r, float i) {
return (cuComplex) { .x = r, .y = i };
}
static inline cuDoubleComplex cu(double _Complex d) {
return (cuDoubleComplex) { .x = creal(d), .y = cimag(d) };
}
static inline cuDoubleComplex cu(double r, double i) {
return (cuDoubleComplex) { .x = r, .y = i };
}
static inline cuComplex cu2(float _Complex f) { return cu(f); }
static inline cuDoubleComplex cu2(double _Complex d) { return cu(d); }
static inline cublasOperation_t cu(CBLAS_TRANSPOSE trans) {
switch (trans) {
case CblasNoTrans:
return CUBLAS_OP_N;
case CblasTrans:
return CUBLAS_OP_T;
case CblasConjTrans:
return CUBLAS_OP_C;
default:
fprintf(stderr, "Invalid value for enum: %d\n", trans);
abort();
break;
}
}
static inline cublasFillMode_t cu(CBLAS_UPLO uplo) {
switch (uplo) {
case CblasUpper:
return CUBLAS_FILL_MODE_UPPER;
case CblasLower:
return CUBLAS_FILL_MODE_LOWER;
default:
fprintf(stderr, "Invalid value for enum: %d\n", uplo);
abort();
break;
}
}
static inline cublasDiagType_t cu(CBLAS_DIAG diag) {
switch (diag) {
case CblasNonUnit:
return CUBLAS_DIAG_NON_UNIT;
case CblasUnit:
return CUBLAS_DIAG_UNIT;
default:
fprintf(stderr, "Invalid value for enum: %d\n", diag);
abort();
break;
}
}
static inline cublasSideMode_t cu(CBLAS_SIDE side) {
switch (side) {
case CblasLeft:
return CUBLAS_SIDE_LEFT;
case CblasRight:
return CUBLAS_SIDE_RIGHT;
default:
fprintf(stderr, "Invalid value for enum: %d\n", side);
abort();
break;
}
}
template <typename T>
using geam_t = cublasStatus_t (*)(cublasHandle_t,
cublasOperation_t, cublasOperation_t,
int, int,
const T *,
const T *, int,
const T *,
const T *, int,
T *, int);
#elif USE_OPENCL
#include <clBLAS.h>
static inline float _Complex* cmplx_ptr(float _Complex *fptr) {
return fptr;
}
static inline double _Complex* cmplx_ptr(double _Complex *dptr) {
return dptr;
}
static inline FloatComplex cu(float _Complex f) {
return floatComplex(crealf(f), cimagf(f));
}
static inline DoubleComplex cu(double _Complex d) {
return doubleComplex(creal(d), cimag(d));
}
static inline cl_float2 cu2(float _Complex f) {
return (cl_float2) { .s = {crealf(f), cimagf(f)} };
}
static inline cl_double2 cu2(double _Complex d) {
return (cl_double2) { .s = {creal(d), cimag(d)} };
}
static inline clblasTranspose clb(CBLAS_TRANSPOSE trans) {
switch (trans) {
case CblasNoTrans:
return clblasNoTrans;
case CblasTrans:
return clblasTrans;
case CblasConjTrans:
return clblasConjTrans;
default:
fprintf(stderr, "Invalid value for CBLAS_TRANSPOSE: %d\n", trans);
abort();
break;
}
}
static inline clblasUplo clb(CBLAS_UPLO uplo) {
switch (uplo) {
case CblasUpper:
return clblasUpper;
case CblasLower:
return clblasLower;
default:
fprintf(stderr, "Invalid value for CBLAS_UPLO: %d\n", uplo);
abort();
break;
}
}
static inline clblasDiag clb(CBLAS_DIAG diag) {
switch (diag) {
case CblasNonUnit:
return clblasNonUnit;
case CblasUnit:
return clblasUnit;
default:
fprintf(stderr, "Invalid value for CBLAS_DIAG: %d\n", diag);
abort();
break;
}
}
static inline clblasSide clb(CBLAS_SIDE side) {
switch (side) {
case CblasLeft:
return clblasLeft;
case CblasRight:
return clblasRight;
default:
fprintf(stderr, "Invalid value for CBLAS_SIDE: %d\n", side);
abort();
break;
}
}
#endif // elif USE_OPENCL
#endif // __cplusplus
#endif