forked from serban/kmeans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_io.cu
183 lines (159 loc) · 6.98 KB
/
cuda_io.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* File: file_io.c */
/* Description: This program reads point data from a file */
/* and write cluster output to files */
/* Input file format: */
/* ascii file: each line contains 1 data object */
/* binary file: first 4-byte integer is the number of data */
/* objects and 2nd integer is the no. of features (or */
/* coordinates) of each object */
/* */
/* Author: Wei-keng Liao */
/* ECE Department Northwestern University */
/* email: wkliao@ece.northwestern.edu */
/* Copyright, 2005, Wei-keng Liao */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strtok() */
#include <sys/types.h> /* open() */
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> /* read(), close() */
#include "kmeans.h"
#define MAX_CHAR_PER_LINE 128
/*---< file_read() >---------------------------------------------------------*/
float** file_read(int isBinaryFile, /* flag: 0 or 1 */
char *filename, /* input file name */
int *numObjs, /* no. data objects (local) */
int *numCoords) /* no. coordinates */
{
float **objects;
int i, j, len;
ssize_t numBytesRead;
if (isBinaryFile) { /* input file is in raw binary format -------------*/
int infile;
if ((infile = open(filename, O_RDONLY, "0600")) == -1) {
fprintf(stderr, "Error: no such file (%s)\n", filename);
return NULL;
}
numBytesRead = read(infile, numObjs, sizeof(int));
assert(numBytesRead == sizeof(int));
numBytesRead = read(infile, numCoords, sizeof(int));
assert(numBytesRead == sizeof(int));
if (_debug) {
printf("File %s numObjs = %d\n",filename,*numObjs);
printf("File %s numCoords = %d\n",filename,*numCoords);
}
/* allocate space for objects[][] and read all objects */
len = (*numObjs) * (*numCoords);
objects = (float**)malloc((*numObjs) * sizeof(float*));
assert(objects != NULL);
objects[0] = (float*) malloc(len * sizeof(float));
assert(objects[0] != NULL);
for (i=1; i<(*numObjs); i++)
objects[i] = objects[i-1] + (*numCoords);
numBytesRead = read(infile, objects[0], len*sizeof(float));
assert(numBytesRead == len*sizeof(float));
close(infile);
}
else { /* input file is in ASCII format -------------------------------*/
FILE *infile;
char *line, *ret;
int lineLen;
if ((infile = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Error: no such file (%s)\n", filename);
return NULL;
}
/* first find the number of objects */
lineLen = MAX_CHAR_PER_LINE;
line = (char*) malloc(lineLen);
assert(line != NULL);
(*numObjs) = 0;
while (fgets(line, lineLen, infile) != NULL) {
/* check each line to find the max line length */
while (strlen(line) == lineLen-1) {
/* this line read is not complete */
len = strlen(line);
fseek(infile, -len, SEEK_CUR);
/* increase lineLen */
lineLen += MAX_CHAR_PER_LINE;
line = (char*) realloc(line, lineLen);
assert(line != NULL);
ret = fgets(line, lineLen, infile);
assert(ret != NULL);
}
if (strtok(line, " \t\n") != 0)
(*numObjs)++;
}
rewind(infile);
if (_debug) printf("lineLen = %d\n",lineLen);
/* find the no. objects of each object */
(*numCoords) = 0;
while (fgets(line, lineLen, infile) != NULL) {
if (strtok(line, " \t\n") != 0) {
/* ignore the id (first coordiinate): numCoords = 1; */
while (strtok(NULL, " ,\t\n") != NULL) (*numCoords)++;
break; /* this makes read from 1st object */
}
}
rewind(infile);
if (_debug) {
printf("File %s numObjs = %d\n",filename,*numObjs);
printf("File %s numCoords = %d\n",filename,*numCoords);
}
/* allocate space for objects[][] and read all objects */
len = (*numObjs) * (*numCoords);
objects = (float**)malloc((*numObjs) * sizeof(float*));
assert(objects != NULL);
objects[0] = (float*) malloc(len * sizeof(float));
assert(objects[0] != NULL);
for (i=1; i<(*numObjs); i++)
objects[i] = objects[i-1] + (*numCoords);
i = 0;
/* read all objects */
while (fgets(line, lineLen, infile) != NULL) {
if (strtok(line, " \t\n") == NULL) continue;
for (j=0; j<(*numCoords); j++)
objects[i][j] = atof(strtok(NULL, " ,\t\n"));
i++;
}
fclose(infile);
free(line);
}
return objects;
}
/*---< file_write() >---------------------------------------------------------*/
int file_write(char *filename, /* input file name */
int numClusters, /* no. clusters */
int numObjs, /* no. data objects */
int numCoords, /* no. coordinates (local) */
float **clusters, /* [numClusters][numCoords] centers */
int *membership) /* [numObjs] */
{
FILE *fptr;
int i, j;
char outFileName[1024];
/* output: the coordinates of the cluster centres ----------------------*/
sprintf(outFileName, "%s.cluster_centres", filename);
printf("Writing coordinates of K=%d cluster centers to file \"%s\"\n",
numClusters, outFileName);
fptr = fopen(outFileName, "w");
for (i=0; i<numClusters; i++) {
fprintf(fptr, "%d ", i);
for (j=0; j<numCoords; j++)
fprintf(fptr, "%f ", clusters[i][j]);
fprintf(fptr, "\n");
}
fclose(fptr);
/* output: the closest cluster centre to each of the data points --------*/
sprintf(outFileName, "%s.membership", filename);
printf("Writing membership of N=%d data objects to file \"%s\"\n",
numObjs, outFileName);
fptr = fopen(outFileName, "w");
for (i=0; i<numObjs; i++)
fprintf(fptr, "%d %d\n", i, membership[i]);
fclose(fptr);
return 1;
}