/perf/fdk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/fdk
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
#include <stdio.h>
#include <stdlib.h>

#include <math.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>

#include <limits.h>

#include <mex.h>

#include <ippi.h>
#include <ipps.h>
#include <ippcore.h>

#include "process.h"

extern int counter;

int is_aligned(void *p, int N)
{
    return (uintptr_t)p % N == 0;
}

void mexFunction( int nlhs, mxArray *plhs[],
        int nrhs, const mxArray *prhs[])
{
    /* prhs[0]      int   n_voxels */
    /* prhs[1]      int   n_proj */
    /* prhs[2]      float   cor */
    /* prhs[3]      float   d */
    /* prhs[4]      float   detector_size */
    /* prhs[5]      float   pixel_size */
    /* prhs[6]      float   cor_offset */
    /* prhs[7]      float   detector_offset_u */
    /* prhs[8]      float   detector_offset_v */
    /* prhs[9]      float   detector_offset_z */
    /* prhs[10]      float   s */
    /* prhs[11]      float   u_detector */
    /* prhs[12]      float   v_detector */
    /* prhs[13]      float   n_detector_r */
    /* prhs[14]      float   projections */
    /* prhs[15]      float   slice_x */
    /* prhs[16]      float   slice_y */
    /* prhs[17]      float   slice_coord_z */
    /* prhs[18]      int     n_threads */
    
    int n_proj, n_threads;
    int n_elements;
    Ipp32f cor, d, detector_size, pixel_size, cor_offset, detector_offset_u, detector_offset_v, detector_offset_z;
    Ipp32f *source = NULL, *u_detector = NULL, *v_detector = NULL, *n_detector = NULL;
    Ipp32f *projections = NULL;
    Ipp32f *slice_x = NULL, *slice_y = NULL, *slice_coord_z = NULL;
    Ipp32f *out_volume = NULL;
    
    int tnum, m;
    struct thread_info *t_info;
    void *res;
    
    int alignment = 32;
    
    /*/////////// PARSE INPUT /////////////////*/
    
    /* radiograph and slice size */
    /* check n_voxels data type */
    if (mxGetNumberOfElements(prhs[0])!=1 || !mxIsInt32(prhs[0]))
    {
        mexErrMsgTxt("mex: Input n_voxels is not scalar or int32.");
    }
     
    n_elements = (int)mxGetScalar(prhs[0]);
    
    if (n_elements % 16 != 0)
    {
        printf("n_elements has to be multiple of 16 since IPPI uses 64 bytes alignment");
        exit(-1);
    }

    
    /* number of projections */
    /* check n_proj data type */
    if (mxGetNumberOfElements(prhs[1])!=1 || !mxIsInt32(prhs[1]))
    {
        mexErrMsgTxt("mex: Input n_proj is not scalar or int32.");
    }
    
    n_proj = (int)mxGetScalar(prhs[1]);
    
    
    /* source to rotation distance */
    /* check input cor data type */
    if (!mxIsSingle(prhs[2]) || mxIsComplex(prhs[2]) || mxGetNumberOfElements(prhs[2])!=1)
    {
        mexErrMsgTxt("mex: Input cor must be scalar and have type single.");
    }
    
    cor = (Ipp32f)mxGetScalar(prhs[2]);
    
    /* source to detector distance */
    /* check input d data type */
    if (!mxIsSingle(prhs[3]) || mxIsComplex(prhs[3]) || mxGetNumberOfElements(prhs[3])!=1)
    {
        mexErrMsgTxt("mex: Input d must be scalar and have type single.");
    }
    
    d = (Ipp32f)mxGetScalar(prhs[3]);
    
    
    /* physical size of detector (assumed to be square) */
    /* check input detector_size data type */
    if (!mxIsSingle(prhs[4]) || mxIsComplex(prhs[4]) || mxGetNumberOfElements(prhs[4])!=1)
    {
        mexErrMsgTxt("mex: Input detector_size must be scalar and have type single.");
    }
    
    detector_size = (Ipp32f)mxGetScalar(prhs[4]);
    
    
    /* physical pixel size */
    /* check input pixel_size data type */
    if (!mxIsSingle(prhs[5]) || mxIsComplex(prhs[5]) || mxGetNumberOfElements(prhs[5])!=1)
    {
        mexErrMsgTxt("mex: Input pixel_size must be scalar and have type single.");
    }
    
    pixel_size = (Ipp32f)mxGetScalar(prhs[5]);
   
    
    /* offset in cor */
    /* check input cor_offset data type */
    if (!mxIsSingle(prhs[6]) || mxIsComplex(prhs[6]) || mxGetNumberOfElements(prhs[6])!=1)
    {
        mexErrMsgTxt("mex: Input cor_offset must be scalar and have type single.");
    }
    
    cor_offset = (Ipp32f)mxGetScalar(prhs[6]);
    
    
    /* detector offset in U direction */
    /* check input detector_offset_u data type */
    if (!mxIsSingle(prhs[7]) || mxIsComplex(prhs[7]) || mxGetNumberOfElements(prhs[7])!=1)
    {
        mexErrMsgTxt("mex: Input detector_offset_u must be scalar and have type single.");
    }
    
    detector_offset_u = (Ipp32f)mxGetScalar(prhs[7]);
    
    
    /* detector offset in V direction */
    /* check input detector_offset_v data type */
    if (!mxIsSingle(prhs[8]) || mxIsComplex(prhs[8]) || mxGetNumberOfElements(prhs[8])!=1)
    {
        mexErrMsgTxt("mex: Input detector_offset_v must be scalar and have type single.");
    }
    
    detector_offset_v = (Ipp32f)mxGetScalar(prhs[8]);
    
    
    /* detector offset along magnification line */
    /* check input detector_offset_z data type */
    if (!mxIsSingle(prhs[9]) || mxIsComplex(prhs[9]) || mxGetNumberOfElements(prhs[9])!=1)
    {
        mexErrMsgTxt("mex: Input detector_offset_z must be scalar and have type single.");
    }
    
    detector_offset_z = (Ipp32f)mxGetScalar(prhs[9]);
    
    
    /* source position */
    /* check input source data type */
    if (!mxIsSingle(prhs[10]) || mxIsComplex(prhs[10]) || mxGetNumberOfElements(prhs[10])!=3)
    {
        mexErrMsgTxt("mex: Input source must be type single and contain 3 elements.");
    }
    
    source = (Ipp32f *)mxGetData(prhs[10]);
    
    if (source == NULL) 
    {
        printf("Cannot get source");
	exit(-1);
    }
    
    
    /* detector coordinate frame */
    /* check input u_detector data type */
    if( !mxIsSingle(prhs[11]) || mxIsComplex(prhs[11]) || mxGetNumberOfElements(prhs[11])!=3)
    {
        mexErrMsgTxt("mex: Input u_detector must be type single and contain 3 elements.");
    }
    
    /* check input v_detector data type */
    if( !mxIsSingle(prhs[12]) || mxIsComplex(prhs[12]) || mxGetNumberOfElements(prhs[12])!=3)
    {
        mexErrMsgTxt("mex: Input v_detector must be type single and contain 3 elements.");
    }
    
    /* check input n_detector data type */
    if( !mxIsSingle(prhs[13]) || mxIsComplex(prhs[13]) || mxGetNumberOfElements(prhs[13])!=3)
    {
        mexErrMsgTxt("mex: Input n_detector must be type single and contain 3 elements.");
    }
    
    u_detector = (Ipp32f *)mxGetData(prhs[11]);
    
    if (u_detector == NULL) 
    {
        printf("Cannot get u_detector");
	exit(-1);
    }
    
    v_detector = (Ipp32f *)mxGetData(prhs[12]);
    
    if (u_detector == NULL) 
    {
        printf("Cannot get v_detector");
	exit(-1);
    }
    
    n_detector = (Ipp32f *)mxGetData(prhs[13]);
    
    if (n_detector == NULL) 
    {
        printf("Cannot get n_detector");
	exit(-1);
    }
    
    
    /* volume with projections, x-y-angle */
    /* check input projections data type */
    if (!mxIsSingle(prhs[14]) || mxIsComplex(prhs[14]))
    {
        mexErrMsgTxt("mex: Input projections array must be type single.");
    }
    
    /* check that number of elements in projections is equal to n_voxels x n_voxels x n_proj */
    if ((mxGetNumberOfElements(prhs[14]) != (long)n_elements * n_elements * n_proj))
    {
        mexErrMsgTxt("mex: Input projections must be a n_elements x n_elements x n_proj array.");
    }
    
    projections = (Ipp32f *)mxGetData(prhs[14]);
    
    if (projections == NULL) {
        printf("Can not get projections");
	exit(-1);
    }
    
    /* check if dataset is aligned by 32 bytes */
    if (is_aligned(projections, (int)alignment) == 0)
    {
        printf("Projections have to be aligned by 32 bytes\n");
        exit(-1);
    }
    
    
    /* grids */
    /* check input slice_x data type */
    if ( !mxIsSingle(prhs[15]) || mxIsComplex(prhs[15]))
    {
        mexErrMsgTxt("mex: Input slice_x array must be type single.");
    }
    
    /* check that number of rows  and columns in slice_x is equal to n_voxels */
    if(mxGetM(prhs[15]) != n_elements || mxGetN(prhs[15]) != n_elements)
    {
        mexErrMsgTxt("mex: Input slice_x must be a n_voxels x n_elements array.");
    }
    
    /* check input slice_y data type */
    if( !mxIsSingle(prhs[16]) || mxIsComplex(prhs[16]))
    {
        mexErrMsgTxt("mex: Input slice_y array must be type single.");
    }
    
    /* check that number of rows  and columns in slice_y is equal to n_voxels */
    if(mxGetM(prhs[16]) != n_elements || mxGetN(prhs[16]) != n_elements)
    {
        mexErrMsgTxt("mex: Input slice_y must be a n_voxels x n_elements array.");
    }
    
    /* check input slice_coord_z data type */
    if( !mxIsSingle(prhs[17]) || mxIsComplex(prhs[17]))
    {
        mexErrMsgTxt("mex: Input slice_coord_z array must be type single.");
    }
    
    /* check that number of elements in slice_coord_z is equal to n_voxels */
    if(mxGetNumberOfElements(prhs[17]) != n_elements)
    {
        mexErrMsgTxt("mex: Input slice_coord_z must be a n_voxels array.");
    }
    
    slice_x = (Ipp32f *)mxGetData(prhs[15]);
    
    if (slice_x == NULL) 
    {
        printf("Cannot get slice_x");
	exit(-1);
    }
    
    /* check if slice_x is aligned by 32 bytes */
    if (is_aligned(slice_x, (int)alignment) == 0)
    {
        printf("slice_x have to be aligned by 32 bytes\n");
        exit(-1);
    }
    
    slice_y = (Ipp32f *)mxGetData(prhs[16]);
    
    if (slice_y == NULL) 
    {
        printf("Cannot get slice_y");
	exit(-1);
    }
    
    /* check if slice_y is aligned by 32 bytes */
    if (is_aligned(slice_y, (int)alignment) == 0)
    {
        printf("slice_y have to be aligned by 32 bytes\n");
        exit(-1);
    }
    
    slice_coord_z = (Ipp32f *)mxGetData(prhs[17]);
    
    if (slice_coord_z == NULL) {
        printf("Cannot get slice_coord_z");
	exit(-1);
    }
    
    
    /* n_threads */
    /* check input n_threads data type */
    if(mxGetNumberOfElements(prhs[18])!=1 || !mxIsInt32(prhs[18]))
    {
        mexErrMsgTxt("mex: Input n_threads is not scalar or int32.");
    }
    
    n_threads = (int)mxGetScalar(prhs[18]);
    
    
    /* create the output matrix im_size x im_size*/
    mwSize dims[3];
    dims[0] = n_elements;
    dims[1] = n_elements;
    dims[2] = n_elements;
    plhs[0] = mxCreateNumericArray((mwSize)3,dims,mxSINGLE_CLASS,mxREAL);
    
    /* get a pointer to the data in the output matrix */
    out_volume = (Ipp32f *)mxGetData(plhs[0]);
    
    if (out_volume == NULL) {
        printf("Cannot get out_volume");
	exit(-1);
    }
    
    /*//////// INITIALIZE THREADS /////////////*/
    
    /* set global variable counter to zero */
    counter = 0;
    
    /* allocate memory for threads */
    t_info = (struct thread_info*) calloc(n_threads, sizeof(struct thread_info));
    
    if (t_info == NULL)
    {
        printf("Structure t_info memory allocation error\n");
        exit(-1);
    }
    
    /* fill tread_info structure and create threads */
    for (tnum = 0; tnum < n_threads; tnum++)
    {
        t_info[tnum].thread_num = tnum;                      /* Application-defined thread # */
        t_info[tnum].n_elements = n_elements;
        t_info[tnum].n_proj = n_proj;
        t_info[tnum].cor = cor;
        t_info[tnum].d = d;
        t_info[tnum].detector_size = detector_size; 
        t_info[tnum].pixel_size = pixel_size;
        t_info[tnum].cor_offset = cor_offset;
        t_info[tnum].detector_offset_u = detector_offset_u;
        t_info[tnum].detector_offset_v = detector_offset_v;
        t_info[tnum].detector_offset_z = detector_offset_z;
        t_info[tnum].source = source;
        t_info[tnum].u_detector = u_detector; 
        t_info[tnum].v_detector = v_detector; 
        t_info[tnum].n_detector = n_detector;
        t_info[tnum].projections = projections;
        t_info[tnum].slice_x = slice_x;
        t_info[tnum].slice_y = slice_y; 
        t_info[tnum].slice_coord_z = slice_coord_z;
        t_info[tnum].out_volume = out_volume;
        t_info[tnum].slices_per_iter = n_elements;	// just to process a single iteration only
        
        m = pthread_create(&t_info[tnum].thread_id, NULL, &process, &t_info[tnum]);
        
        if (m != 0)
        {
            printf("Cannot create thread with error %i\n", m);
            exit(-1);
        }
    }
    
    /* join threads */
    for (tnum = 0; tnum < n_threads; tnum++)
    {
        m = pthread_join(t_info[tnum].thread_id, &res);
        
        if (m != 0)
        {
            printf("Cannot join threads with error %i\n", m);
            exit(-1);
        }
    }

    free(t_info);
}