Newer
Older
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
/***************************************************************************
vector-operations.h - description
-------------------
begin : Dec 30, 2015
copyright : (C) 2015 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
// Implemented by: Jakub Klinkovsky
#pragma once
#include <stdlib.h> // srand48
#include "../Benchmarks.h"
#include <TNL/Containers/Vector.h>
#ifdef HAVE_CUDA
#include "cublasWrappers.h"
#endif
namespace TNL {
namespace Benchmarks {
template< typename Real = double,
typename Index = int >
bool
benchmarkVectorOperations( Benchmark & benchmark,
const long & size )
{
typedef Containers::Vector< Real, Devices::Host, Index > HostVector;
typedef Containers::Vector< Real, Devices::Cuda, Index > CudaVector;
using namespace std;
double datasetSize = (double) size * sizeof( Real ) / oneGB;
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
HostVector hostVector, hostVector2;
CudaVector deviceVector, deviceVector2;
hostVector.setSize( size );
hostVector2.setSize( size );
#ifdef HAVE_CUDA
deviceVector.setSize( size );
deviceVector2.setSize( size );
#endif
Real resultHost, resultDevice;
#ifdef HAVE_CUDA
cublasHandle_t cublasHandle;
cublasCreate( &cublasHandle );
#endif
// reset functions
// (Make sure to always use some in benchmarks, even if it's not necessary
// to assure correct result - it helps to clear cache and avoid optimizations
// of the benchmark loop.)
auto reset1 = [&]() {
hostVector.setValue( 1.0 );
#ifdef HAVE_CUDA
deviceVector.setValue( 1.0 );
#endif
// A relatively harmless call to keep the compiler from realizing we
// don't actually do any useful work with the result of the reduciton.
srand48(resultHost);
resultHost = resultDevice = 0.0;
};
auto reset2 = [&]() {
hostVector2.setValue( 1.0 );
#ifdef HAVE_CUDA
deviceVector2.setValue( 1.0 );
#endif
};
auto reset12 = [&]() {
reset1();
reset2();
};
reset12();
auto maxHost = [&]() {
resultHost = hostVector.max();
};
auto maxCuda = [&]() {
resultDevice = deviceVector.max();
};
benchmark.setOperation( "max", datasetSize );
benchmark.time( reset1, "CPU", maxHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", maxCuda );
#endif
auto minHost = [&]() {
resultHost = hostVector.min();
};
auto minCuda = [&]() {
resultDevice = deviceVector.min();
};
benchmark.setOperation( "min", datasetSize );
benchmark.time( reset1, "CPU", minHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", minCuda );
#endif
auto absMaxHost = [&]() {
resultHost = hostVector.absMax();
};
auto absMaxCuda = [&]() {
resultDevice = deviceVector.absMax();
};
#ifdef HAVE_CUDA
auto absMaxCublas = [&]() {
int index = 0;
cublasIgamax( cublasHandle, size,
deviceVector.getData(), 1,
&index );
resultDevice = deviceVector.getElement( index );
};
#endif
benchmark.setOperation( "absMax", datasetSize );
benchmark.time( reset1, "CPU", absMaxHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", absMaxCuda );
benchmark.time( reset1, "cuBLAS", absMaxCublas );
#endif
auto absMinHost = [&]() {
resultHost = hostVector.absMin();
};
auto absMinCuda = [&]() {
resultDevice = deviceVector.absMin();
};
#ifdef HAVE_CUDA
auto absMinCublas = [&]() {
int index = 0;
cublasIgamin( cublasHandle, size,
deviceVector.getData(), 1,
&index );
resultDevice = deviceVector.getElement( index );
};
#endif
benchmark.setOperation( "absMin", datasetSize );
benchmark.time( reset1, "CPU", absMinHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", absMinCuda );
benchmark.time( reset1, "cuBLAS", absMinCublas );
#endif
auto sumHost = [&]() {
resultHost = hostVector.sum();
};
auto sumCuda = [&]() {
resultDevice = deviceVector.sum();
};
benchmark.setOperation( "sum", datasetSize );
benchmark.time( reset1, "CPU", sumHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", sumCuda );
#endif
auto l1normHost = [&]() {
resultHost = hostVector.lpNorm( 1.0 );
};
auto l1normCuda = [&]() {
resultDevice = deviceVector.lpNorm( 1.0 );
};
#ifdef HAVE_CUDA
auto l1normCublas = [&]() {
cublasGasum( cublasHandle, size,
deviceVector.getData(), 1,
&resultDevice );
};
#endif
benchmark.setOperation( "l1 norm", datasetSize );
benchmark.time( reset1, "CPU", l1normHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", l1normCuda );
benchmark.time( reset1, "cuBLAS", l1normCublas );
#endif
auto l2normHost = [&]() {
resultHost = hostVector.lpNorm( 2.0 );
};
auto l2normCuda = [&]() {
resultDevice = deviceVector.lpNorm( 2.0 );
};
#ifdef HAVE_CUDA
auto l2normCublas = [&]() {
cublasGnrm2( cublasHandle, size,
deviceVector.getData(), 1,
&resultDevice );
};
#endif
benchmark.setOperation( "l2 norm", datasetSize );
benchmark.time( reset1, "CPU", l2normHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", l2normCuda );
benchmark.time( reset1, "cuBLAS", l2normCublas );
#endif
auto l3normHost = [&]() {
resultHost = hostVector.lpNorm( 3.0 );
};
auto l3normCuda = [&]() {
resultDevice = deviceVector.lpNorm( 3.0 );
};
benchmark.setOperation( "l3 norm", datasetSize );
benchmark.time( reset1, "CPU", l3normHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", l3normCuda );
#endif
auto scalarProductHost = [&]() {
resultHost = hostVector.scalarProduct( hostVector2 );
};
auto scalarProductCuda = [&]() {
resultDevice = deviceVector.scalarProduct( deviceVector2 );
};
#ifdef HAVE_CUDA
auto scalarProductCublas = [&]() {
cublasGdot( cublasHandle, size,
deviceVector.getData(), 1,
deviceVector2.getData(), 1,
&resultDevice );
};
#endif
benchmark.setOperation( "scalar product", 2 * datasetSize );
benchmark.time( reset1, "CPU", scalarProductHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", scalarProductCuda );
benchmark.time( reset1, "cuBLAS", scalarProductCublas );
#endif
/*
std::cout << "Benchmarking prefix-sum:" << std::endl;
timer.reset();
timer.start();
hostVector.computePrefixSum();
timer.stop();
timeHost = timer.getTime();
std::cout << " CPU: bandwidth: " << bandwidth << " GB/sec, time: " << timer.getTime() << " sec." << std::endl;
timer.reset();
timer.start();
deviceVector.computePrefixSum();
timer.stop();
timeDevice = timer.getTime();
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
std::cout << " GPU: bandwidth: " << bandwidth << " GB/sec, time: " << timer.getTime() << " sec." << std::endl;
std::cout << " CPU/GPU speedup: " << timeHost / timeDevice << std::endl;
HostVector auxHostVector;
auxHostVector.setLike( deviceVector );
auxHostVector = deviceVector;
for( int i = 0; i < size; i++ )
if( hostVector.getElement( i ) != auxHostVector.getElement( i ) )
{
std::cerr << "Error in prefix sum at position " << i << ": " << hostVector.getElement( i ) << " != " << auxHostVector.getElement( i ) << std::endl;
}
*/
auto multiplyHost = [&]() {
hostVector *= 0.5;
};
auto multiplyCuda = [&]() {
deviceVector *= 0.5;
};
#ifdef HAVE_CUDA
auto multiplyCublas = [&]() {
const Real alpha = 0.5;
cublasGscal( cublasHandle, size,
&alpha,
deviceVector.getData(), 1 );
};
#endif
benchmark.setOperation( "scalar multiplication", 2 * datasetSize );
benchmark.time( reset1, "CPU", multiplyHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", multiplyCuda );
benchmark.time( reset1, "cuBLAS", multiplyCublas );
#endif
auto addVectorHost = [&]() {
hostVector.addVector( hostVector2 );
};
auto addVectorCuda = [&]() {
deviceVector.addVector( deviceVector2 );
};
#ifdef HAVE_CUDA
auto addVectorCublas = [&]() {
const Real alpha = 1.0;
cublasGaxpy( cublasHandle, size,
&alpha,
deviceVector2.getData(), 1,
deviceVector.getData(), 1 );
};
#endif
benchmark.setOperation( "vector addition", 3 * datasetSize );
benchmark.time( reset1, "CPU", addVectorHost );
#ifdef HAVE_CUDA
benchmark.time( reset1, "GPU", addVectorCuda );
benchmark.time( reset1, "cuBLAS", addVectorCublas );
#endif
#ifdef HAVE_CUDA
cublasDestroy( cublasHandle );
#endif
return true;
}
} // namespace Benchmarks
} // namespace TNL