/ani/mrses

To get this branch, use:
bzr branch http://suren.me/webbzr/ani/mrses
1 by Suren A. Chilingaryan
Initial import
1
#include <stdio.h>
2
#include <string.h>
3
#include <assert.h>
4
5
#include <libspe2.h>
6
7
#include "msg.h"
8
#include "mrses_spu.h"
9
10
int mrses_spu_run(HWThread thr __attribute__ ((unused)), spe_context_ptr_t hwctx __attribute__ ((unused)), int block __attribute__ ((unused)), MRSESContext mrses __attribute__ ((unused))) {
11
	// Have no sence to implement
12
    assert(0);
13
    return 0;
14
}
15
16
int mrses_spu_iterate(HWThread thr, spe_context_ptr_t hwctx, int block_group, MRSESContext mrses) {
17
    int err;
18
19
    int i;
20
    unsigned int *hist;
21
22
    MRSESIntType *index; 
23
24
    
25
    MRSESIntType *ires = mrses->ires;
26
27
    int iterate_size = mrses->iterate_size;
28
    int properties = mrses->properties;
29
    int width = mrses->width;
30
31
    int block = block_group * iterate_size;
32
    int block_end = block + iterate_size;
33
34
    SPUParameters param __attribute__ ((aligned (16)));
35
    spe_stop_info_t stop_info;
36
	// We need this variable renewed before each execution of spe_context_run!!!
37
    unsigned int spe_entry = SPE_DEFAULT_ENTRY;
38
39
    //printf("running spu thread: %p\n", hwctx);
40
    
41
42
    if (thr->data) hist = (unsigned int*)(thr->data);
43
    else {
44
	posix_memalign((void*)&hist, HW_ALIGN, calc_alloc(properties * sizeof(uint32_t), HW_ALIGN));
45
	memset(hist, 0, calc_alloc(properties * sizeof(uint32_t), HW_ALIGN));
46
	if (hist) thr->data = hist;
47
	else return 1;
48
    }
49
50
51
    param.mrses = mrses;
52
    param.block_group = block_group;
53
54
    err = spe_context_run(hwctx, &spe_entry,  0, &param, NULL, &stop_info);
55
    if (err < 0) {
56
    	reportError ("Failed to run SPE program, error: %i", err);
57
	return 1;
58
    }
59
    
60
    if (stop_info.stop_reason !=  SPE_EXIT) {
61
    	reportError ("SPE program terminated with non-exit stop reason: %i", stop_info.stop_reason);
62
	return 1;
63
    }	
64
    
65
    err = stop_info.result.spe_exit_code;
66
    if (err) {
67
    	reportError ("SPE program terminated with error code: %i", err);
68
	return 1;
69
    }
70
71
72
    for (; block < block_end; ++block) { 
73
	index = mrses->index + block * properties;
74
	if (ires) memcpy(ires + block * width, index, width * sizeof(MRSESIntType));
75
	for (i = 0; i < width; i++) {
76
	    hist[index[i]]++;
77
	}
78
    }
79
80
    
81
/*
82
    {
83
	int i;
84
	int iterate_size = mrses->iterate_size;
85
	MRSESIntType *index = mrses->index;
86
	mrses->result = malloc(iterate_size * (block_group + 1) * sizeof(MRSESDataType));
87
	mrses->index = malloc(mrses->width * iterate_size * (block_group + 1) * sizeof(MRSESIntType));
88
89
	for (i = 0; i < iterate_size; i++) {
90
	    memcpy(mrses->index + (iterate_size * block_group + i) * mrses->width, index + (iterate_size * block_group + i) * mrses->properties, mrses->width * sizeof(MRSESIntType));
91
	}
92
93
	for (i = 0; i < iterate_size * mrses->width; i++) {
94
	    ++mrses->index[iterate_size * block_group * mrses->width + i];
95
	}
96
97
	float res, mahal, corcor;
98
	MRSESDistance dist = mrses->dist;
99
	void * data = thr->data;
100
101
	thr->data = NULL;
102
	for (i = 0; i < iterate_size; i++) {
103
	    mrses->dist = dist;
104
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
105
	    res = mrses->result[block_group * iterate_size + i];
106
	    mrses->dist = MAHALANOBIS;
107
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
108
	    mahal = mrses->result[block_group * iterate_size + i];
109
	    mrses->dist = CORCOR;
110
	    mrses_ppu_run(thr, hwctx, block_group * iterate_size + i, mrses);
111
	    corcor = mrses->result[block_group * iterate_size + i];
112
113
	    printf("PPU result, block %i: %e (mahal: %e, corcor: %e)\n", i, res, mahal, corcor);
114
	    int j;
115
	    for (j = 0; j < width; j++) {
116
		    printf("%i ", mrses->index[(iterate_size * block_group + i)*mrses->width + j]);
117
	    }
118
	    printf("\n");
119
	}
120
121
	free(thr->data);
122
	thr->data = data;
123
124
	free(mrses->index); mrses->index = index;
125
	free(mrses->result); mrses->result = NULL;
126
	mrses->dist = dist;
127
    }
128
*/
129
    
130
    return 0;
131
}