-
Notifications
You must be signed in to change notification settings - Fork 15
/
cuda_fcm.cu
350 lines (315 loc) · 9.97 KB
/
cuda_fcm.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
============================================================================
Name : cuda_fcm.cu
Author : Roman Baygildin
Version :
Copyright : Your copyright notice
Description : CUDA compute reciprocals
============================================================================
*/
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <random>
#include <ctime>
using namespace std;
#undef DEBUG
#define DEBUG
#define CUDA_CALL(F, ...)\
if((F(__VA_ARGS__)) != cudaSuccess){\
cudaError_t e = cudaGetLastError();\
printf("CUDA failure %s:%d: '%s'\n",__FILE__,__LINE__,cudaGetErrorString(e));\
return(EXIT_FAILURE);\
}
#define PRINT_1D_I(A, S)\
printf("[");\
for(int i = 0; i < S; i++){\
printf("%d, ", A[i]);\
}\
printf("]\n");
#define FILL_1D(A, S, V)\
for(int i = 0; i < S; i++){\
A[i] = V;\
}
const int N = 1024;
const int C = 3;
const float M = 2.0f;
const float ERR = 0.001f;
const int MAX_IT = 2;
const int BLOCKDIM = 256;
float* initMu(const int n, const int c){
random_device rd;
default_random_engine eng (rd());
uniform_real_distribution<float> udist(0.0f, 1.0f);
float *mu = new float[n * c];
for(int i = 0; i < n; i++){
float s = 0.0f;
for(int j = 0; j < c; j++){
mu[i * c + j] = udist(eng);
s += mu[i * c + j];
}
for(int j = 0; j < c; j++){
mu[i * c + j] /= s;
}
}
return mu;
}
struct Cluster{
float xMuS;
float muS;
};
__global__ void calcDist(const float* src, const float* v, const int n, const int c, float* dist, int it){
int pos = threadIdx.x + blockIdx.x * blockDim.x;
if(pos < n){
for(int k = 0; k < c; k++){
dist[pos * c + k] = fabsf(src[pos] - v[k]);
}
}
}
#ifndef DEBUG
__global__ void calcMu(const float* src, const float* v, const float* dist, const int n, const int c, const float m, float *mu){
int pos = threadIdx.x + blockIdx.x * blockDim.x;
float p = 2.0f / (m - 1.0f);
if(pos < n){
for(int j = 0; j < c; j++){
const float currentDist = dist[pos * c + j];
float newMu = 0.0f;
for(int k = 0; k < c; k++){
if(j != k){
newMu += powf(currentDist / dist[pos * c + k], p);
}
}
mu[pos * c + j] = 1.0f / newMu;
}
}
}
#else
__global__ void calcMu(const float* src, const float* v, const float* dist, const int n, const int c, const float m, float *mu, int it){
int pos = threadIdx.x + blockIdx.x * blockDim.x;
const float p = 2.0f / (m - 1.0f);
if(pos < n){
for(int j = 0; j < c; j++){
const float currentDist = dist[pos * c + j];
float newMu = 0.0f;
for(int k = 0; k < c; k++){
if(j != k){
newMu += powf(currentDist / dist[pos * c + k], p);
}
}
printf("CALC_MU: it = %d <> NEW_MU[%d, %d] = %f\n", it, pos, j, newMu);
mu[pos * c + j] = powf(newMu, -1.0f);
}
}
}
#endif
#ifdef DEBUG
__global__ void reduceCenters(const float* src, const float* mu, const int n, const int c, const float m, Cluster *reducedV, int it){
extern __shared__ Cluster _clusters[];
int pos = threadIdx.x + blockDim.x * blockIdx.x;
int tid = threadIdx.x;
int bid = blockIdx.x;
for(int k = 0; k < c; k++){
_clusters[tid * c + k] = Cluster();
_clusters[tid * c + k].xMuS = 0.0f;
_clusters[tid * c + k].muS = 0.0f;
}
if(pos < n){
for(int k = 0; k < c; k++){
float muInPower = powf(mu[pos * c + k], m);
_clusters[tid * c + k].xMuS = src[pos] * muInPower;
_clusters[tid * c + k].muS = muInPower;
// printf("it = %d <> bid = %d <> tid = %d <> XMUS[%d] = %f\n", it, k, bid, tid, _clusters[tid * c + k].xMuS);
// printf("it = %d <> bid = %d <> tid = %d <> MUS[%d] = %f\n", it, k, bid, tid, _clusters[tid * c + k].muS);
}
__syncthreads();
for(int k = 0; k < c; k++){
for(unsigned int stride = blockDim.x / 2; stride > 0; stride /= 2){
if(threadIdx.x < stride){
_clusters[tid * c + k].xMuS += _clusters[(tid + stride) * c + k].xMuS;
_clusters[tid * c + k].muS += _clusters[(tid + stride) * c + k].muS;
}
__syncthreads();
}
}
__syncthreads();
if(tid == 0){
for(int k = 0; k < c; k++){
reducedV[bid * c + k].xMuS = _clusters[k].xMuS;
reducedV[bid * c + k].muS = _clusters[k].muS;
}
}
}
}
#else
__global__ void reduceCenters(const float* src, const float* mu, const int n, const int c, const float m, Cluster *reducedV){
extern __shared__ Cluster _clusters[];
int pos = threadIdx.x + blockDim.x * blockIdx.x;
int tid = threadIdx.x;
int bid = blockIdx.x;
for(int k = 0; k < c; k++){
_clusters[tid * c + k] = Cluster();
_clusters[tid * c + k].xMuS = 0.0f;
_clusters[tid * c + k].muS = 0.0f;
}
if(pos < n){
for(int k = 0; k < c; k++){
float muInPower = powf(mu[pos * c + k], m);
_clusters[tid * c + k].xMuS = src[pos] * muInPower;
_clusters[tid * c + k].muS = muInPower;
}
__syncthreads();
for(int k = 0; k < c; k++){
for(unsigned int stride = blockDim.x / 2; stride > 0; stride /= 2){
if(threadIdx.x < stride){
_clusters[tid * c + k].xMuS += _clusters[(tid + stride) * c + k].xMuS;
_clusters[tid * c + k].muS += _clusters[(tid + stride) * c + k].muS;
}
__syncthreads();
}
}
__syncthreads();
if(tid == 0){
for(int k = 0; k < c; k++){
reducedV[bid * c + k].xMuS = _clusters[k].xMuS;
reducedV[bid * c + k].muS = _clusters[k].muS;
}
}
}
}
#endif
#ifdef DEBUG
__global__ void globalReduceCenters(const Cluster *reducedV, const int n, const int c, float *v, int it){
extern __shared__ Cluster _clusters[];
int pos = threadIdx.x + blockDim.x * blockIdx.x;
int tid = threadIdx.x;
if(pos < n){
for(int k = 0; k < c; k++){
_clusters[tid * c + k] = reducedV[pos * c + k];
}
__syncthreads();
for(int k = 0; k < c; k++){
for(unsigned int stride = blockDim.x / 2; stride > 0; stride /= 2){
if(threadIdx.x < stride){
_clusters[tid * c + k].xMuS += _clusters[(tid + stride) * c + k].xMuS;
_clusters[tid * c + k].muS += _clusters[(tid + stride) * c + k].muS;
}
__syncthreads();
}
}
__syncthreads();
if(tid == 0){
for(int k = 0; k < c; k++){
// printf("it = %d <> XMUS[%d] = %f\n", it, k, _clusters[k].xMuS);
// printf("it = %d <> MUS[%d] = %f\n", it, k, _clusters[k].muS);
v[k] = _clusters[k].xMuS / _clusters[k].muS;
// printf("it = %d <> V[%d] = %f\n", it, k, v[k]);
}
}
}
}
#else
__global__ void globalReduceCenters(const Cluster *reducedV, const int n, const int c, float *v){
extern __shared__ Cluster _clusters[];
int pos = threadIdx.x + blockDim.x * blockIdx.x;
int tid = threadIdx.x;
if(pos < n){
for(int k = 0; k < c; k++){
_clusters[tid * c + k] = reducedV[pos * c + k];
}
__syncthreads();
for(int k = 0; k < c; k++){
for(unsigned int stride = blockDim.x / 2; stride > 0; stride /= 2){
if(threadIdx.x < stride){
_clusters[tid * c + k].xMuS += _clusters[(tid + stride) * c + k].xMuS;
_clusters[tid * c + k].muS += _clusters[(tid + stride) * c + k].muS;
}
__syncthreads();
}
}
__syncthreads();
if(tid == 0){
for(int k = 0; k < c; k++){
v[k] = _clusters[k].xMuS / _clusters[k].muS;
}
}
}
}
#endif
__global__ void defuzzLabels(const float* mu, const int n, const int c, int* labels){
int pos = threadIdx.x + blockIdx.x * blockDim.x;
if(pos < n){
int index = 0;
float maxMu = -1.0f;
for(int k = 0; k < c; k++){
if(mu[pos * c + k] > maxMu){
index = k;
maxMu = mu[pos * c + k];
}
}
labels[pos] = index;
}
}
int main(int argc, const char** argv){
//Host variables
float *src = new float[N];
int* labels = new int[N];
float* mu = initMu(N, C);
//Fill src by random values
random_device rd;
default_random_engine eng (rd());
uniform_real_distribution<float> udist(0.0f, 1.0f);
FILL_1D(src, N, udist(eng));
//Num of iterations
int it = 0;
//Device variables
float *src_d = new float[N];
int* labels_d = new int[N];
float *mu_d = new float[N * C];
float *v_d = new float[C];
float *dist_d = new float[N * C];
Cluster *reduced_v = new Cluster[((BLOCKDIM + N - 1) / BLOCKDIM)];
//Allocated device variables
CUDA_CALL(cudaMalloc, (void**) &src_d, sizeof(float) * N);
CUDA_CALL(cudaMalloc, (void**) &labels_d, sizeof(int) * N);
CUDA_CALL(cudaMalloc, (void**) &mu_d, sizeof(float) * N * C);
CUDA_CALL(cudaMalloc, (void**) &v_d, sizeof(float) * C);
CUDA_CALL(cudaMalloc, (void**) &dist_d, sizeof(float) * N * C);
CUDA_CALL(cudaMalloc, (void**) &reduced_v, sizeof(float) * BLOCKDIM * sizeof(struct Cluster) * C)
//Copy from host to device
CUDA_CALL(cudaMemcpy, src_d, src, sizeof(float) * N, cudaMemcpyHostToDevice);
CUDA_CALL(cudaMemcpy, mu_d, mu, sizeof(float) * N * C, cudaMemcpyHostToDevice);
//Size of shared memory in reduction
const int sharedSize1 = BLOCKDIM * sizeof(struct Cluster) * C;
const int sharedSize2 = ((BLOCKDIM + N - 1) / BLOCKDIM) * sizeof(struct Cluster) * C;
//Run Fuzzy C Means
it = 1;
do{
#ifndef DEBUG
reduceCenters<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM, sharedSize1>>>(src_d, mu_d, N, C, M, reduced_v);
globalReduceCenters<<<1, (BLOCKDIM + N - 1) / BLOCKDIM, sharedSize2>>>(reduced_v, N, C, v_d);
calcDist<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM>>>(src_d, mu_d, N, C, dist_d);
calcMu<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM>>>(src_d, v_d, dist_d, N, C, M, mu_d);
#else
reduceCenters<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM, sharedSize1>>>(src_d, mu_d, N, C, M, reduced_v, it);
globalReduceCenters<<<1, (BLOCKDIM + N - 1) / BLOCKDIM, sharedSize2>>>(reduced_v, N, C, v_d, it);
calcDist<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM>>>(src_d, mu_d, N, C, dist_d, it);
calcMu<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM>>>(src_d, v_d, dist_d, N, C, M, mu_d, it);
#endif
}while(++it <= MAX_IT);
//Defuzzification of labels
defuzzLabels<<<(BLOCKDIM + N - 1) / BLOCKDIM, BLOCKDIM>>>(mu_d, N, C, labels_d);
//Copy from device to host
CUDA_CALL(cudaMemcpy, labels, labels_d, sizeof(int) * N, cudaMemcpyDeviceToHost);
PRINT_1D_I(labels, N);
//Clean device memory
CUDA_CALL(cudaFree, src_d);
CUDA_CALL(cudaFree, labels_d);
CUDA_CALL(cudaFree, mu_d);
CUDA_CALL(cudaFree, v_d);
CUDA_CALL(cudaFree, dist_d);
//Clean host memory
delete[] src;
delete[] labels;
delete[] mu;
return 0;
}