summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/xgemm2.c
blob: 2bba00aed6b93c62e9adb911c32b8459726751a6 (plain)
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/* ************************************************************************
 * Copyright 2013 Advanced Micro Devices, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ************************************************************************/


#include <stdio.h>
#include <string.h>
#include <clBLAS.h>

#include <devinfo.h>
#include "clblas-internal.h"
#include "solution_seq.h"

//#define DEBUG_GEMM_2

int
gemmHasMTail(size_t M,  int vecLen, clblasOrder order, clblasTranspose transA, clblasTranspose transB)
{
	transB = transB;    // Dummy- to remove warning
    if (order == clblasColumnMajor)
	{
		if (transA == clblasNoTrans)
		{
			return (M % vecLen);
		} else {
			return 0;
		}
	} else {
		printf("gemmHasMTail: Not handling Row Major - FIXME\n");
		return 0;
	}
}

int
gemmHasNTail(size_t N, int vecLen, clblasOrder order, clblasTranspose transA, clblasTranspose transB)
{
	if (order == clblasColumnMajor)
	{
		if (transA == clblasNoTrans)
		{
			if (transB == clblasNoTrans)
			{
				return 0;
			} else {
				return (N % vecLen);
			}
		} else {
			if (transB == clblasNoTrans)
			{
				return 0;
			} else {
				return (N % vecLen);
			}
		}
	} else {
		printf("gemmHasNTail: Not handling Row Major - FIXME\n");
		return 0;
	}
}

int
gemmHasTails(size_t M,  size_t N, size_t K, int vecLen, clblasOrder order, clblasTranspose transA, clblasTranspose transB)
{
	K = K;  // Dummy- to remove warning
    if (order == clblasColumnMajor)
	{
		if (transA == clblasNoTrans)
		{
			if (transB == clblasNoTrans)
			{
				return (M % vecLen);
			} else {
				return ((M % vecLen) || (N % vecLen));
			}
		} else {
			if (transB == clblasNoTrans)
			{
				//
				// Vectoring on A is on K dimension and we handle tail directly in the kernel
				//
				return 0;
			} else {
				return (N % vecLen);
			}
		}
	} else {
		printf("gemmHasTails: Not handling Row Major - FIXME\n");
		return 0;
	}
}

clblasStatus executeGEMM( CLBlasKargs *kargs, cl_uint numCommandQueues, cl_command_queue *commandQueues, cl_uint numEventsInWaitList,
                             const cl_event *eventWaitList, cl_event *events)
{
    cl_int err = CL_SUCCESS;
    ListHead seq, tailSeq;
	cl_event nontail;
	cl_uint gemmVeclen;
	CLBLASKernExtra *kextra;
    size_t M, N, K;

    M = kargs->M; N = kargs->N; K = kargs->K;
    #ifdef DEBUG_GEMM_2
    printf("executeGEMM Called\n");
    #endif
    listInitHead(&seq);
    err = makeSolutionSeq(CLBLAS_GEMM2, kargs, numCommandQueues, commandQueues,
        numEventsInWaitList, eventWaitList, &nontail, &seq);
    if (err == CL_SUCCESS) {
	    ListNode *f = listNodeFirst(&seq);
		SolutionStep *gemm2;
		size_t tailStartM, tailStartN;
		bool processTails;

		gemm2 = container_of(f, node, SolutionStep);
		kextra = gemm2->kernels[CLBLAS_COMPUTING_KERNEL]->extra;
		gemmVeclen = kextra->vecLen;

		if (gemmHasTails(M, N, K, gemmVeclen, kargs->order, kargs->transA, kargs->transB) == 0)
		{
			#ifdef DEBUG_GEMM_2
			printf("No M or N Tails to process..\n");
			#endif
			processTails = false;
			gemm2->event = events;
		} else {
			processTails = true;
			if (gemmHasMTail(M, gemmVeclen, kargs->order, kargs->transA, kargs->transB))
			{
				tailStartM = M - (M%gemmVeclen);
			} else {
				tailStartM = M;
			}

			if (gemmHasNTail(N, gemmVeclen, kargs->order, kargs->transA, kargs->transB))
			{
				tailStartN = N - (N%gemmVeclen);
			} else {
				tailStartN = N;
            }
		}
        err = executeSolutionSeq(&seq);
		if ((err == CL_SUCCESS) && (processTails == true))
		{
			CLBlasKargs targs;

			memcpy(&targs, &gemm2->args, sizeof(CLBlasKargs));
			targs.tailStartM = tailStartM;
			targs.tailStartN = tailStartN;
			#ifdef DEBUG_GEMM_2
			printf("Processing Tails\n");
			#endif
    		listInitHead(&tailSeq);
    		err = makeSolutionSeq(CLBLAS_GEMM_TAIL, &targs, numCommandQueues, commandQueues,
        						  1, &nontail, events, &tailSeq);
			if (err == CL_SUCCESS)
			{
				err = executeSolutionSeq(&tailSeq);
			}
			freeSolutionSeq(&tailSeq);
		}
    }
    freeSolutionSeq(&seq);
    return (clblasStatus) err;
}

