Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create function to populate cell_in #251

Merged
merged 17 commits into from
Nov 9, 2023
99 changes: 37 additions & 62 deletions tools/fregrid/conserve_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
double *xgrid_area=NULL, *xgrid_clon=NULL, *xgrid_clat=NULL;
int mxxgrid, zero=0;

typedef struct{
double *area;
double *clon;
double *clat;
} CellStruct;
CellStruct *cell_in;

double time_nxgrid=0;
Expand All @@ -68,26 +63,29 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
read_remap_file(ntiles_in,ntiles_out, grid_out, interp, opcode);
}
else {

//only needed for order2?
cell_in = (CellStruct *)malloc(ntiles_in * sizeof(CellStruct));
for(m=0; m<ntiles_in; m++) {
nx_in = grid_in[m].nx;
ny_in = grid_in[m].ny;
cell_in[m].area = (double *)malloc(nx_in*ny_in*sizeof(double));
cell_in[m].clon = (double *)malloc(nx_in*ny_in*sizeof(double));
cell_in[m].clat = (double *)malloc(nx_in*ny_in*sizeof(double));
for(n=0; n<nx_in*ny_in; n++) {
cell_in[m].area[n] = 0;
cell_in[m].clon[n] = 0;
cell_in[m].clat[n] = 0;
}
cell_in[m].area = (double *)calloc(nx_in*ny_in,sizeof(double));
cell_in[m].clon = (double *)calloc(nx_in*ny_in,sizeof(double));
cell_in[m].clat = (double *)calloc(nx_in*ny_in,sizeof(double));
}

//START NTILES_OUT
for(n=0; n<ntiles_out; n++) {

double *lon_out_min_list=NULL, *lon_out_max_list=NULL, *lon_out_avg=NULL;
double *lat_out_min_list=NULL, *lat_out_max_list=NULL;
double *lon_out_list=NULL, *lat_out_list=NULL;
int *n2_list=NULL;
Minmaxavg_lists out_minmaxavg_lists;
out_minmaxavg_lists.lon_list=NULL;
out_minmaxavg_lists.lat_list=NULL;
out_minmaxavg_lists.lon_min_list=NULL;
out_minmaxavg_lists.lat_min_list=NULL;
out_minmaxavg_lists.lon_max_list=NULL;
out_minmaxavg_lists.lat_max_list=NULL;
out_minmaxavg_lists.n_list=NULL;
out_minmaxavg_lists.lon_avg=NULL;

nx_out = grid_out[n].nxc;
ny_out = grid_out[n].nyc;
Expand All @@ -96,21 +94,24 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
#pragma acc enter data copyin(grid_out[n].lonc[0:(nx_out+1)*(ny_out+1)], \
grid_out[n].latc[0:(nx_out+1)*(ny_out+1)])


//allocate memory for the lists
malloc_minmaxavg_lists(nx_out*ny_out, &lon_out_min_list, &lon_out_max_list,
&lat_out_min_list, &lat_out_max_list, &n2_list,
&lon_out_avg, &lon_out_list, &lat_out_list);
malloc_minmaxavg_lists(nx_out*ny_out, &out_minmaxavg_lists);

#define MAX_V 8
#pragma acc enter data create(lon_out_list[0:MAX_V*nx_out*ny_out], lat_out_list[0:MAX_V*nx_out*ny_out], \
lat_out_min_list[0:nx_out*ny_out], lat_out_max_list[0:nx_out*ny_out], \
lon_out_min_list[0:nx_out*ny_out], lon_out_max_list[0:nx_out*ny_out], \
lon_out_avg[0:nx_out*ny_out], n2_list[0:nx_out*ny_out])
#pragma acc enter data create(out_minmaxavg_lists)
#pragma acc enter data create(out_minmaxavg_lists.lon_list[0:MAX_V*nx_out*ny_out], \
out_minmaxavg_lists.lat_list[0:MAX_V*nx_out*ny_out], \
out_minmaxavg_lists.lon_min_list[0:nx_out*ny_out], \
out_minmaxavg_lists.lon_max_list[0:nx_out*ny_out], \
out_minmaxavg_lists.lat_min_list[0:nx_out*ny_out], \
out_minmaxavg_lists.lat_max_list[0:nx_out*ny_out], \
out_minmaxavg_lists.n_list[0:nx_out*ny_out], \
out_minmaxavg_lists.lon_avg[0:nx_out*ny_out] )


