/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to cuda/local_sum.cu

  • Committer: Suren A. Chilingaryan
  • Date: 2009-12-02 05:08:22 UTC
  • Revision ID: csa@dside.dyndns.org-20091202050822-n6ouznm1zp2n2i5l
Instead of transfer compute local sums and denormals on board

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
 
 
4
#include <mex.h>
 
5
 
 
6
#include "normxcorr_hw.h"
 
7
#include "local_sum.h"
 
8
 
 
9
#include "local_sum_kernel.cu"
 
10
 
 
11
#include <unistd.h>
 
12
 
 
13
int local_sum(TProcessingState *ps, 
 
14
    float *lsum, float *denom,
 
15
    float *tmp1, float *tmp2,
 
16
    float *in1, float *in2) {
 
17
 
 
18
    int size = ps->subimage_size;
 
19
    int lsize = ps->lsum_size;
 
20
 
 
21
    int temp_size = ps->lsum_temp_size;
 
22
    int alloc_size = ps->lsum_alloc_size;
 
23
    
 
24
    int aligned_size = ps->lsum_aligned_size;
 
25
    int short_size = ps->lsum_short_aligned_size;
 
26
 
 
27
        // All arrays should be aligned
 
28
    cudppMultiScan(ps->cudpp_plan, tmp1 + lsize * alloc_size, in1 + lsize * alloc_size, temp_size, size);
 
29
    cudppMultiScan(ps->cudpp_plan, tmp2 + lsize * alloc_size, in2 + lsize * alloc_size, temp_size, size);
 
30
 
 
31
        // Actually we can transpose here only a middle(image) rows, but in output lsum/denom
 
32
        // arrays we should somehow manage to have zeros in other places. This will require
 
33
        // memset which is not much faster? than our transpose routine, therefore it left
 
34
        // as is for simplicity
 
35
    dim3 block_dim(BLOCK_SIZE_2D, BLOCK_SIZE_2D, 1);
 
36
    dim3 grid_dim1(short_size / BLOCK_SIZE_2D, aligned_size / BLOCK_SIZE_2D, 1);
 
37
    transpose1<<<grid_dim1,block_dim>>>(lsum, tmp1, alloc_size, alloc_size, lsize);
 
38
    transpose1<<<grid_dim1,block_dim>>>(denom, tmp2, alloc_size, alloc_size, lsize);
 
39
 
 
40
    cudppMultiScan(ps->cudpp_plan, tmp1, lsum, temp_size, size + lsize - 1);
 
41
    cudppMultiScan(ps->cudpp_plan, tmp2, denom, temp_size, size + lsize - 1);
 
42
 
 
43
    dim3 grid_dim2(short_size / BLOCK_SIZE_2D, short_size / BLOCK_SIZE_2D, 1);
 
44
    transpose2<<<grid_dim2,block_dim>>>(lsum, denom, tmp1, tmp2, alloc_size, ps->fft_size, lsize);
 
45
 
 
46
    return 0;
 
47
}
 
48
 
 
49
int local_sum_validate(TProcessingState *ps, int icp, const mxArray *lsum, const mxArray *denom) {
 
50
    int res = 0;
 
51
#ifdef VALIDATE_LSUM
 
52
    int size = ps->fft_size;
 
53
    int size2 = size*size;
 
54
    int alloc_size = ps->fft_alloc_size;
 
55
 
 
56
    if (lsum&&denom) {
 
57
        float *tmp = (float*)malloc(size2*sizeof(float));
 
58
        
 
59
        cudaMemcpy(tmp, ps->cuda_lsum_buffer + icp * alloc_size, size2*sizeof(float), cudaMemcpyDeviceToHost);
 
60
        float *real =  (float*)mxGetData(lsum);
 
61
        if (memcmp(tmp, real, size2*sizeof(float))) {
 
62
            printf("lsum fault: %i\n", 1);
 
63
            for (int i = 0; i < size2; i++) {
 
64
                if (tmp[i] != real[i]) {
 
65
                    res = 1;
 
66
                    printf("lsum fault: %i %i - %f %f\n", i / size, i % size, tmp[i], real[i]);
 
67
                    break;
 
68
                }
 
69
            }
 
70
        }
 
71
 
 
72
        cudaMemcpy(tmp, ps->cuda_denom_buffer + icp * alloc_size, size2*sizeof(float), cudaMemcpyDeviceToHost);
 
73
        real =  (float*)mxGetData(denom);
 
74
        if (memcmp(tmp, real, size2*sizeof(float))) {
 
75
            for (int i = 0; i < size2; i++) {
 
76
                float diff = (tmp[i] == real[i])?0:2*fabs(tmp[i] - real[i])/(tmp[i] = real[i]);
 
77
                if (diff > 0.0001) {
 
78
                    res = 1;
 
79
                    printf("denom fault: %i %i - %f %f %f\n", i / size, i % size, tmp[i], real[i], diff);
 
80
                    break;
 
81
                }
 
82
            }
 
83
        }
 
84
 
 
85
        free(tmp);
 
86
    }
 
87
#endif /* VALIDATE_LSUM */
 
88
 
 
89
    return res;
 
90
}