MIDAPACK - MIcrowave Data Analysis PACKage  1.1b
Parallel software tools for high performance CMB DA analysis
toeplitz_gappy_seq.dev.c
Go to the documentation of this file.
1 
2 
3 //=========================================================================
4 // Alternave version of the sequential routine for the gaps - in dev
5 // Need to change the name to gap_reduce_matrix for more explicit purpose
6 // fd@apc
7 int gstmm(double **V, int n, int m, int id0, int l, fftw_complex *T_fft,
8  int lambda, fftw_complex *V_fft, double *V_rfft, fftw_plan plan_f,
9  fftw_plan plan_b, int blocksize, int nfft, int *id0gap, int *lgap,
10  int ngap) {
11 
12  // routine variable
13  int i, j, k, p; // loop index
14  int idf = id0 + l - 1;
15  int cfirst = id0 / n; // first column index
16  int clast = idf / n; // last column index
17  int clast_1 = (idf + 1) / n;
18  int m_eff = clast - cfirst + 1; // number of columns
19  int rfirst = id0 % n;
20  int rlast = idf % n;
21 
22  if (l < lambda) // test to avoid communications errors
23  return print_error_message(1, __FILE__, __LINE__);
24 
25  int nnew = 0;
26  int lcolnew = 0;
27  int lcol;
28  double *Vcol;
29  int icol, igap;
30  ;
31  int id0col;
32  int id0out = 0;
33  int lnew;
34 
35  int nfullcol;
36  nfullcol = (l - (n - id0 % n) - (id0 + l) % n)
37  / n; // check how many full columns input data have
38 
39  int ifirstgap, ilastgap;
40 
41  if (id0 % n != 0) {
42  // first column if not full
43  id0out = 0;
44  id0col = id0 % n;
45  Vcol = (*V) + id0col;
46  lcol = n - id0col;
47 
48  // find the first and last gap on the column
49  for (k = 0; k < ngap; k++) {
50  if (lgap[k] != 0 && id0col < id0gap[k] + lgap[k]) break;
51  }
52  ifirstgap = k;
53 
54  for (k = ngap - 1; k >= 0; k--) {
55  if (lgap[k] != 0 && id0gap[k] <= id0col) break;
56  }
57  ilastgap = k;
58 
59 
60  for (i = 0; i < ngap; i++) printf("id0gap[%d]=%d\n", i, id0gap[i]);
61  for (i = 0; i < ngap; i++) printf("lgap[%d]=%d\n", i, lgap[i]);
62 
63  printf("---\n");
64 
65  printf("lcol=%d\n", lcol);
66  for (i = 0; i < lcol; i++) printf("Vcol[%d]=%f\n", i, Vcol[i]);
67 
68  // reduce the gap to lambda zeros on the column
69  gap_reduce(&Vcol, id0col, lcol, lambda, id0gap, lgap, ngap, &lcolnew,
70  id0out);
71 
72  id0out += lcolnew;
73 
74  printf("---\n");
75 
76  for (i = 0; i < lcol; i++) printf("Vcolout[%d]=%f\n", i, Vcol[i]);
77  printf("===\n");
78 
79  printf("---\n");
80 
81  for (i = 0; i < l; i++) printf("(*V)[%d]=%f\n", i, (*V)[i]);
82  }
83 
84  // generic loop on the full column
85  for (icol = 0; icol < nfullcol; icol++) {
86 
87  id0out = lcolnew;
88  id0col = 0;
89  Vcol = (*V) + id0col;
90  lcol = n - id0col;
91 
92  gap_reduce(&Vcol, id0col, lcol, lambda, id0gap, lgap, ngap, &lcolnew,
93  id0out);
94 
95  id0out += lcolnew;
96 
97  if (icol == 0) // take the nnew directly if they is a full column
98  nnew = lcolnew;
99  }
100 
101 
102  // last column if not full and more than one column
103  if (idf % n != n && m > 1) {
104 
105  id0out = 0;
106  id0col = 0;
107  Vcol = (*V) + id0col;
108  lcol = idf % n;
109 
110  // find the first and last gap on the column
111  ifirstgap = 0; // we already know it, no need to compute
112 
113  for (k = ngap - 1; k >= 0; k--) {
114  if (lgap[k] != 0 && id0gap[k] <= id0col) break;
115  }
116  ilastgap = k;
117 
118  // reduce the gap to lambda zeros on the column
119  gap_reduce(&Vcol, id0col, lcol, lambda, id0gap, lgap, ngap, &lcolnew,
120  id0out);
121  }
122 
123 
124  // cleaning the extra terms
125  if (nnew == 0) { // compute the nnew if there is no full column
126  for (igap = 0; igap < ngap; igap++)
127  nnew += id0gap[igap] - min(id0gap[igap], lambda);
128  }
129 
130  for (i = nnew * m; i < n * m; i++) (*V)[i] = 0;
131 
132  printf("===\n");
133  for (i = 0; i < l; i++) printf("(*V)[%d]=%f\n", i, (*V)[i]);
134 
135 
136  // now do the product
137  // int id0new = ..;
138 
139  // stmm( V, nnew, m, id0new, lnew, T_fft, lambda, V_fft, V_rfft, plan_f,
140  // plan_b, blocksize, nfft)
141 
142 
143  // gap extend - same thing as above and reverse gap_reduce routine
144  // gap_extend()
145 
146  // maybe create gap_blockreduce to clarify this routine.
147 
148  return 0;
149 }
150 
151 //=========================================================================
152 
155 
162 int gap_reduce(double **V, int id0, int l, int lambda, int *id0gap, int *lgap,
163  int ngap, int *newl, int id0out) {
164  // routine variables
165  int i, k, ii;
166  int lg;
167  int newid0gap_ip1;
168  int newlgap_ip1;
169 
170 
171  double *Vout;
172  Vout = (*V) - (id0 - id0out);
173 
174 
175  if (id0gap[0] > id0) {
176  lg = id0gap[0] - id0;
177  printf("lg=%d\n", lg);
178  memmove(&(Vout)[0], &(*V)[0], lg * sizeof(double));
179  // to do if the first element isn't a gap
180  }
181 
182  int offset_first = id0gap[0] - id0 + min(lambda, lgap[0]);
183  offset_first = max(0, offset_first);
184 
185 
186  // for (k=0; k<offset_first; k++)
187  // (Vout)[k] = 0;
188 
189  printf("offset_first=%d\n", offset_first);
190 
191  for (i = 0; i < l; i++) printf("(Vout)[%d]=%f\n", i, (Vout)[i]);
192  printf("---\n");
193 
194  printf("l=%d\n", l);
195 
196 
197  for (i = 0; i < (ngap - 1); i++) {
198  lg = id0gap[i + 1] - id0 - (id0gap[i] - id0 + lgap[i]);
199  printf("lg=%d\n", lg);
200  memmove(&(Vout)[offset_first], &(*V)[id0gap[i] - id0 + lgap[i]],
201  lg * sizeof(double));
202 
203  for (ii = 0; ii < l; ii++) printf("(Vout)[%d]=%f\n", ii, (Vout)[ii]);
204 
205  newid0gap_ip1 = offset_first + lg;
206  newlgap_ip1 = min(lambda, lgap[i + 1]);
207  for (k = newid0gap_ip1; k < newid0gap_ip1 + newlgap_ip1; k++)
208  (Vout)[k] = 0;
209 
210  printf("lg=%d ; lambda=%d ; lgap[i+1]=%d\n", lg, lambda, lgap[i + 1]);
211  offset_first += lg + min(lambda, lgap[i + 1]);
212  } // end gaps loop
213 
214  printf("---\n");
215  printf("offset_first=%d\n", offset_first);
216  for (i = 0; i < l; i++) printf("(Vout)[%d]=%f\n", i, (Vout)[i]);
217 
218  i = (ngap - 1);
219 
220  if (id0gap[i] - id0 + lgap[i] < l) {
221  printf("toc\n");
222  lg = l - (id0gap[i] - id0 + lgap[i]);
223  memmove(&(Vout)[offset_first], &(*V)[id0gap[i] - id0 + lgap[i]],
224  lg * sizeof(double));
225  offset_first += lg;
226  *newl = offset_first;
227  } else {
228  *newl = offset_first - min(lambda, lgap[ngap - 1])
229  + min(lambda, (id0 + l - id0gap[ngap - 1]));
230  }
231 
232  printf("---\n");
233  printf("l=%d, *newl=%d, offset_first=%d\n", l, *newl, offset_first);
234 
235  for (i = offset_first; i < l; i++) (Vout)[i] = 0;
236 
237  printf("*newl=%d\n", *newl);
238 
239 
240  return 0;
241 }
242 //=========================================================================
243 
245 
252 int gap_masking(double **V, int l, int *id0gap, int *lgap, int ngap) {
253 
254  int i, k;
255  int lg;
256 
257  int offset_first = id0gap[0];
258  for (i = 0; i < (ngap - 1); i++) {
259  lg = id0gap[i + 1] - (id0gap[i] + lgap[i]);
260  memmove(&(*V)[offset_first], &(*V)[id0gap[i] + lgap[i]],
261  lg * sizeof(double));
262  offset_first += lg;
263  }
264 
265  i = (ngap - 1);
266  lg = l - (id0gap[i] + lgap[i]);
267 
268  memmove(&(*V)[offset_first], &(*V)[id0gap[i] + lgap[i]],
269  lg * sizeof(double));
270  offset_first += lg;
271 
272 
273  for (i = offset_first; i < l; i++) (*V)[i] = 0;
274 
275 
276  return 0;
277 }
278 
279 
280 //=========================================================================
281 
283 
290 int gap_filling(double **V, int l, int *id0gap, int *lgap, int ngap) {
291 
292  int i, k;
293  int lg;
294 
295  int lgaptot = 0;
296  for (i = 0; i < ngap; i++) lgaptot += lgap[i];
297 
298  int id0_Vmv;
299  int offset_last;
300 
301  id0_Vmv = id0gap[ngap - 1] + lgap[ngap - 1];
302  offset_last = id0_Vmv - lgaptot;
303  lg = l - id0_Vmv;
304 
305  if (lg > 0)
306  memmove(&(*V)[id0_Vmv], &(*V)[offset_last], lg * sizeof(double));
307 
308  for (i = ngap - 2; i >= 0; i--) {
309  id0_Vmv = id0gap[i] + lgap[i];
310  lg = id0gap[i + 1] - id0_Vmv;
311  offset_last -= lg;
312 
313  memmove(&(*V)[id0_Vmv], &(*V)[offset_last], lg * sizeof(double));
314  }
315 
316  for (i = 0; i < ngap; i++)
317  for (k = 0; k < lgap[i]; k++) (*V)[id0gap[i] + k] = 0;
318 
319  return 0;
320 }
321 
322 
323 // a naive version - in dev
324 int gap_masking_naive(double **V, int id0, int local_V_size, int m, int nrow,
325  int *id0gap, int *lgap, int ngap) {
326  int i, j, k;
327  int icol;
328 
329  int offsetV = id0;
330  k = 0;
331 
332  for (i = 0; i < local_V_size; i++) {
333  icol = (i + id0) % nrow;
334 
335  if (icol < id0gap[k]) {
336  (*V)[offsetV] = (*V)[i];
337  offsetV = offsetV + 1;
338  } else if (icol >= id0gap[k] + lgap[k]) {
339  k = k + 1;
340  i = i - 1;
341  // (*V)[offsetV]=(*V)[i];
342  // offsetV=offsetV+1;
343  } else { // do not copy, just skip
344  }
345 
346 
347  return 0;
348  }
int print_error_message(int error_number, char const *file, int line)
Prints error message corresponding to an error number.
Definition: toeplitz.c:127
int gap_masking(double **V, int l, int *id0gap, int *lgap, int ngap)
Reduce the vector and mask the defined gaps.
int gap_reduce(double **V, int id0, int l, int lambda, int *id0gap, int *lgap, int ngap, int *newl, int id0out)
int gap_masking_naive(double **V, int id0, int local_V_size, int m, int nrow, int *id0gap, int *lgap, int ngap)
int gstmm(double **V, int n, int m, int id0, int l, fftw_complex *T_fft, int lambda, fftw_complex *V_fft, double *V_rfft, fftw_plan plan_f, fftw_plan plan_b, int blocksize, int nfft, int *id0gap, int *lgap, int ngap)
int gap_filling(double **V, int l, int *id0gap, int *lgap, int ngap)
Extend the vector and add zeros on the gaps locations.