/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
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
513
514
515
516
517
518
519
520
521
522
523
#include <stdio.h>
#include <stdlib.h>

#ifdef DICT_HW_MEASURE_TIMINGS
# include <sys/time.h>
#endif /* DICT_HW_MEASURE_TIMINGS */

#include "dict_hw.h"

#include "dict_image.h"

#include "helpers.h"

#include "local_sum.cu.h"

#include "normxcorr_hw.h"
#include "normxcorr_hw.cu.h"

#define MAX_DEVICES 16

static int device_number = 0;
static int devices[MAX_DEVICES];
static size_t device_memory = 2147483648;

#ifdef DICT_HW_MEASURE_TIMINGS
static int hardware_detection_time = 0;
#endif /* DICT_HW_MEASURE_TIMINGS */

int dictDetectHardware() {
    int memory;
    int deviceCount;
    cudaDeviceProp deviceProp;

#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */


    cudaGetDeviceCount(&deviceCount);
    if (!deviceCount) return -1;

    for (int i = 0; i < deviceCount; i++) {
        cudaGetDeviceProperties(&deviceProp, i);
        if ((deviceProp.major > 1)||((deviceProp.major == 1)&&(deviceProp.minor > 2))) {
	    memory = deviceProp.totalGlobalMem;
	    if (memory > 268435455) {	// 256 MB
		if (memory < device_memory) device_memory = memory;
    		devices[device_number++] = i;
	    }
        }
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    hardware_detection_time = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    return device_number;
}

DICTContext dictCreateContext() {
#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
#endif /* DICT_HW_MEASURE_TIMINGS */

    if (!device_number) {
        if (dictDetectHardware() <= 0) return NULL;
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    TProcessingState *pstate = pstateInit();

#ifdef DICT_HW_MEASURE_TIMINGS
    if (pstate) {
	gettimeofday(&tv2, NULL);
	pstate->time[0] = hardware_detection_time;
	pstate->time[1] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
    }
#endif /* DICT_HW_MEASURE_TIMINGS */

    return pstate;
}

void dictDestroyContext(DICTContext ctx) {
#ifdef DICT_HW_MEASURE_TIMINGS
    int time;
    struct timeval tv1, tv2;

    int init_time = ctx->time[0]+ctx->time[1]+ctx->time[2]+ctx->time[3]+ctx->time[12]+ctx->time[13];
    int load_time = ctx->time[14] + ctx->time[15];
    int process_time = ctx->time[5];
    
    if (ctx->use_cache) init_time += ctx->time[6];
    
    reportMessage("");
    reportMessage("Library timings");
    reportMessage("---------------");
    print_timing("Overall", init_time + load_time + process_time + ctx->time[16]);
    print_timing("Initialization", init_time);
    print_timing("  Hardware Detection", ctx->time[0]);
    print_timing("  Context Initialization", ctx->time[1]);
    print_timing("  Setting Template Points", ctx->time[2]);
    print_timing("  Setting Current Points", ctx->time[3]);
    print_timing("  Loading Template Image", ctx->time[12]);
    print_timing("  Reducting Template Image", ctx->time[13]);
    if (ctx->use_cache) {
	print_timing("  Processing Template Image", ctx->time[6]);
    }
    print_timing("Loading Images", load_time);
    print_timing("  Load", ctx->time[14]);
    print_timing("  Reduction", ctx->time[15]);
    print_timing("Processing Images", process_time);
    if (!ctx->use_cache) {
	print_timing("  Processing Template Image", ctx->time[6]);
    }
    print_timing("  Copy Image   ", ctx->time[4]);
    print_timing("  Load Fragment", ctx->time[7]);
    print_timing("  Preprocessing", ctx->time[8]);
    print_timing("  FFT", ctx->time[9]);
    print_timing("  Postprocessing", ctx->time[10]);
    print_timing("Waiting Result", ctx->time[16]);

    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */
    pstateFree(ctx);
#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
    print_timing("Destruction", time);
    reportMessage("");
#endif /* DICT_HW_MEASURE_TIMINGS */
}

int dictSetLogger(DICTLogger error_reporter, DICTLogger message_writer) {
    return reportSet(error_reporter, message_writer);
}

int dictSetup(DICTContext ps, int ncp, int corr_size, int precision, int flags) {
    int err;
    int base_blocks, side_blocks;

#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    fftFree(ps);

    ps->ncp = ncp;
    ps->corr_size = corr_size;
    ps->precision = precision;

    ps->subimage_size = ps->corr_size * 4 + 1;
    ps->fft_size = 6 * corr_size + 1;

    if (flags&DICT_FLAGS_FIXED_FFT_SIZE) {
        ps->fft_real_size = ps->fft_size;
    } else {
        ps->fft_real_size = next_power(ps->fft_size);
    }
    
    if (flags&DICT_FLAGS_MATLAB_MODE) {
	ps->matlab_mode = 1;
    } else {
	ps->matlab_mode = 0;
    }

    ps->ncp_alloc_size = calc_alloc(ps->ncp, CP_BLOCK);
    ps->side_alloc_size = calc_alloc(ps->fft_size, SIDE_BLOCK_SIZE);

    ps->fft_alloc_size = calc_alloc(ps->fft_real_size * ps->fft_real_size, BLOCK_SIZE_1D);

    ps->lsum_size = ps->corr_size * 2 + 1;
    ps->lsum_temp_size = ps->subimage_size + 2*ps->lsum_size - 1;

    ps->lsum_short_aligned_size = calc_alloc(ps->fft_size, BLOCK_SIZE_2D);
    ps->lsum_aligned_size = calc_alloc(ps->lsum_temp_size, BLOCK_SIZE_2D);
    ps->lsum_alloc_size = calc_alloc(ps->lsum_temp_size + ps->lsum_size, BLOCK_SIZE_2D);

    side_blocks = calc_blocks(2 * ps->corr_size  + 1, SIDE_BLOCK_SIZE);
    ps->side_blocks_power = get_power(side_blocks);

    base_blocks = calc_blocks(4 * ps->corr_size  + 1, BLOCK_SIZE_1D);
    ps->base_blocks_power = get_power(base_blocks);

    ps->use_cache = 1;
    err = fftInit(ps, device_memory);
    if ((err == DICT_ERROR_CUDA_MALLOC)&&(ps->use_cache)) {
	ps->use_cache = 0;
	err = fftInit(ps, device_memory);
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[1] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    if (ps->use_cache) {
    	reportMessage("Caching is enabled");
    } else {
    	reportMessage("Caching is disabled");
    }

    return err;
}

int dictSetTemplatePoints(DICTContext ps, const float *points_x, const float *points_y) {
#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    memcpy(ps->points,                      points_x, ps->ncp * sizeof(float));
    memcpy(ps->points + ps->ncp_alloc_size, points_y, ps->ncp * sizeof(float));

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[2] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    return 0;
}

int dictSetDimensions(DICTContext ps, int width, int height) {
    if ((width != ps->width)||(height != ps->height)) {
	ps->width = width;
	ps->height = height;

	return fftSetupDimensions(ps);    
    }
    
    return 0;
}

int dictSetPointsBuffer(DICTContext ps, float *point_x, float *point_y) {
    ps->res_x = point_x;
    ps->res_y = point_y;
    
    return 0;
}

int dictSetCurrentPoints(DICTContext ps, const float *points_x, const float *points_y) {
#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    memcpy(ps->points + 2 * ps->ncp_alloc_size, points_x, ps->ncp * sizeof(float));
    memcpy(ps->points + 3 * ps->ncp_alloc_size, points_y, ps->ncp * sizeof(float));

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[3] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */


    ps->stored = 0;

    return 0;
}

int dictCompute(DICTContext ps) {
    int err;
    
#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftGetCurrentPoints(ps);

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[12] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */
    
    return err;
}

int dictGetCurrentPoints(DICTContext ps, float *res_x, float *res_y) {
    int err;

#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftGetCurrentPoints(ps);
    if (err) return err;

    if ((res_x)&&(res_x != ps->res_x)) {
        float *data_x;
	if (ps->stored) data_x = ps->res_x;
	else data_x = ps->points + 2 * ps->ncp_alloc_size;
	
	memcpy(res_x, data_x, ps->ncp * sizeof(float));
    }

    if ((res_y)&&(res_y != ps->res_y)) {
	float *data_y;
        if (ps->stored) data_y = ps->res_y;
        else data_y = ps->points + 3 * ps->ncp_alloc_size;

	memcpy(res_y, data_y, ps->ncp * sizeof(float));
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[16] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */
    
    return 0;
}

int dictLoadTemplateFragment(DICTContext ps, int icp, int ncp, const unsigned char *img) {
    int err;

    err = fftLoadBaseImage(ps, img);
    if (!err) err = fftLoadBaseFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), img);

    return err;
}

int dictLoadTemplateImage(DICTContext ps, const unsigned char *img, int width, int height) {
    int err;

    int icp;

#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    dictSetDimensions(ps, width, height);

/*    
    ps->width = width;
    ps->height = height;

    int size = 2 * ps->corr_size + 1;
    int size2 = size * size;

    int base_size = 4 * ps->corr_size + 1;
    int base_size2 = base_size * base_size;

    if ((ps->use_cache)&&(width * height > ps->ncp * size2)) {
        ps->mode = 0;
    } else {
        ps->mode = 1;
    }

    if ((ps->use_cache)&&(width * height > ps->ncp * base_size2)) {
        ps->base_mode = 0;
    } else {
        ps->base_mode = 1;
    }
*/

    err = fftLoadBaseImage(ps, img);
    
    if (ps->use_cache) {
	for (icp = 0; (icp < ps->ncp)&&(!err); icp+=CP_BLOCK) {
	    err = fftLoadBaseFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), img);
	}
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[6] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

/*
    if (ps->mode) {
	if (ps->use_cache) {
    	    reportMessage("Running in the image mode, caching enabled");
	} else {
    	    reportMessage("Running in the image mode, caching disabled");
	}
    } else {
	if (ps->base_mode) {
	    if (ps->use_cache) {
    		reportMessage("Running in the image/fragment mode, caching enabled");
	    } else {
    		reportMessage("Running in the image/fragment mode, caching disabled");
	    }
	} else {
	    if (ps->use_cache) {
    		reportMessage("Running in the fragment mode, caching enabled");
	    } else {
    		reportMessage("Running in the fragment mode, caching disabled");
	    }
	}
    }
*/

    return err;
}


int dictLoadFragment(DICTContext ps, int icp, int ncp, const unsigned char *input) {
    int err;
    int load_base = !ps->use_cache;
    cudaStream_t stream = NULL;

#ifdef DICT_HW_MEASURE_TIMINGS
    int time[8];
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    if (load_base) {
	    // We are enforcing image mode if the cache is disabled
	err = fftLoadBaseFragment(ps, icp, ncp, NULL);
	if (err) return err;
    }

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time[1] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftLoadFragment(ps, icp, ncp, input);
    if (err) return err;

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time[2] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftPreprocessFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), stream);
    if (err) return err;

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time[3] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftProcessFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), stream);
    if (err) return err;

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time[4] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftPostprocessFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), stream);
    if (err) return err;

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    time[5] = (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
    
    time[0] = 0;
    for (int i=5;i>0;i--) {
	time[i] -= time[i-1];
    }
    
    for (int i = 1; i < 6; i++) {
	ps->time[i+5] += time[i];
    }
#endif /* DICT_HW_MEASURE_TIMINGS */

	
	return 0;
}

