-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatvec.cpp
More file actions
146 lines (116 loc) · 3.42 KB
/
matvec.cpp
File metadata and controls
146 lines (116 loc) · 3.42 KB
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
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <iostream>
#include "../../cuda_error_check.h"
using namespace std;
#define CUBLAS_ASSERT(x) if ((x) != CUBLAS_STATUS_SUCCESS) { \
cout << "Error at " << __FILE__ << ":" << __LINE__ << endl; \
exit(1); \
}
void initialize_matrix(double *A, int n);
void initialize_vector(double *x, int n);
void verify_result(double *y, int n);
void check(int argc, char **argv);
int read_args(int argc, char **argv);
// ------------------ Main ------------------
int main(int argc, char **argv)
{
int n = read_args(argc, argv);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cublasHandle_t handle;
CUBLAS_ASSERT(cublasCreate(&handle));
size_t size_A = n * n * sizeof(double);
size_t size_x = n * sizeof(double);
double *h_A = (double *)malloc(size_A);
double *h_x = (double *)malloc(size_x);
double *h_y = (double *)malloc(size_x);
initialize_matrix(h_A, n);
initialize_vector(h_x, n);
double *d_A;
gpuErrorCheck(cudaMalloc(&d_A, size_A));
gpuErrorCheck(cudaMemcpy(d_A, h_A, size_A, cudaMemcpyHostToDevice));
double *d_x;
gpuErrorCheck(cudaMalloc(&d_x, size_x));
gpuErrorCheck(cudaMemcpy(d_x, h_x, size_x, cudaMemcpyHostToDevice));
double *d_y;
gpuErrorCheck(cudaMalloc(&d_y, size_x));
double alpha = 1.0;
double beta = 0.0; // if beta=0 then d_y can be uninitialized
cublasOperation_t op;
op = CUBLAS_OP_N; // (no transpose)
// op = CUBLAS_OP_T (tranpose of A)
// op = CUBLAS_OP_C (conjugate transpose of A).
// Start the timer
cudaEventRecord(start, 0);
// Usually lda=n, incx=1, and incy=1
CUBLAS_ASSERT(cublasDgemv(handle, op, n, n, &alpha, d_A, n, d_x, 1, &beta, d_y, 1));
cudaDeviceSynchronize();
// Stop the timer
cudaEventRecord(stop, 0);
gpuErrorCheck(cudaMemcpy(h_y, d_y, size_x, cudaMemcpyDeviceToHost));
verify_result(h_y, n);
// Destroy the handle
CUBLAS_ASSERT(cublasDestroy(handle));
// Free the memory
gpuErrorCheck(cudaFree(d_A));
gpuErrorCheck(cudaFree(d_x));
gpuErrorCheck(cudaFree(d_y));
free(h_A);
free(h_x);
free(h_y);
// Calculate the time
cudaEventSynchronize(stop);
float elapsedTime;
cudaEventElapsedTime(&elapsedTime, start, stop);
cout << "Elapsed time: " << elapsedTime << " ms" << endl;
cudaEventDestroy(start);
cudaEventDestroy(stop);
return 0;
}
// ------------------ Functions ------------------
void initialize_matrix(double *A, int n)
{
for (int j = 0; j < n; j++)
for (int i = 0; i < n; i++)
A[i + j * n] = 1.0;
return;
}
void initialize_vector(double *x, int n)
{
for (int i = 0; i < n; i++)
x[i] = 1.0;
return;
}
void verify_result(double *y, int n)
{
double expected = n;
for (int i = 0; i < n; i++)
{
if (y[i] != expected)
{
cout << "ERROR: mismatch at position " << i << " expected " << expected << " but got " << y[i] << endl;
return;
}
}
// cout << "PASSED!" << endl;
return;
}
void check(int argc, char **argv)
{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " m" << endl;
cout << "m: matrix dimension will be 2^m x 2^m" << endl;
exit(1);
}
return ;
}
int read_args(int argc, char **argv)
{
int m;
check(argc, argv);
m = atoi(argv[1]);
return 1 << m;
}