Debian Codegraph @

cuda/plink2_matrix_cuda.cu

raw
1 #include "cuda_runtime.h"
2 #include "cublas_v2.h"
3
4 #include "plink2_matrix_cuda.h"
5 #include <stdio.h>
6
7 int CudaGetDeviceCount() {
8 int device_count;
9 if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
10 return 0;
11 }
12 return device_count;
13 }
14
15 int CudaSetDevice(int device_idx) {
16 if (cudaSetDevice(device_idx) != cudaSuccess) {
17 return 1;
18 }
19 return 0;
20 }
21
22 void CublasFmultiplierPreinit(CublasFmultiplier* cfmp) {
23 cfmp->handle = NULL;
24 cfmp->dev_inmatrix1 = NULL;
25 cfmp->dev_inmatrix2 = NULL;
26 cfmp->dev_outmatrix = NULL;
27 cfmp->in_borrowed = 0;
28 cfmp->device_idx = -1;
29 }
30
31 void CublasFmultiplierBorrowColMajor1(CublasFmultiplier* srcp, CublasFmultiplier* dstp) {
32 dstp->dev_inmatrix1 = srcp->dev_inmatrix1;
33 dstp->in_borrowed |= 1;
34 }
35
36 void CublasFmultiplierBorrowColMajor2(CublasFmultiplier* srcp, CublasFmultiplier* dstp) {
37 dstp->dev_inmatrix2 = srcp->dev_inmatrix2;
38 dstp->in_borrowed |= 2;
39 }
40
41 // Initializes device handle and allocates device memory.
42 // Returns 0 on success, 1 on failure.
43 int CublasFmultiplierColMajorInit(int row1_ct, int col2_ct, int common_ct, CublasFmultiplier* cfmp) {
44 if ((cfmp->device_idx != -1) || (cfmp->handle != NULL)) {
45 // sanity check: improper function call
46 return 1;
47 }
48 if (cudaGetDevice(&cfmp->device_idx) != cudaSuccess) {
49 cfmp->device_idx = -1;
50 return 1;
51 }
52 cublasHandle_t* handlep = (cublasHandle_t*)(&cfmp->handle);
53 cublasStatus_t stat = cublasCreate(handlep);
54 if (stat != CUBLAS_STATUS_SUCCESS) {
55 return 1;
56 }
57 if (!(cfmp->in_borrowed & 1)) {
58 cudaError_t cudaStat = cudaMalloc((void**)&cfmp->dev_inmatrix1, row1_ct * sizeof(float) * common_ct);
59 if (cudaStat != cudaSuccess) {
60 return 1;
61 }
62 }
63 if (!(cfmp->in_borrowed & 2)) {
64 cudaError_t cudaStat = cudaMalloc((void**)&cfmp->dev_inmatrix2, common_ct * sizeof(float) * col2_ct);
65 if (cudaStat != cudaSuccess) {
66 return 1;
67 }
68 }
69 cudaError_t cudaStat = cudaMalloc((void**)&cfmp->dev_outmatrix, row1_ct * sizeof(float) * col2_ct);
70 if (cudaStat != cudaSuccess) {
71 return 1;
72 }
73 cfmp->row1_ct = row1_ct;
74 cfmp->col2_ct = col2_ct;
75 cfmp->common_ct = common_ct;
76 return 0;
77 }
78
79 int CublasFmultiplierPreloadColMajor1(const float* inmatrix1, CublasFmultiplier* cfmp) {
80 const int row1_ct = cfmp->row1_ct;
81 cublasStatus_t stat = cublasSetMatrix(row1_ct, cfmp->common_ct, sizeof(float), inmatrix1, row1_ct, cfmp->dev_inmatrix1, row1_ct);
82 return (stat != CUBLAS_STATUS_SUCCESS);
83 }
84
85 int CublasFmultiplierPreloadColMajor2(const float* inmatrix2, CublasFmultiplier* cfmp) {
86 const int common_ct = cfmp->common_ct;
87 cublasStatus_t stat = cublasSetMatrix(common_ct, cfmp->col2_ct, sizeof(float), inmatrix2, common_ct, cfmp->dev_inmatrix2, common_ct);
88 return (stat != CUBLAS_STATUS_SUCCESS);
89 }
90
91 int CublasFmultiplyColMajor1(const float* inmatrix1, CublasFmultiplier* cfmp, float* outmatrix) {
92 const int row1_ct = cfmp->row1_ct;
93 const int col2_ct = cfmp->col2_ct;
94 const int common_ct = cfmp->common_ct;
95 cublasStatus_t stat = cublasSetMatrix(row1_ct, common_ct, sizeof(float), inmatrix1, row1_ct, cfmp->dev_inmatrix1, row1_ct);
96 if (stat != CUBLAS_STATUS_SUCCESS) {
97 return 1;
98 }
99 float alpha = 1.0;
100 float beta = 0.0;
101 stat = cublasSgemm((cublasHandle_t)cfmp->handle, CUBLAS_OP_N, CUBLAS_OP_N, row1_ct, col2_ct, common_ct, &alpha, cfmp->dev_inmatrix1, row1_ct, cfmp->dev_inmatrix2, common_ct, &beta, cfmp->dev_outmatrix, row1_ct);
102 if (stat != CUBLAS_STATUS_SUCCESS) {
103 return 1;
104 }
105 stat = cublasGetMatrix(row1_ct, col2_ct, sizeof(float), cfmp->dev_outmatrix, row1_ct, outmatrix, row1_ct);
106 return (stat != CUBLAS_STATUS_SUCCESS);
107 }
108
109 int CublasFmultiplyColMajor2(const float* inmatrix2, CublasFmultiplier* cfmp, float* outmatrix) {
110 const int row1_ct = cfmp->row1_ct;
111 const int col2_ct = cfmp->col2_ct;
112 const int common_ct = cfmp->common_ct;
113 cublasStatus_t stat = cublasSetMatrix(common_ct, col2_ct, sizeof(float), inmatrix2, common_ct, cfmp->dev_inmatrix2, common_ct);
114 if (stat != CUBLAS_STATUS_SUCCESS) {
115 return 1;
116 }
117 float alpha = 1.0;
118 float beta = 0.0;
119 stat = cublasSgemm((cublasHandle_t)cfmp->handle, CUBLAS_OP_N, CUBLAS_OP_N, row1_ct, col2_ct, common_ct, &alpha, cfmp->dev_inmatrix1, row1_ct, cfmp->dev_inmatrix2, common_ct, &beta, cfmp->dev_outmatrix, row1_ct);
120 if (stat != CUBLAS_STATUS_SUCCESS) {
121 return 1;
122 }
123 stat = cublasGetMatrix(row1_ct, col2_ct, sizeof(float), cfmp->dev_outmatrix, row1_ct, outmatrix, row1_ct);
124 return (stat != CUBLAS_STATUS_SUCCESS);
125 }
126
127 int CublasFmultiplierCleanup(CublasFmultiplier* cfmp) {
128 if (cfmp->device_idx == -1) {
129 return 0;
130 }
131 int retval = 0;
132 if (cudaSetDevice(cfmp->device_idx) != cudaSuccess) {
133 retval = 1;
134 }
135 cfmp->device_idx = -1;
136 if (cudaFree(cfmp->dev_outmatrix) != cudaSuccess) {
137 retval = 1;
138 }
139 cfmp->dev_outmatrix = NULL;
140 if (!(cfmp->in_borrowed & 2)) {
141 if (cudaFree(cfmp->dev_inmatrix2) != cudaSuccess) {
142 retval = 1;
143 }
144 }
145 cfmp->dev_inmatrix2 = NULL;
146 if (!(cfmp->in_borrowed & 1)) {
147 if (cudaFree(cfmp->dev_inmatrix1) != cudaSuccess) {
148 retval = 1;
149 }
150 }
151 cfmp->dev_inmatrix1 = NULL;
152 cfmp->in_borrowed = 0;
153 cublasDestroy((cublasHandle_t)cfmp->handle); // don't care about return value
154 cfmp->handle = NULL;
155 return retval;
156 }
157
158 // Only for testing purposes.
159 int CublasColMajorFmatrixMultiply(const float* inmatrix1, const float* inmatrix2, int row1_ct, int col2_ct, int common_ct, float* outmatrix) {
160 CublasFmultiplier cfm;
161 CublasFmultiplierPreinit(&cfm);
162 int retval = 0;
163 {
164 if (CublasFmultiplierColMajorInit(row1_ct, col2_ct, common_ct, &cfm)) {
165 goto CublasColMajorFmatrixMultiply_fail;
166 }
167 cublasStatus_t stat = cublasSetMatrix(row1_ct, common_ct, sizeof(float), inmatrix1, row1_ct, cfm.dev_inmatrix1, row1_ct);
168 if (stat != CUBLAS_STATUS_SUCCESS) {
169 goto CublasColMajorFmatrixMultiply_fail;
170 }
171 stat = cublasSetMatrix(common_ct, col2_ct, sizeof(float), inmatrix2, common_ct, cfm.dev_inmatrix2, common_ct);
172 if (stat != CUBLAS_STATUS_SUCCESS) {
173 goto CublasColMajorFmatrixMultiply_fail;
174 }
175 float alpha = 1.0;
176 float beta = 0.0;
177 stat = cublasSgemm((cublasHandle_t)cfm.handle, CUBLAS_OP_N, CUBLAS_OP_N, row1_ct, col2_ct, common_ct, &alpha, cfm.dev_inmatrix1, row1_ct, cfm.dev_inmatrix2, common_ct, &beta, cfm.dev_outmatrix, row1_ct);
178 if (stat != CUBLAS_STATUS_SUCCESS) {
179 goto CublasColMajorFmatrixMultiply_fail;
180 }
181 stat = cublasGetMatrix(row1_ct, col2_ct, sizeof(float), cfm.dev_outmatrix, row1_ct, outmatrix, row1_ct);
182 if (stat != CUBLAS_STATUS_SUCCESS) {
183 goto CublasColMajorFmatrixMultiply_fail;
184 }
185 }
186 while (0) {
187 CublasColMajorFmatrixMultiply_fail:
188 retval = 1;
189 }
190 if (CublasFmultiplierCleanup(&cfm)) {
191 retval = 1;
192 }
193 return retval;
194 }