1 module grain.cublas;
2 
3 import derelict.cuda;
4 
5 /++
6  TODO: make derelict-cublas (already exist?)
7  +/
8 
9 extern (C):
10 
11 
12 alias cublasStatus_t = int;
13 enum : cublasStatus_t {
14     CUBLAS_STATUS_SUCCESS         =0,
15     CUBLAS_STATUS_NOT_INITIALIZED =1,
16     CUBLAS_STATUS_ALLOC_FAILED    =3,
17     CUBLAS_STATUS_INVALID_VALUE   =7,
18     CUBLAS_STATUS_ARCH_MISMATCH   =8,
19     CUBLAS_STATUS_MAPPING_ERROR   =11,
20     CUBLAS_STATUS_EXECUTION_FAILED=13,
21     CUBLAS_STATUS_INTERNAL_ERROR  =14,
22     CUBLAS_STATUS_NOT_SUPPORTED   =15,
23     CUBLAS_STATUS_LICENSE_ERROR   =16
24 }
25 
26 struct cublasContext;
27 alias cublasHandle_t = cublasContext*;
28 
29 alias cublasOperation_t = int;
30 enum : cublasOperation_t {
31     CUBLAS_OP_N, // the non-transpose operation is selected
32     CUBLAS_OP_T, // the transpose operation is selected
33     CUBLAS_OP_C // the conjugate transpose operation is selected
34 }
35 
36 
37 // TODO: parse and retrieve cublas_api.h
38 cublasStatus_t cublasCreate_v2(cublasHandle_t*);
39 cublasStatus_t cublasDestroy_v2(cublasHandle_t handle);
40 
41  cublasStatus_t cublasSgemm_v2(cublasHandle_t handle, cublasOperation_t transa, cublasOperation_t transb, int m, int n, int k, const float *alpha, const float *A, int lda, const float *B, int ldb, const float *beta, float *C, int ldc);
42 
43 /*
44 cublasStatus_t cublasSgemm_v2(cublasHandle_t handle,
45                            cublasOperation_t transa, cublasOperation_t transb,
46                            int m, int n, int k,
47                            const float           *alpha,
48                            const float           *A, int lda,
49                            const float           *B, int ldb,
50                            const float           *beta,
51                            float           *C, int ldc);
52 */
53 
54 cublasStatus_t cublasDgemm_v2(cublasHandle_t handle,
55                            cublasOperation_t transa, cublasOperation_t transb,
56                            int m, int n, int k,
57                            const double          *alpha,
58                            const double          *A, int lda,
59                            const double          *B, int ldb,
60                            const double          *beta,
61                            double          *C, int ldc);
62 
63 cublasStatus_t cublasSaxpy_v2(cublasHandle_t handle, int n,
64                               const float           *alpha,
65                               const float           *x, int incx,
66                               float                 *y, int incy);
67 
68 cublasStatus_t cublasDaxpy_v2(cublasHandle_t handle, int n,
69                               const double          *alpha,
70                               const double          *x, int incx,
71                               double                *y, int incy);
72 
73 
74 auto cublasGetErrorEnum(cublasStatus_t error) {
75     final switch (error)
76     {
77         case CUBLAS_STATUS_SUCCESS:
78             return "CUBLAS_STATUS_SUCCESS";
79 
80         case CUBLAS_STATUS_NOT_INITIALIZED:
81             return "CUBLAS_STATUS_NOT_INITIALIZED";
82 
83         case CUBLAS_STATUS_ALLOC_FAILED:
84             return "CUBLAS_STATUS_ALLOC_FAILED";
85 
86         case CUBLAS_STATUS_INVALID_VALUE:
87             return "CUBLAS_STATUS_INVALID_VALUE";
88 
89         case CUBLAS_STATUS_ARCH_MISMATCH:
90             return "CUBLAS_STATUS_ARCH_MISMATCH";
91 
92         case CUBLAS_STATUS_MAPPING_ERROR:
93             return "CUBLAS_STATUS_MAPPING_ERROR";
94 
95         case CUBLAS_STATUS_EXECUTION_FAILED:
96             return "CUBLAS_STATUS_EXECUTION_FAILED";
97 
98         case CUBLAS_STATUS_INTERNAL_ERROR:
99             return "CUBLAS_STATUS_INTERNAL_ERROR";
100     }
101 }