int dictLoadImage(DICTContext ps, unsigned char *img) {
    int err;
    int ncp = ps->ncp;

#ifdef DICT_HW_MEASURE_TIMINGS
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
#endif /* DICT_HW_MEASURE_TIMINGS */

    err = fftLoadImage(ps, img);
    if (err) return err;

#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[4] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    for (int icp = 0; icp < ncp; icp+=CP_BLOCK) {
	err = dictLoadFragment(ps, icp, min2(CP_BLOCK, ps->ncp - icp), img);
	if (err) return err;
    }
    
#ifdef DICT_HW_MEASURE_TIMINGS
    gettimeofday(&tv2, NULL);
    ps->time[5] += (tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif /* DICT_HW_MEASURE_TIMINGS */

    return 0;
}


int dictProcessImage(DICTContext ps, unsigned char *img) {
    int err = dictLoadImage(ps, img);
    if (err) return err;
    return dictCompute(ps);
}

int dictLoadTemplateImageFile(DICTContext ps, const char *name) {
    int err = dictImageLoadTemplateImage(ps, name);
    if (err) return err;
    return dictLoadTemplateImage(ps, ps->image, ps->width, ps->height);
}
int dictLoadImageFile(DICTContext ps, const char *name) {
    int err = dictImageLoadImage(ps, name);
    if (err) return err;
    return dictLoadImage(ps, ps->image);
}

int dictProcessImageFile(DICTContext ps, const char *name) {
    int err = dictLoadImageFile(ps, name);
    if (err) return err;
    return dictCompute(ps);
}