/ani/mrses

To get this branch, use:
bzr branch http://de.suren.me/webbzr/ani/mrses
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
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <libspe2.h>

#include "msg.h"
#include "mrses_spu.h"

int mrses_spu_run(HWThread thr __attribute__ ((unused)), spe_context_ptr_t hwctx __attribute__ ((unused)), int block __attribute__ ((unused)), MRSESContext mrses __attribute__ ((unused))) {
	// Have no sence to implement
    assert(0);
    return 0;
}

int mrses_spu_iterate(HWThread thr, spe_context_ptr_t hwctx, int block_group, MRSESContext mrses) {
    int err;

    int i;
    unsigned int *hist;

    MRSESIntType *index; 

    
    MRSESIntType *ires = mrses->ires;

    int iterate_size = mrses->iterate_size;
    int properties = mrses->properties;
    int width = mrses->width;
    
    int palloc = mrses->palloc;

    int block = block_group * iterate_size;
    int block_end = block + iterate_size;

    SPUParameters param __attribute__ ((aligned (16)));
    spe_stop_info_t stop_info;
	// We need this variable renewed before each execution of spe_context_run!!!
    unsigned int spe_entry = SPE_DEFAULT_ENTRY;

    //printf("running spu thread: %p\n", hwctx);
    

    if (thr->data) hist = (unsigned int*)(thr->data);
    else {
	posix_memalign((void*)&hist, HW_ALIGN, calc_alloc(properties * sizeof(uint32_t), HW_ALIGN));
	memset(hist, 0, calc_alloc(properties * sizeof(uint32_t), HW_ALIGN));
	if (hist) thr->data = hist;
	else return 1;
    }


    param.mrses = mrses;
    param.block_group = block_group;

    err = spe_context_run(hwctx, &spe_entry,  0, &param, NULL, &stop_info);
    if (err < 0) {
    	reportError ("Failed to run SPE program, error: %i", err);
	return 1;
    }
    
    if (stop_info.stop_reason !=  SPE_EXIT) {
    	reportError ("SPE program terminated with non-exit stop reason: %i", stop_info.stop_reason);
	return 1;
    }	
    
    err = stop_info.result.spe_exit_code;
    if (err) {
    	reportError ("SPE program terminated with error code: %i", err);
	return 1;
    }


    for (; block < block_end; ++block) { 
	index = mrses->index + block * palloc;
	if (ires) memcpy(ires + block * width, index, width * sizeof(MRSESIntType));
	for (i = 0; i < width; i++) {
	    hist[index[i]]++;
	}
    }

    
/*
    {
	int i;
	int iterate_size = mrses->iterate_size;
	MRSESIntType *index = mrses->index;
	mrses->result = malloc(iterate_size * (block_group + 1) * sizeof(MRSESDataType));
	mrses->index = malloc(mrses->width * iterate_size * (block_group + 1) * sizeof(MRSESIntType));

	for (i = 0; i < iterate_size; i++) {
	    memcpy(mrses->index + (iterate_size * block_group + i) * mrses->width, index + (iterate_size * block_group + i) * palloc, mrses->width * sizeof(MRSESIntType));
	}

	for (i = 0; i < iterate_size * mrses->width; i++) {
	    ++mrses->index[iterate_size * block_group * mrses->width + i];
	}

	float res, mahal, corcor;
	MRSESDistance dist = mrses->dist;
	void * data = thr->data;

	thr->data = NULL;
	for (i = 0; i < iterate_size; i++) {
	    mrses->dist = dist;
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
	    res = mrses->result[block_group * iterate_size + i];
	    mrses->dist = MAHALANOBIS;
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
	    mahal = mrses->result[block_group * iterate_size + i];
	    mrses->dist = CORCOR;
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
	    corcor = mrses->result[block_group * iterate_size + i];

	    printf("PPU result, block %i: %e (mahal: %e, corcor: %e)\n", i, res, mahal, corcor);
	    int j;
	    for (j = 0; j < width; j++) {
		    printf("%i ", mrses->index[(iterate_size * block_group + i)*mrses->width + j]);
	    }
	    printf("\n");
	}

	free(thr->data);
	thr->data = data;

	free(mrses->index); mrses->index = index;
	free(mrses->result); mrses->result = NULL;
	mrses->dist = dist;
    }
*/
    
    return 0;
}