-
Notifications
You must be signed in to change notification settings - Fork 7
/
ferret-ff-pipeoffarms.cpp
513 lines (399 loc) · 10.2 KB
/
ferret-ff-pipeoffarms.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* AUTORIGHTS
Copyright (C) 2007 Princeton University
This file is part of Ferret Toolkit.
Ferret Toolkit is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
FastFlow version by Daniele De Sensi (d.desensi.software@gmail.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pthread.h>
#include <cass.h>
#include <cass_timer.h>
#include <../image/image.h>
#include "tpool.h"
#define NO_DEFAULT_MAPPING
#define ENABLE_FF_ONDEMAND
#include <ff/farm.hpp>
#include <ff/pipeline.hpp>
#include <iostream>
#ifdef ENABLE_PARSEC_HOOKS
#include <hooks.h>
#endif
#define DEFAULT_DEPTH 25
#define MAXR 100
#define IMAGE_DIM 14
const char *db_dir = NULL;
const char *table_name = NULL;
const char *query_dir = NULL;
const char *output_path = NULL;
size_t nthreads;
FILE *fout;
int DEPTH = DEFAULT_DEPTH;
int top_K = 10;
char *extra_params = "-L 8 - T 20";
cass_env_t *env;
cass_table_t *table;
cass_table_t *query_table;
int vec_dist_id = 0;
int vecset_dist_id = 0;
struct load_data
{
int width, height;
char *name;
unsigned char *HSV, *RGB;
};
struct seg_data
{
int width, height, nrgn;
char *name;
unsigned char *mask;
unsigned char *HSV;
};
struct extract_data
{
cass_dataset_t ds;
char *name;
};
struct vec_query_data
{
char *name;
cass_dataset_t *ds;
cass_result_t result;
};
struct rank_data
{
char *name;
cass_dataset_t *ds;
cass_result_t result;
};
/* ------- The Helper Functions ------- */
int cnt_enqueue;
int cnt_dequeue;
char path[BUFSIZ];
/* ------ The Stages ------ */
class Load: public ff::ff_node{
private:
int scan_dir (const char *, char *head);
int dir_helper (char *dir, char *head)
{
DIR *pd = NULL;
struct dirent *ent = NULL;
int result = 0;
pd = opendir(dir);
if (pd == NULL) goto except;
for (;;)
{
ent = readdir(pd);
if (ent == NULL) break;
if (scan_dir(ent->d_name, head) != 0) return -1;
}
goto final;
except:
result = -1;
perror("Error:");
final:
if (pd != NULL) closedir(pd);
return result;
}
/* the whole path to the file */
int file_helper (const char *file)
{
int r;
struct load_data *data;
data = (struct load_data *)malloc(sizeof(struct load_data));
assert(data != NULL);
data->name = strdup(file);
r = image_read_rgb_hsv(file, &data->width, &data->height, &data->RGB, &data->HSV);
assert(r == 0);
/*
r = image_read_rgb(file, &data->width, &data->height, &data->RGB);
r = image_read_hsv(file, &data->width, &data->height, &data->HSV);
*/
cnt_enqueue++;
ff_send_out((void*) data);
return 0;
}
public:
Load(){
path[0] = 0;
}
void* svc(void* task){
if (strcmp(query_dir, ".") == 0){
dir_helper(".", path);
}else{
scan_dir(query_dir, path);
}
return EOS;
}
};
int Load::scan_dir (const char *dir, char *head)
{
struct stat st;
int ret;
/* test for . and .. */
if (dir[0] == '.')
{
if (dir[1] == 0) return 0;
else if (dir[1] == '.')
{
if (dir[2] == 0) return 0;
}
}
/* append the name to the path */
strcat(head, dir);
ret = stat(path, &st);
if (ret != 0)
{
perror("Error:");
return -1;
}
if (S_ISREG(st.st_mode)) file_helper(path);
else if (S_ISDIR(st.st_mode))
{
strcat(head, "/");
dir_helper(path, head + strlen(head));
}
/* removed the appended part */
head[0] = 0;
return 0;
}
class Seg: public ff::ff_node{
private:
struct seg_data *seg;
struct load_data *load;
public:
void* svc(void* task){
load = (struct load_data*) task;
assert(load != NULL);
seg = (struct seg_data *)calloc(1, sizeof(struct seg_data));
seg->name = load->name;
seg->width = load->width;
seg->height = load->height;
seg->HSV = load->HSV;
image_segment(&seg->mask, &seg->nrgn, load->RGB, load->width, load->height);
free(load->RGB);
free(load);
return (void*) seg;
}
};
class Extract: public ff::ff_node{
private:
struct seg_data *seg;
struct extract_data *extract;
public:
void* svc(void* task){
seg = (struct seg_data*) task;
assert(seg != NULL);
extract = (struct extract_data *)calloc(1, sizeof(struct extract_data));
extract->name = seg->name;
image_extract_helper(seg->HSV, seg->mask, seg->width, seg->height, seg->nrgn, &extract->ds);
free(seg->mask);
free(seg->HSV);
free(seg);
return (void*) extract;
}
};
class Vec: public ff::ff_node{
private:
struct extract_data *extract;
struct vec_query_data *vec;
cass_query_t query;
public:
void* svc(void* task){
extract = (struct extract_data*) task;
assert(extract != NULL);
vec = (struct vec_query_data *)calloc(1, sizeof(struct vec_query_data));
vec->name = extract->name;
memset(&query, 0, sizeof query);
query.flags = CASS_RESULT_LISTS | CASS_RESULT_USERMEM;
vec->ds = query.dataset = &extract->ds;
query.vecset_id = 0;
query.vec_dist_id = vec_dist_id;
query.vecset_dist_id = vecset_dist_id;
query.topk = 2*top_K;
query.extra_params = extra_params;
cass_result_alloc_list(&vec->result, vec->ds->vecset[0].num_regions, query.topk);
// cass_result_alloc_list(&result_m, 0, T1);
// cass_table_query(table, &query, &vec->result);
cass_table_query(table, &query, &vec->result);
return (void*) vec;
}
};
class Rank: public ff::ff_node{
private:
struct vec_query_data *vec;
struct rank_data *rank;
cass_result_t *candidate;
cass_query_t query;
public:
void* svc(void* task){
vec = (struct vec_query_data*) task;
assert(vec != NULL);
rank = (struct rank_data *)calloc(1, sizeof(struct rank_data));
rank->name = vec->name;
query.flags = CASS_RESULT_LIST | CASS_RESULT_USERMEM | CASS_RESULT_SORT;
query.dataset = vec->ds;
query.vecset_id = 0;
query.vec_dist_id = vec_dist_id;
query.vecset_dist_id = vecset_dist_id;
query.topk = top_K;
query.extra_params = NULL;
candidate = cass_result_merge_lists(&vec->result, (cass_dataset_t *)query_table->__private, 0);
query.candidate = candidate;
cass_result_alloc_list(&rank->result, 0, top_K);
cass_table_query(query_table, &query, &rank->result);
cass_result_free(&vec->result);
cass_result_free(candidate);
free(candidate);
cass_dataset_release(vec->ds);
free(vec->ds);
free(vec);
return (void*) rank;
}
};
class Out: public ff::ff_node{
private:
struct rank_data *rank;
public:
void* svc(void* task){
rank = (struct rank_data*) task;
assert(rank != NULL);
fprintf(fout, "%s", rank->name);
ARRAY_BEGIN_FOREACH(rank->result.u.list, cass_list_entry_t p)
{
char *obj = NULL;
if (p.dist == HUGE) continue;
cass_map_id_to_dataobj(query_table->map, p.id, &obj);
assert(obj != NULL);
fprintf(fout, "\t%s:%g", obj, p.dist);
} ARRAY_END_FOREACH;
fprintf(fout, "\n");
cass_result_free(&rank->result);
free(rank->name);
free(rank);
cnt_dequeue++;
fprintf(stderr, "(%d,%d)\n", cnt_enqueue, cnt_dequeue);
return GO_ON;
}
};
int main (int argc, char *argv[])
{
stimer_t tmr;
int ret, i;
#ifdef PARSEC_VERSION
#define __PARSEC_STRING(x) #x
#define __PARSEC_XSTRING(x) __PARSEC_STRING(x)
printf("PARSEC Benchmark Suite Version " __PARSEC_XSTRING(PARSEC_VERSION) "\n");
fflush(NULL);
#else
printf("PARSEC Benchmark Suite\n");
fflush(NULL);
#endif //PARSEC_VERSION
#ifdef ENABLE_PARSEC_HOOKS
__parsec_bench_begin(__parsec_ferret);
#endif
if (argc < 8)
{
printf("%s <database> <table> <query dir> <top K> <depth> <n> <out>\n", argv[0]);
return 0;
}
db_dir = argv[1];
table_name = argv[2];
query_dir = argv[3];
top_K = atoi(argv[4]);
DEPTH = atoi(argv[5]);
nthreads = atoi(argv[6]);
output_path = argv[7];
fout = fopen(output_path, "w");
assert(fout != NULL);
cass_init();
ret = cass_env_open(&env, db_dir, 0);
if (ret != 0) { printf("ERROR: %s\n", cass_strerror(ret)); return 0; }
vec_dist_id = cass_reg_lookup(&env->vec_dist, "L2_float");
assert(vec_dist_id >= 0);
vecset_dist_id = cass_reg_lookup(&env->vecset_dist, "emd");
assert(vecset_dist_id >= 0);
i = cass_reg_lookup(&env->table, table_name);
table = query_table = cass_reg_get(&env->table, i);
i = table->parent_id;
if (i >= 0)
{
query_table = cass_reg_get(&env->table, i);
}
if (query_table != table) cass_table_load(query_table);
cass_map_load(query_table->map);
cass_table_load(table);
image_init(argv[0]);
stimer_tick(&tmr);
cnt_enqueue = cnt_dequeue = 0;
#ifdef ENABLE_PARSEC_HOOKS
__parsec_roi_begin();
#endif
ff::ff_pipeline p;
ff::ff_farm<> segFarm, extFarm, vecFarm, rankFarm;
std::vector<ff::ff_node*> segW, extW, vecW, rankW;
segFarm.cleanup_all();
extFarm.cleanup_all();
vecFarm.cleanup_all();
rankFarm.cleanup_all();
for(size_t i = 0; i < nthreads; i++){
segW.push_back(new Seg());
extW.push_back(new Extract());
vecW.push_back(new Vec());
rankW.push_back(new Rank());
}
segFarm.add_emitter(new Load());
segFarm.add_workers(segW);
extFarm.add_workers(extW);
vecFarm.add_workers(vecW);
rankFarm.add_workers(rankW);
segFarm.remove_collector();
extFarm.setMultiInput();
extFarm.remove_collector();
vecFarm.setMultiInput();
vecFarm.remove_collector();
rankFarm.setMultiInput();
rankFarm.add_collector(new Out());
#ifdef ENABLE_FF_ONDEMAND
segFarm.set_scheduling_ondemand();
extFarm.set_scheduling_ondemand();
vecFarm.set_scheduling_ondemand();
rankFarm.set_scheduling_ondemand();
#endif
p.add_stage(&segFarm);
p.add_stage(&extFarm);
p.add_stage(&vecFarm);
p.add_stage(&rankFarm);
p.run_and_wait_end();
assert(cnt_enqueue == cnt_dequeue);
#ifdef ENABLE_PARSEC_HOOKS
__parsec_roi_end();
#endif
stimer_tuck(&tmr, "QUERY TIME");
ret = cass_env_close(env, 0);
if (ret != 0) { printf("ERROR: %s\n", cass_strerror(ret)); return 0; }
cass_cleanup();
image_cleanup();
fclose(fout);
#ifdef ENABLE_PARSEC_HOOKS
__parsec_bench_end();
#endif
return 0;
}