-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
270 lines (218 loc) · 7.22 KB
/
main.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
/////////////////////////////////////////////////////////////////////////////
// Name: main.cpp
// Purpose: Simulating the discrete element model on a GPU
// Author: Mae Woods UCL
// Modified by:
// Created: Jan/2014
// Copyright: Open source
// Licence: None
// Dependencies: CUDA see http://www.nvidia.co.uk/object/cuda-parallel-computing-uk.html
/////////////////////////////////////////////////////////////////////////////
// ==========================================================================
// headers, declarations, constants
// ==========================================================================
#include <cstdlib>
#include <cstdio>
#include <sdkHelper.h>
#include <unistd.h>
#include <fcntl.h>
#include <cuda_runtime.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <cuda.h>
#include "device_functions.h"
#include <curand_kernel.h>
#include <cutil_inline.h>
#include <iostream>
#include <fstream>
#include "main.h"
#include <iomanip>
#include "HostMemoryAlloc.h"
#include <cutil_inline.h>
#include <cstdlib>
#include <cstdio>
#include <string.h>
//Compute capability 2.x architectures
#define CUPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
using namespace std;
extern "C" void cudaInit(int argc, char **argv);
HostMemoryAlloc *hmalloc;
unsigned int timer;
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
inline void __checkCudaErrors(cudaError err, const char *file, const int line )
{
if(cudaSuccess != err)
{
fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",file, line, (int)err, cudaGetErrorString( err ) );
exit(-1);
}
}
// This will output the proper error string when calling cudaGetLastError
#define getLastCudaError(msg) __getLastCudaError (msg, __FILE__, __LINE__)
inline void __getLastCudaError(const char *errorMessage, const char *file, const int line )
{
cudaError_t err = cudaGetLastError();
if (cudaSuccess != err)
{
fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",
file, line, errorMessage, (int)err, cudaGetErrorString( err ) );
exit(-1);
}
}
// General GPU Device CUDA Initialization
int gpuDeviceInit(int devID)
{
int deviceCount;
checkCudaErrors(cudaGetDeviceCount(&deviceCount));
if (deviceCount == 0)
{
fprintf(stderr, "gpuDeviceInit() CUDA error: no devices supporting CUDA.\n");
exit(-1);
}
if (devID < 0)
devID = 0;
if (devID > deviceCount-1)
{
fprintf(stderr, "\n");
fprintf(stderr, ">> %d CUDA capable GPU device(s) detected. <<\n", deviceCount);
fprintf(stderr, ">> gpuDeviceInit (-device=%d) is not a valid GPU device. <<\n", devID);
fprintf(stderr, "\n");
return -devID;
}
cudaDeviceProp deviceProp;
checkCudaErrors( cudaGetDeviceProperties(&deviceProp, devID) );
if (deviceProp.major < 1)
{
fprintf(stderr, "gpuDeviceInit(): GPU device does not support CUDA.\n");
exit(-1);
}
checkCudaErrors( cudaSetDevice(devID) );
//printf("gpuDeviceInit() CUDA Device [%d]: \"%s\n", devID, deviceProp.name);
return devID;
}
#define MAX(a,b) ((a > b) ? a : b)
// This function returns the best GPU (with maximum GFLOPS)
int gpuGetMaxGflopsDeviceId()
{
int current_device = 0, sm_per_multiproc = 0;
int max_compute_perf = 0, max_perf_device = 0;
int device_count = 0, best_SM_arch = 0;
cudaDeviceProp deviceProp;
cudaGetDeviceCount( &device_count );
// Find the best major SM Architecture GPU device
while (current_device < device_count)
{
cudaGetDeviceProperties( &deviceProp, current_device );
if (deviceProp.major > 0 && deviceProp.major < 9999)
{
best_SM_arch = MAX(best_SM_arch, deviceProp.major);
}
current_device++;
}
// Find the best CUDA capable GPU device
current_device = 0;
while( current_device < device_count )
{
cudaGetDeviceProperties( &deviceProp, current_device );
if (deviceProp.major == 9999 && deviceProp.minor == 9999)
{
sm_per_multiproc = 1;
}
else
{
sm_per_multiproc = _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor);
}
int compute_perf = deviceProp.multiProcessorCount * sm_per_multiproc * deviceProp.clockRate;
if( compute_perf > max_compute_perf )
{
// If we find GPU with SM major > 2, search only these
if ( best_SM_arch > 2 )
{
// If our device==dest_SM_arch, choose this, or else pass
if (deviceProp.major == best_SM_arch)
{
max_compute_perf = compute_perf;
max_perf_device = current_device;
}
}
else
{
max_compute_perf = compute_perf;
max_perf_device = current_device;
}
}
++current_device;
}
return max_perf_device;
}
int getRunNo(int argc, const char **argv)
{
int Rint = 0;
if (checkCmdLineFlag(argc, argv, "run"))
{
Rint = getCmdLineArgumentInt(argc, argv, "run=");
}
return Rint;
}
int findCudaDevice(int argc, const char **argv)
{
cudaDeviceProp deviceProp;
int devID = 0;
// If the command-line has a device number specified, use it
if (checkCmdLineFlag(argc, argv, "device"))
{
devID = getCmdLineArgumentInt(argc, argv, "device=");
if (devID < 0)
{
exit(-1);
}
else
{
devID = gpuDeviceInit(devID);
if (devID < 0)
{
exit(-1);
}
}
}
else
{
// Otherwise pick the device with highest Gflops/s
devID = gpuGetMaxGflopsDeviceId();
checkCudaErrors( cudaSetDevice( devID ) );
checkCudaErrors( cudaGetDeviceProperties(&deviceProp, devID) );
}
return devID;
}
int main(int argc, char** argv)
{
int devID;
cudaDeviceProp props;
CUdevice cudaDevice;
//Initialize cuda (pass in device number on cmd line to select GPU, e.g. ./particles -device=4
//check for a command line argument that specifies a GPU (this is optional)
devID = findCudaDevice((const int)argc, (const char **)argv);
checkCudaErrors(cudaGetDevice(&devID));
checkCudaErrors(cudaGetDeviceProperties(&props, devID));
// Set the number of cells here
int numThreads=50;
// Set the number of independent simulations
int numBlocks=10;
int runNum = 0;
//Check if this is the first simulation, or a repeat using input data.
runNum = getRunNo((const int)argc, (const char **)argv);
//initialize class and set memory allocation on device
hmalloc = new HostMemoryAlloc(numBlocks, numThreads);
hmalloc->Initialise(runNum);
hmalloc->randkernel(numThreads, numBlocks);
//If you want to print kernel data to file use cuPrintf
cudaPrintfInit();
hmalloc->XData(numThreads, numBlocks, runNum);
cudaPrintfDisplay(stdout, 2000000000);
cudaPrintfEnd();
hmalloc->copyAfromD(runNum);
cudaThreadExit();
cudaDeviceReset();
return 0;
}