Commit 65a3a187 authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

replaced c arrays with std::vector, replaced GridStore *. Refactored functions...

replaced c arrays with std::vector, replaced GridStore *. Refactored functions that take GridStore*  to GridStore&
parent 5f0ac0ab
Pipeline #4378 passed with stages
in 15 minutes and 1 second
......@@ -15,7 +15,6 @@
GNU General Public License for more details.
*/
#include "dmemory.h"
#include "process_int.h"
#include "cdo_wtime.h"
#include "remap.h"
......@@ -23,6 +22,7 @@
#include "cdo_options.h"
#include "progress.h"
#include "cimdOmp.h"
#include "dmemory.h"
#define ZERO 0.0
#define ONE 1.0
......@@ -99,8 +99,6 @@ pole_intersection(long *location, double *intrsct_lat, double *intrsct_lon, bool
double s1, s2, determ; /* variables used for linear solve to */
double mat1, mat2, mat3, mat4, rhs1, rhs2; /* find intersection */
double *srch_corner_x; /* x of each corner of srch cells */
double *srch_corner_y; /* y of each corner of srch cells */
/*printf("pole_intersection: %g %g %g %g\n", beglat, beglon, endlat,
* endlon);*/
......@@ -117,8 +115,8 @@ pole_intersection(long *location, double *intrsct_lat, double *intrsct_lon, bool
/* Convert coordinates */
srch_corner_x = (double *) Malloc(srch_corners * num_srch_cells * sizeof(double));
srch_corner_y = (double *) Malloc(srch_corners * num_srch_cells * sizeof(double));
std::vector<double> srch_corner_x(srch_corners * num_srch_cells);
std::vector<double> srch_corner_y(srch_corners * num_srch_cells);
if (beglat > ZERO)
{
......@@ -288,8 +286,6 @@ pole_intersection(long *location, double *intrsct_lat, double *intrsct_lon, bool
if (s1 >= ONE)
{
Free(srch_corner_y);
Free(srch_corner_x);
*luse_last = false;
return;
}
......@@ -433,9 +429,6 @@ after_srch_loop:
} /* intrsct_loop */
Free(srch_corner_y);
Free(srch_corner_x);
/*
If segment manages to cross over pole, shift the beginning
endpoint in order to avoid hitting pole directly
......@@ -1016,7 +1009,7 @@ line_integral(double *weights, double in_phi1, double in_phi2, double theta1, do
static void
correct_pole(RemapGrid *src_grid, RemapGrid *tgt_grid, RemapVars &rv, double *src_centroid_lat, double *src_centroid_lon,
double *tgt_centroid_lat, double *tgt_centroid_lon, GridStore *grid_store)
double *tgt_centroid_lat, double *tgt_centroid_lon, GridStore &grid_store)
{
/*
Correct for situations where N/S pole not explicitly included in
......@@ -1155,8 +1148,7 @@ normalize_weights(RemapGrid *tgt_grid, RemapVars &rv, double *src_centroid_lat,
if (rv.normOpt == NormOpt::DESTAREA)
{
#ifdef _OPENMP
#pragma omp parallel for default(none) \
shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#pragma omp parallel for default(none) shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#endif
for (long n = 0; n < num_links; ++n)
{
......@@ -1170,8 +1162,7 @@ normalize_weights(RemapGrid *tgt_grid, RemapVars &rv, double *src_centroid_lat,
else if (rv.normOpt == NormOpt::FRACAREA)
{
#ifdef _OPENMP
#pragma omp parallel for default(none) \
shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#pragma omp parallel for default(none) shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#endif
for (long n = 0; n < num_links; ++n)
{
......@@ -1179,16 +1170,17 @@ normalize_weights(RemapGrid *tgt_grid, RemapVars &rv, double *src_centroid_lat,
const auto tgt_add = rv.tgt_cell_add[n];
const auto tgt_cell_frac = tgt_grid->cell_frac[tgt_add];
const auto norm_factor = IS_NOT_EQUAL(tgt_cell_frac, 0) ? ONE / tgt_cell_frac : ZERO;
// printf("i: %ld %g %g %g %g %g %g\n", n+1, norm_factor, src_centroid_lon[src_add], src_centroid_lat[src_add], weights[n * 3], weights[n * 3+1], weights[n * 3+2]);
// printf("i: %ld %g %g %g %g %g %g\n", n+1, norm_factor, src_centroid_lon[src_add], src_centroid_lat[src_add],
// weights[n * 3], weights[n * 3+1], weights[n * 3+2]);
norm_weight(norm_factor, &weights[n * 3], src_centroid_lat[src_add], src_centroid_lon[src_add]);
// printf("o: %ld %g %g %g %g %g %g\n", n+1, norm_factor, src_centroid_lon[src_add], src_centroid_lat[src_add], weights[n * 3], weights[n * 3+1], weights[n * 3+2]);
// printf("o: %ld %g %g %g %g %g %g\n", n+1, norm_factor, src_centroid_lon[src_add], src_centroid_lat[src_add],
// weights[n * 3], weights[n * 3+1], weights[n * 3+2]);
}
}
else if (rv.normOpt == NormOpt::NONE)
{
#ifdef _OPENMP
#pragma omp parallel for default(none) \
shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#pragma omp parallel for default(none) shared(num_links, rv, weights, tgt_grid, src_centroid_lat, src_centroid_lon)
#endif
for (long n = 0; n < num_links; ++n)
{
......@@ -1252,7 +1244,7 @@ remapConservWeightsScrip(RemapSearch &rsearch, RemapVars &rv)
long num_wts = rv.num_wts;
GridStore *grid_store = (GridStore *) Malloc(sizeof(GridStore));
GridStore grid_store;
grid_store_init(grid_store, tgt_grid->size);
if (Options::cdoVerbose)
......@@ -1695,7 +1687,6 @@ remapConservWeightsScrip(RemapSearch &rsearch, RemapVars &rv)
correct_pole(src_grid, tgt_grid, rv, src_centroid_lat, src_centroid_lon, tgt_centroid_lat, tgt_centroid_lon, grid_store);
grid_store_delete(grid_store);
Free(grid_store);
if (rv.num_links != rv.max_links) remapVarsResize(rv, rv.num_links);
......
......@@ -22,7 +22,7 @@
#include "remap_store_link_cnsrv.h"
void
grid_store_init(GridStore *grid_store, long gridsize)
grid_store_init(GridStore &grid_store, long gridsize)
{
long iblk;
long blksize[] = { 128, 256, 512, 1024, 2048, 4096, 8192 };
......@@ -34,43 +34,43 @@ grid_store_init(GridStore *grid_store, long gridsize)
if (iblk < 0) iblk = 0;
/* grid_store->blk_size = BLK_SIZE; */
grid_store->blk_size = blksize[iblk];
grid_store->max_size = gridsize;
/* grid_store.blk_size = BLK_SIZE; */
grid_store.blk_size = blksize[iblk];
grid_store.max_size = gridsize;
grid_store->nblocks = grid_store->max_size / grid_store->blk_size;
if (grid_store->max_size % grid_store->blk_size > 0) grid_store->nblocks++;
grid_store.nblocks = grid_store.max_size / grid_store.blk_size;
if (grid_store.max_size % grid_store.blk_size > 0) grid_store.nblocks++;
if (Options::cdoVerbose)
fprintf(stdout, "blksize = %ld lastblksize = %ld max_size = %ld nblocks = %ld\n", grid_store->blk_size,
grid_store->max_size % grid_store->blk_size, grid_store->max_size, grid_store->nblocks);
fprintf(stdout, "blksize = %ld lastblksize = %ld max_size = %ld nblocks = %ld\n", grid_store.blk_size,
grid_store.max_size % grid_store.blk_size, grid_store.max_size, grid_store.nblocks);
grid_store->blksize = (long *) Malloc(grid_store->nblocks * sizeof(long));
grid_store->nlayers = (long *) Malloc(grid_store->nblocks * sizeof(long));
grid_store->layers = (GridLayer **) Malloc(grid_store->nblocks * sizeof(GridLayer *));
grid_store.blksize = (long *) Malloc(grid_store.nblocks * sizeof(long));
grid_store.nlayers = (long *) Malloc(grid_store.nblocks * sizeof(long));
grid_store.layers = (GridLayer **) Malloc(grid_store.nblocks * sizeof(GridLayer *));
nblocks = grid_store->nblocks;
nblocks = grid_store.nblocks;
for (iblk = 0; iblk < nblocks; ++iblk)
{
grid_store->blksize[iblk] = grid_store->blk_size;
grid_store->nlayers[iblk] = 0;
grid_store->layers[iblk] = nullptr;
grid_store.blksize[iblk] = grid_store.blk_size;
grid_store.nlayers[iblk] = 0;
grid_store.layers[iblk] = nullptr;
}
if (grid_store->max_size % grid_store->blk_size > 0)
grid_store->blksize[grid_store->nblocks - 1] = grid_store->max_size % grid_store->blk_size;
if (grid_store.max_size % grid_store.blk_size > 0)
grid_store.blksize[grid_store.nblocks - 1] = grid_store.max_size % grid_store.blk_size;
}
void
grid_store_delete(GridStore *grid_store)
grid_store_delete(GridStore &grid_store)
{
long nblocks = grid_store->nblocks;
long nblocks = grid_store.nblocks;
for (long iblk = 0; iblk < nblocks; ++iblk)
{
long j = 0;
GridLayer *grid_layer = grid_store->layers[iblk];
long nlayers = grid_store->nlayers[iblk];
long blksize = grid_store->blksize[iblk];
GridLayer *grid_layer = grid_store.layers[iblk];
long nlayers = grid_store.nlayers[iblk];
long blksize = grid_store.blksize[iblk];
for (long ilayer = 0; ilayer < nlayers; ++ilayer)
{
if (Options::cdoVerbose)
......@@ -93,9 +93,9 @@ grid_store_delete(GridStore *grid_store)
*/
}
Free(grid_store->blksize);
Free(grid_store->layers);
Free(grid_store->nlayers);
Free(grid_store.blksize);
Free(grid_store.layers);
Free(grid_store.nlayers);
}
/*
......@@ -103,7 +103,7 @@ grid_store_delete(GridStore *grid_store)
address and weight arrays and resizes those arrays if necessary.
*/
void
store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weights, GridStore *grid_store)
store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weights, GridStore &grid_store)
{
/*
Input variables:
......@@ -131,8 +131,8 @@ store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weig
long iadd2 = BLK_IDX(add2);
bool lstore_link = false;
GridLayer **grid_layer2 = &grid_store->layers[iblk];
long nlayer = grid_store->nlayers[iblk];
GridLayer **grid_layer2 = &grid_store.layers[iblk];
long nlayer = grid_store.nlayers[iblk];
long ilayer;
for (ilayer = 0; ilayer < nlayer; ++ilayer)
{
......@@ -162,7 +162,7 @@ store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weig
*/
nlink = rv.num_links;
if (ilayer < grid_store->nlayers[iblk])
if (ilayer < grid_store.nlayers[iblk])
{
grid_layer->grid2_link[iadd2] = nlink;
}
......@@ -170,14 +170,14 @@ store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weig
{
grid_layer = (GridLayer *) Malloc(sizeof(GridLayer));
grid_layer->next = nullptr;
grid_layer->grid2_link = (long *) Malloc(grid_store->blksize[iblk] * sizeof(long));
grid_layer->grid2_link = (long *) Malloc(grid_store.blksize[iblk] * sizeof(long));
long blksize = grid_store->blksize[iblk];
long blksize = grid_store.blksize[iblk];
for (long i = 0; i < blksize; ++i) grid_layer->grid2_link[i] = -1;
grid_layer->grid2_link[iadd2] = nlink;
*grid_layer2 = grid_layer;
grid_store->nlayers[iblk]++;
grid_store.nlayers[iblk]++;
}
rv.num_links++;
......
......@@ -20,8 +20,8 @@
// used for store_link_cnsrv
#define BLK_SIZE 4096
#define BLK_NUM(x) (x / grid_store->blk_size)
#define BLK_IDX(x) (x % grid_store->blk_size)
#define BLK_NUM(x) (x / grid_store.blk_size)
#define BLK_IDX(x) (x % grid_store.blk_size)
// Predeclarations
struct RemapVars;
......@@ -42,9 +42,9 @@ struct GridStore
GridLayer **layers;
};
void grid_store_init(GridStore *grid_store, long gridsize);
void grid_store_delete(GridStore *grid_store);
void grid_store_init(GridStore &grid_store, long gridsize);
void grid_store_delete(GridStore &grid_store);
void store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weights, GridStore *grid_store);
void store_link_cnsrv(RemapVars &rv, long add1, long add2, long num_wts, double *weights, GridStore &grid_store);
#endif /* REMAP_STORE_LINK_CNSRV */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment