-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIMDBDemoCUDABits.cu
224 lines (173 loc) · 6.17 KB
/
IMDBDemoCUDABits.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
/*
Copyright (c) 2019 Ole-Christoffer Granmo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This code implements a multiclass version of the Tsetlin Machine from paper arXiv:1804.01508
https://arxiv.org/abs/1804.01508
*/
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <cuda.h>
#include <curand.h>
#include <curand_kernel.h>
#include "TsetlinMachineConfig.cuh"
#include "MultiClassTsetlinMachine.cuh"
#include "GPUConfig.cuh"
#define NUMBER_OF_TRAINING_EXAMPLES 25000
#define NUMBER_OF_TEST_EXAMPLES 25000
#define EXPERIMENTS 100
#define EPOCHS 200
#define DEVICE 0
int y_train[NUMBER_OF_TRAINING_EXAMPLES], y_test[NUMBER_OF_TEST_EXAMPLES];
int *X_train;
int *X_test;
void read_file(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
const char *s = " ";
char *token = NULL;
// Training Dataset
for (int i = 0; i < NUMBER_OF_TRAINING_EXAMPLES; i++) {
for (int j = 0; j < LA_CHUNKS; j++) {
X_train[i*LA_CHUNKS + j] = 0;
}
}
fp = fopen("IMDBTrainingData.txt", "r");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUMBER_OF_TRAINING_EXAMPLES; i++) {
getline(&line, &len, fp);
token = strtok(line, s);
for (int j = 0; j < FEATURES; j++) {
if (atoi(token) == 1) {
int chunk_nr = j / INT_SIZE;
int chunk_pos = j % INT_SIZE;
X_train[i*LA_CHUNKS + chunk_nr] |= (1 << chunk_pos);
} else {
int chunk_nr = (j + FEATURES) / INT_SIZE;
int chunk_pos = (j + FEATURES) % INT_SIZE;
X_train[i*LA_CHUNKS + chunk_nr] |= (1 << chunk_pos);
}
token=strtok(NULL,s);
}
y_train[i] = atoi(token);
}
fclose(fp);
// Test Dataset
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
for (int j = 0; j < LA_CHUNKS; j++) {
X_test[i*LA_CHUNKS + j] = 0;
}
}
fp = fopen("IMDBTestData.txt", "r");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
getline(&line, &len, fp);
token = strtok(line, s);
for (int j = 0; j < FEATURES; j++) {
if (atoi(token) == 1) {
int chunk_nr = j / INT_SIZE;
int chunk_pos = j % INT_SIZE;
X_test[i*LA_CHUNKS + chunk_nr] |= (1 << chunk_pos);
} else {
int chunk_nr = (j + FEATURES) / INT_SIZE;
int chunk_pos = (j + FEATURES) % INT_SIZE;
X_test[i*LA_CHUNKS + chunk_nr] |= (1 << chunk_pos);
}
token=strtok(NULL,s);
}
y_test[i] = atoi(token);
}
fclose(fp);
}
__global__ void setup_kernel(curandState *state)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
/* Each thread gets same seed, a different sequence
number, no offset */
curand_init(1234, id, 0, &state[id]);
}
int main(void)
{
FILE *fp;
curandState *devStates;
cudaSetDevice(DEVICE);
int numSMs;
cudaDeviceGetAttribute(&numSMs, cudaDevAttrMultiProcessorCount, DEVICE);
printf("Num SMS: %d\n", numSMs);
// Allocate Unified Memory – accessible from CPU or GPU
cudaMallocManaged(&X_train, NUMBER_OF_TRAINING_EXAMPLES * LA_CHUNKS * sizeof(int));
cudaMallocManaged(&X_test, NUMBER_OF_TEST_EXAMPLES * LA_CHUNKS * sizeof(int));
read_file();
cudaMallocManaged((void **)&devStates, GRID_SIZE * BLOCK_SIZE *
sizeof(curandState));
setup_kernel<<<GRID_SIZE,BLOCK_SIZE>>>(devStates);
cudaDeviceSynchronize();
MultiClassTsetlinMachine<2> mc_tm;
fp = fopen("./statistics.txt","w");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int e = 0; e < EXPERIMENTS; ++e) {
printf("\nEXPERIMENT %d\n", e+1);
mc_tm.initialize();
for (int i = 0; i < EPOCHS; ++i) {
printf("\n##### EPOCH %d #####\n", i+1);
clock_t start, end;
double gpu_time_testing, gpu_time_training;
start = clock();
mc_tm.fit(devStates, X_train, y_train, NUMBER_OF_TRAINING_EXAMPLES, S, 1);
end = clock();
gpu_time_training = ((double) (end - start)) / CLOCKS_PER_SEC;
start = clock();
mc_tm.evaluate(X_test, y_test, NUMBER_OF_TEST_EXAMPLES);
end = clock();
gpu_time_testing = ((double) (end - start)) / CLOCKS_PER_SEC;
for (int n = 0; n < 2; ++n) {
printf("\n-- CLASS %d --\n\n", n+1);
float precision = 1.0 * mc_tm.true_positive[n] / (mc_tm.true_positive[n] + mc_tm.false_positive[n]);
printf("PRECISION: %.3f\n", precision);
float recall = 1.0 * mc_tm.true_positive[n] / (mc_tm.true_positive[n] + mc_tm.false_negative[n]);
printf("RECALL: %.3f\n", recall);
float fscore = 2 * precision * recall / (precision + recall);
printf("F-SCORE: %.3f\n", fscore);
fprintf(fp, "%d %d %d %d %d %d %d %.4f %.4f %.4f %f %f\n", e, i, n, mc_tm.true_positive[n], mc_tm.false_positive[n],
mc_tm.true_negative[n], mc_tm.false_negative[n], precision, recall, fscore, gpu_time_training, gpu_time_testing);
fflush(fp);
}
printf("\n");
printf("TRAINING TIME: %f\n", gpu_time_training);
printf("TESTING TIME: %f\n", gpu_time_testing);
}
}
fclose(fp);
delete &mc_tm;
cudaFree(devStates);
cudaFree(X_train);
cudaFree(X_test);
return 0;
}