-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
executable file
·82 lines (54 loc) · 2.2 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
#include "kernel.h"
int main(int argc, char** argv)
{
// Particle
float positions[NUM_OF_PARTICLES * NUM_OF_DIMENSIONS];
float velocities[NUM_OF_PARTICLES * NUM_OF_DIMENSIONS];
float pBests[NUM_OF_PARTICLES * NUM_OF_DIMENSIONS];
// gBest
float gBest[NUM_OF_DIMENSIONS];
srand((unsigned) time(NULL));
// Initialize particles
for (int i = 0; i < NUM_OF_PARTICLES * NUM_OF_DIMENSIONS; i++)
{
positions[i] = getRandom(START_RANGE_MIN, START_RANGE_MAX);
pBests[i] = positions[i];
velocities[i] = 0;
}
for (int k = 0; k < NUM_OF_DIMENSIONS; k++)
gBest[k] = pBests[k];
clock_t begin = clock();
// PSO main function
cuda_pso(positions, velocities, pBests, gBest);
clock_t end = clock();
printf("==================== GPU =======================\n");
printf("Time elapsed : %10.3lf ms\n",
(double)(end - begin) / CLOCKS_PER_SEC);
// gBest berisi nilai minimum
for (int i = 0; i < NUM_OF_DIMENSIONS; i++)
printf("x%d = %f\n", i, gBest[i]);
printf("Minimum = %f\n", host_fitness_function(gBest));
// ======================== END OF GPU ====================== //
// Initialize particles
for (int i = 0; i < NUM_OF_PARTICLES * NUM_OF_DIMENSIONS; i++)
{
positions[i] = getRandom(START_RANGE_MIN, START_RANGE_MAX);
pBests[i] = positions[i];
velocities[i] = 0;
}
for (int k = 0; k < NUM_OF_DIMENSIONS; k++)
gBest[k] = pBests[k];
begin = clock();
// PSO main function
pso(positions, velocities, pBests, gBest);
end = clock();
printf("==================== CPU =======================\n");
printf("Time elapsed : %10.3lf ms\n",
(double)(end - begin) / CLOCKS_PER_SEC);
// gBest berisi nilai minimum
for (int i = 0; i < NUM_OF_DIMENSIONS; i++)
printf("x%d = %f\n", i, gBest[i]);
printf("Minimum = %f\n", host_fitness_function(gBest));
// ======================== END OF GPU ====================== //
return 0;
}