Skip to content
Snippets Groups Projects
Commit 4dd07d72 authored by Prakalp Srivastava's avatar Prakalp Srivastava
Browse files

Added verification tool for kmeans

parent 99b540c1
No related branches found
No related tags found
No related merge requests found
include ../../common/make.config
# C compiler
CC = g++
CC_FLAGS = -g -O2
PARBOIL_ROOT = $(LLVM_SRC_ROOT)/test/VISC/parboil
APP = kmeans
kmeans: cluster.o getopt.o read_input.o kmeans_clustering.o rmse.o
$(CC) $(KERNEL_DIM) $(CC_FLAGS) -lOpenCL kmeans.cpp cluster.o getopt.o read_input.o kmeans_clustering.o rmse.o -o kmeans -I$(OPENCL_INC) -I$(OPENCL_DIR)/shared/inc/ -L$(OPENCL_LIB)
# Default compile visc
ifeq ($(VERSION),)
VERSION = opencl
endif
%.o: %.[ch]
$(CC) $(CC_FLAGS) $< -c
# Default use small test case
ifeq ($(TEST),)
TEST = small
endif
cluster.o: cluster.c
$(CC) $(CC_FLAGS) cluster.c -c
ifeq ($(PLATFORM),)
PLATFORM=default
endif
getopt.o: getopt.c
$(CC) $(CC_FLAGS) getopt.c -c
BIN = $(addsuffix -$(VERSION), $(APP))
kmeans.o: kmeans.c
$(CC) $(CC_FLAGS) read_input.c -c
SRCDIR = src/$(VERSION)
BUILDDIR = build/$(VERSION)_$(PLATFORM)
DATASET_DIR = $(PARBOIL_ROOT)/datasets/$(APP)
rmse.o: rmse.c
$(CC) $(CC_FLAGS) rmse.c -c
ifeq ($(TEST),small)
INPUT = $(DATASET_DIR)/input/kdd_cup
REF_OUTPUT = $(DATASET_DIR)/output/kdd_cup.out
RUNDIR = run/$(VERSION)/small
OUTPUT = $(RUNDIR)/kdd_cup.out
endif
clean:
rm -f *.o *~ kmeans *.linkinfo
ARGS = -i $(INPUT) -o $(OUTPUT)
TOOL = tools/compare-output
include $(PARBOIL_ROOT)/common/mk/Makefile
include ../../../../common/Makefile.conf
# C compiler
CC = clang++
CC_FLAGS = -g -O2
OPENCL_INC = $(OPENCL_PATH)/include
# (c) 2010 The Board of Trustees of the University of Illinois.
kmeans: cluster.o getopt.o read_input.o kmeans_clustering.o rmse.o
$(CC) $(KERNEL_DIM) $(CC_FLAGS) -lOpenCL kmeans.cpp cluster.o getopt.o read_input.o kmeans_clustering.o rmse.o -o kmeans -I$(OPENCL_INC) -I$(OPENCL_PATH)/shared/inc/ -L$(OPENCL_LIB_PATH)
%.o: %.[ch]
$(CC) $(CC_FLAGS) $< -c
cluster.o: cluster.c
$(CC) $(CC_FLAGS) cluster.c -c
getopt.o: getopt.c
$(CC) $(CC_FLAGS) getopt.c -c
kmeans.o: kmeans.c
$(CC) $(CC_FLAGS) read_input.c -c
rmse.o: rmse.c
$(CC) $(CC_FLAGS) rmse.c -c
clean:
rm -f *.o *~ kmeans *.linkinfo
LANGUAGE=opencl
SRCDIR_OBJS=kmeans.o cluster.o getopt.o read_input.o kmeans_clustering.o rmse.o #compute_gold.o
APP_CUDALDFLAGS=-lm -lstdc++
APP_CFLAGS= -O2
APP_CXXFLAGS= -O2
......@@ -149,7 +149,7 @@ int allocate(int n_points, int n_features, int n_clusters, float **feature)
}
// read the kernel core source
char * tempchar = "./kmeans.cl";
char * tempchar = "src/opencl/kmeans.cl";
FILE * fp = fopen(tempchar, "rb");
if(!fp) {
printf("ERROR: unable to open '%s'\n", tempchar);
......@@ -267,7 +267,7 @@ int main( int argc, char** argv)
shutdown();
}
int kmeansOCL(float **feature, /* in: [npoints][nfeatures] */
int kmeansOCL(float **feature, /* in: [npoints][nfeatures] */
int n_features,
int n_points,
int n_clusters,
......
......@@ -49,10 +49,16 @@ float euclid_dist_2 (float*, float*, int);
int find_nearest_point (float* , int, float**, int);
float rms_err(float**, int, int, float**, int);
int cluster(int, int, float**, int, int, float, int*, float***, float*, int, int);
int setup(int argc, char** argv);
int allocate(int npoints, int nfeatures, int nclusters, float **feature);
void deallocateMemory();
int kmeansOCL(float **feature, int nfeatures, int npoints, int nclusters, int *membership, float **clusters, int *new_centers_len, float **new_centers);
#ifdef __cplusplus
extern "C" {
#endif
int setup(int argc, char** argv);
int allocate(int npoints, int nfeatures, int nclusters, float **feature);
void deallocateMemory();
int kmeansOCL(float **feature, int nfeatures, int npoints, int nclusters, int *membership, float **clusters, int *new_centers_len, float **new_centers);
#ifdef __cplusplus
}
#endif
float** kmeans_clustering(float **feature, int nfeatures, int npoints, int nclusters, float threshold, int *membership);
#endif
......@@ -105,6 +105,7 @@ int setup(int argc, char **argv) {
int opt;
extern char *optarg;
char *filename = 0;
char* outfilename = 0;
float *buf;
char line[1024];
int isBinaryFile = 0;
......@@ -129,7 +130,7 @@ int setup(int argc, char **argv) {
//float cluster_timing, io_timing;
/* obtain command line arguments and change appropriate options */
while ( (opt=getopt(argc,argv,"i:t:m:n:l:bro"))!= EOF) {
while ( (opt=getopt(argc,argv,"i:t:m:n:l:o:br"))!= EOF) {
switch (opt) {
case 'i':
filename=optarg;
......@@ -151,6 +152,7 @@ int setup(int argc, char **argv) {
break;
case 'o':
isOutput = 1;
outfilename = optarg;
break;
case 'l':
nloops = atoi(optarg);
......@@ -265,15 +267,27 @@ int setup(int argc, char **argv) {
/* cluster center coordinates
:displayed only for when k=1*/
//printf("Input file = %s\n", filename);
//printf("Output file = %s\n", outfilename);
if((min_nclusters == max_nclusters) && (isOutput == 1)) {
printf("\n================= Centroid Coordinates =================\n");
for(i = 0; i < max_nclusters; i++) {
printf("%d:", i);
for(j = 0; j < nfeatures; j++) {
printf(" %.2f", cluster_centres[i][j]);
}
printf("\n\n");
FILE *outfile;
if ((outfile = fopen(outfilename, "w")) == NULL) {
fprintf(stderr, "Error: no such file (%s)\n", outfilename);
exit(1);
}
fwrite(&max_nclusters, sizeof(int), 1, outfile);
fwrite(&nfeatures, sizeof(int), 1, outfile);
fwrite(cluster_centres, sizeof(float), max_nclusters*nfeatures, outfile);
fclose(outfile);
//printf("\n================= Centroid Coordinates =================\n");
//for(i = 0; i < max_nclusters; i++) {
//printf("%d:", i);
//for(j = 0; j < nfeatures; j++) {
//printf(" %.2f", cluster_centres[i][j]);
//}
//printf("\n\n");
//}
}
len = (float) ((max_nclusters - min_nclusters + 1)*nloops);
......
#!/usr/bin/env python
import os
import sys
import struct
tol_diff = 0.001
tol_ratio = 0.002
def Exit(b):
if b:
print "Pass"
sys.exit(0)
else:
print "Mismatch"
sys.exit(1)
def Run():
try:
hx = open(sys.argv[1], 'rb')
hy = open(sys.argv[2], 'rb')
except:
Exit(False)
try:
# size (int)
dx = hx.read(8)
dy = hy.read(8)
lx = struct.unpack("i", dx[0:4])[0]
ly = struct.unpack("i", dy[0:4])[0]
fx = struct.unpack("i", dx[4:8])[0]
fy = struct.unpack("i", dy[4:8])[0]
except:
Exit(False)
data_r = hx.read()
data_c = hy.read()
hx.close()
hy.close()
if lx != ly or fx != fy:
print "Reference and compare are different in size"
Exit(False)
if len(data_r) != 4 * lx * fx:
print "Reference: sanity check failed"
Exit(False)
if len(data_c) != 4 * ly * fy:
print "Compare: sanity check failed"
Exit(False)
for i in range(0, lx*fx*4, 4):
r = struct.unpack('f', data_r[i:i+4])[0]
c = struct.unpack('f', data_c[i:i+4])[0]
diff = abs(r - c)
if not (diff <= tol_diff or diff < tol_ratio * abs(r)):
print r, c, "at" , (i/4)/fx, (i/4)%fx
Exit(False)
Exit(True)
Run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment