Skip to content

Commit 71773fd

Browse files
committed
Merge branch 'resolution_merge_1_parallel' into develop
2 parents 64ea6eb + 4691d2a commit 71773fd

File tree

1 file changed

+85
-76
lines changed

1 file changed

+85
-76
lines changed

src/lower-level/resmerge-ll.c

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -120,110 +120,119 @@ 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-
124-
/** initialize and allocate
125-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
126-
127123
// kernel size
128124
w = r*2+1; nw = w*w;
129125

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);
137126

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);
127+
#pragma omp parallel private(k,b,j,p,ii,jj,ni,nj,np,X,x,y,c,cov,work,chisq,est,err) shared(r,w,nb,nw,nv,ny,nx,QAI,toa_,bands,green,red,bnir) default(none)
128+
{
141129

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);
130+
/** initialize and allocate
131+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
145132

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);
133+
// nw-by-nv predictor variables; kernel + central pixel
134+
X = gsl_matrix_calloc(nw, nv);
135+
x = gsl_vector_calloc(nv);
136+
137+
// set first column of X to 1 -> intercept c0
138+
for (k=0; k<nw; k++) gsl_matrix_set(X, k, 0, 1.0);
139+
gsl_vector_set(x, 0, 1.0);
149140

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);
141+
// vector of nw observations
142+
alloc((void**)&y, nb, sizeof(gsl_vector*));
143+
for (b=0; b<nb; b++) y[b] = gsl_vector_calloc(nw);
153144

145+
// nv regression coefficients
146+
alloc((void**)&c, nb, sizeof(gsl_vector*));
147+
for (b=0; b<nb; b++) c[b] = gsl_vector_calloc(nv);
154148

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++){
149+
// nv-by-nv covariance matrix
150+
alloc((void**)&cov, nb, sizeof(gsl_matrix*));
151+
for (b=0; b<nb; b++) cov[b] = gsl_matrix_calloc(nv, nv);
159152

160-
if (get_off(QAI, p) || get_cloud(QAI, p) > 0 || get_shadow(QAI, p)) continue;
153+
// workspace
154+
alloc((void**)&work, nb, sizeof(gsl_multifit_linear_workspace*));
155+
for (b=0; b<nb; b++) work[b] = gsl_multifit_linear_alloc(nw, nv);
161156

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);
166157

167-
k = 0;
158+
/** do regression for every valid pixel, and for each 20m band
159+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
168160

169-
// add neighboring pixels
170-
for (ii=-r; ii<=r; ii++){
171-
for (jj=-r; jj<=r; jj++){
161+
#pragma omp for schedule(guided)
162+
for (i=0; i<ny; i++){
163+
for (j=0; j<nx; j++){
172164

173-
ni = i+ii; nj = j+jj;
174-
if (ni < 0 || ni >= ny || nj < 0 || nj >= nx) continue;
175-
np = ni*nx+nj;
165+
p = i*nx+j;
176166

177-
if (get_off(QAI, np)) continue;
167+
if (get_off(QAI, p) || get_cloud(QAI, p) > 0 || get_shadow(QAI, p)) continue;
178168

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);
169+
// add central pixel
170+
gsl_vector_set(x, 1, toa_[green][p]/10000.0);
171+
gsl_vector_set(x, 2, toa_[red][p]/10000.0);
172+
gsl_vector_set(x, 3, toa_[bnir][p]/10000.0);
184173

185-
k++;
174+
k = 0;
186175

187-
}
188-
}
176+
// add neighboring pixels
177+
for (ii=-r; ii<=r; ii++){
178+
for (jj=-r; jj<=r; jj++){
189179

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-
}
180+
ni = i+ii; nj = j+jj;
181+
if (ni < 0 || ni >= ny || nj < 0 || nj >= nx) continue;
182+
np = ni*nx+nj;
183+
184+
if (get_off(QAI, np)) continue;
185+
186+
gsl_matrix_set(X, k, 1, toa_[green][np]/10000.0);
187+
gsl_matrix_set(X, k, 2, toa_[red][np]/10000.0);
188+
gsl_matrix_set(X, k, 3, toa_[bnir][np]/10000.0);
189+
190+
for (b=0; b<nb; b++) gsl_vector_set(y[b], k, toa_[bands[b]][np]/10000.0);
191+
192+
k++;
198193

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

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);
209+
gsl_multifit_linear(X, y[b], c[b], cov[b], &chisq, work[b]);
210+
gsl_multifit_linear_est(x, c[b], cov[b], &est, &err);
211+
toa_[bands[b]][p] = (short)(est*10000);
205212

213+
}
214+
215+
}
206216
}
207217

208-
}
209-
}
210218

219+
#ifdef FORCE_DEBUG
220+
set_brick_filename(TOA, "TOA-RESMERGED");
221+
print_brick_info(TOA); set_brick_open(TOA, OPEN_CREATE); write_brick(TOA);
222+
#endif
211223

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
216224

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

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);
235+
}
227236

228237
#ifdef FORCE_CLOCK
229238
proctime_print("Resolution merge", TIME);

0 commit comments

Comments
 (0)