ivan@137
|
1 /**************************************************************************
|
ivan@137
|
2 *
|
ivan@137
|
3 * File name: ompcore.c
|
ivan@137
|
4 *
|
ivan@137
|
5 * Ron Rubinstein
|
ivan@137
|
6 * Computer Science Department
|
ivan@137
|
7 * Technion, Haifa 32000 Israel
|
ivan@137
|
8 * ronrubin@cs
|
ivan@137
|
9 *
|
ivan@137
|
10 * Last Updated: 25.8.2009
|
ivan@137
|
11 *
|
ivan@137
|
12 *************************************************************************/
|
ivan@137
|
13
|
ivan@137
|
14
|
ivan@137
|
15 #include "ompcore.h"
|
ivan@137
|
16 #include "omputils.h"
|
ivan@137
|
17 #include "ompprof.h"
|
ivan@137
|
18 #include "myblas.h"
|
ivan@137
|
19 #include <math.h>
|
ivan@137
|
20 #include <string.h>
|
ivan@137
|
21
|
ivan@137
|
22
|
ivan@137
|
23
|
ivan@137
|
24 /******************************************************************************
|
ivan@137
|
25 * *
|
ivan@137
|
26 * Batch-OMP Implementation *
|
ivan@137
|
27 * *
|
ivan@137
|
28 ******************************************************************************/
|
ivan@137
|
29
|
ivan@137
|
30 mxArray* ompcore(double D[], double x[], double DtX[], double XtX[], double G[], mwSize n, mwSize m, mwSize L,
|
ivan@137
|
31 int T, double eps, int gamma_mode, int profile, double msg_delta, int erroromp)
|
ivan@137
|
32 {
|
ivan@137
|
33
|
ivan@137
|
34 profdata pd;
|
ivan@137
|
35 mxArray *Gamma;
|
ivan@137
|
36 mwIndex i, j, signum, pos, *ind, *gammaIr, *gammaJc, gamma_count;
|
ivan@137
|
37 mwSize allocated_coefs, allocated_cols;
|
ivan@137
|
38 int DtX_specified, XtX_specified, batchomp, standardomp, *selected_atoms;
|
ivan@137
|
39 double *alpha, *r, *Lchol, *c, *Gsub, *Dsub, sum, *gammaPr, *tempvec1, *tempvec2;
|
ivan@137
|
40 double eps2, resnorm, delta, deltaprev, secs_remain;
|
ivan@137
|
41 int mins_remain, hrs_remain;
|
ivan@137
|
42 clock_t lastprint_time, starttime;
|
ivan@137
|
43
|
ivan@137
|
44
|
ivan@137
|
45
|
ivan@137
|
46 /*** status flags ***/
|
ivan@137
|
47
|
ivan@137
|
48 DtX_specified = (DtX!=0); /* indicates whether D'*x was provided */
|
ivan@137
|
49 XtX_specified = (XtX!=0); /* indicates whether sum(x.*x) was provided */
|
ivan@137
|
50
|
ivan@137
|
51 standardomp = (G==0); /* batch-omp or standard omp are selected depending on availability of G */
|
ivan@137
|
52 batchomp = !standardomp;
|
ivan@137
|
53
|
ivan@137
|
54
|
ivan@137
|
55
|
ivan@137
|
56 /*** allocate output matrix ***/
|
ivan@137
|
57
|
ivan@137
|
58
|
ivan@137
|
59 if (gamma_mode == FULL_GAMMA) {
|
ivan@137
|
60
|
ivan@137
|
61 /* allocate full matrix of size m X L */
|
ivan@137
|
62
|
ivan@137
|
63 Gamma = mxCreateDoubleMatrix(m, L, mxREAL);
|
ivan@137
|
64 gammaPr = mxGetPr(Gamma);
|
ivan@137
|
65 gammaIr = 0;
|
ivan@137
|
66 gammaJc = 0;
|
ivan@137
|
67 }
|
ivan@137
|
68 else {
|
ivan@137
|
69
|
ivan@137
|
70 /* allocate sparse matrix with room for allocated_coefs nonzeros */
|
ivan@137
|
71
|
ivan@137
|
72 /* for error-omp, begin with L*sqrt(n)/2 allocated nonzeros, otherwise allocate L*T nonzeros */
|
ivan@137
|
73 allocated_coefs = erroromp ? (mwSize)(ceil(L*sqrt((double)n)/2.0) + 1.01) : L*T;
|
ivan@137
|
74 Gamma = mxCreateSparse(m, L, allocated_coefs, mxREAL);
|
ivan@137
|
75 gammaPr = mxGetPr(Gamma);
|
ivan@137
|
76 gammaIr = mxGetIr(Gamma);
|
ivan@137
|
77 gammaJc = mxGetJc(Gamma);
|
ivan@137
|
78 gamma_count = 0;
|
ivan@137
|
79 gammaJc[0] = 0;
|
ivan@137
|
80 }
|
ivan@137
|
81
|
ivan@137
|
82
|
ivan@137
|
83 /*** helper arrays ***/
|
ivan@137
|
84
|
ivan@137
|
85 alpha = (double*)mxMalloc(m*sizeof(double)); /* contains D'*residual */
|
ivan@137
|
86 ind = (mwIndex*)mxMalloc(n*sizeof(mwIndex)); /* indices of selected atoms */
|
ivan@137
|
87 selected_atoms = (int*)mxMalloc(m*sizeof(int)); /* binary array with 1's for selected atoms */
|
ivan@137
|
88 c = (double*)mxMalloc(n*sizeof(double)); /* orthogonal projection result */
|
ivan@137
|
89
|
ivan@137
|
90 /* current number of columns in Dsub / Gsub / Lchol */
|
ivan@137
|
91 allocated_cols = erroromp ? (mwSize)(ceil(sqrt((double)n)/2.0) + 1.01) : T;
|
ivan@137
|
92
|
ivan@137
|
93 /* Cholesky decomposition of D_I'*D_I */
|
ivan@137
|
94 Lchol = (double*)mxMalloc(n*allocated_cols*sizeof(double));
|
ivan@137
|
95
|
ivan@137
|
96 /* temporary vectors for various computations */
|
ivan@137
|
97 tempvec1 = (double*)mxMalloc(m*sizeof(double));
|
ivan@137
|
98 tempvec2 = (double*)mxMalloc(m*sizeof(double));
|
ivan@137
|
99
|
ivan@137
|
100 if (batchomp) {
|
ivan@137
|
101 /* matrix containing G(:,ind) - the columns of G corresponding to the selected atoms, in order of selection */
|
ivan@137
|
102 Gsub = (double*)mxMalloc(m*allocated_cols*sizeof(double));
|
ivan@137
|
103 }
|
ivan@137
|
104 else {
|
ivan@137
|
105 /* matrix containing D(:,ind) - the selected atoms from D, in order of selection */
|
ivan@137
|
106 Dsub = (double*)mxMalloc(n*allocated_cols*sizeof(double));
|
ivan@137
|
107
|
ivan@137
|
108 /* stores the residual */
|
ivan@137
|
109 r = (double*)mxMalloc(n*sizeof(double));
|
ivan@137
|
110 }
|
ivan@137
|
111
|
ivan@137
|
112 if (!DtX_specified) {
|
ivan@137
|
113 /* contains D'*x for the current signal */
|
ivan@137
|
114 DtX = (double*)mxMalloc(m*sizeof(double));
|
ivan@137
|
115 }
|
ivan@137
|
116
|
ivan@137
|
117
|
ivan@137
|
118
|
ivan@137
|
119 /*** initializations for error omp ***/
|
ivan@137
|
120
|
ivan@137
|
121 if (erroromp) {
|
ivan@137
|
122 eps2 = eps*eps; /* compute eps^2 */
|
ivan@137
|
123 if (T<0 || T>n) { /* unspecified max atom num - set max atoms to n */
|
ivan@137
|
124 T = n;
|
ivan@137
|
125 }
|
ivan@137
|
126 }
|
ivan@137
|
127
|
ivan@137
|
128
|
ivan@137
|
129
|
ivan@137
|
130 /*** initialize timers ***/
|
ivan@137
|
131
|
ivan@137
|
132 initprofdata(&pd); /* initialize profiling counters */
|
ivan@137
|
133 starttime = clock(); /* record starting time for eta computations */
|
ivan@137
|
134 lastprint_time = starttime; /* time of last status display */
|
ivan@137
|
135
|
ivan@137
|
136
|
ivan@137
|
137
|
ivan@137
|
138 /********************** perform omp for each signal **********************/
|
ivan@137
|
139
|
ivan@137
|
140
|
ivan@137
|
141
|
ivan@137
|
142 for (signum=0; signum<L; ++signum) {
|
ivan@137
|
143
|
ivan@137
|
144
|
ivan@137
|
145 /* initialize residual norm and deltaprev for error-omp */
|
ivan@137
|
146
|
ivan@137
|
147 if (erroromp) {
|
ivan@137
|
148 if (XtX_specified) {
|
ivan@137
|
149 resnorm = XtX[signum];
|
ivan@137
|
150 }
|
ivan@137
|
151 else {
|
ivan@137
|
152 resnorm = dotprod(x+n*signum, x+n*signum, n);
|
ivan@137
|
153 addproftime(&pd, XtX_TIME);
|
ivan@137
|
154 }
|
ivan@137
|
155 deltaprev = 0; /* delta tracks the value of gamma'*G*gamma */
|
ivan@137
|
156 }
|
ivan@137
|
157 else {
|
ivan@137
|
158 /* ignore residual norm stopping criterion */
|
ivan@137
|
159 eps2 = 0;
|
ivan@137
|
160 resnorm = 1;
|
ivan@137
|
161 }
|
ivan@137
|
162
|
ivan@137
|
163
|
ivan@137
|
164 if (resnorm>eps2 && T>0) {
|
ivan@137
|
165
|
ivan@137
|
166 /* compute DtX */
|
ivan@137
|
167
|
ivan@137
|
168 if (!DtX_specified) {
|
ivan@137
|
169 matT_vec(1, D, x+n*signum, DtX, n, m);
|
ivan@137
|
170 addproftime(&pd, DtX_TIME);
|
ivan@137
|
171 }
|
ivan@137
|
172
|
ivan@137
|
173
|
ivan@137
|
174 /* initialize alpha := DtX */
|
ivan@137
|
175
|
ivan@137
|
176 memcpy(alpha, DtX + m*signum*DtX_specified, m*sizeof(double));
|
ivan@137
|
177
|
ivan@137
|
178
|
ivan@137
|
179 /* mark all atoms as unselected */
|
ivan@137
|
180
|
ivan@137
|
181 for (i=0; i<m; ++i) {
|
ivan@137
|
182 selected_atoms[i] = 0;
|
ivan@137
|
183 }
|
ivan@137
|
184
|
ivan@137
|
185 }
|
ivan@137
|
186
|
ivan@137
|
187
|
ivan@137
|
188 /* main loop */
|
ivan@137
|
189
|
ivan@137
|
190 i=0;
|
ivan@137
|
191 while (resnorm>eps2 && i<T) {
|
ivan@137
|
192
|
ivan@137
|
193 /* index of next atom */
|
ivan@137
|
194
|
ivan@137
|
195 pos = maxabs(alpha, m);
|
ivan@137
|
196 addproftime(&pd, MAXABS_TIME);
|
ivan@137
|
197
|
ivan@137
|
198
|
ivan@137
|
199 /* stop criterion: selected same atom twice, or inner product too small */
|
ivan@137
|
200
|
ivan@137
|
201 if (selected_atoms[pos] || alpha[pos]*alpha[pos]<1e-14) {
|
ivan@137
|
202 break;
|
ivan@137
|
203 }
|
ivan@137
|
204
|
ivan@137
|
205
|
ivan@137
|
206 /* mark selected atom */
|
ivan@137
|
207
|
ivan@137
|
208 ind[i] = pos;
|
ivan@137
|
209 selected_atoms[pos] = 1;
|
ivan@137
|
210
|
ivan@137
|
211
|
ivan@137
|
212 /* matrix reallocation */
|
ivan@137
|
213
|
ivan@137
|
214 if (erroromp && i>=allocated_cols) {
|
ivan@137
|
215
|
ivan@137
|
216 allocated_cols = (mwSize)(ceil(allocated_cols*MAT_INC_FACTOR) + 1.01);
|
ivan@137
|
217
|
ivan@137
|
218 Lchol = (double*)mxRealloc(Lchol,n*allocated_cols*sizeof(double));
|
ivan@137
|
219
|
ivan@137
|
220 batchomp ? (Gsub = (double*)mxRealloc(Gsub,m*allocated_cols*sizeof(double))) :
|
ivan@137
|
221 (Dsub = (double*)mxRealloc(Dsub,n*allocated_cols*sizeof(double))) ;
|
ivan@137
|
222 }
|
ivan@137
|
223
|
ivan@137
|
224
|
ivan@137
|
225 /* append column to Gsub or Dsub */
|
ivan@137
|
226
|
ivan@137
|
227 if (batchomp) {
|
ivan@137
|
228 memcpy(Gsub+i*m, G+pos*m, m*sizeof(double));
|
ivan@137
|
229 }
|
ivan@137
|
230 else {
|
ivan@137
|
231 memcpy(Dsub+i*n, D+pos*n, n*sizeof(double));
|
ivan@137
|
232 }
|
ivan@137
|
233
|
ivan@137
|
234
|
ivan@137
|
235 /*** Cholesky update ***/
|
ivan@137
|
236
|
ivan@137
|
237 if (i==0) {
|
ivan@137
|
238 *Lchol = 1;
|
ivan@137
|
239 }
|
ivan@137
|
240 else {
|
ivan@137
|
241
|
ivan@137
|
242 /* incremental Cholesky decomposition: compute next row of Lchol */
|
ivan@137
|
243
|
ivan@137
|
244 if (standardomp) {
|
ivan@137
|
245 matT_vec(1, Dsub, D+n*pos, tempvec1, n, i); /* compute tempvec1 := Dsub'*d where d is new atom */
|
ivan@137
|
246 addproftime(&pd, DtD_TIME);
|
ivan@137
|
247 }
|
ivan@137
|
248 else {
|
ivan@137
|
249 vec_assign(tempvec1, Gsub+i*m, ind, i); /* extract tempvec1 := Gsub(ind,i) */
|
ivan@137
|
250 }
|
ivan@137
|
251 backsubst('L', Lchol, tempvec1, tempvec2, n, i); /* compute tempvec2 = Lchol \ tempvec1 */
|
ivan@137
|
252 for (j=0; j<i; ++j) { /* write tempvec2 to end of Lchol */
|
ivan@137
|
253 Lchol[j*n+i] = tempvec2[j];
|
ivan@137
|
254 }
|
ivan@137
|
255
|
ivan@137
|
256 /* compute Lchol(i,i) */
|
ivan@137
|
257 sum = 0;
|
ivan@137
|
258 for (j=0; j<i; ++j) { /* compute sum of squares of last row without Lchol(i,i) */
|
ivan@137
|
259 sum += SQR(Lchol[j*n+i]);
|
ivan@137
|
260 }
|
ivan@137
|
261 if ( (1-sum) <= 1e-14 ) { /* Lchol(i,i) is zero => selected atoms are dependent */
|
ivan@137
|
262 break;
|
ivan@137
|
263 }
|
ivan@137
|
264 Lchol[i*n+i] = sqrt(1-sum);
|
ivan@137
|
265 }
|
ivan@137
|
266
|
ivan@137
|
267 addproftime(&pd, LCHOL_TIME);
|
ivan@137
|
268
|
ivan@137
|
269 i++;
|
ivan@137
|
270
|
ivan@137
|
271
|
ivan@137
|
272 /* perform orthogonal projection and compute sparse coefficients */
|
ivan@137
|
273
|
ivan@137
|
274 vec_assign(tempvec1, DtX + m*signum*DtX_specified, ind, i); /* extract tempvec1 = DtX(ind) */
|
ivan@137
|
275 cholsolve('L', Lchol, tempvec1, c, n, i); /* solve LL'c = tempvec1 for c */
|
ivan@137
|
276 addproftime(&pd, COMPCOEF_TIME);
|
ivan@137
|
277
|
ivan@137
|
278
|
ivan@137
|
279 /* update alpha = D'*residual */
|
ivan@137
|
280
|
ivan@137
|
281 if (standardomp) {
|
ivan@137
|
282 mat_vec(-1, Dsub, c, r, n, i); /* compute r := -Dsub*c */
|
ivan@137
|
283 vec_sum(1, x+n*signum, r, n); /* compute r := x+r */
|
ivan@137
|
284
|
ivan@137
|
285
|
ivan@137
|
286 /*memcpy(r, x+n*signum, n*sizeof(double)); /* assign r := x */
|
ivan@137
|
287 /*mat_vec1(-1, Dsub, c, 1, r, n, i); /* compute r := r-Dsub*c */
|
ivan@137
|
288
|
ivan@137
|
289 addproftime(&pd, COMPRES_TIME);
|
ivan@137
|
290 matT_vec(1, D, r, alpha, n, m); /* compute alpha := D'*r */
|
ivan@137
|
291 addproftime(&pd, DtR_TIME);
|
ivan@137
|
292
|
ivan@137
|
293 /* update residual norm */
|
ivan@137
|
294 if (erroromp) {
|
ivan@137
|
295 resnorm = dotprod(r, r, n);
|
ivan@137
|
296 addproftime(&pd, UPDATE_RESNORM_TIME);
|
ivan@137
|
297 }
|
ivan@137
|
298 }
|
ivan@137
|
299 else {
|
ivan@137
|
300 mat_vec(1, Gsub, c, tempvec1, m, i); /* compute tempvec1 := Gsub*c */
|
ivan@137
|
301 memcpy(alpha, DtX + m*signum*DtX_specified, m*sizeof(double)); /* set alpha = D'*x */
|
ivan@137
|
302 vec_sum(-1, tempvec1, alpha, m); /* compute alpha := alpha - tempvec1 */
|
ivan@137
|
303 addproftime(&pd, UPDATE_DtR_TIME);
|
ivan@137
|
304
|
ivan@137
|
305 /* update residual norm */
|
ivan@137
|
306 if (erroromp) {
|
ivan@137
|
307 vec_assign(tempvec2, tempvec1, ind, i); /* assign tempvec2 := tempvec1(ind) */
|
ivan@137
|
308 delta = dotprod(c,tempvec2,i); /* compute c'*tempvec2 */
|
ivan@137
|
309 resnorm = resnorm - delta + deltaprev; /* residual norm update */
|
ivan@137
|
310 deltaprev = delta;
|
ivan@137
|
311 addproftime(&pd, UPDATE_RESNORM_TIME);
|
ivan@137
|
312 }
|
ivan@137
|
313 }
|
ivan@137
|
314 }
|
ivan@137
|
315
|
ivan@137
|
316
|
ivan@137
|
317 /*** generate output vector gamma ***/
|
ivan@137
|
318
|
ivan@137
|
319 if (gamma_mode == FULL_GAMMA) { /* write the coefs in c to their correct positions in gamma */
|
ivan@137
|
320 for (j=0; j<i; ++j) {
|
ivan@137
|
321 gammaPr[m*signum + ind[j]] = c[j];
|
ivan@137
|
322 }
|
ivan@137
|
323 }
|
ivan@137
|
324 else {
|
ivan@137
|
325 /* sort the coefs by index before writing them to gamma */
|
ivan@137
|
326 quicksort(ind,c,i);
|
ivan@137
|
327 addproftime(&pd, INDEXSORT_TIME);
|
ivan@137
|
328
|
ivan@137
|
329 /* gamma is full - reallocate */
|
ivan@137
|
330 if (gamma_count+i >= allocated_coefs) {
|
ivan@137
|
331
|
ivan@137
|
332 while(gamma_count+i >= allocated_coefs) {
|
ivan@137
|
333 allocated_coefs = (mwSize)(ceil(GAMMA_INC_FACTOR*allocated_coefs) + 1.01);
|
ivan@137
|
334 }
|
ivan@137
|
335
|
ivan@137
|
336 mxSetNzmax(Gamma, allocated_coefs);
|
ivan@137
|
337 mxSetPr(Gamma, mxRealloc(gammaPr, allocated_coefs*sizeof(double)));
|
ivan@137
|
338 mxSetIr(Gamma, mxRealloc(gammaIr, allocated_coefs*sizeof(mwIndex)));
|
ivan@137
|
339
|
ivan@137
|
340 gammaPr = mxGetPr(Gamma);
|
ivan@137
|
341 gammaIr = mxGetIr(Gamma);
|
ivan@137
|
342 }
|
ivan@137
|
343
|
ivan@137
|
344 /* append coefs to gamma and update the indices */
|
ivan@137
|
345 for (j=0; j<i; ++j) {
|
ivan@137
|
346 gammaPr[gamma_count] = c[j];
|
ivan@137
|
347 gammaIr[gamma_count] = ind[j];
|
ivan@137
|
348 gamma_count++;
|
ivan@137
|
349 }
|
ivan@137
|
350 gammaJc[signum+1] = gammaJc[signum] + i;
|
ivan@137
|
351 }
|
ivan@137
|
352
|
ivan@137
|
353
|
ivan@137
|
354
|
ivan@137
|
355 /*** display status messages ***/
|
ivan@137
|
356
|
ivan@137
|
357 if (msg_delta>0 && (clock()-lastprint_time)/(double)CLOCKS_PER_SEC >= msg_delta)
|
ivan@137
|
358 {
|
ivan@137
|
359 lastprint_time = clock();
|
ivan@137
|
360
|
ivan@137
|
361 /* estimated remainig time */
|
ivan@137
|
362 secs2hms( ((L-signum-1)/(double)(signum+1)) * ((lastprint_time-starttime)/(double)CLOCKS_PER_SEC) ,
|
ivan@137
|
363 &hrs_remain, &mins_remain, &secs_remain);
|
ivan@137
|
364
|
ivan@137
|
365 mexPrintf("omp: signal %d / %d, estimated remaining time: %02d:%02d:%05.2f\n",
|
ivan@137
|
366 signum+1, L, hrs_remain, mins_remain, secs_remain);
|
ivan@137
|
367 mexEvalString("drawnow;");
|
ivan@137
|
368 }
|
ivan@137
|
369
|
ivan@137
|
370 }
|
ivan@137
|
371
|
ivan@137
|
372 /* end omp */
|
ivan@137
|
373
|
ivan@137
|
374
|
ivan@137
|
375
|
ivan@137
|
376 /*** print final messages ***/
|
ivan@137
|
377
|
ivan@137
|
378 if (msg_delta>0) {
|
ivan@137
|
379 mexPrintf("omp: signal %d / %d\n", signum, L);
|
ivan@137
|
380 }
|
ivan@137
|
381
|
ivan@137
|
382 if (profile) {
|
ivan@137
|
383 printprofinfo(&pd, erroromp, batchomp, L);
|
ivan@137
|
384 }
|
ivan@137
|
385
|
ivan@137
|
386
|
ivan@137
|
387
|
ivan@137
|
388 /* free memory */
|
ivan@137
|
389
|
ivan@137
|
390 if (!DtX_specified) {
|
ivan@137
|
391 mxFree(DtX);
|
ivan@137
|
392 }
|
ivan@137
|
393 if (standardomp) {
|
ivan@137
|
394 mxFree(r);
|
ivan@137
|
395 mxFree(Dsub);
|
ivan@137
|
396 }
|
ivan@137
|
397 else {
|
ivan@137
|
398 mxFree(Gsub);
|
ivan@137
|
399 }
|
ivan@137
|
400 mxFree(tempvec2);
|
ivan@137
|
401 mxFree(tempvec1);
|
ivan@137
|
402 mxFree(Lchol);
|
ivan@137
|
403 mxFree(c);
|
ivan@137
|
404 mxFree(selected_atoms);
|
ivan@137
|
405 mxFree(ind);
|
ivan@137
|
406 mxFree(alpha);
|
ivan@137
|
407
|
ivan@137
|
408 return Gamma;
|
ivan@137
|
409 }
|