//compute the list values
get_minmaxavg_lists(nx_out, ny_out, grid_out[n].lonc, grid_out[n].latc,
lon_out_min_list, lon_out_max_list, lat_out_min_list, lat_out_max_list,
n2_list, lon_out_avg, lon_out_list, lat_out_list);
get_minmaxavg_lists(nx_out, ny_out, grid_out[n].lonc, grid_out[n].latc, &out_minmaxavg_lists);

//START NTILES_IN
for(m=0; m<ntiles_in; m++) {
Expand Down Expand Up @@ -161,11 +162,10 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
mask, i_in, j_in, i_out, j_out, xgrid_area);
for(i=0; i<nxgrid; i++) j_in[i] += jstart;
free(mask);
#pragma acc exit data delete(mask)
} //opcode CONSERVE_ORDER1

else if(opcode & CONSERVE_ORDER2) {
int g_nxgrid, *g_i_in, *g_j_in;
double *g_area, *g_clon, *g_clat;

time_start = clock();

Expand All @@ -177,10 +177,8 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
#ifdef _OPENACC
nxgrid = create_xgrid_2dx2d_order2_acc(&nx_in, &ny_now, &nx_out, &ny_out, grid_in[m].lonc+jstart*(nx_in+1),
grid_in[m].latc+jstart*(nx_in+1), grid_out[n].lonc, grid_out[n].latc,
lon_out_min_list, lon_out_max_list, lon_out_avg,
lat_out_min_list, lat_out_max_list, n2_list,
lon_out_list, lat_out_list, mask,
i_in, j_in, i_out, j_out, xgrid_area, xgrid_clon, xgrid_clat);
&out_minmaxavg_lists, mask, i_in, j_in, i_out, j_out,
xgrid_area, xgrid_clon, xgrid_clat);
#else
nxgrid = create_xgrid_2dx2d_order2(&nx_in, &ny_now, &nx_out, &ny_out, grid_in[m].lonc+jstart*(nx_in+1),
grid_in[m].latc+jstart*(nx_in+1), grid_out[n].lonc, grid_out[n].latc,
Expand All @@ -198,34 +196,14 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
for(i=0; i<nxgrid; i++) j_in[i] += jstart;

/* For the purpose of bitiwise reproducing, the following operation is needed. */
g_nxgrid = nxgrid;
mpp_sum_int(1, &g_nxgrid);
if(g_nxgrid > 0) {
g_i_in = (int *)malloc(g_nxgrid*sizeof(int ));
g_j_in = (int *)malloc(g_nxgrid*sizeof(int ));
g_area = (double *)malloc(g_nxgrid*sizeof(double));
g_clon = (double *)malloc(g_nxgrid*sizeof(double));
g_clat = (double *)malloc(g_nxgrid*sizeof(double));
mpp_gather_field_int (nxgrid, i_in, g_i_in);
mpp_gather_field_int (nxgrid, j_in, g_j_in);
mpp_gather_field_double(nxgrid, xgrid_area, g_area);
mpp_gather_field_double(nxgrid, xgrid_clon, g_clon);
mpp_gather_field_double(nxgrid, xgrid_clat, g_clat);
for(i=0; i<g_nxgrid; i++) {
ii = g_j_in[i]*nx_in+g_i_in[i];
cell_in[m].area[ii] += g_area[i];
cell_in[m].clon[ii] += g_clon[i];
cell_in[m].clat[ii] += g_clat[i];
}
free(g_i_in); free(g_j_in); free(g_area); free(g_clon); free(g_clat);
} // if g_nxgrid > 0
}
get_CellStruct(m,nx_in, nxgrid, i_in, j_in, xgrid_area, xgrid_clon, xgrid_clat, cell_in);

} //opcode CONSERVE_ORDER2
else
mpp_error("conserve_interp: interp_method should be CONSERVE_ORDER1 or CONSERVE_ORDER2");
} //opcode CONSERVE_ORDER2
} // opcode GREAT_CIRCLE or CONSERVE_ORDERs


//for all opcodes
if(nxgrid > 0) {
nxgrid_prev = interp[n].nxgrid;
interp[n].nxgrid += nxgrid;
Expand Down Expand Up @@ -281,11 +259,8 @@ void setup_conserve_interp(int ntiles_in, const Grid_config *grid_in, int ntiles
malloc_xgrid_arrays(zero, &i_in, &j_in, &i_out, &j_out, &xgrid_area, &xgrid_clon , &xgrid_clat);
#pragma acc exit data delete(grid_in[m].latc, grid_in[m].lonc)
} // ntiles_in
malloc_minmaxavg_lists(zero, &lon_out_min_list, &lon_out_max_list,
&lat_out_min_list, &lat_out_max_list, &n2_list,
&lon_out_avg, &lon_out_list, &lat_out_list);
#pragma acc exit data delete(lon_out_min_list, lon_out_max_list, lat_out_min_list, \
lat_out_max_list, n2_list, lon_out_avg, lon_out_list, lat_out_list)
malloc_minmaxavg_lists(zero, &out_minmaxavg_lists);
#pragma acc exit data delete(out_minmaxavg_lists)
#pragma acc exit data delete(grid_out[n].latc, grid_out[n].lonc)
} // ntimes_out

