-
Notifications
You must be signed in to change notification settings - Fork 0
/
HIP_Particle.cpp
289 lines (197 loc) · 11 KB
/
HIP_Particle.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include "HIP_Particle.h"
#include <hip/hip_runtime.h>
// Custom Class Particle which contains all info about the location and speed of a particle
// all data types 'double' to maximize precision
class Particle{
public:
double velocity[2];
double current_position[2];
double best_position[2];
double best_position_value;
double current_position_value;
// Class Constructor
Particle(){
int n_dimensions = 2;
for(int i = 0; i < n_dimensions; i++){
velocity[i] = (double) rand() / RAND_MAX * 10; // ensures value is bounded by 10
current_position[i] = (double) rand() / RAND_MAX * 10; // ^
best_position[i] = current_position[i]; // best position is initial position during Particle construction/initialization
}
best_position_value = f(best_position[0], best_position[1]);
current_position_value = f(current_position[0], current_position[1]);
// EXPERIMENT B ONLY
// best_position_value = g(best_position[0], best_position[1],best_position[2], best_position[3],
// best_position[4], best_position[5]);
// current_position_value = g(best_position[0], best_position[1],best_position[2], best_position[3],
// best_position[4], best_position[5]);
}
};
// The function we are trying to optimize
// This is a host and device function since we're using it in both the kernel and the host functions
__host__ __device__ double f(double x, double y){
// EXPERIMENT A
return pow(x, 2) + pow(y, 2);
// EXPERIMENT C
// return -1*cos(x)*cos(y)*exp(-(pow((x-M_PI),2) + pow((y-M_PI),2)));
// EXPERIMENT D
//return pow(x, 0.5) + pow(y, 0.5); // pass decimals into pow function, not fractions
// EXPERIMENT E
//return !((x<0) ^ (y<0));
// EXPERIMENT F
// if((x < 0) ^ (y<0)){
// return 0;
// }
// return abs(x)+abs(y);
}
// EXPERIMENT B
__host__ __device__ double g(double a, double b, double c, double d, double e, double f){
return pow(a, 2) + pow(b, 2) + pow(c, 2) + pow(d, 2) + pow(e, 2) + pow(f, 2);
}
// Kernel to update the velocity and position of each dimension of each particle
__global__ void update(Particle all_particles[], double global_position[], double c1, double c2, double w, double r1, double r2, int n_dimensions)
{
int particle_number = threadIdx.x / n_dimensions;
int dimension_number = threadIdx.x % n_dimensions;
all_particles[particle_number].velocity[dimension_number] = w*all_particles[particle_number].velocity[dimension_number]
+ c1*r1*(all_particles[particle_number].best_position[dimension_number]
- all_particles[particle_number].current_position[dimension_number])
+ c2*r2*(global_position[dimension_number]
- all_particles[particle_number].current_position[dimension_number]);
all_particles[particle_number].current_position[dimension_number] = all_particles[particle_number].current_position[dimension_number] + all_particles[particle_number].velocity[dimension_number];
}
// Kernel to calculate the value of the particle given its new position
__global__ void calculate(Particle all_particles[])
{
int particle_number = blockIdx.x;
double new_value = f(all_particles[particle_number].current_position[0], all_particles[particle_number].current_position[1]);
// EXPERIMENT B ONLY
// double new_value = g(all_particles[particle_number].current_position[0], all_particles[particle_number].current_position[1],
// all_particles[particle_number].current_position[2], all_particles[particle_number].current_position[3],
// all_particles[particle_number].current_position[4], all_particles[particle_number].current_position[5]);
all_particles[particle_number].current_position_value = new_value;
}
// Kernel to update the global minimum if the value is a new-found minima
__global__ void compare(Particle all_particles[], double global_position[], double global_position_value[], int n_dimensions)
{
//__shared__ double buffer[2];
//buffer[0] = global_position[0];
//buffer[1] = global_position[1];
__syncthreads();
//int particle_number = threadIdx.x / 2;
//int dimension_number = threadIdx.x % 2;
int particle_number = blockIdx.x;
if (all_particles[particle_number].current_position_value <= all_particles[particle_number].best_position_value){
all_particles[particle_number].best_position_value = all_particles[particle_number].current_position_value;
for(int j = 0; j < n_dimensions; j++){
all_particles[particle_number].best_position[j] = all_particles[particle_number].current_position[j];
}
}
if (all_particles[particle_number].best_position_value <= *global_position_value){
*global_position_value = all_particles[particle_number].best_position_value;
for(int j = 0; j < n_dimensions; j++){
global_position[j] = all_particles[particle_number].best_position[j];
}
}
//global_position[0] = buffer[0];
//global_position[1] = buffer[1];
}
// Shared Memory Version of previous 'compare' kernel.
// This kernel does not get invoked.
__global__ void parallel_compare(Particle all_particles[], double global_position[], double global_position_value[])
{
__shared__ double buffer[2];
buffer[0] = global_position[0];
buffer[1] = global_position[1];
int particle_number = blockIdx.x;
int dimension_number = threadIdx.x;
//int particle_number = threadIdx.x / 2;
//int dimension_number = threadIdx.x % 2;
//int particle_number = blockIdx.x;
if (all_particles[particle_number].current_position_value <= all_particles[particle_number].best_position_value){
all_particles[particle_number].best_position_value = all_particles[particle_number].current_position_value;
all_particles[particle_number].best_position[dimension_number] = all_particles[particle_number].current_position[dimension_number];
}
__syncthreads();
if (all_particles[particle_number].best_position_value <= *global_position_value){
*global_position_value = all_particles[particle_number].best_position_value;
buffer[dimension_number] = all_particles[particle_number].best_position[dimension_number];
}
__syncthreads();
global_position[0] = buffer[0];
global_position[1] = buffer[1];
}
int main(){
// initialize timing variables
hipEvent_t start, stop;
float time;
hipEventCreate(&start);
hipEventCreate(&stop);
// for reproducibility
srand(1);
// Size of the Swarm - Hyperparamter
// We use a size of 100 Particles to emphasize the GPU speedup
int n_particles = 100;
int n_dimensions = 2;
Particle all_particles[n_particles];
//allocate our memory on GPU
Particle *d_particles;
hipMalloc(&d_particles, n_particles * sizeof(Particle));
hipMemcpy(d_particles, all_particles, n_particles * sizeof(Particle), hipMemcpyHostToDevice);
double global_position_value = DBL_MAX;
double global_position[n_dimensions];
// Make sure the current global minimum is the minimum amongst particles
// This is important because the initial global minimum is included in the first velocity update
for(int i = 0; i < n_particles; i++){
if (all_particles[i].best_position_value < global_position_value){
global_position_value = all_particles[i].best_position_value;
for(int j = 0; j < n_dimensions; j++){
global_position[j] = all_particles[i].best_position[j];
}
}
}
//allocate our memory on GPU
double *d_global_position_value;
double *d_global_position;
hipMalloc(&d_global_position_value, 1*sizeof(double));
hipMalloc(&d_global_position, n_dimensions*sizeof(double));
// Initialization of hyperparameters
double c1 = 1.49618;
double c2 = 1.49618;
double w = 0.7298;
double r1 = (double) rand() / RAND_MAX;
double r2 = (double) rand() / RAND_MAX;
int j = 0;
// Start recording
hipEventRecord(start, 0);
// EXPERIMENT A, B, D, E, F
while(global_position_value > 0 && j < 100000){
// EXPERIMENT C
// while(global_position_value != -1){
j++;
// Memory Transfer to Device (GPU)
hipMemcpy(d_global_position_value, &global_position_value, 1*sizeof(double), hipMemcpyHostToDevice);
hipMemcpy(d_global_position, global_position, n_dimensions*sizeof(double), hipMemcpyHostToDevice);
update<<<1, n_dimensions*n_particles>>>(d_particles, d_global_position, c1, c2, w, r1, r2, n_dimensions);
calculate<<<n_particles, 1>>>(d_particles);
compare<<<n_particles, 1>>>(d_particles, d_global_position, d_global_position_value, n_dimensions);
//parallel_compare<<<n_particles, 2>>>(d_particles, d_global_position, d_global_position_value);
// Transfer Memory back from Device (GPU) to Host (Local PC)
hipMemcpy(&global_position_value, d_global_position_value, 1 * sizeof(double), hipMemcpyDeviceToHost);
hipMemcpy(global_position, d_global_position, n_dimensions * sizeof(double), hipMemcpyDeviceToHost);
hipMemcpy(all_particles, d_particles, n_particles * sizeof(Particle), hipMemcpyDeviceToHost);
}
// Stop recording
hipEventRecord(stop, 0);
hipEventSynchronize(stop);
hipEventElapsedTime(&time, start, stop);
std::cout << "Time: "<< time/1000 << " seconds \n";
std::cout << "In " << j << " iterations, PSO Found Best Solution at: " << (double) global_position[0] << ","<< global_position[1] << " which evaluates to " << global_position_value << "\n";
// EXPERIMENT B ONLY
// std::cout << "In " << j << " iterations, PSO Found Best Solution at: " << global_position[0] << ","<< global_position[1] << "," << global_position[2] << ","<< global_position[3] << ","
// << global_position[4] << ","<< global_position[5] << " which evaluates to " << global_position_value << "\n";
return 0;
}