Newer
Older
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
#ifndef _KMEANS_CUDA_KERNEL_H_
#define _KMEANS_CUDA_KERNEL_H_
#include <stdio.h>
#include <cuda.h>
#include "kmeans.h"
// FIXME: Make this a runtime selectable variable!
#define ASSUMED_NR_CLUSTERS 32
#define SDATA( index) CUT_BANK_CHECKER(sdata, index)
// t_features has the layout dim0[points 0-m-1]dim1[ points 0-m-1]...
texture<float, 1, cudaReadModeElementType> t_features;
// t_features_flipped has the layout point0[dim 0-n-1]point1[dim 0-n-1]
texture<float, 1, cudaReadModeElementType> t_features_flipped;
texture<float, 1, cudaReadModeElementType> t_clusters;
__constant__ float c_clusters[ASSUMED_NR_CLUSTERS*34]; /* constant memory for cluster centers */
/* ----------------- invert_mapping() --------------------- */
/* inverts data array from row-major to column-major.
[p0,dim0][p0,dim1][p0,dim2] ...
[p1,dim0][p1,dim1][p1,dim2] ...
[p2,dim0][p2,dim1][p2,dim2] ...
to
[dim0,p0][dim0,p1][dim0,p2] ...
[dim1,p0][dim1,p1][dim1,p2] ...
[dim2,p0][dim2,p1][dim2,p2] ...
*/
__global__ void invert_mapping(float *input, /* original */
float *output, /* inverted */
int npoints, /* npoints */
int nfeatures) /* nfeatures */
{
int point_id = threadIdx.x + blockDim.x*blockIdx.x; /* id of thread */
int i;
if(point_id < npoints){
for(i=0;i<nfeatures;i++)
output[point_id + npoints*i] = input[point_id*nfeatures + i];
}
return;
}
/* ----------------- invert_mapping() end --------------------- */
/* to turn on the GPU delta and center reduction */
//#define GPU_DELTA_REDUCTION
//#define GPU_NEW_CENTER_REDUCTION
/* ----------------- kmeansPoint() --------------------- */
/* find the index of nearest cluster centers and change membership*/
__global__ void
kmeansPoint(float *features, /* in: [npoints*nfeatures] */
int nfeatures,
int npoints,
int nclusters,
int *membership,
float *clusters,
float *block_clusters,
int *block_deltas)
{
// block ID
const unsigned int block_id = gridDim.x*blockIdx.y+blockIdx.x;
// point/thread ID
const unsigned int point_id = block_id*blockDim.x*blockDim.y + threadIdx.x;
int index = -1;
if (point_id < npoints)
{
int i, j;
float min_dist = FLT_MAX;
float dist; /* distance square between a point to cluster center */
/* find the cluster center id with min distance to pt */
for (i=0; i<nclusters; i++) {
int cluster_base_index = i*nfeatures; /* base index of cluster centers for inverted array */
float ans=0.0; /* Euclidean distance sqaure */
for (j=0; j < nfeatures; j++)
{
int addr = point_id + j*npoints; /* appropriate index of data point */
float diff = (tex1Dfetch(t_features,addr) -
c_clusters[cluster_base_index + j]); /* distance between a data point to cluster centers */
ans += diff*diff; /* sum of squares */
}
dist = ans;
/* see if distance is smaller than previous ones:
if so, change minimum distance and save index of cluster center */
if (dist < min_dist) {
min_dist = dist;
index = i;
}
}
}
#ifdef GPU_DELTA_REDUCTION
// count how many points are now closer to a different cluster center
__shared__ int deltas[THREADS_PER_BLOCK];
if(threadIdx.x < THREADS_PER_BLOCK) {
deltas[threadIdx.x] = 0;
}
#endif
if (point_id < npoints)
{
#ifdef GPU_DELTA_REDUCTION
/* if membership changes, increase delta by 1 */
if (membership[point_id] != index) {
deltas[threadIdx.x] = 1;
}
#endif
/* assign the membership to object point_id */
membership[point_id] = index;
}
#ifdef GPU_DELTA_REDUCTION
// make sure all the deltas have finished writing to shared memory
__syncthreads();
// now let's count them
// primitve reduction follows
unsigned int threadids_participating = THREADS_PER_BLOCK / 2;
for(;threadids_participating > 1; threadids_participating /= 2) {
if(threadIdx.x < threadids_participating) {
deltas[threadIdx.x] += deltas[threadIdx.x + threadids_participating];
}
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
}
if(threadIdx.x < 1) {deltas[threadIdx.x] += deltas[threadIdx.x + 1];}
__syncthreads();
// propagate number of changes to global counter
if(threadIdx.x == 0) {
block_deltas[blockIdx.y * gridDim.x + blockIdx.x] = deltas[0];
//printf("original id: %d, modified: %d\n", blockIdx.y*gridDim.x+blockIdx.x, blockIdx.x);
}
#endif
#ifdef GPU_NEW_CENTER_REDUCTION
int center_id = threadIdx.x / nfeatures;
int dim_id = threadIdx.x - nfeatures*center_id;
__shared__ int new_center_ids[THREADS_PER_BLOCK];
new_center_ids[threadIdx.x] = index;
__syncthreads();
/***
determine which dimension calculte the sum for
mapping of threads is
center0[dim0,dim1,dim2,...]center1[dim0,dim1,dim2,...]...
***/
int new_base_index = (point_id - threadIdx.x)*nfeatures + dim_id;
float accumulator = 0.f;
if(threadIdx.x < nfeatures * nclusters) {
// accumulate over all the elements of this threadblock
for(int i = 0; i< (THREADS_PER_BLOCK); i++) {
float val = tex1Dfetch(t_features_flipped,new_base_index+i*nfeatures);
if(new_center_ids[i] == center_id)
accumulator += val;
}
// now store the sum for this threadblock
/***
mapping to global array is
block0[center0[dim0,dim1,dim2,...]center1[dim0,dim1,dim2,...]...]block1[...]...
***/
block_clusters[(blockIdx.y*gridDim.x + blockIdx.x) * nclusters * nfeatures + threadIdx.x] = accumulator;
}
#endif
}
#endif // #ifndef _KMEANS_CUDA_KERNEL_H_