summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/generic/common.c
blob: 477a01bb185dbb22d276a009d8b0cd8da269da2c (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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/* ************************************************************************
 * 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 <string.h>
#include <stdlib.h>

#include <clBLAS.h>
#include <clkern.h>
#include <cltypes.h>
#include <stdio.h>
#include <ctype.h>

#include "clblas-internal.h"

#if defined(DUMP_CLBLAS_KERNELS) && !defined(KEEP_CLBLAS_KERNEL_SOURCES)
#define KEEP_CLBLAS_KERNEL_SOURCES
#endif

int clblasInitialized = 0;
CLBlasSolvers clblasSolvers[BLAS_FUNCTIONS_NUMBER];
struct KernelCache *clblasKernelCache = NULL;

enum {
    BUILD_LOG_SIZE = 65536
};

static __inline void
storeErrorCode(cl_int *error, cl_int code)
{
    if (error != NULL) {
        *error = code;
    }
}

#ifndef PRINT_BUILD_ERRORS
    #define PRINT_BUILD_ERRORS
#endif

#ifdef PRINT_BUILD_ERRORS

static char
*allocBuildLog(void)
{
	char *log;

    log = malloc(BUILD_LOG_SIZE);
	if (log) {
		log[0] = '\0';
	}

	return log;
}

static void
freeBuildLog(char *buildLog)
{
    free(buildLog);
}

static void
printBuildError(
    cl_int error,
    cl_device_id device,
    SolverKgen kgen,
    const SubproblemDim *dims,
    const PGranularity *pgran,
    const CLBLASKernExtra *kextra,
    const char *source,
    const char *buildLog)
{
    char name[128];
    char dimStr[1024];
    char pgranStr[1024];
    char *p;
    MemoryPattern *mempat = NULL;
    unsigned int i, j;
    const char *s;

    name[0] = '\0';
    clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(name), name, NULL);

    // lookup memory pattern
    s = NULL;
    for (i = 0; i < BLAS_FUNCTIONS_NUMBER; i++) {
        for (j = 0; j < clblasSolvers[i].nrPatterns; j++) {
            mempat = &clblasSolvers[i].memPatterns[j];
            if (kgen == mempat->sops->genKernel) {
                s = kernelTypeString(kextra->kernType);
                break;
            }
        }
        if (s != NULL) {
            break;
        }
    }

    // sprintf Subproblem dimensions
    p = dimStr;
    for (i = 0; i < mempat->nrLevels; i++) {
        p = sprintfGranulation(p, dims, i);
        strcat(p, "; ");
        p += strlen(p);
    }

    // sprintf data parallelism granularity
    sprintf(pgranStr, "pgran->wgDim = %d, pgran->wgSize[0] = %u, "
            "pgran->wgSize[1] = %u, pgran->wfSize = %u",
            pgran->wgDim, pgran->wgSize[0], pgran->wgSize[1],
            pgran->wfSize);

    fprintf(stderr, "\n========================================================\n\n");
    fprintf(stderr, "AN INTERNAL KERNEL BUILD ERROR OCCURRED!\n");
    fprintf(stderr, "device name = %s\n", name);
    fprintf(stderr, "error = %d\n", error);
    fprintf(stderr, "memory pattern = %s, %s kernel generator\n", mempat->name, s);
    fprintf(stderr, "Subproblem dimensions: %s\n", dimStr);
    fprintf(stderr, "Parallelism granularity: %s\n", pgranStr);
    fprintf(stderr, "Kernel extra flags: %u\n", kextra->flags);
    fprintf(stderr, "Source:\n\n%s\n\n", source);
    fprintf(stderr, "--------------------------------------------------------\n\n");
    if (buildLog) {
        fprintf(stderr, "Build log:\n\n%s\n", buildLog);
    }
    else {
        fprintf(stderr, "Build log is unavailable\n");
    }
    fprintf(stderr, "========================================================\n\n");
}

#else               /* PRINT_BUILD_ERRORS */

static __inline char*
allocBuildLog(void)
{
    /* stub, do nothing */
    return NULL;
}

#define freeBuildLog(log)                       /* stub, do nothing */
#define printBuildError(error, device, kgen, \
    dims, pgran, kextra, source, buildLog)      /* stub, do nothing */

#endif              /* !PRINT_BUILD_ERRORS */

