-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnappy_cuda.cu
347 lines (287 loc) · 8.39 KB
/
snappy_cuda.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <getopt.h>
#include <string>
#include <iostream>
#include "snappy_cuda.h"
#include "snappy_compress.h"
#include "snappy_decompress.h"
const char options[]="dcb:i:o:x:y:";
/**
* Read the contents of a file into an in-memory buffer. Upon success,
* writes the amount read to input->length.
*
* @param in_file: input file name.
* @param input: holds input buffer information
* @return 1 if file does not exist, is too long, or different number of bytes
* were read than expected, 0 otherwise
*/
static int read_input_host(char *in_file, struct host_buffer_context *input)
{
FILE *fin = fopen(in_file, "r");
if (fin == NULL) {
fprintf(stderr, "Invalid input file: %s\n", in_file);
return 1;
}
fseek(fin, 0, SEEK_END);
input->length = ftell(fin);
fseek(fin, 0, SEEK_SET);
if (input->length > input->max) {
fprintf(stderr, "input_size is too big (%ld > %ld)\n",
input->length, input->max);
return 1;
}
input->buffer = (uint8_t *)malloc(ALIGN_LONG(input->length, 8) * sizeof(*(input->buffer)));
input->curr = input->buffer;
size_t n = fread(input->buffer, sizeof(*(input->buffer)), input->length, fin);
fclose(fin);
#ifdef DEBUG
printf("%s: read %ld bytes from %s (%lu)\n", __func__, input->length, in_file, n);
#endif
return (n != input->length);
}
/**
* Read the contents of a file into an in-memory buffer. Upon success,
* writes the amount read to input->length.
*
* @param in_file: input file name.
* @param input: holds input buffer information
* @return 1 if file does not exist, is too long, or different number of bytes
* were read than expected, 0 otherwise
*/
static int read_input_cuda(char *in_file, struct host_buffer_context *input)
{
FILE *fin = fopen(in_file, "r");
if (fin == NULL) {
fprintf(stderr, "Invalid input file: %s\n", in_file);
return 1;
}
fseek(fin, 0, SEEK_END);
input->length = ftell(fin);
fseek(fin, 0, SEEK_SET);
if (input->length > input->max) {
fprintf(stderr, "input_size is too big (%ld > %ld)\n",
input->length, input->max);
return 1;
}
//input->buffer = (uint8_t *)malloc(ALIGN_LONG(input->length, 8) * sizeof(*(input->buffer)));
input->total_size = ALIGN_LONG(input->length, 8) * sizeof(*(input->buffer));
checkCudaErrors(cudaMallocManaged(&input->buffer,input->total_size));
input->curr = input->buffer;
size_t n = fread(input->buffer, sizeof(*(input->buffer)), input->length, fin);
fclose(fin);
#ifdef DEBUG
printf("%s: read %ld bytes from %s (%lu)\n", __func__, input->length, in_file, n);
#endif
return (n != input->length);
}
/**
* Write the contents of the output buffer to a file.
*
* @param out_file: output filename.
* @param output: holds output buffer information
*/
static void write_output_host(char *out_file, struct host_buffer_context *output)
{
FILE *fout = fopen(out_file, "w");
fwrite(output->buffer, 1, output->length, fout);
fclose(fout);
}
/**
* Print out application usage.
*
* @param exe_name: name of the application
*/
static void usage(const char *exe_name)
{
#ifdef DEBUG
fprintf(stderr, "**DEBUG BUILD**\n");
#endif //DEBUG
fprintf(stderr, "Compress or decompress a file with Snappy\nCan use either the host CPU or CUDA\n");
fprintf(stderr, "usage: %s [-d] [-x <cuda blocks>] [-y <cuda threads per block] [-c] [-b <block_size>] -i <input_file> [-o <output_file>]\n", exe_name);
fprintf(stderr, "d: use CUDA, by default host is used\n");
fprintf(stderr, "x: Grid size - number of blocks (Carefull! no error checks are done)\n");
fprintf(stderr, "y: number of threads per block (Carefull! no error checks are done)\n");
fprintf(stderr, "c: perform compression, by default performs decompression\n");
fprintf(stderr, "b: block size used for compression, default is 32KB, ignored for decompression\n");
fprintf(stderr, "i: input file\n");
fprintf(stderr, "o: output file\n");
}
/**
* Calculate the difference between two timeval structs.
*/
double get_runtime(struct timeval *start, struct timeval *end) {
double start_time = start->tv_sec + start->tv_usec / 1000000.0;
double end_time = end->tv_sec + end->tv_usec / 1000000.0;
return (end_time - start_time);
}
int main(int argc, char **argv)
{
int opt;
snappy_status status;
int use_cuda = 0;
int compress = 0;
int block_size = 32 * 1024; // Default is 32KB
char * input_file = NULL;
char * output_file = NULL;
const char * default_output_file = "output.txt";
struct host_buffer_context *input;
struct host_buffer_context *output;
struct program_runtime runtime;
runtime.blocks = runtime.threads_per_block = 0; //user didn't set values so use defaults which are set later
while ((opt = getopt(argc, argv, options)) != -1)
{
switch(opt)
{
case 'd':
use_cuda = 1;
break;
case 'c':
compress = 1;
break;
case 'b':
block_size = atoi(optarg);
break;
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'x':
runtime.blocks = atoi(optarg);
break;
case 'y':
runtime.threads_per_block = atoi(optarg);
break;
default:
usage(argv[0]);
return -2;
}
}
if(use_cuda)
{
checkCudaErrors(cudaMallocManaged(&input,sizeof(host_buffer_context)));
checkCudaErrors(cudaMallocManaged(&output,sizeof(host_buffer_context)));
}
else
{
input = (host_buffer_context *)malloc(sizeof(host_buffer_context));
output = (host_buffer_context *)malloc(sizeof(host_buffer_context));
}
input->buffer = NULL;
input->length = 0;
input->max = ULONG_MAX;
output->buffer = NULL;
output->length = 0;
output->max = ULONG_MAX;
if (!input_file)
{
usage(argv[0]);
return -1;
}
input->file_name = input_file;
printf("Using input file %s\n", input_file);
// If no output file was provided, use a default file
if (output_file == NULL) {
output_file = (char *)default_output_file;
}
output->file_name = output_file;
printf("Using output file %s\n", output_file);
// Read the input file into main memory
if(use_cuda) {
if (read_input_cuda(input_file, input))
return -1;
input->block_size = block_size;
}
else {
if (read_input_host(input_file, input))
return -1;
}
if (compress) {
if (use_cuda)
{
setup_compression_cuda(input, output, &runtime);
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
status = snappy_compress_cuda(input, output, block_size, &runtime);
gettimeofday(&end, NULL);
runtime.run = get_runtime(&start, &end);
}
else
{
setup_compression(input, output, &runtime);
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
status = snappy_compress_host(input, output, block_size);
gettimeofday(&end, NULL);
runtime.run = get_runtime(&start, &end);
}
}
else {
if (use_cuda)
{
if (setup_decompression_cuda(input, output, &runtime))
return -1;
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
status = snappy_decompress_cuda(input, output, &runtime);
gettimeofday(&end, NULL);
runtime.run = get_runtime(&start, &end);
}
else
{
if (setup_decompression(input, output, &runtime))
return -1;
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
status = snappy_decompress_host(input, output);
gettimeofday(&end, NULL);
runtime.run = get_runtime(&start, &end);
}
}
if (status == SNAPPY_OK)
{
// Write the output buffer from main memory to a file
//if (!(compress && use_cuda))
write_output_host(output_file, output);
if (compress) {
printf("Compressed %ld bytes to: %s\n", output->length, output_file);
printf("Compression ratio: %f\n", 1 - (double)output->length / (double)input->length);
}
else {
printf("Decompressed %ld bytes to: %s\n", output->length, output_file);
printf("Compression ratio: %f\n", 1 - (double)input->length / (double)output->length);
}
printf("Pre-processing time: %f\n", runtime.pre);
printf("Alloc time: %f\n", runtime.d_alloc);
printf("Load time: %f\n", runtime.load);
printf("Copy in time: %f\n", runtime.copy_in);
printf("Elapsed time: %f\n", runtime.run);
printf("Copy out time: %f\n", runtime.copy_out);
printf("Free time: %f\n", runtime.d_free);
}
else
{
fprintf(stderr, "Encountered Snappy error %u\n", status);
return -1;
}
if(use_cuda)
{
checkCudaErrors(cudaFree(input));
checkCudaErrors(cudaFree(output));
}
else
{
free(input);
free(output);
}
return 0;
}