Expand Down
42 changes: 42 additions & 0 deletions tools/fregrid/conserve_interp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,45 @@ void malloc_xgrid_arrays( int nsize, int **i_in, int **j_in, int **i_out, int **
}

}

/*******************************************************************************
void get_CellStruct
populate CellStruct
*******************************************************************************/
void get_CellStruct(const int tile_in, const int nx_in, const int nxgrid, int *i_in, int *j_in,
double *xgrid_area, double *xgrid_clon, double *xgrid_clat,
CellStruct *cell_in)
{

int g_nxgrid;
int *g_i_in, *g_j_in;
double *g_area, *g_clon, *g_clat;

g_nxgrid = nxgrid;
mpp_sum_int(1, &g_nxgrid);
if(g_nxgrid > 0) {
g_i_in = (int *)malloc(g_nxgrid*sizeof(int ));
g_j_in = (int *)malloc(g_nxgrid*sizeof(int ));
g_area = (double *)malloc(g_nxgrid*sizeof(double));
g_clon = (double *)malloc(g_nxgrid*sizeof(double));
g_clat = (double *)malloc(g_nxgrid*sizeof(double));
mpp_gather_field_int (nxgrid, i_in, g_i_in);
mpp_gather_field_int (nxgrid, j_in, g_j_in);
mpp_gather_field_double(nxgrid, xgrid_area, g_area);
mpp_gather_field_double(nxgrid, xgrid_clon, g_clon);
mpp_gather_field_double(nxgrid, xgrid_clat, g_clat);
for(int i=0; i<g_nxgrid; i++) {
int ii;
ii = g_j_in[i]*nx_in+g_i_in[i];
cell_in[tile_in].area[ii] += g_area[i];
cell_in[tile_in].clon[ii] += g_clon[i];
cell_in[tile_in].clat[ii] += g_clat[i];
}
free(g_i_in);
free(g_j_in);
free(g_area);
free(g_clon);
free(g_clat);
}

}
4 changes: 4 additions & 0 deletions tools/fregrid/conserve_interp_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ void read_remap_file( int ntiles_in, int ntiles_out, Grid_config *grid_out,
void malloc_xgrid_arrays( int nsize, int **i_in, int **j_in, int **i_out, int **j_out,
double **xgrid_area, double **xgrid_clon, double **xgrid_clat );

void get_CellStruct(const tile_in, const int nx_in, const int nxgrid, int *i_in, int *j_in,
double *xgrid_area, double *xgrid_clon, double *xgrid_clat,
CellStruct *cell_in);

#endif
24 changes: 21 additions & 3 deletions tools/fregrid/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef struct {
char area_name[STRING];
int do_regrid;
int is_axis_data;
int dimsize[5];
int dimsize[5];
} Var_config;

typedef struct {
Expand All @@ -121,11 +121,11 @@ typedef struct {
int bndid;
int size;
nc_type type;
char cart;
char cart;
int bndtype;
int is_defined;
double *bnddata;
double *data;
double *data;
} Axis_config;

typedef struct {
Expand Down Expand Up @@ -236,4 +236,22 @@ typedef struct{
double *f_min;
} Monotone_config;


typedef struct{
double *area;
double *clon;
double *clat;
} CellStruct;

typedef struct{
double *lat_min_list;
double *lat_max_list;
double *lon_min_list;
double *lon_max_list;
double *lon_avg;
double *lon_list;
double *lat_list;
int *n_list;
} Minmaxavg_lists;

#endif
2 changes: 1 addition & 1 deletion tools/libfrencutils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if WITH_OPENACC
libfrencutils_gpu_a_CFLAGS = $(CC_OPENACC_FLAGS) $(AM_CFLAGS)
endif

AM_CFLAGS = $(NETCDF_CFLAGS)
AM_CFLAGS = $(NETCDF_CFLAGS) -I$(top_srcdir)/tools/fregrid

libfrencutils_a_SOURCES = affinity.c \
constant.h \
Expand Down
Loading
Loading