static void
extraDtor(struct Kernel *kernel)
{
    if (kernel->extra != NULL) {
        free(kernel->extra);
        kernel->extra = NULL;
    }
}

static char
*sprintfDim(
    char *buf,
    size_t dim,
    const char *dimName,
    int level,
    bool first)
{
    if (!first) {
        strcat(buf, ", ");
        buf += strlen(buf);
    }
    if (dim == SUBDIM_UNUSED) {
        sprintf(buf, "dims[%d].%s = SUBDIM_UNUSED", level, dimName);
    }
    else {
        sprintf(buf, "dims[%d].%s = %lu", level, dimName, dim);
    }

    buf += strlen(buf);

    return buf;
}

const char VISIBILITY_HIDDEN
*kernelTypeString(CLBlasKernelType ktype)
{
    switch (ktype) {
    case CLBLAS_COMPUTING_KERNEL:
        return "computing";
    case CLBLAS_PREP_A_KERNEL:
        return "preparative for matrix A";
    case CLBLAS_PREP_B_KERNEL:
        return "preparative for matrix B";
    default:
        return NULL;
    }
}

/*
 * Assign a scalar multiplied on a matrix a kernel argument
 */
void VISIBILITY_HIDDEN
assignScalarKarg(KernelArg *arg, const void *value, DataType dtype)
{
    arg->typeSize = dtypeSize(dtype);
    memcpy(arg->arg.data, value, arg->typeSize);
}

void VISIBILITY_HIDDEN
calcGlobalThreads(
    size_t globalThreads[2],
    const SubproblemDim *wgDim,
    const PGranularity *pgran,
    size_t M,
    size_t N)
{
    globalThreads[1] = 1;

    if ((wgDim->itemX != SUBDIM_UNUSED) &&
        (wgDim->itemY != SUBDIM_UNUSED)) {

        size_t groupWorkX, groupWorkY;
        size_t nrGroupsX, nrGroupsY;
        int nrDims;

        groupWorkX = wgDim->itemX;
        groupWorkY = wgDim->itemY;

        nrGroupsX = N / groupWorkX;
        if (N % groupWorkX) {
            nrGroupsX++;
        }

        nrGroupsY = M / groupWorkY;
        if (M % groupWorkY) {
            nrGroupsY++;
        }

        nrDims = (pgran == NULL) ? 1 : pgran->wgDim;
        if (nrDims == 1) {
            globalThreads[0] = nrGroupsX * nrGroupsY;
        }
        else {
            globalThreads[0] = nrGroupsY;
            globalThreads[1] = nrGroupsX;
        }
    }
    else {
        size_t totalWork, groupWork;

        if (wgDim->itemX != SUBDIM_UNUSED) {
            totalWork = N;
            groupWork = wgDim->itemX;
        }
        else {
            totalWork = M;
            groupWork = wgDim->itemY;
        }

        globalThreads[0] = totalWork / groupWork;
        if (totalWork % groupWork) {
            globalThreads[0]++;
        }
    }

    if (pgran != NULL) {
        globalThreads[0] *= pgran->wgSize[0];
        globalThreads[1] *= pgran->wgSize[1];
    }
}

cl_int VISIBILITY_HIDDEN
getKernelContext(cl_kernel kernel, cl_context *context)
{
    cl_int err;
    cl_context ctx;

    err = clGetKernelInfo(kernel, CL_KERNEL_CONTEXT,
        sizeof(cl_context), &ctx, NULL);
    if (err != CL_SUCCESS)
        return err;
    if (context != NULL)
        *context = ctx;
    return err;
}

cl_int VISIBILITY_HIDDEN
getQueueContext(cl_command_queue queue, cl_context *context)
{
    cl_int err;
    cl_context ctx;

    err = clGetCommandQueueInfo(queue, CL_QUEUE_CONTEXT,
        sizeof(cl_context), &ctx, NULL);
    if (err != CL_SUCCESS)
        return err;
    if (context != NULL)
        *context = ctx;
    return err;
}

cl_int VISIBILITY_HIDDEN
getQueueDevice(cl_command_queue queue, cl_device_id *device)
{
    cl_int err;
    cl_device_id dev;

    err = clGetCommandQueueInfo(queue, CL_QUEUE_DEVICE,
        sizeof(cl_device_id), &dev, NULL);
    if (err != CL_SUCCESS)
        return err;
    if (device != NULL)
        *device = dev;
    return err;
}

cl_int VISIBILITY_HIDDEN
getQueueProperties(
    cl_command_queue queue,
    cl_command_queue_properties *props)
{
    cl_int err;
    cl_command_queue_properties p;

    err = clGetCommandQueueInfo(queue, CL_QUEUE_PROPERTIES,
        sizeof(cl_command_queue_properties), &p, NULL);
    if (err != CL_SUCCESS)
        return err;
    if (props != NULL)
        *props = p;
    return err;
}

Kernel VISIBILITY_HIDDEN
*loadKernel( const unsigned char** buffer,
             size_t sizeBuffer,
             KernelKey *key,
             const CLBLASKernExtra *extra,
             cl_int *error)

{
    cl_int status = CL_SUCCESS;
    Kernel* kernel;

    kernel = allocKernel();
    if (kernel == NULL) {
        return NULL;
    }

    kernel->program = createClProgramWithBinary(key->context,
                                                key->device,
                                                (unsigned char*)*buffer,
                                                sizeBuffer,
                                                &status);
    if (status == CL_SUCCESS) {
        kernel->extraSize = sizeof(CLBLASKernExtra);
        kernel->extra = calloc(1, kernel->extraSize);
        *(CLBLASKernExtra*)(kernel->extra) = *extra;
        kernel->dtor = extraDtor;
        kernel->noSource = 1;
    }
    else {
        putKernel(NULL, kernel);
        storeErrorCode(error, status);
        kernel = NULL;
    }

    return kernel;
}

#if !defined(DUMP_CLBLAS_KERNELS)

/*
 * Drop the program's source so as to consume memory as few as possible
 * at caching
 */
static cl_int
dropProgramSource(cl_program *program, cl_context ctx, cl_device_id devID)
{
    size_t size;
    unsigned char *bin;
    cl_program p = *program;
    cl_int err;

    size = getProgramBinarySize(p);
    bin = getProgramBinary(p);

    /*
     * Don't release the original program until a new one is created
     * in order to retain its own reference to the context if it is
     * released by user
     */
    p = createClProgramWithBinary(ctx, devID, bin, size, &err);
    if (err == CL_SUCCESS) {
        clReleaseProgram(*program);
        *program = p;
    }

    free(bin);

    return err;
}

#endif /* !DUMP_CLBLAS_KERNELS */

Kernel
*makeKernel(
    cl_device_id device,
    cl_context context,
    SolverKgen kernelGenerator,
    const SubproblemDim *dims,
    const PGranularity *pgran,
    const CLBLASKernExtra *extra,
    const char *buildOpts,
    cl_int *error)
{

    cl_int err;
    char *source;
    ssize_t size;
    Kernel *kernel;
    char *log;

	#ifdef DEBUG_2
	printf("Make kernel called\n");
	printf("x : %d, y : %d, itemX: %d, itemY: %d\n",  dims->x, dims->y, dims->itemX, dims->itemY);
	printf("PG : wgSize[0] : %d, wgSize[1] : %d, wfSize: %d\n",  pgran->wgSize[0], pgran->wgSize[1], pgran->wfSize);
	#endif

    size = kernelGenerator(NULL, 0, dims, pgran, (void*)extra);
    if (size < 0) {
        storeErrorCode(error, CL_OUT_OF_HOST_MEMORY);
        return NULL;
    }
    source = calloc(1, size);
    if (source == NULL) {
        storeErrorCode(error, CL_OUT_OF_HOST_MEMORY);
        return NULL;
    }
    if (kernelGenerator(source, size, dims, pgran, (void*)extra) != size) {
        free(source);
        storeErrorCode(error, CL_OUT_OF_HOST_MEMORY);
        return NULL;
    }

	kernel = allocKernel();
    if (kernel == NULL) {
        free(source);
        storeErrorCode(error, CL_OUT_OF_HOST_MEMORY);
        return NULL;
    }

    log = allocBuildLog();

	//#define DEBUG_2
	#ifdef DEBUG_2
	printf("Build Options used %s \n", buildOpts);
	printf("Source kernel used %s \n", source);
	#endif
	#undef DEBUG_2

    kernel->program = buildClProgram(source, buildOpts, context, device,
                                     log, BUILD_LOG_SIZE, &err);
    if (err != CL_SUCCESS) {
        printBuildError(err, device, kernelGenerator, dims,
                        pgran, extra, source, log);
        freeBuildLog(log);
        putKernel(NULL, kernel);
        free(source);
        storeErrorCode(error, err);
        return NULL;
    }
	else
	{
		// #define DEBUG_2
		#ifdef DEBUG_2
		printf("Kernel compilation succeeded\n");
		#endif
		#undef DEBUG_2
	}

    freeBuildLog(log);
    free(source);

#if !defined(KEEP_CLBLAS_KERNEL_SOURCES)
    if (err == CL_SUCCESS) {
        err = dropProgramSource(&kernel->program, context, device);
        kernel->noSource = 1;
    }
#endif  /* !DUMP_CLBLAS_KERNELS */

    if (err != CL_SUCCESS) {
        putKernel(NULL, kernel);
        storeErrorCode(error, err);
        return NULL;
    }

    kernel->extraSize = sizeof(CLBLASKernExtra);
    kernel->extra = calloc(1, kernel->extraSize);
    *(CLBLASKernExtra*)(kernel->extra) = *extra;
    kernel->dtor = extraDtor;

    storeErrorCode(error, CL_SUCCESS);

    return kernel;

}

