/ani/mrses

To get this branch, use:
bzr branch http://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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>


#include <mex.h>

#include "mrses.h"
#include "msg.h"

#define MAX_FILE_NAME 2048
/*#define USE_UNDOCUMENTED*/

#ifndef EXTERN_C
# ifdef __cplusplus
   #define EXTERN_C extern "C"
# else
   #define EXTERN_C extern
# endif
#endif

#ifdef USE_UNDOCUMENTED
EXTERN_C mxArray *mxCreateSharedDataCopy(const mxArray *pr);
#endif /* USE_UNDOCUMENTED */


typedef enum {
    ACTION_SETUP = 1,
    ACTION_COMPUTE = 10,
    ACTION_ITERATE = 15,
    ACTION_GET_HIST = 16
} TAction;


struct MEXContextT {
    MRSESContext mrses;
    mxArray *res; 
    mxArray *hist;
    unsigned int initialized;
    unsigned int width, max_block;
};
typedef struct MEXContextT MEXContextS;
typedef struct MEXContextT *MEXContext;

static MEXContext pstate = NULL;

MEXContext mexCreateContext() {
    pstate = (MEXContext)malloc(sizeof(MEXContextS));
    if (pstate) {
	MRSESContext ctx = mrses_create_context();
	if (!ctx) {
	    free(pstate);
	    return NULL;
	}
	
	memset(pstate, 0, sizeof(MEXContextS));
	pstate->mrses = ctx;
    }
    return pstate;
}

void mexDestroyContext(MEXContext pstate) {
    if (pstate) {
	if (pstate->res) {
	    mxDestroyArray(pstate->res);
	}
	if (pstate->hist) {
	    mxDestroyArray(pstate->hist);
	}
	if (pstate->mrses) {
	    mrses_destroy_context(pstate->mrses);
	}
	free(pstate);
    }
}

