-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans.h
More file actions
69 lines (58 loc) · 2.48 KB
/
kmeans.h
File metadata and controls
69 lines (58 loc) · 2.48 KB
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
#ifndef KMEANS_HEADER
#define KMEANS_HEADER
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_profiler_api.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define optimLevel 1
/* To index element (i,j) of a 2D array stored as 1D */
#define index(i, j, N) ((i) * (N)) + (j)
#define gpuErrchk(ans) \
{ gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line,
bool abort = true) {
if (code != cudaSuccess) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file,
line);
if (abort) exit(code);
}
}
enum InitMethod { InitMethodRandom = 0, InitMethodPlusPlus, InitMethodImport };
typedef enum InitMethod Init_Method;
__host__ __device__ int copy_vectors(float *dst_vec, float *src_vec,
const uint32_t n_dim,
const uint32_t dst_skip,
const uint32_t src_skip);
float **file_read(int, char *, uint32_t *, uint32_t *);
int file_write(char *, uint32_t, uint32_t, uint32_t, float **, uint32_t *);
float *transpose(float *src, uint32_t n_samples, uint32_t n_features,
int pinned_result = 0);
void print1d(uint32_t *src, uint32_t dim);
void print2d(float *src, uint32_t dim1, uint32_t dim2);
/// @brief Performs K-means clustering on GPU / CUDA.
/// @param init centroids initialization method.
/// @param tolerance if the number of reassignments drop below this ratio, stop.
/// @param num_samples number of samples.
/// @param num_features number of features.
/// @param num_clusters number of clusters.
/// @param seed random generator seed passed to srand().
/// @param samples input array of sample points
/// @param centroids output array of centroids
/// @param memberships array of cluster indices
/// @return cudaError_t
//
// Assume samples,centroids,memberships to be already initialized arrays in
// device
cudaError_t kmeans_cuda(InitMethod init, float tolerance, uint32_t n_samples,
uint32_t n_features, uint32_t n_clusters, uint32_t seed,
float *samples, float *centroids, uint32_t *memberships,
int *iterations);
int init_centroids(InitMethod method, uint32_t n_samples, uint32_t n_features,
uint32_t n_clusters, uint32_t seed, float *h_samples,
float *h_centroids);
extern int _debug;
#endif