Skip to content

Commit

Permalink
Fix memory allocation in cmultiscale code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lars2015 committed Sep 25, 2024
1 parent dde113b commit 9b62292
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/mptrac.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,12 @@ void compress_cms(
#pragma omp parallel for default(shared)
for (size_t ip = 0; ip < np; ip++) {

/* Allocate... */
float *tmp_arr;
ALLOC(tmp_arr, float,
nxy);

/* Copy level data... */
float tmp_arr[nxy];
for (size_t ix = 0; ix < nx; ++ix)
for (size_t iy = 0; iy < ny; ++iy)
tmp_arr[ARRAY_2D(ix, iy, ny)] = array[ARRAY_3D(ix, iy, ny, ip, np)];
Expand Down Expand Up @@ -557,14 +561,25 @@ void compress_cms(
/* Coarsening... */
cms_coarsening(cms_ptr[ip], cms_sol[ip],
(unsigned int) ctl->met_cms_heur);

/* Free... */
free(tmp_arr);
}

/* Loop over levels... */
double cr = 0;
for (size_t ip = 0; ip < np; ip++) {

/* Allocate... */
double *tmp_cms, *tmp_org, *tmp_diff;
ALLOC(tmp_cms, double,
nxy);
ALLOC(tmp_org, double,
nxy);
ALLOC(tmp_diff, double,
nxy);

/* Evaluate... */
double tmp_cms[nxy], tmp_org[nxy], tmp_diff[nxy];
#pragma omp parallel for default(shared)
for (size_t ix = 0; ix < nx; ix++)
for (size_t iy = 0; iy < ny; iy++) {
Expand Down Expand Up @@ -593,6 +608,9 @@ void compress_cms(
/* Free... */
cms_delete_sol(cms_sol[ip]);
cms_delete_module(cms_ptr[ip]);
free(tmp_cms);
free(tmp_org);
free(tmp_diff);
}

/* Write info... */
Expand Down Expand Up @@ -8901,7 +8919,7 @@ void write_csi(
|| modmean[idx] >= ctl->csi_modmin)) {
x[n] = modmean[idx];
y[n] = obsmean[idx];
if (modmean[idx] >= ctl->csi_modmin)
if (modmean[idx] >= ctl->csi_modmin)
obsstdn[n] = obsstd[idx];
if ((++n) >= NCSI)
ERRMSG("Too many data points to calculate statistics!");
Expand Down

0 comments on commit 9b62292

Please sign in to comment.