-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparing_dct.cpp
302 lines (223 loc) · 8.57 KB
/
comparing_dct.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// Created by Juan Francisco on 2020-02-22.
//
#include <cstdio>
#include <fftw3.h>
//
////CBLAS
//extern "C"
//{
//#include <cblas.h>
//}
//#include "mkl.h"
#include "./include/cuda_helper/cuda_include_helper.h"
#include "include/common_gpu_utils.h"
#include "test_libs/include/tools.h"
#include "include/dct/cublas/DctCuBlas.h"
#include <thrust/device_vector.h>
#include <cublas_v2.h>
#include <cufft.h>
#include <chrono>
using namespace std::chrono;
void cublas_dct(int &dim_y, int &dim_x, thrust::device_vector<double> &x_n, thrust::device_vector<double> &x_k){
std::cout<< "cublas_dct" << std::endl;
// DCT
cudaEvent_t startcublas;
cudaEvent_t stopcublas;
DctCuBlas dctCuBlas(dim_y, dim_x);
cudaEventCreate(&startcublas);
cudaEventCreate(&stopcublas);
// Init DCT
cudaEventRecord(startcublas);
dctCuBlas.dct(x_n, x_k);
cudaEventRecord(stopcublas);
float cublasTime;
cudaEventSynchronize(stopcublas);
cudaEventElapsedTime(&cublasTime, startcublas, stopcublas);
std::cout << "cublas took[ms]: " << cublasTime << std::endl;
}
void cublas_idct(int &dim_y, int &dim_x, thrust::device_vector<double> &x_n, thrust::device_vector<double> &x_k){
std::cout<< "cublas_idct" << std::endl;
// DCT
cudaEvent_t startcublas;
cudaEvent_t stopcublas;
DctCuBlas dctCuBlas(dim_y, dim_x);
cudaEventCreate(&startcublas);
cudaEventCreate(&stopcublas);
// Init iDCT
cudaEventRecord(startcublas);
dctCuBlas.idct(x_k, x_n);
cudaEventRecord(stopcublas);
float cublasTime;
cudaEventSynchronize(stopcublas);
cudaEventElapsedTime(&cublasTime, startcublas, stopcublas);
std::cout << "cublas took[ms]: " << cublasTime << std::endl;
}
void fftw_dct(int &dim_y, int &dim_x, double *x_n){
std::cout<< "fftw_dct" << std::endl;
// FFTW two dimensions using m_line.dot(n_line) = x_n
fftw_complex *data_in, *data_out;
fftw_plan p;
// allocating data
data_in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * dim_x * dim_y);
data_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * dim_x * dim_y);
// fill data
// std::cout<< "Data in FFTW -------------" << std::endl;
for (int i=0; i<dim_y; i++){
for (int j=0; j<dim_x; j++){
data_in[i*dim_x + j][0] = x_n[i*dim_x + j]; // real data
data_in[i*dim_x + j][1] = 0.0; // imaginary data
// std::cout << data_in[i*dim_y + j][0] << " - "<< data_in[i*dim_y + j][1] << std::endl;
}
}
p = fftw_plan_dft_2d(
dim_x,
dim_y,
data_in,
data_out,
FFTW_FORWARD,
FFTW_ESTIMATE);
auto start_global = high_resolution_clock::now();
// executing fft
fftw_execute(p);
auto stop_global = high_resolution_clock::now();
auto duration_t = duration_cast<milliseconds>(stop_global - start_global);
std::cout << "fftw took[ms]: " << duration_t.count() << std::endl;
}
void cufft_float_fft(int &dim_y, int &dim_x, double *x_n){
std::cout<< "cufft_float_fft" << std::endl;
// FFTW two dimensions using m_line.dot(n_line) = x_n
cufftComplex *data_in, *data_out;
cufftComplex *data_in_d, *data_out_d;
cudaEvent_t startcufft;
cudaEvent_t stopcufft;
cudaEventCreate(&startcufft);
cudaEventCreate(&stopcufft);
data_in = new cufftComplex[dim_x*dim_y];
data_out = new cufftComplex[dim_x*dim_y];
// std::cout<< "Data in FFTW -------------" << std::endl;
for (int i=0; i<dim_y; i++){
for (int j=0; j<dim_x; j++){
data_in[i*dim_x + j].x = (float)x_n[i*dim_x + j]; // real data
data_in[i*dim_x + j].y = 0.0f; // imaginary data
// std::cout << data_in[i*dim_y + j][0] << " - "<< data_in[i*dim_y + j][1] << std::endl;
}
}
// cuda stuff
cufftResult cufftResult_t;
cufftHandle plan;
cufftPlan2d(&plan, dim_x, dim_y, CUFFT_C2C);
cudaMalloc(&data_in_d, sizeof(cufftComplex)*dim_x*dim_y);
cudaMalloc(&data_out_d, sizeof(cufftComplex)*dim_x*dim_y);
cudaMemcpy(data_in_d, data_in, sizeof(cufftComplex)*dim_x*dim_y, cudaMemcpyHostToDevice);
// auto start_global = high_resolution_clock::now();
cudaEventRecord(startcufft);
cufftResult_t = cufftExecC2C(plan, (cufftComplex *)data_in_d, (cufftComplex *)data_out_d, CUFFT_FORWARD);
assert(cufftResult_t == CUFFT_SUCCESS);
cudaDeviceSynchronize();
cudaEventRecord(stopcufft);
float cublasTime;
cudaEventSynchronize(stopcufft);
cudaEventElapsedTime(&cublasTime, startcufft, stopcufft);
std::cout << "cufft took[ms]: " << cublasTime << std::endl;
// auto stop_global = high_resolution_clock::now();
// auto duration_t = duration_cast<milliseconds>(stop_global - start_global);
// std::cout << "cuFFT took[ms]: " << duration_t.count() << std::endl;
cufftDestroy(plan);
cudaFree(data_in_d); cudaFree(data_out_d);
delete[](data_in);
delete[](data_out);
}
void cufft_double_fft(int &dim_y, int &dim_x, double *x_n){
std::cout<< "cufft_double_fft" << std::endl;
// FFTW two dimensions using m_line.dot(n_line) = x_n
cufftDoubleComplex *data_in, *data_out;
cufftDoubleComplex *data_in_d, *data_out_d;
cudaEvent_t startcufft;
cudaEvent_t stopcufft;
cudaEventCreate(&startcufft);
cudaEventCreate(&stopcufft);
data_in = new cufftDoubleComplex[dim_x*dim_y];
data_out = new cufftDoubleComplex[dim_x*dim_y];
// std::cout<< "Data in FFTW -------------" << std::endl;
for (int i=0; i<dim_y; i++){
for (int j=0; j<dim_x; j++){
data_in[i*dim_x + j].x = x_n[i*dim_x + j]; // real data
data_in[i*dim_x + j].y = 0.0; // imaginary data
// std::cout << data_in[i*dim_y + j][0] << " - "<< data_in[i*dim_y + j][1] << std::endl;
}
}
// cuda stuff
cufftResult cufftResult_t;
cufftHandle plan;
cufftPlan2d(&plan, dim_x, dim_y, CUFFT_Z2Z);
cudaMalloc(&data_in_d, sizeof(cufftDoubleComplex)*dim_x*dim_y);
cudaMalloc(&data_out_d, sizeof(cufftDoubleComplex)*dim_x*dim_y);
cudaMemcpy(data_in_d, data_in, sizeof(cufftDoubleComplex)*dim_x*dim_y, cudaMemcpyHostToDevice);
// auto start_global = high_resolution_clock::now();
cudaEventRecord(startcufft);
cufftResult_t = cufftExecZ2Z(plan, (cufftDoubleComplex *)data_in_d, (cufftDoubleComplex *)data_out_d, CUFFT_FORWARD);
assert(cufftResult_t == CUFFT_SUCCESS);
cudaDeviceSynchronize();
cudaEventRecord(stopcufft);
float cublasTime;
cudaEventSynchronize(stopcufft);
cudaEventElapsedTime(&cublasTime, startcufft, stopcufft);
std::cout << "cufft took[ms]: " << cublasTime << std::endl;
// auto stop_global = high_resolution_clock::now();
// auto duration_t = duration_cast<milliseconds>(stop_global - start_global);
// std::cout << "cuFFT took[ms]: " << duration_t.count() << std::endl;
cufftDestroy(plan);
cudaFree(data_in_d); cudaFree(data_out_d);
delete[](data_in);
delete[](data_out);
}
int main(int argv, char** argc){
printf("Comparing DCT tensorcores , cublas and cufft\n");
int size_m = 3000;
int size_n = 3000;
int dim_y = size_m;
int dim_x = size_n;
printf("dim_y %d dim_x %d\n",dim_y, dim_x);
// thrust::host_vector<>
auto *x_n_host = new double[size_m * size_n];
auto *m_line = new double[size_m];
auto *n_line = new double[size_n];
// Fill and Print
fill_vector_cos(3, size_m, m_line);
fill_vector_cos(2, size_n, n_line);
// print_array(m_line, size_m);
// print_array(n_line, size_n);
int M = size_m;
int N = size_n;
int K = 1;
for (int i=0; i<M; i++){
for (int j=0; j<N; j++){
for (int k=0; k<K; k++){
x_n_host[i*size_n + j] += m_line[i*K + k] * n_line[k*N + j];
}
}
}
// print_array(x_n_host , M, N);
// GPU copies
int seq_size = size_m * size_n;
thrust::device_vector<double> x_n(seq_size, 0.0);
double *x_n_ptr = thrust::raw_pointer_cast(&x_n[0]);
thrust::device_vector<double> x_k(seq_size, 0.0);
double *x_k_ptr = thrust::raw_pointer_cast(&x_k[0]);
cudaMemcpy(x_n_ptr, x_n_host, sizeof(double)*size_m*size_n, cudaMemcpyHostToDevice);
// Printing!
// print_dvector(x_n, "x_n");
// print_dvector(x_k, "x_k");
// print_dvector(x_n, "x_n");
// cublas
cublas_dct(dim_y, dim_x, x_n, x_k);
cublas_idct(dim_y, dim_x, x_n, x_k);
fftw_dct(dim_y, dim_x, x_n_host);
cufft_float_fft(dim_y, dim_x, x_n_host);
cufft_double_fft(dim_y, dim_x, x_n_host);
delete[] x_n_host;
delete[] m_line;
delete[] n_line;
return 0;
}