static void selfClean() {
    if (pstate) {
	reportMessage("Cleaning hw instance");

	mexDestroyContext(pstate);
	pstate = NULL;
    }

}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    int err;

    mxArray *idMatrix;
    int32_t *idPtr;

    static int32_t id = 0;
    
    MRSESDistance dist;
    MRSESIntType *ires;
    const mxArray *A, *B, *index;
    mxArray *res;
    unsigned int iterations;
    unsigned int properties;
    unsigned int block_size;
    unsigned int width;

    MEXContext ps;
    TAction action;


    if (!nrhs) {
	reportMessage("Initializing hw instance");

	if (nlhs != 1) {
	    reportError("You should accept a single result from initialization call");
	    return;
	}
	
	idMatrix = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
	if (!idMatrix) {
	    reportError("Initialization is failed");
	    return;
	}

	if (pstate) mexDestroyContext(pstate);
	pstate = mexCreateContext();
	
	if (pstate) {
	    mexAtExit(selfClean);
	    id = 1;
	} else if (id > 0) {
	    reportError("Context initialization has failed");
	    id = -1;
	}

	idPtr = (int32_t*)mxGetData(idMatrix);
	idPtr[0] = id;
	
	plhs[0] = idMatrix;
	return;
    } else {
	if (!pstate) {
	    reportError("normxcorr_hw should be initialized first");
	    return;
	}
    }

	/* Clean request */
    if (nrhs == 1) {
	selfClean();
	return;
    }

    ps = pstate;

    action = (TAction)(mxGetScalar((mxArray*)prhs[1]));

    /*reportMessage("Executing normxcorr_hw action: %u", action);*/

    switch (action) {
     case ACTION_SETUP:
        if ((nrhs < 6)||(nrhs > 7)) {
	    reportError("Initialization call expects following arguments: 'width', 'block_size', matrices 'A' and 'B', distance_type");
	    return;
	}
	
	width = (int)mxGetScalar(prhs[2]);
	block_size = (int)mxGetScalar(prhs[3]);
	if (nrhs == 7) {
	    dist = (int)mxGetScalar(prhs[6]) - 1;
	    if (dist >= MRSES_DISTANCE_LAST) {
		reportError("Invalid distance mode is requested: %i", dist);
		return;
	    }
	} else dist = BHATTACHARYYA;
	A = prhs[4];
	B = prhs[5];
	
	properties = mxGetN(A);
	if (properties != mxGetN(B)) {
	    reportError("Dimenssions of matrices A and B does not match: %i, %i", properties, mxGetN(B));
	    return;
	}
	
	if ((mxGetClassID(A) == mxSINGLE_CLASS)&&(mxGetClassID(B) == mxSINGLE_CLASS)) {
	    err = mrses_init_context(ps->mrses, properties, mxGetM(A), mxGetData(A), mxGetM(B), mxGetData(B));
	} else if ((mxGetClassID(A) == mxDOUBLE_CLASS)&&(mxGetClassID(B) == mxDOUBLE_CLASS)) {
	    err = mrses_init_context_from_double(ps->mrses, properties, mxGetM(A), mxGetData(A), mxGetM(B), mxGetData(B));
	} else {
	    reportError("Only single-precision floating point numbers are supported");
	    return;
	}
	
	if (err) {
	    reportError("Initialization is failed with error, %i", err);
	    return;
	}
	
	err = mrses_set_distance_mode(ps->mrses, dist);
	if (err) {
	    reportError("Failed to set distance mode, error %i", err);
	    return;
	}
	
	err = mrses_prepare(ps->mrses, width, block_size);
	if (err) {
	    reportError("Failed to prepare for execution, error %i", err);
	    return;
	}

	if (ps->res) mxDestroyArray(ps->res);
	ps->res = mxCreateNumericMatrix(block_size, 1, mxSINGLE_CLASS, mxREAL);
	if (ps->res) mexMakeArrayPersistent(ps->res);
	else {
	    reportError("Allocation of result matrix of size %u*float bytes is failed", block_size);
	    return;
	}
	
	if (ps->hist) mxDestroyArray(ps->hist);
	ps->hist = mxCreateNumericMatrix(1, properties, mxUINT32_CLASS, mxREAL);
	if (ps->hist) mexMakeArrayPersistent(ps->hist);
	else {
	    reportError("Allocation of result matrix of size %u*uint32_t bytes is failed", properties);
	    return;
	}

	ps->width = width;
	ps->max_block = block_size;
	ps->initialized = 1;
     break;
     case ACTION_COMPUTE:
	if (!pstate->initialized) {
	    reportError("We are not initialized yet, issue intialize call first");
	    return;
	}

        if ((nlhs != 1)||((nrhs < 3)||(nrhs > 4))) {
	    reportError("Compute call expects following arguments: 'block_size', 'index'. It should accept a single result matrix as well.");
	    return;
	}
	
	if (nrhs > 3) {
	    block_size = (int)mxGetScalar(prhs[2]);
	    index = prhs[3];

	    if (mxGetN(index) < block_size) {
		reportError("Index matrix does not contain specified number of blocks");
		return;
	    }
	} else {
	    index = prhs[2];
	    block_size = mxGetN(index);
	}

	if (block_size > pstate->max_block) {
	    reportError("The block is too big: maximum configured size is %i, but %i supplied", pstate->max_block, block_size);
	    return;
	}
	
	width = mxGetM(index);
	if (pstate->width != width) {
	    reportError("The index matrix dimension does not fit requirement: width = %i, but index side is %i", pstate->width, width);
	    return;
	}
	
	if (mxGetClassID(index) != mxINT16_CLASS) {
	    reportError("Only 16 bit integers are supported for index");
	    return;
	}
	
	res = ps->res;
	mxSetN(res, block_size);
	mxSetM(res, 1);

	err = mrses_compute(ps->mrses, block_size, mxGetData(index), mxGetData(res));
	if (err) {
	    reportError("Error executing compute, failed with code: %i", err);
	    return;
	}

#ifdef USE_UNDOCUMENTED
	plhs[0] = mxCreateSharedDataCopy(res);
#else /* USE_UNDOCUMENTED */
	plhs[0] = mxDuplicateArray(res);
#endif /* USE_UNDOCUMENTED */
	
     break;
     case ACTION_ITERATE:
	if (!pstate->initialized) {
	    reportError("We are not initialized yet, issue intialize call first");
	    return;
	}

        if (nrhs != 4) {
	    reportError("Iterate call expects following arguments: 'iterations', 'block_size'.");
	    return;
	}
	
	iterations = (int)mxGetScalar(prhs[2]);
	block_size = (int)mxGetScalar(prhs[3]);

	if (block_size > pstate->max_block) {
	    reportError("The block is too big: maximum configured size is %i, but %i supplied", pstate->max_block, block_size);
	    return;
	}

	if (nlhs == 1) {
	    idMatrix = mxCreateNumericMatrix(pstate->width, pstate->max_block, mxUINT16_CLASS, mxREAL);
	    mxSetN(idMatrix, block_size);
	    ires = mxGetData(idMatrix);
	    plhs[0] = idMatrix;
	} else {
	    ires = NULL;
	}
	err = mrses_iterate(ps->mrses, iterations, block_size, ires);
	if (err) {
	    reportError("Error executing iterate, failed with code: %i", err);
	    return;
	}
     break;
     case ACTION_GET_HIST:
	if (!pstate->initialized) {
	    reportError("We are not initialized yet, issue intialize call first");
	    return;
	}

        if ((nlhs != 1)&&(nrhs != 2)) {
	    reportError("GetHist call returns histogram vector and expects no arguments.");
	    return;
	}

	res = ps->hist;
	err = mrses_get_results(ps->mrses, mxGetData(res));
	if (err) {
	    reportError("Error executing iterate, failed with code: %i", err);
	    return;
	}

	plhs[0] = mxDuplicateArray(res);
     break;
     default:
        reportError("Unknown request %i", action);
    }
}