void
setupBuildOpts(
    char opts[BUILD_OPTS_MAXLEN],
    cl_device_id devID,
    MemoryPattern *mempat)
{
    TargetDevice target;

    target.id = devID;
    identifyDevice(&target);
    opts[0] = '\0';

#if !defined NDEBUG
    // Nvidia runtime does not appear to support the -g flag, at least in their OpenCL v1.1 runtime
    if( target.ident.vendor != VENDOR_NVIDIA )
        addBuildOpt( opts, BUILD_OPTS_MAXLEN, "-g" );
#endif  /* NDEBUG */

    if (target.ident.vendor == VENDOR_NVIDIA &&
        !strcmp(mempat->name, "2-staged cached global memory based "
                              "block trsm")) {

        addBuildOpt(opts, BUILD_OPTS_MAXLEN, "-cl-opt-disable");
    }
}

void addBuildOpt(
    char * opts,
    size_t len,
    const char * option)
{
    size_t l = strlen(opts);

    if (l > 0 && !isspace(opts[l-1]) && l+1 < len) {
      opts[l] = ' ';
      opts[l+1]   = '\0';
      l++;
    }

    strncat(opts, option, len - l - 1);
}


char VISIBILITY_HIDDEN
*sprintfGranulation(char *buf, const SubproblemDim *dim, int level)
{
    buf = sprintfDim(buf, dim[level].itemY, "itemY", level, true);
    buf = sprintfDim(buf, dim[level].itemX, "itemX", level, false);
    buf = sprintfDim(buf, dim[level].y, "y", level, false);
    buf = sprintfDim(buf, dim[level].x, "x", level, false);
    buf = sprintfDim(buf, dim[level].bwidth, "bwidth", level, false);
    strcat(buf, "; ");
    buf += strlen(buf);

    return buf;
}

