-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrixInverse.cu
257 lines (223 loc) · 8.38 KB
/
matrixInverse.cu
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
/*
============================================================================
Name : MatrixInverse.cu
Author : Yingliang
Version :
Copyright : Shanghaitech
Description : CUDA compute reciprocals
https://github.com/yingliangzh/Matrix-Inversion-On-CUDA
============================================================================
*/
#include <iostream>
#include <numeric>
#include <cuda_runtime.h>
#include <stdlib.h>
#include <ctime>
using namespace std;
#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)
#define random(x) (rand()%x)
/**
* Check the return value of the CUDA runtime API call and exit
* the application if the call has failed.
*/
static void CheckCudaErrorAux(const char *file, unsigned line, const char *statement, cudaError_t err)
{
if (err == cudaSuccess)
return;
std::cerr << statement << " returned " << cudaGetErrorString(err) << "(" << err << ") at " << file << ":" << line << std::endl;
exit(1);
}
void printMatrix(double* inputMatrix, const int rows, const int cols)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << inputMatrix[i * cols + j] << "\t";
}
std::cout << std::endl;
}
}
/**
* CUDA kernel that computes reciprocal values for a given vector
*/
__global__ void harnessZeroKernel(double *d_augmentedMatrix, const int rowId1, const int rowId2, const int size) {
__shared__ double blockR1[512];
__shared__ double blockR2[512];
const int tIdx = threadIdx.x;
const int bIdx = blockIdx.x;
const int colI = blockIdx.x * blockDim.x + threadIdx.x;
if (colI < size * 2) {
blockR1[tIdx] = d_augmentedMatrix[rowId1 * 2 * size + blockDim.x * bIdx + tIdx];
blockR2[tIdx] = d_augmentedMatrix[rowId2 * 2 * size + blockDim.x * bIdx + tIdx];
__syncthreads();
d_augmentedMatrix[rowId1 * 2 * size + blockDim.x * bIdx + tIdx] = blockR1[tIdx] + blockR2[tIdx];
}
}
__global__ void computeRowsKernel(double *d_augmentedMatrix, const int rowId, const int size) {
__shared__ double blockR[512];
__shared__ double Aii;
const int tIdx = threadIdx.x;
const int bIdx = blockIdx.x;
const int colI = blockIdx.x * blockDim.x + threadIdx.x;
if (colI < size * 2) {
blockR[tIdx] = d_augmentedMatrix[rowId * 2 * size + blockDim.x * bIdx + tIdx];
Aii = d_augmentedMatrix[rowId * 2 * size + rowId];
__syncthreads();
blockR[tIdx] = blockR[tIdx] / Aii;
d_augmentedMatrix[rowId * 2 * size + blockDim.x * bIdx + tIdx] = blockR[tIdx];
}
}
__global__ void computeColsKernel(double *d_augmentedMatrix, const int colId, const int size) {
__shared__ double blockC[16][16]; // which col need to be zero
__shared__ double blockCCurent[16][16]; // which col is the current col
__shared__ double ARow[16]; // the pivot row
const int tIdx = threadIdx.x;
const int tIdy = threadIdx.y;
const int rowI = blockIdx.y * blockDim.y + threadIdx.y;
const int colI = blockIdx.x * blockDim.x + threadIdx.x;
if (colI < size * 2 && rowI < size) {
blockC[tIdy][tIdx] = d_augmentedMatrix[rowI * size * 2 + colId];
if (blockC[tIdy][tIdx] != 0) {
blockCCurent[tIdy][tIdx] = d_augmentedMatrix[rowI * size * 2 + colI];
ARow[tIdx] = d_augmentedMatrix[colId * size * 2 + colI];
__syncthreads();
if (rowI != colId) { // current row can't sub by current row
blockCCurent[tIdy][tIdx] = blockCCurent[tIdy][tIdx] - blockC[tIdy][tIdx] * ARow[tIdx];
}
d_augmentedMatrix[rowI * size * 2 + colI] = blockCCurent[tIdy][tIdx];
//d_augmentedMatrix[rowI * size * 2 + colI] = ARow[tIdx];
}
}
}
__global__ void augmentMatrixKernel(double *d_augmentedMatrix, double *d_inputMatrix, const int rows, const int cols) {
const int rowI = blockIdx.y * blockDim.y + threadIdx.y;
const int colI = blockIdx.x * blockDim.x + threadIdx.x;
if (colI < cols && rowI < rows) {
// initialize augmentedMatrix
if (colI < cols / 2) {
d_augmentedMatrix[rowI * cols + colI] = d_inputMatrix[rowI * cols / 2 + colI];
}
else if (colI - cols / 2 == rowI) {
d_augmentedMatrix[rowI * cols + colI] = 1;
}
else {
d_augmentedMatrix[rowI * cols + colI] = 0;
}
}
}
__global__ void getInverseMatrixKernel(double *d_augmentedMatrix, double *d_inverseMatrix, const int rows, const int cols) {
const int rowI = blockIdx.y * blockDim.y + threadIdx.y;
const int colI = blockIdx.x * blockDim.x + threadIdx.x;
if (colI < cols / 2 && rowI < rows) {
// initialize augmentedMatrix
d_inverseMatrix[rowI * cols / 2 + colI] = d_augmentedMatrix[rowI * cols + colI + cols / 2];
}
}
/**
* Host function that copies the data and launches the work on GPU
*/
double *gpuMatrixInverse(double *inputMatrix, const int rows, const int cols)
{
double *h_inverseMatrix;
//double *h_augmentedMatrix;
double *d_inputMatrix;
double *d_inverseMatrix;
double *d_augmentedMatrix;
const int length = rows * cols;
const int size = rows;
//printMatrix(inputMatrix, rows, cols);
cout << endl;
// initialization
h_inverseMatrix = (double *)malloc(length * sizeof(double));
//h_augmentedMatrix = (double *)malloc(length * 2 * sizeof(double));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_augmentedMatrix, sizeof(double) * length * 2));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_inputMatrix, sizeof(double) * length));
CUDA_CHECK_RETURN(cudaMalloc((void **)&d_inverseMatrix, sizeof(double) * length));
CUDA_CHECK_RETURN(cudaMemcpy(d_inputMatrix, inputMatrix, sizeof(double) * length, cudaMemcpyHostToDevice));
dim3 blockSize1(16, 16);
dim3 gridSize1(cols * 2.0 / blockSize1.x + 1, rows * 1.0 / blockSize1.y + 1);
augmentMatrixKernel << <gridSize1, blockSize1 >> >(d_augmentedMatrix, d_inputMatrix, rows, cols * 2);
cudaDeviceSynchronize();
int i = 0;
while (i < size) {
if (inputMatrix[i * size + i] != 0) {
dim3 blockSize2(256);
dim3 gridSize2(cols * 2.0 / blockSize2.x + 1, 1);
computeRowsKernel << <gridSize2, blockSize2 >> >(d_augmentedMatrix, i, size);
cudaDeviceSynchronize();
}
else {
int nonZeroRowIndex = 0;
for (int j = 0; j < size; j++) {
if (inputMatrix[j * size + i] != 0) {
nonZeroRowIndex = j;
break;
}
}
dim3 blockSize3(256);
dim3 gridSize3(cols * 2.0 / blockSize3.x + 1, 1);
harnessZeroKernel << <gridSize3, blockSize3 >> >(d_augmentedMatrix, i, nonZeroRowIndex, size);
cudaDeviceSynchronize();
dim3 blockSize4(256);
dim3 gridSize4(cols * 2.0 / blockSize4.x + 1, 1);
computeRowsKernel << <gridSize4, blockSize4 >> >(d_augmentedMatrix, i, size);
cudaDeviceSynchronize();
}
dim3 blockSize5(16, 16);
dim3 gridSize5(cols * 2.0 / blockSize5.x + 1, rows * 1.0 / blockSize5.y + 1);
computeColsKernel << <gridSize5, blockSize5 >> >(d_augmentedMatrix, i, size);
cudaDeviceSynchronize();
i++;
}
dim3 blockSize6(16, 16);
dim3 gridSize6(cols * 2.0 / blockSize6.x + 1, rows * 1.0 / blockSize6.y + 1);
getInverseMatrixKernel << <gridSize1, blockSize1 >> >(d_augmentedMatrix, d_inverseMatrix, rows, cols * 2);
CUDA_CHECK_RETURN(cudaMemcpy(h_inverseMatrix, d_inverseMatrix, sizeof(double) * length, cudaMemcpyDeviceToHost));
//CUDA_CHECK_RETURN(cudaMemcpy(h_augmentedMatrix, d_augmentedMatrix, sizeof(double) * length * 2, cudaMemcpyDeviceToHost));
CUDA_CHECK_RETURN(cudaFree(d_augmentedMatrix));
CUDA_CHECK_RETURN(cudaFree(d_inverseMatrix));
CUDA_CHECK_RETURN(cudaFree(d_inputMatrix));
return h_inverseMatrix;
}
double *createTest(const int rows, const int cols)
{
double *data = (double *)malloc(rows * cols * sizeof(double));
for (int i = 0; i < rows * cols; i++) {
data[i] = random(100);
}
return data;
}
int sh(void)
{
const int rows = 1280*2;
const int cols = 1280*2;
double *testMatrix = createTest(rows, cols);
double *inverseMatrixGPU;
// GPU code
clock_t start1, end1;
start1 = clock();
inverseMatrixGPU = gpuMatrixInverse(testMatrix, rows, cols);
end1 = clock();
double dur1 = (double)(end1 - start1);
cout << "\n running time on GPU is " << dur1 / CLOCKS_PER_SEC << " secs!\n" << endl;
if (rows < 20) {
printMatrix(inverseMatrixGPU, rows, cols);
double *resultMatrix = (double *)malloc(cols * rows * sizeof(double));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
resultMatrix[i * cols + j] = 0;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < cols; k++) {
resultMatrix[i * cols + j] += testMatrix[i * cols + k] * inverseMatrixGPU[k * cols + j];
}
}
}
cout << "\nTest the result from GPU\n" << endl;
printMatrix(resultMatrix, rows, cols);
}
/* Free memory */
delete[] inverseMatrixGPU;
return 0;
}