-
Notifications
You must be signed in to change notification settings - Fork 20
/
AS_Demo.cu
195 lines (167 loc) · 4.89 KB
/
AS_Demo.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
/*
This script is a mockup of the fuctionality to be parallelized in the Monte Carlo
Simulation. It calculates "energy" among pairs of "atoms" in a system, and compares
serial and parallel performance.
The command line arguments are as follows:
first argument (optional) - integer representing number of atoms
- defaults to 100
- input -1 to run benchmarking suite for
10000 <= N <= 40000 and specified thread
block size
second argument (optional) - integer <= 1024 representing thread block size
- input -1 to run benchmarking suite for
64 <= BS <= 1024 and specified N value
For example, -1 512 will run all N with block size = 512
-1 or -1 -1 will run all N for all block sizes
20000 -1 will run N = 20000 for all block sizes
Each simulation adds a line into RunLog.log with data about the run.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//Given two indices in an array (representing atoms),
//calculate their product (potential energy),
//and store in energies array.
//Parallel
__global__ void calcEnergyParallel(int *atoms, int numAtoms, int *energies, int numEnergies)
{
int atom1 = blockIdx.x, atom2 = blockIdx.y * blockDim.x + threadIdx.x,
energyIdx;
if (atom2 < numAtoms && atom2 > atom1)
{
energyIdx = gridDim.x * atom1 + atom2 - (blockIdx.x + 1) * (blockIdx.x + 2) / 2;
energies[energyIdx] = atoms[atom1] * atoms[atom2];
}
}
//Given two indices in an array (representing atoms),
//calculate their product (potential energy),
//and store in energies array.
//Serial
void calcEnergySerial(int *atoms, int numAtoms, int *energies, int numEnergies)
{
int i, j, k;
for (i = 0; i < numAtoms; i++)
{
for (j = 0; j < numAtoms; j++)
{
if (j > i)
{
k = numAtoms * i + j - (i + 1) * (i + 2) / 2;
energies[k] = atoms[i] * atoms[j];
}
}
}
}
void run(int N, int BLOCK_SIZE)
{
printf("Called with %u and %u\n", N, BLOCK_SIZE);
clock_t S_TIME, P_TIME;
int *atomsHost, *atomsDevice, *energiesHost, *energiesDevice, gridYDim = 1, blockXDim = N;
unsigned long int totalEnergy, atomsSize, energiesSize;
atomsSize = N * sizeof(int);
energiesSize = sizeof(int) * N * (N - 1) / 2;
atomsHost = (int*) malloc(atomsSize);
energiesHost = (int*) malloc(energiesSize);
int i;
for (i = 0; i < N; i++)
{
atomsHost[i] = i;
}
for (i = 0; i < energiesSize / sizeof(int); i++)
{
energiesHost[i] = 0;
}
//Serial Run
S_TIME = clock();
calcEnergySerial(atomsHost, N, energiesHost, energiesSize / sizeof(int));
totalEnergy = 0;
for (i = 0; i < energiesSize / sizeof(int); i++)
{
totalEnergy += energiesHost[i];
}
printf("Serial: Total Energy for %u atoms is %u Pseudo-Joules.\n", N, totalEnergy);
S_TIME = clock() - S_TIME;
//Reset energiesHost
for (i = 0; i < energiesSize / sizeof(int); i++)
{
energiesHost[i] = 0;
}
//Parallel Run
P_TIME = clock();
if (N > BLOCK_SIZE)
{
gridYDim = N / BLOCK_SIZE + 1;
blockXDim = BLOCK_SIZE;
}
dim3 gridDim(N, gridYDim, 1);
dim3 blockDim(blockXDim, 1, 1);
cudaMalloc(&atomsDevice, atomsSize);
cudaMalloc(&energiesDevice, energiesSize);
cudaMemcpy(atomsDevice, atomsHost, atomsSize, cudaMemcpyHostToDevice);
cudaMemcpy(energiesDevice, energiesHost, energiesSize, cudaMemcpyHostToDevice);
//N blocks of N threads (every atom pair)
calcEnergyParallel<<<gridDim, blockDim>>>(atomsDevice, N, energiesDevice, energiesSize / sizeof(int));
cudaMemcpy(energiesHost, energiesDevice, energiesSize, cudaMemcpyDeviceToHost);
totalEnergy = 0;
for (i = 0; i < energiesSize / sizeof(int); i++)
{
totalEnergy += energiesHost[i];
}
printf("Parallel: Total Energy for %u atoms is %u Pseudo-Joules.\n", N, totalEnergy);
P_TIME = clock() - P_TIME;
printf("The parallel code runs %fx as fast as the serial version.\n", (float) S_TIME / (float) P_TIME);
free(atomsHost);
free(energiesHost);
cudaFree(atomsDevice);
cudaFree(energiesDevice);
FILE *log = fopen("RunLog.log", "a");
fprintf(log, "%u\t\t%u\t\t%.2f\t\t%.2f\t\t%.2f\n", N, BLOCK_SIZE, (float) S_TIME / CLOCKS_PER_SEC, (float) P_TIME / CLOCKS_PER_SEC, (float) S_TIME / (float) P_TIME);
fclose(log);
}
int main(int argc, char *argv[])
{
int BLOCK_SIZE = 128, N = 100;
if (argc > 1 && atoi(argv[1]) != -1)
{
N = atoi(argv[1]);
if (argc == 3 && atoi(argv[2]) == -1)
{
for (BLOCK_SIZE = 64; BLOCK_SIZE <= 1024; BLOCK_SIZE <<= 1)
{
run(N, BLOCK_SIZE);
}
}
else if (argc == 3)
{
BLOCK_SIZE = atoi(argv[2]);
run(N, BLOCK_SIZE);
}
}
else if (argc > 1 && atoi(argv[1]) == -1)
{
for (N = 10000; N <= 40000; N += 10000)
{
if (argc == 2 && atoi(argv[2]) > 0)
{
run (N, atoi(argv[2]));
}
else
{
for (BLOCK_SIZE = 64; BLOCK_SIZE <= 1024; BLOCK_SIZE <<= 1)
{
run(N, BLOCK_SIZE);
}
}
}
}
else
{
run(N, BLOCK_SIZE);
}
if (N <= 0 || BLOCK_SIZE <= 0)
{
printf("Invalid Parameters for Number of Atoms and GPU Block Size (#threads).\n");
return 1;
}
return 0;
}