clblasStatus VISIBILITY_HIDDEN
checkMatrixSizes(
    DataType dtype,
    clblasOrder order,
    clblasTranspose transA,
    size_t M,
    size_t N,
    cl_mem A,
    size_t offA,
    size_t lda,         // lda is passed as zero for packed matrices
    ErrorCodeSet err )
{
    size_t memSize, matrSize, tsize, memUsed;
    size_t unusedTail = 0;
    bool tra;

    if ((M == 0) || (N == 0)) {
        return clblasInvalidDim;
    }

    tsize = dtypeSize(dtype);
    tra = (order == clblasRowMajor && transA != clblasNoTrans) ||
          (order == clblasColumnMajor && transA == clblasNoTrans);

    if( lda > 0 )              // For Non-packed matrices
    {
        if (tra) {
            if (lda < M) {
                switch( err )
                {
                case A_MAT_ERRSET:
                    return clblasInvalidLeadDimA;
                case B_MAT_ERRSET:
                    return clblasInvalidLeadDimB;
                case C_MAT_ERRSET:
                    return clblasInvalidLeadDimC;
                default:
                    return clblasNotImplemented;
                }
            }
            matrSize = ((N - 1) * lda + M) * tsize;
            unusedTail = ( lda - N ) * tsize;
        }
        else {
            if (lda < N) {
                switch( err )
                {
                case A_MAT_ERRSET:
                    return clblasInvalidLeadDimA;
                case B_MAT_ERRSET:
                    return clblasInvalidLeadDimB;
                case C_MAT_ERRSET:
                    return clblasInvalidLeadDimC;
                default:
                    return clblasNotImplemented;
                }
            }
            matrSize = ((M - 1) * lda + N) * tsize;
            unusedTail = ( lda - M ) * tsize;
        }
    }
    else {                     // For the case of packed matrices
         matrSize = ((M * (N+1)) / 2) * tsize;
    }

    offA *= tsize;

    if (clGetMemObjectInfo(A, CL_MEM_SIZE, sizeof(memSize), &memSize, NULL) !=
                                CL_SUCCESS) {
        switch( err )
        {
        case A_MAT_ERRSET:
            return clblasInvalidMatA;
        case B_MAT_ERRSET:
            return clblasInvalidMatB;
        case C_MAT_ERRSET:
            return clblasInvalidMatC;
        default:
            return clblasNotImplemented;
        }
    }

    //  It is possible to allocate a buffer, and set up lda & ldb such that it looks like it will access outside of the allocated buffer, but if
    //  M & N are kept small enough, no out of bounds access will occur.  Compensate for the offset values and the unused tail memory caused by lda & ldb.
    //  Ex: BuffSize=6 floats, M=1, N=2, lda=ldb=3, offA = 0, offB = 2 :  |A[0,0]|unused|B[0,0]|A[0,1]|unused|B[0,1]|
    memUsed = (( offA + matrSize ) > unusedTail) ? offA + matrSize - unusedTail: 0;

    // Note: this is a hack to get the xsymm tests to work.
    // TODO: Find out why "memUsed" is set to 0 in some cases!
    memUsed = offA + matrSize;
    //printf("%lu required but found %lu\n", memUsed/tsize, memSize/tsize);

    if (( memUsed > memSize ) || (offA + matrSize < offA)) {
        switch( err )
        {
        case A_MAT_ERRSET:
            return clblasInsufficientMemMatA;
        case B_MAT_ERRSET:
            return clblasInsufficientMemMatB;
        case C_MAT_ERRSET:
            return clblasInsufficientMemMatC;
        default:
            return clblasNotImplemented;
        }
    }

    return clblasSuccess;
}


clblasStatus VISIBILITY_HIDDEN
checkBandedMatrixSizes(
    DataType dtype,
    clblasOrder order,
    clblasTranspose transA,
    size_t M,
    size_t N,
    size_t KL,
    size_t KU,
    cl_mem A,
    size_t offA,
    size_t lda,
    ErrorCodeSet err )
{
    size_t memSize, matrSize, tsize, K, memUsed;
    size_t unusedTail = 0;
    bool tra;

    if ((M == 0) || (N == 0)) {
        return clblasInvalidDim;
    }

    tsize = dtypeSize(dtype);
    K = KL + KU + 1;
    tra = (order == clblasRowMajor && transA != clblasNoTrans) ||
          (order == clblasColumnMajor && transA == clblasNoTrans);

    if (lda < K) {
        switch( err )
        {
        case A_MAT_ERRSET:
            return clblasInvalidLeadDimA;
        case B_MAT_ERRSET:
            return clblasInvalidLeadDimB;
        case C_MAT_ERRSET:
            return clblasInvalidLeadDimC;
        default:
            return clblasNotImplemented;
        }
    }

    if (tra) {
        matrSize = ((N - 1) * lda + K) * tsize;
        unusedTail = ( lda - N ) * tsize;
    }
    else {
        matrSize = ((M - 1) * lda + K) * tsize;
        unusedTail = ( lda - M ) * tsize;
    }

    offA *= tsize;

    if (clGetMemObjectInfo(A, CL_MEM_SIZE, sizeof(memSize), &memSize, NULL) !=
                                CL_SUCCESS) {
        switch( err )
        {
        case A_MAT_ERRSET:
            return clblasInvalidMatA;
        case B_MAT_ERRSET:
            return clblasInvalidMatB;
        case C_MAT_ERRSET:
            return clblasInvalidMatC;
        default:
            return clblasNotImplemented;
        }
    }

    //  It is possible to allocate a buffer, and set up lda & ldb such that it looks like it will access outside of the allocated buffer, but if
    //  M & N are kept small, no out of bounds access will occur.  Compensate for the offset values and the unused tail memory caused by lda & ldb.
    //  Ex: BuffSize=6 floats, M=1, N=2, lda=ldb=3, offA = 0, offB = 2 :  |A[0,0]|unused|B[0,0]|A[0,1]|unused|B[0,1]|
    memUsed = (( offA + matrSize ) > unusedTail) ? offA + matrSize - unusedTail: 0;
    if (memUsed > memSize) {
        switch( err )
        {
        case A_MAT_ERRSET:
            return clblasInsufficientMemMatA;
        case B_MAT_ERRSET:
            return clblasInsufficientMemMatB;
        case C_MAT_ERRSET:
            return clblasInsufficientMemMatC;
        default:
            return clblasNotImplemented;
        }
    }

    return clblasSuccess;
}

