-
Notifications
You must be signed in to change notification settings - Fork 3
/
kernel.cu
executable file
·154 lines (120 loc) · 4.93 KB
/
kernel.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
#include <cuda_runtime.h>
#include <cuda.h>
#include <math_functions.h>
#include "kernel.h"
__device__ float tempParticle1[NUM_OF_DIMENSIONS];
__device__ float tempParticle2[NUM_OF_DIMENSIONS];
// Fungsi yang dioptimasi
// Levy 3-dimensional
__device__ float fitness_function(float x[])
{
float res = 0;
float y1 = 1 + (x[0] - 1) / 4;
float yn = 1 + (x[NUM_OF_DIMENSIONS - 1] - 1) / 4;
res += pow(sin(phi * y1), 2);
for (int i = 0; i < NUM_OF_DIMENSIONS - 1; i++)
{
float y = 1 + (x[i] - 1) / 4;
float yp = 1 + (x[i + 1] - 1) / 4;
res += pow(y - 1, 2) * (1 + 10 * pow(sin(phi * yp), 2))
+ pow(yn - 1, 2);
}
return res;
}
__global__ void kernelUpdateParticle(float *positions, float *velocities,
float *pBests, float *gBest, float r1,
float r2)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if(i >= NUM_OF_PARTICLES * NUM_OF_DIMENSIONS)
return;
//float rp = getRandomClamped();
//float rg = getRandomClamped();
float rp = r1;
float rg = r2;
velocities[i] = OMEGA * velocities[i] + c1 * rp * (pBests[i] - positions[i])
+ c2 * rg * (gBest[i % NUM_OF_DIMENSIONS] - positions[i]);
// Update posisi particle
positions[i] += velocities[i];
}
__global__ void kernelUpdatePBest(float *positions, float *pBests, float* gBest)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if(i >= NUM_OF_PARTICLES * NUM_OF_DIMENSIONS || i % NUM_OF_DIMENSIONS != 0)
return;
for (int j = 0; j < NUM_OF_DIMENSIONS; j++)
{
tempParticle1[j] = positions[i + j];
tempParticle2[j] = pBests[i + j];
}
if (fitness_function(tempParticle1) < fitness_function(tempParticle2))
{
for (int k = 0; k < NUM_OF_DIMENSIONS; k++)
pBests[i + k] = positions[i + k];
}
}
extern "C" void cuda_pso(float *positions, float *velocities, float *pBests,
float *gBest)
{
int size = NUM_OF_PARTICLES * NUM_OF_DIMENSIONS;
float *devPos;
float *devVel;
float *devPBest;
float *devGBest;
float temp[NUM_OF_DIMENSIONS];
// Memory allocation
cudaMalloc((void**)&devPos, sizeof(float) * size);
cudaMalloc((void**)&devVel, sizeof(float) * size);
cudaMalloc((void**)&devPBest, sizeof(float) * size);
cudaMalloc((void**)&devGBest, sizeof(float) * NUM_OF_DIMENSIONS);
// Thread & Block number
int threadsNum = 32;
int blocksNum = NUM_OF_PARTICLES / threadsNum;
// Copy particle datas from host to device
cudaMemcpy(devPos, positions, sizeof(float) * size, cudaMemcpyHostToDevice);
cudaMemcpy(devVel, velocities, sizeof(float) * size,
cudaMemcpyHostToDevice);
cudaMemcpy(devPBest, pBests, sizeof(float) * size, cudaMemcpyHostToDevice);
cudaMemcpy(devGBest, gBest, sizeof(float) * NUM_OF_DIMENSIONS,
cudaMemcpyHostToDevice);
// PSO main function
for (int iter = 0; iter < MAX_ITER; iter++)
{
// Update position and velocity
kernelUpdateParticle<<<blocksNum, threadsNum>>>(devPos, devVel,
devPBest, devGBest,
getRandomClamped(),
getRandomClamped());
// Update pBest
kernelUpdatePBest<<<blocksNum, threadsNum>>>(devPos, devPBest,
devGBest);
// Update gBest
cudaMemcpy(pBests, devPBest,
sizeof(float) * NUM_OF_PARTICLES * NUM_OF_DIMENSIONS,
cudaMemcpyDeviceToHost);
for(int i = 0; i < size; i += NUM_OF_DIMENSIONS)
{
for(int k = 0; k < NUM_OF_DIMENSIONS; k++)
temp[k] = pBests[i + k];
if (host_fitness_function(temp) < host_fitness_function(gBest))
{
for (int k = 0; k < NUM_OF_DIMENSIONS; k++)
gBest[k] = temp[k];
}
}
cudaMemcpy(devGBest, gBest, sizeof(float) * NUM_OF_DIMENSIONS,
cudaMemcpyHostToDevice);
}
// Retrieve particle datas from device to host
cudaMemcpy(positions, devPos, sizeof(float) * size, cudaMemcpyDeviceToHost);
cudaMemcpy(velocities, devVel, sizeof(float) * size,
cudaMemcpyDeviceToHost);
cudaMemcpy(pBests, devPBest, sizeof(float) * size, cudaMemcpyDeviceToHost);
cudaMemcpy(gBest, devGBest, sizeof(float) * NUM_OF_DIMENSIONS,
cudaMemcpyDeviceToHost);
// cleanup
cudaFree(devPos);
cudaFree(devVel);
cudaFree(devPBest);
cudaFree(devGBest);
}