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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/*********************************************************************/
/* Copyright 2009, 2010 The University of Texas at Austin. */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* 1. Redistributions of source code must retain the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above */
/* copyright notice, this list of conditions and the following */
/* disclaimer in the documentation and/or other materials */
/* provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
/* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
/* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
/* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
/* POSSIBILITY OF SUCH DAMAGE. */
/* */
/* The views and conclusions contained in the software and */
/* documentation are those of the authors and should not be */
/* interpreted as representing official policies, either expressed */
/* or implied, of The University of Texas at Austin. */
/*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#ifdef FUNCTION_PROFILE
#include "functable.h"
#endif
#ifndef COMPLEX
#ifdef XDOUBLE
#define ERROR_NAME "QGEMM "
#elif defined(DOUBLE)
#define ERROR_NAME "DGEMM "
#else
#define ERROR_NAME "SGEMM "
#endif
#else
#ifndef GEMM3M
#ifdef XDOUBLE
#define ERROR_NAME "XGEMM "
#elif defined(DOUBLE)
#define ERROR_NAME "ZGEMM "
#else
#define ERROR_NAME "CGEMM "
#endif
#else
#ifdef XDOUBLE
#define ERROR_NAME "XGEMM3M "
#elif defined(DOUBLE)
#define ERROR_NAME "ZGEMM3M "
#else
#define ERROR_NAME "CGEMM3M "
#endif
#endif
#endif
#ifndef GEMM_MULTITHREAD_THRESHOLD
#define GEMM_MULTITHREAD_THRESHOLD 4
#endif
static int (*gemm[])(blas_arg_t *, BLASLONG *, BLASLONG *, FLOAT *, FLOAT *, BLASLONG) = {
#ifndef GEMM3M
GEMM_NN, GEMM_TN, GEMM_RN, GEMM_CN,
GEMM_NT, GEMM_TT, GEMM_RT, GEMM_CT,
GEMM_NR, GEMM_TR, GEMM_RR, GEMM_CR,
GEMM_NC, GEMM_TC, GEMM_RC, GEMM_CC,
#if defined(SMP) && !defined(USE_SIMPLE_THREADED_LEVEL3)
GEMM_THREAD_NN, GEMM_THREAD_TN, GEMM_THREAD_RN, GEMM_THREAD_CN,
GEMM_THREAD_NT, GEMM_THREAD_TT, GEMM_THREAD_RT, GEMM_THREAD_CT,
GEMM_THREAD_NR, GEMM_THREAD_TR, GEMM_THREAD_RR, GEMM_THREAD_CR,
GEMM_THREAD_NC, GEMM_THREAD_TC, GEMM_THREAD_RC, GEMM_THREAD_CC,
#endif
#else
GEMM3M_NN, GEMM3M_TN, GEMM3M_RN, GEMM3M_CN,
GEMM3M_NT, GEMM3M_TT, GEMM3M_RT, GEMM3M_CT,
GEMM3M_NR, GEMM3M_TR, GEMM3M_RR, GEMM3M_CR,
GEMM3M_NC, GEMM3M_TC, GEMM3M_RC, GEMM3M_CC,
#if defined(SMP) && !defined(USE_SIMPLE_THREADED_LEVEL3)
GEMM3M_THREAD_NN, GEMM3M_THREAD_TN, GEMM3M_THREAD_RN, GEMM3M_THREAD_CN,
GEMM3M_THREAD_NT, GEMM3M_THREAD_TT, GEMM3M_THREAD_RT, GEMM3M_THREAD_CT,
GEMM3M_THREAD_NR, GEMM3M_THREAD_TR, GEMM3M_THREAD_RR, GEMM3M_THREAD_CR,
GEMM3M_THREAD_NC, GEMM3M_THREAD_TC, GEMM3M_THREAD_RC, GEMM3M_THREAD_CC,
#endif
#endif
};
#ifndef CBLAS
void NAME(char *TRANSA, char *TRANSB,
blasint *M, blasint *N, blasint *K,
FLOAT *alpha,
FLOAT *a, blasint *ldA,
FLOAT *b, blasint *ldB,
FLOAT *beta,
FLOAT *c, blasint *ldC){
blas_arg_t args;
int transa, transb, nrowa, nrowb;
blasint info;
char transA, transB;
FLOAT *buffer;
FLOAT *sa, *sb;
#ifdef SMP
#ifndef COMPLEX
#ifdef XDOUBLE
int mode = BLAS_XDOUBLE | BLAS_REAL;
#elif defined(DOUBLE)
int mode = BLAS_DOUBLE | BLAS_REAL;
#else
int mode = BLAS_SINGLE | BLAS_REAL;
#endif
#else
#ifdef XDOUBLE
int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
#elif defined(DOUBLE)
int mode = BLAS_DOUBLE | BLAS_COMPLEX;
#else
int mode = BLAS_SINGLE | BLAS_COMPLEX;
#endif
#endif
#endif
#if defined(SMP) && !defined(NO_AFFINITY) && !defined(USE_SIMPLE_THREADED_LEVEL3)
int nodes;
#endif
PRINT_DEBUG_NAME;
args.m = *M;
args.n = *N;
args.k = *K;
args.a = (void *)a;
args.b = (void *)b;
args.c = (void *)c;
args.lda = *ldA;
args.ldb = *ldB;
args.ldc = *ldC;
args.alpha = (void *)alpha;
args.beta = (void *)beta;
transA = *TRANSA;
transB = *TRANSB;
TOUPPER(transA);
TOUPPER(transB);
transa = -1;
transb = -1;
if (transA == 'N') transa = 0;
if (transA == 'T') transa = 1;
#ifndef COMPLEX
if (transA == 'R') transa = 0;
if (transA == 'C') transa = 1;
#else
if (transA == 'R') transa = 2;
if (transA == 'C') transa = 3;
#endif
if (transB == 'N') transb = 0;
if (transB == 'T') transb = 1;
#ifndef COMPLEX
if (transB == 'R') transb = 0;
if (transB == 'C') transb = 1;
#else
if (transB == 'R') transb = 2;
if (transB == 'C') transb = 3;
#endif
nrowa = args.m;
if (transa & 1) nrowa = args.k;
nrowb = args.k;
if (transb & 1) nrowb = args.n;
info = 0;
if (args.ldc < args.m) info = 13;
if (args.ldb < nrowb) info = 10;
if (args.lda < nrowa) info = 8;
if (args.k < 0) info = 5;
if (args.n < 0) info = 4;
if (args.m < 0) info = 3;
if (transb < 0) info = 2;
if (transa < 0) info = 1;
if (info){
BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
return;
}
#else
void CNAME(enum CBLAS_ORDER order, enum CBLAS_TRANSPOSE TransA, enum CBLAS_TRANSPOSE TransB,
blasint m, blasint n, blasint k,
#ifndef COMPLEX
FLOAT alpha,
#else
FLOAT *alpha,
#endif
FLOAT *a, blasint lda,
FLOAT *b, blasint ldb,
#ifndef COMPLEX
FLOAT beta,
#else
FLOAT *beta,
#endif
FLOAT *c, blasint ldc) {
blas_arg_t args;
int transa, transb;
blasint nrowa, nrowb, info;
XFLOAT *buffer;
XFLOAT *sa, *sb;
#ifdef SMP
#ifndef COMPLEX
#ifdef XDOUBLE
int mode = BLAS_XDOUBLE | BLAS_REAL;
#elif defined(DOUBLE)
int mode = BLAS_DOUBLE | BLAS_REAL;
#else
int mode = BLAS_SINGLE | BLAS_REAL;
#endif
#else
#ifdef XDOUBLE
int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
#elif defined(DOUBLE)
int mode = BLAS_DOUBLE | BLAS_COMPLEX;
#else
int mode = BLAS_SINGLE | BLAS_COMPLEX;
#endif
#endif
#endif
#if defined(SMP) && !defined(NO_AFFINITY) && !defined(USE_SIMPLE_THREADED_LEVEL3)
int nodes;
#endif
PRINT_DEBUG_CNAME;
#ifndef COMPLEX
args.alpha = (void *)α
args.beta = (void *)β
#else
args.alpha = (void *)alpha;
args.beta = (void *)beta;
#endif
transa = -1;
transb = -1;
info = 0;
if (order == CblasColMajor) {
args.m = m;
args.n = n;
args.k = k;
args.a = (void *)a;
args.b = (void *)b;
args.c = (void *)c;
args.lda = lda;
args.ldb = ldb;
args.ldc = ldc;
if (TransA == CblasNoTrans) transa = 0;
if (TransA == CblasTrans) transa = 1;
#ifndef COMPLEX
if (TransA == CblasConjNoTrans) transa = 0;
if (TransA == CblasConjTrans) transa = 1;
#else
if (TransA == CblasConjNoTrans) transa = 2;
if (TransA == CblasConjTrans) transa = 3;
#endif
if (TransB == CblasNoTrans) transb = 0;
if (TransB == CblasTrans) transb = 1;
#ifndef COMPLEX
if (TransB == CblasConjNoTrans) transb = 0;
if (TransB == CblasConjTrans) transb = 1;
#else
if (TransB == CblasConjNoTrans) transb = 2;
if (TransB == CblasConjTrans) transb = 3;
#endif
nrowa = args.m;
if (transa & 1) nrowa = args.k;
nrowb = args.k;
if (transb & 1) nrowb = args.n;
info = -1;
if (args.ldc < args.m) info = 13;
if (args.ldb < nrowb) info = 10;
if (args.lda < nrowa) info = 8;
if (args.k < 0) info = 5;
if (args.n < 0) info = 4;
if (args.m < 0) info = 3;
if (transb < 0) info = 2;
if (transa < 0) info = 1;
}
if (order == CblasRowMajor) {
args.m = n;
args.n = m;
args.k = k;
args.a = (void *)b;
args.b = (void *)a;
args.c = (void *)c;
args.lda = ldb;
args.ldb = lda;
args.ldc = ldc;
if (TransB == CblasNoTrans) transa = 0;
if (TransB == CblasTrans) transa = 1;
#ifndef COMPLEX
if (TransB == CblasConjNoTrans) transa = 0;
if (TransB == CblasConjTrans) transa = 1;
#else
if (TransB == CblasConjNoTrans) transa = 2;
if (TransB == CblasConjTrans) transa = 3;
#endif
if (TransA == CblasNoTrans) transb = 0;
if (TransA == CblasTrans) transb = 1;
#ifndef COMPLEX
if (TransA == CblasConjNoTrans) transb = 0;
if (TransA == CblasConjTrans) transb = 1;
#else
if (TransA == CblasConjNoTrans) transb = 2;
if (TransA == CblasConjTrans) transb = 3;
#endif
nrowa = args.m;
if (transa & 1) nrowa = args.k;
nrowb = args.k;
if (transb & 1) nrowb = args.n;
info = -1;
if (args.ldc < args.m) info = 13;
if (args.ldb < nrowb) info = 10;
if (args.lda < nrowa) info = 8;
if (args.k < 0) info = 5;
if (args.n < 0) info = 4;
if (args.m < 0) info = 3;
if (transb < 0) info = 2;
if (transa < 0) info = 1;
}
if (info >= 0) {
BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
return;
}
#endif
if ((args.m == 0) || (args.n == 0)) return;
#if 0
fprintf(stderr, "m = %4d n = %d k = %d lda = %4d ldb = %4d ldc = %4d\n",
args.m, args.n, args.k, args.lda, args.ldb, args.ldc);
#endif
IDEBUG_START;
FUNCTION_PROFILE_START();
buffer = (XFLOAT *)blas_memory_alloc(0);
sa = (XFLOAT *)((BLASLONG)buffer +GEMM_OFFSET_A);
sb = (XFLOAT *)(((BLASLONG)sa + ((GEMM_P * GEMM_Q * COMPSIZE * SIZE + GEMM_ALIGN) & ~GEMM_ALIGN)) + GEMM_OFFSET_B);
#ifdef SMP
mode |= (transa << BLAS_TRANSA_SHIFT);
mode |= (transb << BLAS_TRANSB_SHIFT);
int nthreads_max = num_cpu_avail(3);
int nthreads_avail = nthreads_max;
#ifndef COMPLEX
double MNK = (double) args.m * (double) args.n * (double) args.k;
if ( MNK <= (1024.0 * (double) GEMM_MULTITHREAD_THRESHOLD) )
nthreads_max = 1;
else
{
if ( MNK <= (65536.0 * (double) GEMM_MULTITHREAD_THRESHOLD) )
{
nthreads_max = 4;
if ( args.m < 16 * GEMM_MULTITHREAD_THRESHOLD )
{
nthreads_max = 2;
if ( args.m < 3 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
if ( args.n < 1 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
if ( args.k < 3 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
}
else
{
if ( args.n <= 1 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 2;
}
}
}
#else
double MNK = (double) args.m * (double) args.n * (double) args.k;
if ( MNK <= (256.0 * (double) GEMM_MULTITHREAD_THRESHOLD) )
nthreads_max = 1;
else
{
if ( MNK <= (16384.0 * (double) GEMM_MULTITHREAD_THRESHOLD) )
{
nthreads_max = 4;
if ( args.m < 3 * GEMM_MULTITHREAD_THRESHOLD )
{
nthreads_max = 2;
if ( args.m <= 1 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
if ( args.n < 1 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
if ( args.k < 1 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 1;
}
else
{
if ( args.n < 2 * GEMM_MULTITHREAD_THRESHOLD ) nthreads_max = 2;
}
}
}
#endif
args.common = NULL;
if ( nthreads_max > nthreads_avail )
args.nthreads = nthreads_avail;
else
args.nthreads = nthreads_max;
if (args.nthreads == 1) {
#endif
(gemm[(transb << 2) | transa])(&args, NULL, NULL, sa, sb, 0);
#ifdef SMP
} else {
#ifndef USE_SIMPLE_THREADED_LEVEL3
#ifndef NO_AFFINITY
nodes = get_num_nodes();
if ((nodes > 1) && get_node_equal()) {
args.nthreads /= nodes;
gemm_thread_mn(mode, &args, NULL, NULL, gemm[16 | (transb << 2) | transa], sa, sb, nodes);
} else {
#endif
(gemm[16 | (transb << 2) | transa])(&args, NULL, NULL, sa, sb, 0);
#else
GEMM_THREAD(mode, &args, NULL, NULL, gemm[(transb << 2) | transa], sa, sb, args.nthreads);
#endif
#ifndef USE_SIMPLE_THREADED_LEVEL3
#ifndef NO_AFFINITY
}
#endif
#endif
#endif
#ifdef SMP
}
#endif
blas_memory_free(buffer);
FUNCTION_PROFILE_END(COMPSIZE * COMPSIZE, args.m * args.k + args.k * args.n + args.m * args.n, 2 * args.m * args.n * args.k);
IDEBUG_END;
return;
}
|