clblasStatus VISIBILITY_HIDDEN
checkVectorSizes(
    DataType dtype,
    size_t N,
    cl_mem x,
    size_t offx,
    int incx,
    ErrorCodeSet err )
{
    size_t memSize, sizev;
    size_t tsize;

    if (N == 0) {
        return clblasInvalidDim;
    }

    if (incx == 0) {
        switch( err )
        {
        case X_VEC_ERRSET:
            return clblasInvalidIncX;
        case Y_VEC_ERRSET:
            return clblasInvalidIncY;
        default:
            return clblasNotImplemented;
        }
    }

    if (clGetMemObjectInfo(x, CL_MEM_SIZE, sizeof(memSize), &memSize, NULL) !=
                                CL_SUCCESS) {
        switch( err )
        {
        case X_VEC_ERRSET:
            return clblasInvalidVecX;
        case Y_VEC_ERRSET:
            return clblasInvalidVecY;
        default:
            return clblasNotImplemented;
        }
    }

    tsize = dtypeSize(dtype);
    sizev = ((N - 1) * abs(incx) + 1) * tsize;
    offx *= tsize;

    if ((offx + sizev > memSize) || (offx + sizev < offx)) {
        switch( err )
        {
        case X_VEC_ERRSET:
            return clblasInsufficientMemVecX;
        case Y_VEC_ERRSET:
            return clblasInsufficientMemVecY;
        default:
            return clblasNotImplemented;
        }
    }

    return clblasSuccess;
}

clblasStatus
checkMemObjects(
    cl_mem A,
    cl_mem B,
    cl_mem C,
    bool checkC,
    ErrorCodeSet errA,
    ErrorCodeSet errB,
    ErrorCodeSet errC )
{
    cl_mem_object_type mobjType = 0;

    if (!clGetMemObjectInfo(A, CL_MEM_TYPE, sizeof(mobjType), &mobjType, NULL) &&
        (mobjType != CL_MEM_OBJECT_BUFFER)) {
        switch( errA )
        {
        case A_MAT_ERRSET:
            return clblasInvalidMatA;
        case B_MAT_ERRSET:
            return clblasInvalidMatB;
        case C_MAT_ERRSET:
            return clblasInvalidMatC;
        case X_VEC_ERRSET:
            return clblasInvalidVecX;
        case Y_VEC_ERRSET:
            return clblasInvalidVecY;
        default:
            return clblasNotImplemented;
        }
    }

    mobjType = 0;
    if (!clGetMemObjectInfo(B, CL_MEM_TYPE, sizeof(mobjType), &mobjType, NULL) &&
        (mobjType != CL_MEM_OBJECT_BUFFER)) {
        switch( errB )
        {
        case A_MAT_ERRSET:
            return clblasInvalidMatA;
        case B_MAT_ERRSET:
            return clblasInvalidMatB;
        case C_MAT_ERRSET:
            return clblasInvalidMatC;
        case X_VEC_ERRSET:
            return clblasInvalidVecX;
        case Y_VEC_ERRSET:
            return clblasInvalidVecY;
        default:
            return clblasNotImplemented;
        }
    }

    mobjType = 0;
    if (checkC && !clGetMemObjectInfo(C, CL_MEM_TYPE, sizeof(mobjType),
                                     &mobjType, NULL) &&
        (mobjType != CL_MEM_OBJECT_BUFFER)) {
        switch( errC )
        {
        case A_MAT_ERRSET:
            return clblasInvalidMatA;
        case B_MAT_ERRSET:
            return clblasInvalidMatB;
        case C_MAT_ERRSET:
            return clblasInvalidMatC;
        case X_VEC_ERRSET:
            return clblasInvalidVecX;
        case Y_VEC_ERRSET:
            return clblasInvalidVecY;
        default:
            return clblasNotImplemented;
        }
    }

    return clblasSuccess;
}