-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmkl.c
436 lines (383 loc) · 12.3 KB
/
libmkl.c
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include"yapi.h"
// Macro that simplifies calls to y_error() when reporting errors
// to Yorick.
// The do{... } while(0) is there to avoid forgetting { ... } around it.
// __FILE__ and __LINE__ are replaced at compilation by actual filename and file line.
// (note: __FUNCTION__ also exists ..)
#define ERROR2YORICK(fmt, args...) do { char errmsg[128]; sprintf(errmsg, "[%s@%d]: " fmt, __FILE__, __LINE__, ##args); y_error(errmsg); } while(0)
#include<mkl_lapack.h>
#include<mkl_blas.h>
#include<stdlib.h>
#include<stdio.h>
int Y_potri_cpu(int argc) {
/** @brief wrapper routine for mkl_lapack potri method
* @param[in] argc : command line arguments
* can work as a subroutine (return discarded)
* - first : the matrix to inverse
*/
if (yarg_subroutine()) {
int yType;
long ntot;
long dims[Y_DIMSIZE];
MKL_INT info=0;
char uplo='L';
MKL_INT one = 1;
MKL_INT row;
void *h_mat=ygeta_any(argc - 1, &ntot, dims, &yType);
MKL_INT n=dims[1];
MKL_INT m=dims[2];
if (yType == Y_FLOAT) {
spotrf( &uplo, &n, (float*) h_mat, &n, &info );
if(info) ERROR2YORICK("Cholesky failed");
spotri( &uplo, &n, (float*) h_mat, &n, &info );
if(info) ERROR2YORICK("Cholesky inverse failed");
// Copy lower part to the upper
MKL_INT size=n-1;
float *matL = (float*)h_mat+1;
float *matU = (float*)h_mat+n;
do{
scopy(&size, matL, &one, matU, &n);
size--;
matL+=n+1;
matU+=n+1;
} while (size>0);
} else if (yType == Y_DOUBLE) {
dpotrf( &uplo, &n, (double*) h_mat, &n, &info);
if(info) ERROR2YORICK("Cholesky failed");
dpotri( &uplo, &n, (double*) h_mat, &n, &info );
if(info) ERROR2YORICK("Cholesky inverse failed");
// Copy lower part to the upper
MKL_INT size=n-1;
double *matL = (double*)h_mat+1;
double *matU = (double*)h_mat+n;
do{
dcopy(&size, matL, &one, matU, &n);
size--;
matL+=n+1;
matU+=n+1;
} while (size>0);
} else {
y_error("carma_potri not implemented for this type");
}
} else {
y_error("carma_potri must be call as a subroutine");
}
return 0;
}
void fComputeMatrixEigenVectors(const MKL_INT matrix_rank, float *matrix,float *EV)
{
char JOBZ='V';
char UPLO='L';
int info=0;
MKL_INT n=matrix_rank;
MKL_INT lda=matrix_rank;
MKL_INT lwork=-1;
MKL_INT liwork=-1;
MKL_INT iwkopt;
MKL_INT *iwork;
float wkopt;
float *work;
fsyevd(&JOBZ,&UPLO,&n,matrix,&lda,EV,&wkopt,&lwork,&iwkopt,&liwork,&info);
if(info>0) ERROR2YORICK("The algorithm failed to query workspace.");
lwork=(MKL_INT)wkopt;
liwork=iwkopt;
work=malloc(lwork*sizeof(float));
iwork=malloc(liwork*sizeof(MKL_INT));
fsyevd(&JOBZ,&UPLO,&n,matrix,&lda,EV,work,&lwork,iwork,&liwork,&info);
if(info>0) ERROR2YORICK("The algorithm failed to compute eigenvalues.");
free(work);
free(iwork);
}
void dComputeMatrixEigenVectors(const MKL_INT matrix_rank, double *matrix,double *EV)
{
char JOBZ='V';
char UPLO='L';
MKL_INT info=0;
MKL_INT n=matrix_rank;
MKL_INT lda=matrix_rank;
MKL_INT lwork=-1;
MKL_INT liwork=-1;
MKL_INT iwkopt;
MKL_INT *iwork;
double wkopt;
double *work;
dsyevd(&JOBZ,&UPLO,&n,matrix,&lda,EV,&wkopt,&lwork,&iwkopt,&liwork,&info);
if(info>0) ERROR2YORICK("The algorithm failed to query workspace.");
lwork=(MKL_INT)wkopt;
liwork=iwkopt;
work=malloc(lwork*sizeof(double));
iwork=malloc(liwork*sizeof(MKL_INT));
dsyevd(&JOBZ,&UPLO,&n,matrix,&lda,EV,work,&lwork,iwork,&liwork,&info);
if(info>0) ERROR2YORICK("The algorithm failed to compute eigenvalues.");
free(work);
free(iwork);
}
int Y_syevd_cpu(int argc) {
/** @brief wrapper routine for mkl_lapack potri method
* @param[in] argc : command line arguments
* can work as a subroutine (return discarded)
* - first : the matrix to inverse
*/
if (yarg_subroutine()) {
int yType;
long ntot;
long dims[Y_DIMSIZE];
char uplo='L';
MKL_INT one = 1;
MKL_INT row;
void *h_mat=ygeta_any(argc - 1, &ntot, dims, &yType);
void *h_ev=ygeta_any(argc - 2, &ntot, dims, &yType);
MKL_INT n=dims[1];
if (yType == Y_FLOAT) {
fComputeMatrixEigenVectors(n, (float*)h_mat, (float*)h_ev);
} else if (yType == Y_DOUBLE) {
dComputeMatrixEigenVectors(n, (double*)h_mat, (double*)h_ev);
} else {
y_error("carma_potri not implemented for this type");
}
} else {
int yType;
long ntot;
long dims[Y_DIMSIZE];
char uplo='L';
MKL_INT one = 1;
MKL_INT row;
void *h_mat=ygeta_any(argc - 1, &ntot, dims, &yType);
dims[0]=1;
MKL_INT n=dims[1];
if (yType == Y_FLOAT) {
float *h_ev=ypush_f(dims);
fComputeMatrixEigenVectors(n, (float*)h_mat, h_ev);
} else if (yType == Y_DOUBLE) {
double *h_ev=ypush_d(dims);
dComputeMatrixEigenVectors(n, (double*)h_mat, h_ev);
} else {
y_error("carma_potri not implemented for this type");
}
}
return 0;
}
void Y_gemm_cpu(int argc)
/** @brief wrapper routine for mkl_blas gemm method
* @param[in] argc : command line arguments
* can work as a (1) subroutine (return discarded) or (2) as a function
* - first : (1) the C matrix / (2) the A matrix
* - second : (1) the A matrix / (2) the B matrix
* - third : (1) the B matrix
* - fourth : (1) the alpha coeff
* - fifth : (1) the beta coeff
* - sixth : (1) the opA
* - seventh : (1) the opB
* in case (2) the destination is pushed on the stack as a yoga_obj
* only floating point types supported (single or double precision)
*/
{
int yType=yarg_typeid(argc - 1);
if (yarg_subroutine()) {
char opA = 'n';
if (argc > 3)
opA = ygets_c(argc - 4);
char opB = 'n';
if (argc > 4)
opB = ygets_c(argc - 5);
if (yType == Y_FLOAT) {
float alpha = 1.0f;
if (argc > 5)
alpha = ygets_f(argc - 6);
float beta = 0.0f;
if (argc > 6)
beta = ygets_f(argc - 7);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
float *matA = ygeta_f(argc - 1, &ntot, dimA);
float *matB = ygeta_f(argc - 2, &ntot, dimB);
float *matC = ygeta_f(argc - 3, &ntot, dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT k = (opA == 'n') ? dimA[2] : dimA[1];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
sgemm(&opA, &opB, &m, &n, &k, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
if (yType == Y_DOUBLE) {
double alpha = 1.0;
if (argc > 5)
alpha = ygets_d(argc - 6);
double beta = 0.0;
if (argc > 6)
beta = ygets_d(argc - 7);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
double *matA = ygeta_d(argc - 1, &ntot, dimA);
double *matB = ygeta_d(argc - 2, &ntot, dimB);
double *matC = ygeta_d(argc - 3, &ntot, dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT k = (opA == 'n') ? dimA[2] : dimA[1];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
dgemm(&opA, &opB, &m, &n, &k, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
} else {
// called as a function : need to allocate space
char opA = 'n';
if (argc > 2)
opA = ygets_c(argc - 3);
char opB = 'n';
if (argc > 3)
opB = ygets_c(argc - 4);
if (yType == Y_FLOAT) {
float alpha = 1.0f;
if (argc > 4)
alpha = ygets_f(argc - 5);
float beta = 0.0f;
if (argc > 5)
beta = ygets_f(argc - 6);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
float *matA = ygeta_f(argc - 1, &ntot, dimA);
float *matB = ygeta_f(argc - 2, &ntot, dimB);
dimC[0] = 2;
dimC[1] = (opA == 'n') ? dimA[1] : dimA[2];
dimC[2] = (opB == 'n') ? dimB[2] : dimB[1];
float *matC = ypush_f(dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT k = (opA == 'n') ? dimA[2] : dimA[1];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
sgemm(&opA, &opB, &m, &n, &k, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
} else if (yType == Y_DOUBLE) {
double alpha = 1.0;
if (argc > 4)
alpha = ygets_d(argc - 5);
double beta = 0.0;
if (argc > 5)
beta = ygets_d(argc - 6);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
double *matA = ygeta_d(argc - 1, &ntot, dimA);
double *matB = ygeta_d(argc - 2, &ntot, dimB);
dimC[0] = 2;
dimC[1] = (opA == 'n') ? dimA[1] : dimA[2];
dimC[2] = (opB == 'n') ? dimB[2] : dimB[1];
double *matC = ypush_d(dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT k = (opA == 'n') ? dimA[2] : dimA[1];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
dgemm(&opA, &opB, &m, &n, &k, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
}
}
void Y_symm_cpu(int argc)
/** @brief wrapper routine for mkl_blas symm method
* @param[in] argc : command line arguments
* can work as a (1) subroutine (return discarded) or (2) as a function
* - first : (1) the C matrix / (2) the A matrix
* - second : (1) the A matrix / (2) the B matrix
* - third : (1) the B matrix
* - fourth : (1) the alpha coeff
* - fifth : (1) the beta coeff
* - sixth : (1) the opA
* - seventh : (1) the opB
* in case (2) the destination is pushed on the stack as a yoga_obj
* only floating point types supported (single or double precision)
*/
{
int yType=yarg_typeid(argc - 1);
if (yarg_subroutine()) {
char side = 'L'; //or 'R'
if (argc > 3)
side = ygets_c(argc - 4);
if (yType == Y_FLOAT) {
float alpha = 1.0f;
if (argc > 4)
alpha = ygets_f(argc - 5);
float beta = 0.0f;
if (argc > 5)
beta = ygets_f(argc - 6);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
float *matA = ygeta_f(argc - 1, &ntot, dimA);
float *matB = ygeta_f(argc - 2, &ntot, dimB);
float *matC = ygeta_f(argc - 3, &ntot, dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
ssymm(&side, "L", &m, &n, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
if (yType == Y_DOUBLE) {
double alpha = 1.0f;
if (argc > 4)
alpha = ygets_d(argc - 5);
double beta = 0.0f;
if (argc > 5)
beta = ygets_d(argc - 6);
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE],dimC[Y_DIMSIZE];
double *matA = ygeta_d(argc - 1, &ntot, dimA);
double *matB = ygeta_d(argc - 2, &ntot, dimB);
double *matC = ygeta_d(argc - 3, &ntot, dimC);
MKL_INT m = dimC[1];
MKL_INT n = dimC[2];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimC[1];
dsymm(&side, "L", &m, &n, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
} else {
// called as a function : need to allocate space
char side = 'L'; // or'R'
if (argc > 2)
side = ygets_c(argc - 3);
if (yType == Y_FLOAT) {
float alpha = 1.0f;
if (argc > 3)
alpha = ygets_f(argc - 4);
float beta = 0.0f;
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE];
float *matA = ygeta_f(argc - 1, &ntot, dimA);
float *matB = ygeta_f(argc - 2, &ntot, dimB);
float *matC = ypush_f(dimB);
MKL_INT m = dimB[1];
MKL_INT n = dimB[2];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimB[1];
ssymm(&side, "L", &m, &n, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
} else if (yType == Y_DOUBLE) {
double alpha = 1.0;
if (argc > 3)
alpha = ygets_d(argc - 4);
double beta = 0.0;
long ntot;
long dimA[Y_DIMSIZE],dimB[Y_DIMSIZE];
double *matA = ygeta_d(argc - 1, &ntot, dimA);
double *matB = ygeta_d(argc - 2, &ntot, dimB);
double *matC = ypush_d(dimB);
MKL_INT m = dimB[1];
MKL_INT n = dimB[2];
MKL_INT lda = dimA[1];
MKL_INT ldb = dimB[1];
MKL_INT ldc = dimB[1];
dsymm(&side, "L", &m, &n, &alpha, matA,
&lda, matB,&ldb, &beta, matC, &ldc);
}
}
}