-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuMatrix.cpp
168 lines (162 loc) · 3.89 KB
/
cuMatrix.cpp
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
#include "cuMatrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include "cublas_v2.h"
#include <iostream>
void printMatrixInfo(cuMatrix<float>* mat) {
std::cout << "shape: (" << mat->rows << ", " << mat->cols << ")" << std::endl;
for (int i = 0; i < mat->rows; i++) {
for (int j = 0; j < mat->cols; j++) {
std::cout << mat->getHost()[i*mat->cols+j] << "\t";
}
std::cout << std::endl;
}
}
cublasHandle_t& getHandle()
{
static cublasHandle_t handle = NULL;
if(handle == NULL){
cublasStatus_t stat;
stat = cublasCreate(&handle);
if(stat != CUBLAS_STATUS_SUCCESS) {
printf ("init: CUBLAS initialization failed\n");
exit(0);
}
}
return handle;
}
/*matrix multiply*/
/*z = x * y*/
void matrixMul(cuMatrix<float>* x, cuMatrix<float>*y, cuMatrix<float>*z)
{
if(x->channels != 1 || y->channels != 1 || z->channels != 1){
printf("matrix mul channels != 1\n");
exit(0);
}
if(x->cols != y->rows || z->rows != x->rows || z->cols != y->cols){
printf("matrix mul dimension mismatch\n");
exit(0);
}
float alpha = 1.0;
float beta = 0.0;
cublasStatus_t stat;
stat = cublasSgemm(
getHandle(),
CUBLAS_OP_N,
CUBLAS_OP_N,
y->cols,
x->rows,
y->rows,
&alpha,
y->getDev(),
y->cols,
x->getDev(),
x->cols,
&beta,
z->getDev(),
z->cols);
cudaStreamSynchronize(0);
getLastCudaError("matrixMul");
if(stat != CUBLAS_STATUS_SUCCESS) {
printf("matrixMul cublasSgemm error\n");
cudaFree(x->getDev());
cudaFree(y->getDev());
cudaFree(z->getDev());
exit(0);
}
}
/*z = T(x) * y*/
void matrixMulTA(cuMatrix<float>* x, cuMatrix<float>*y, cuMatrix<float>*z)
{
if(x->channels != 1 || y->channels != 1 || z->channels != 1){
printf("matrix mul chanels != 1\n");
exit(0);
}
if(x->rows != y->rows || z->rows != x->cols || z->cols != y->cols){
printf("matrix mul chanels != 1\n");
exit(0);
}
cublasStatus_t stat;
float alpha = 1.0;
float beta = 0.0;
stat = cublasSgemm(
getHandle(),
CUBLAS_OP_N,
CUBLAS_OP_T,
y->cols,
x->cols,
y->rows,
&alpha,
y->getDev(),
y->cols,
x->getDev(),
x->cols,
&beta,
z->getDev(),
z->cols);
cudaStreamSynchronize(0);
getLastCudaError("matrixMulTA");
if(stat != CUBLAS_STATUS_SUCCESS) {
printf( "matrixMulTA cublasSgemm error\n");
exit(0);
}
}
/*z = x * T(y)*/
void matrixMulTB(cuMatrix<float>* x, cuMatrix<float>*y, cuMatrix<float>*z)
{
if(x->channels != 1 || y->channels != 1 || z->channels != 1){
printf("matrix mul chanels != 1\n");
exit(0);
}
if(x->cols != y->cols || z->rows != x->rows || z->cols != y->rows){
printf("matrix mul chanels != 1\n");
exit(0);
}
cublasStatus_t stat;
float alpha = 1.0;
float beta = 0.0;
stat = cublasSgemm(
getHandle(),
CUBLAS_OP_T,
CUBLAS_OP_N,
y->rows,
x->rows,
y->cols,
&alpha,
y->getDev(),
y->cols,
x->getDev(),
x->cols,
&beta,
z->getDev(),
z->cols);
cudaStreamSynchronize(0);
getLastCudaError("matrixMulTB");
if(stat != CUBLAS_STATUS_SUCCESS) {
printf("matrixMulTB cublasSgemm error\n");
exit(0);
}
}
void matrixAdd(cuMatrix<float> *x, cuMatrix<float> *y, cuMatrix<float> *z, float lambda) {
float alpha = 1.0;
cublasStatus_t stat;
stat = cublasSgeam(getHandle(),
CUBLAS_OP_N,
CUBLAS_OP_N,
x->cols, y->rows,
&alpha,
x->getDev(), x->cols,
&lambda,
y->getDev(), y->cols,
z->getDev(), z->cols);
cudaStreamSynchronize(0);
getLastCudaError("matrixAdd");
if (stat != CUBLAS_STATUS_SUCCESS) {
printf("matrixAdd cublasSgemm error\n");
cudaFree(x->getDev());
cudaFree(y->getDev());
cudaFree(z->getDev());
exit(0);
}
}