Skip to content

Commit ba4d5c8

Browse files
committed
added omp pragmas
1 parent 64ea6eb commit ba4d5c8

File tree

1 file changed

+82
-76
lines changed

1 file changed

+82
-76
lines changed

src/lower-level/resmerge-ll.c

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -120,110 +120,116 @@ short **toa_ = NULL;
120120
if ((bands[b++] = find_domain(TOA, "SWIR1")) < 0) return FAILURE;
121121
if ((bands[b++] = find_domain(TOA, "SWIR2")) < 0) return FAILURE;
122122

123+
#pragma omp parallel private() shared() default(none)
124+
{
123125

124-
/** initialize and allocate
125-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
126+
/** initialize and allocate
127+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
126128

127-
// kernel size
128-
w = r*2+1; nw = w*w;
129+
// kernel size
130+
w = r*2+1; nw = w*w;
129131

130-
// nw-by-nv predictor variables; kernel + central pixel
131-
X = gsl_matrix_calloc(nw, nv);
132-
x = gsl_vector_calloc(nv);
133-
134-
// set first column of X to 1 -> intercept c0
135-
for (k=0; k<nw; k++) gsl_matrix_set(X, k, 0, 1.0);
136-
gsl_vector_set(x, 0, 1.0);
132+
// nw-by-nv predictor variables; kernel + central pixel
133+
X = gsl_matrix_calloc(nw, nv);
134+
x = gsl_vector_calloc(nv);
135+
136+
// set first column of X to 1 -> intercept c0
137+
for (k=0; k<nw; k++) gsl_matrix_set(X, k, 0, 1.0);
138+
gsl_vector_set(x, 0, 1.0);
137139

138-
// vector of nw observations
139-
alloc((void**)&y, nb, sizeof(gsl_vector*));
140-
for (b=0; b<nb; b++) y[b] = gsl_vector_calloc(nw);
140+
// vector of nw observations
141+
alloc((void**)&y, nb, sizeof(gsl_vector*));
142+
for (b=0; b<nb; b++) y[b] = gsl_vector_calloc(nw);
141143

142-
// nv regression coefficients
143-
alloc((void**)&c, nb, sizeof(gsl_vector*));
144-
for (b=0; b<nb; b++) c[b] = gsl_vector_calloc(nv);
144+
// nv regression coefficients
145+
alloc((void**)&c, nb, sizeof(gsl_vector*));
146+
for (b=0; b<nb; b++) c[b] = gsl_vector_calloc(nv);
145147

146-
// nv-by-nv covariance matrix
147-
alloc((void**)&cov, nb, sizeof(gsl_matrix*));
148-
for (b=0; b<nb; b++) cov[b] = gsl_matrix_calloc(nv, nv);
148+
// nv-by-nv covariance matrix
149+
alloc((void**)&cov, nb, sizeof(gsl_matrix*));
150+
for (b=0; b<nb; b++) cov[b] = gsl_matrix_calloc(nv, nv);
149151

150-
// workspace
151-
alloc((void**)&work, nb, sizeof(gsl_multifit_linear_workspace*));
152-
for (b=0; b<nb; b++) work[b] = gsl_multifit_linear_alloc(nw, nv);
152+
// workspace
153+
alloc((void**)&work, nb, sizeof(gsl_multifit_linear_workspace*));
154+
for (b=0; b<nb; b++) work[b] = gsl_multifit_linear_alloc(nw, nv);
153155

154156

155-
/** do regression for every valid pixel, and for each 20m band
156-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
157-
for (i=0, p=0; i<ny; i++){
158-
for (j=0; j<nx; j++, p++){
157+
/** do regression for every valid pixel, and for each 20m band
158+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
159159

160-
if (get_off(QAI, p) || get_cloud(QAI, p) > 0 || get_shadow(QAI, p)) continue;
160+
#pragma omp for schedule(guided)
161+
for (i=0, p=0; i<ny; i++){
162+
for (j=0; j<nx; j++, p++){
161163

162-
// add central pixel
163-
gsl_vector_set(x, 1, toa_[green][p]/10000.0);
164-
gsl_vector_set(x, 2, toa_[red][p]/10000.0);
165-
gsl_vector_set(x, 3, toa_[bnir][p]/10000.0);
164+
if (get_off(QAI, p) || get_cloud(QAI, p) > 0 || get_shadow(QAI, p)) continue;
166165

167-
k = 0;
166+
// add central pixel
167+
gsl_vector_set(x, 1, toa_[green][p]/10000.0);
168+
gsl_vector_set(x, 2, toa_[red][p]/10000.0);
169+
gsl_vector_set(x, 3, toa_[bnir][p]/10000.0);
168170

169-
// add neighboring pixels
170-
for (ii=-r; ii<=r; ii++){
171-
for (jj=-r; jj<=r; jj++){
171+
k = 0;
172172

173-
ni = i+ii; nj = j+jj;
174-
if (ni < 0 || ni >= ny || nj < 0 || nj >= nx) continue;
175-
np = ni*nx+nj;
173+
// add neighboring pixels
174+
for (ii=-r; ii<=r; ii++){
175+
for (jj=-r; jj<=r; jj++){
176176

177-
if (get_off(QAI, np)) continue;
177+
ni = i+ii; nj = j+jj;
178+
if (ni < 0 || ni >= ny || nj < 0 || nj >= nx) continue;
179+
np = ni*nx+nj;
178180

179-
gsl_matrix_set(X, k, 1, toa_[green][np]/10000.0);
180-
gsl_matrix_set(X, k, 2, toa_[red][np]/10000.0);
181-
gsl_matrix_set(X, k, 3, toa_[bnir][np]/10000.0);
182-
183-
for (b=0; b<nb; b++) gsl_vector_set(y[b], k, toa_[bands[b]][np]/10000.0);
181+
if (get_off(QAI, np)) continue;
184182

185-
k++;
183+
gsl_matrix_set(X, k, 1, toa_[green][np]/10000.0);
184+
gsl_matrix_set(X, k, 2, toa_[red][np]/10000.0);
185+
gsl_matrix_set(X, k, 3, toa_[bnir][np]/10000.0);
186+
187+
for (b=0; b<nb; b++) gsl_vector_set(y[b], k, toa_[bands[b]][np]/10000.0);
186188

187-
}
188-
}
189+
k++;
189190

190-
// append zeros, if less than nw neighboring pixels were added
191-
while (k < nw){
192-
gsl_matrix_set(X, k, 1, 0.0);
193-
gsl_matrix_set(X, k, 2, 0.0);
194-
gsl_matrix_set(X, k, 3, 0.0);
195-
for (b=0; b<nb; b++) gsl_vector_set(y[b], k, 0.0);
196-
k++;
197-
}
191+
}
192+
}
198193

199-
// solve model, and predict central pixel
200-
for (b=0; b<nb; b++){
194+
// append zeros, if less than nw neighboring pixels were added
195+
while (k < nw){
196+
gsl_matrix_set(X, k, 1, 0.0);
197+
gsl_matrix_set(X, k, 2, 0.0);
198+
gsl_matrix_set(X, k, 3, 0.0);
199+
for (b=0; b<nb; b++) gsl_vector_set(y[b], k, 0.0);
200+
k++;
201+
}
202+
203+
// solve model, and predict central pixel
204+
for (b=0; b<nb; b++){
201205

202-
gsl_multifit_linear(X, y[b], c[b], cov[b], &chisq, work[b]);
203-
gsl_multifit_linear_est(x, c[b], cov[b], &est, &err);
204-
toa_[bands[b]][p] = (short)(est*10000);
206+
gsl_multifit_linear(X, y[b], c[b], cov[b], &chisq, work[b]);
207+
gsl_multifit_linear_est(x, c[b], cov[b], &est, &err);
208+
toa_[bands[b]][p] = (short)(est*10000);
205209

210+
}
211+
212+
}
206213
}
207214

208-
}
209-
}
210215

216+
#ifdef FORCE_DEBUG
217+
set_brick_filename(TOA, "TOA-RESMERGED");
218+
print_brick_info(TOA); set_brick_open(TOA, OPEN_CREATE); write_brick(TOA);
219+
#endif
211220

212-
#ifdef FORCE_DEBUG
213-
set_brick_filename(TOA, "TOA-RESMERGED");
214-
print_brick_info(TOA); set_brick_open(TOA, OPEN_CREATE); write_brick(TOA);
215-
#endif
216221

222+
/** clean
223+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
224+
gsl_matrix_free (X); gsl_vector_free (x);
225+
for (b=0; b<nb; b++) gsl_vector_free(y[b]);
226+
for (b=0; b<nb; b++) gsl_vector_free (c[b]);
227+
for (b=0; b<nb; b++) gsl_matrix_free (cov[b]);
228+
for (b=0; b<nb; b++) gsl_multifit_linear_free(work[b]);
229+
free((void*)y); free((void*)c);
230+
free((void*)cov); free((void*)work);
217231

218-
/** clean
219-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
220-
gsl_matrix_free (X); gsl_vector_free (x);
221-
for (b=0; b<nb; b++) gsl_vector_free(y[b]);
222-
for (b=0; b<nb; b++) gsl_vector_free (c[b]);
223-
for (b=0; b<nb; b++) gsl_matrix_free (cov[b]);
224-
for (b=0; b<nb; b++) gsl_multifit_linear_free(work[b]);
225-
free((void*)y); free((void*)c);
226-
free((void*)cov); free((void*)work);
232+
}
227233

228234
#ifdef FORCE_CLOCK
229235
proctime_print("Resolution merge", TIME);

0 commit comments

Comments
 (0)