static clblasStatus
doGemm(
    CLBlasKargs *kargs,
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    const cl_mem A,
    size_t offA,
    size_t lda,
    const cl_mem B,
    size_t offB,
    size_t ldb,
    cl_mem C,
    size_t offC,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    clblasStatus err;
    clblasStatus retCode = clblasSuccess;

    if (!clblasInitialized) {
        return clblasNotInitialized;
    }

    /* Validate arguments */

    if ((retCode = checkMemObjects(A, B, C, true, A_MAT_ERRSET, B_MAT_ERRSET, C_MAT_ERRSET))) {
        return retCode;
    }
    if (K != 0) {
        if ((retCode = checkMatrixSizes(kargs->dtype, order, transA, M, K, A, offA, lda, A_MAT_ERRSET))) {
            return retCode;
        }
        if ((retCode = checkMatrixSizes(kargs->dtype, order, transB, K, N, B, offB, ldb, B_MAT_ERRSET))) {
            return retCode;
        }
    }
    if ((retCode = checkMatrixSizes(kargs->dtype, order, clblasNoTrans, M, N, C, offC, ldc, C_MAT_ERRSET))) {
            return retCode;
    }

	numCommandQueues = 1;
	#ifdef DEBUG_2
	printf("DoGemm being called...\n");
	#endif
    kargs->pigFuncID = CLBLAS_GEMM2;
    kargs->order = order;
    kargs->transA = transA;
    kargs->transB = transB;
    kargs->M = M;
    kargs->N = N;
    kargs->K = K;
    kargs->A = A;
    kargs->offA = offA;
    kargs->offa = offA;
    kargs->lda.matrix = lda;
    kargs->B = B;
    kargs->offBX = offB;
    kargs->ldb.matrix = ldb;
    kargs->C = C;
    kargs->offCY = offC;
    kargs->ldc.matrix = ldc;

    kargs->offsetM = 0;
    kargs->offsetN = 0;
    kargs->scimage[0] = 0;
    kargs->scimage[1] = 0;

    err = executeGEMM(kargs, numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events);
    return err;
			}

/*
clblasStatus
clblasSgemmV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    cl_float alpha,
    const cl_mem A,
    size_t lda,
    const cl_mem B,
    size_t ldb,
    cl_float beta,
    cl_mem C,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_FLOAT;
    kargs.alpha.argFloat = alpha;
    kargs.beta.argFloat = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, 0, lda, B, 0, ldb,
                  C, 0, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasDgemmV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    cl_double alpha,
    const cl_mem A,
    size_t lda,
    const cl_mem B,
    size_t ldb,
    cl_double beta,
    cl_mem C,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_DOUBLE;
    kargs.alpha.argDouble = alpha;
    kargs.beta.argDouble = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, 0, lda, B, 0, ldb,
                  C, 0, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasCgemmV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    FloatComplex alpha,
    const cl_mem A,
    size_t lda,
    const cl_mem B,
    size_t ldb,
    FloatComplex beta,
    cl_mem C,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_COMPLEX_FLOAT;
    kargs.alpha.argFloatComplex = alpha;
    kargs.beta.argFloatComplex = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, 0, lda, B, 0, ldb,
                  C, 0, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasZgemmV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    DoubleComplex alpha,
    const cl_mem A,
    size_t lda,
    const cl_mem B,
    size_t ldb,
    DoubleComplex beta,
    cl_mem C,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_COMPLEX_DOUBLE;
    kargs.alpha.argDoubleComplex = alpha;
    kargs.beta.argDoubleComplex = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, 0, lda, B, 0, ldb,
                  C, 0, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasSgemmExV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    cl_float alpha,
    const cl_mem A,
	size_t offA,
    size_t lda,
    const cl_mem B,
	size_t offB,
    size_t ldb,
    cl_float beta,
    cl_mem C,
	size_t offC,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_FLOAT;
    kargs.alpha.argFloat = alpha;
    kargs.beta.argFloat = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, offA, lda, B, offB, ldb,
                  C, offC, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasDgemmExV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    cl_double alpha,
    const cl_mem A,
	size_t offA,
    size_t lda,
    const cl_mem B,
	size_t offB,
    size_t ldb,
    cl_double beta,
    cl_mem C,
	size_t offC,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_DOUBLE;
    kargs.alpha.argDouble = alpha;
    kargs.beta.argDouble = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, offA, lda, B, offB, ldb,
                  C, offC, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasCgemmExV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    FloatComplex alpha,
    const cl_mem A,
	size_t offA,
    size_t lda,
    const cl_mem B,
	size_t offB,
    size_t ldb,
    FloatComplex beta,
    cl_mem C,
	size_t offC,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_COMPLEX_FLOAT;
    kargs.alpha.argFloatComplex = alpha;
    kargs.beta.argFloatComplex = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, offA, lda, B, offB, ldb,
                  C, offC, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}

clblasStatus
clblasZgemmExV2(
    clblasOrder order,
    clblasTranspose transA,
    clblasTranspose transB,
    size_t M,
    size_t N,
    size_t K,
    DoubleComplex alpha,
    const cl_mem A,
	size_t offA,
    size_t lda,
    const cl_mem B,
	size_t offB,
    size_t ldb,
    DoubleComplex beta,
    cl_mem C,
	size_t offC,
    size_t ldc,
    cl_uint numCommandQueues,
    cl_command_queue *commandQueues,
    cl_uint numEventsInWaitList,
    const cl_event *eventWaitList,
    cl_event *events)
{
    CLBlasKargs kargs;

    memset(&kargs, 0, sizeof(kargs));
    kargs.dtype = TYPE_COMPLEX_DOUBLE;
    kargs.alpha.argDoubleComplex = alpha;
    kargs.beta.argDoubleComplex = beta;

    return doGemm(&kargs, order, transA, transB, M, N, K, A, offA, lda, B, offB, ldb,
                  C, offC, ldc, numCommandQueues, commandQueues,
                  numEventsInWaitList, eventWaitList, events);
}
*/