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

Image initialization speedup #684

Merged
merged 14 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions vic/drivers/image/src/display_current_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ display_current_settings(int mode)

fprintf(LOG_DEST, "\n");
fprintf(LOG_DEST, "Input Domain Data:\n");
fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain);
fprintf(LOG_DEST, "Domain file\t\t%s\n", filenames.domain.nc_filename);

fprintf(LOG_DEST, "\n");
fprintf(LOG_DEST, "Constants File\t\t%s\n", filenames.constants);
fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params);
fprintf(LOG_DEST, "Parameters file\t\t%s\n", filenames.params.nc_filename);
if (options.BASEFLOW == ARNO) {
fprintf(LOG_DEST, "BASEFLOW\t\tARNO\n");
}
Expand Down Expand Up @@ -378,7 +378,7 @@ display_current_settings(int mode)
fprintf(LOG_DEST, "\n");
fprintf(LOG_DEST, "Input State File:\n");
if (options.INIT_STATE) {
fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state);
fprintf(LOG_DEST, "INIT_STATE\t\tTRUE\t%s\n", filenames.init_state.nc_filename);
if (options.STATE_FORMAT == NETCDF3_CLASSIC) {
fprintf(LOG_DEST, "STATE_FORMAT\t\tNETCDF3_CLASSIC\n");
}
Expand Down
32 changes: 21 additions & 11 deletions vic/drivers/image/src/get_global_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ get_global_param(FILE *gp)
char flgstr2[MAXSTRING];
size_t file_num;
int field;
int status;
unsigned int tmpstartdate;
unsigned int tmpenddate;
unsigned short int lastday[MONTHS_PER_YEAR];
Expand Down Expand Up @@ -296,7 +297,7 @@ get_global_param(FILE *gp)
}
else {
options.INIT_STATE = true;
strcpy(filenames.init_state, flgstr);
strcpy(filenames.init_state.nc_filename, flgstr);
}
}
else if (strcasecmp("STATENAME", optstr) == 0) {
Expand Down Expand Up @@ -374,13 +375,13 @@ get_global_param(FILE *gp)
sscanf(cmdstr, "%*s %s", filenames.constants);
}
else if (strcasecmp("DOMAIN", optstr) == 0) {
sscanf(cmdstr, "%*s %s", filenames.domain);
sscanf(cmdstr, "%*s %s", filenames.domain.nc_filename);
}
else if (strcasecmp("DOMAIN_TYPE", optstr) == 0) {
get_domain_type(cmdstr);
}
else if (strcasecmp("PARAMETERS", optstr) == 0) {
sscanf(cmdstr, "%*s %s", filenames.params);
sscanf(cmdstr, "%*s %s", filenames.params.nc_filename);
}
else if (strcasecmp("ARNO_PARAMS", optstr) == 0) {
sscanf(cmdstr, "%*s %s", flgstr);
Expand Down Expand Up @@ -787,12 +788,21 @@ get_global_param(FILE *gp)
}

// Get information from the forcing file(s)
sprintf(filenames.forcing[0], "%s%4d.nc", filenames.f_path_pfx[0],
global_param.startyear);
// Open first-year forcing files and get info
sprintf(filenames.forcing[0].nc_filename, "%s%4d.nc",
filenames.f_path_pfx[0], global_param.startyear);
status = nc_open(filenames.forcing[0].nc_filename, NC_NOWRITE,
&(filenames.forcing[0].nc_id));
check_nc_status(status, "Error opening %s",
filenames.forcing[0].nc_filename);
get_forcing_file_info(&param_set, 0);
if (param_set.N_TYPES[1] != 0) {
sprintf(filenames.forcing[1], "%s%4d.nc", filenames.f_path_pfx[1],
global_param.startyear);
sprintf(filenames.forcing[1].nc_filename, "%s%4d.nc",
filenames.f_path_pfx[1], global_param.startyear);
status = nc_open(filenames.forcing[1].nc_filename, NC_NOWRITE,
&(filenames.forcing[1].nc_id));
check_nc_status(status, "Error opening %s",
filenames.forcing[1].nc_filename);
get_forcing_file_info(&param_set, 1);
}

Expand Down Expand Up @@ -829,7 +839,7 @@ get_global_param(FILE *gp)
}

// Validate parameter file information
if (strcmp(filenames.params, "MISSING") == 0) {
if (strcmp(filenames.params.nc_filename, "MISSING") == 0) {
log_err("A parameters file has not been defined. Make sure that the "
"global file defines the parameters parameter file on the line "
"that begins with \"PARAMETERS\".");
Expand Down Expand Up @@ -868,7 +878,7 @@ get_global_param(FILE *gp)

// Validate the input state file information
if (options.INIT_STATE) {
if (strcmp(filenames.init_state, "MISSING") == 0) {
if (strcmp(filenames.init_state.nc_filename, "MISSING") == 0) {
log_err("\"INIT_STATE\" was specified, but no input state file "
"has been defined. Make sure that the global file "
"defines the inputstate file on the line that begins "
Expand Down Expand Up @@ -919,11 +929,11 @@ get_global_param(FILE *gp)
global_param.statesec);
}
if (options.INIT_STATE && options.SAVE_STATE &&
(strcmp(filenames.init_state, flgstr2) == 0)) {
(strcmp(filenames.init_state.nc_filename, flgstr2) == 0)) {
log_err("The save state file (%s) has the same name as the "
"initialize state file (%s). The initialize state file "
"will be destroyed when the save state file is opened.",
filenames.statefile, filenames.init_state);
filenames.statefile, filenames.init_state.nc_filename);
}

// Validate soil parameter/simulation mode combinations
Expand Down
96 changes: 72 additions & 24 deletions vic/drivers/image/src/vic_force.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ vic_force(void)
extern size_t NF;
extern size_t NR;
extern size_t current;
extern int mpi_rank;
extern force_data_struct *force;
extern dmy_struct *dmy;
extern domain_struct global_domain;
Expand All @@ -56,6 +57,7 @@ vic_force(void)
size_t v;
size_t band;
int vidx;
int status;
size_t d3count[3];
size_t d3start[3];
size_t d4count[4];
Expand All @@ -66,16 +68,29 @@ vic_force(void)
dvar = malloc(local_domain.ncells_active * sizeof(*dvar));
check_alloc_status(dvar, "Memory allocation error.");

// for now forcing file is determined by the year
sprintf(filenames.forcing[0], "%s%4d.nc", filenames.f_path_pfx[0],
dmy[current].year);

// global_param.forceoffset[0] resets every year since the met file restarts
// every year
// global_param.forceskip[0] should also reset to 0 after the first year
if (current > 0 && (dmy[current].year != dmy[current - 1].year)) {
global_param.forceoffset[0] = 0;
global_param.forceskip[0] = 0;
// close the forcing file for the previous year and open the forcing
// file for the current new year
// (forcing file for the first year should already be open in
// get_global_param)
if (mpi_rank == VIC_MPI_ROOT) {
// close previous forcing file
status = nc_close(filenames.forcing[0].nc_id);
check_nc_status(status, "Error closing %s",
filenames.forcing[0].nc_filename);
// open new forcing file
sprintf(filenames.forcing[0].nc_filename, "%s%4d.nc",
filenames.f_path_pfx[0], dmy[current].year);
status = nc_open(filenames.forcing[0].nc_filename, NC_NOWRITE,
&(filenames.forcing[0].nc_id));
check_nc_status(status, "Error opening %s",
filenames.forcing[0].nc_filename);
}
}

// only the time slice changes for the met file reads. The rest is constant
Expand All @@ -89,7 +104,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing &(filenames.forcing[0])? Isn't filenames.forcing[0] already a string (char *)? I think that you can revert all these to filenames.forcing[0].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. filenames.forcing[0] is of type nameid_struct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right!

param_set.TYPE[AIR_TEMP].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -101,7 +116,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[PREC].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -113,7 +128,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[SWDOWN].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -125,7 +140,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[LWDOWN].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -137,7 +152,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[WIND].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -149,7 +164,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[VP].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -161,7 +176,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[PRESSURE].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -173,7 +188,7 @@ vic_force(void)
// Channel inflow to lake
d3start[0] = global_param.forceskip[0] + global_param.forceoffset[0] +
j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[CHANNEL_IN].varname,
d3start, d3count, dvar);
for (j = 0; j < NF; j++) {
Expand All @@ -187,7 +202,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] +
global_param.forceoffset[0] + j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[CATM].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -208,7 +223,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] +
global_param.forceoffset[0] + j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[FDIR].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -219,7 +234,7 @@ vic_force(void)
for (j = 0; j < NF; j++) {
d3start[0] = global_param.forceskip[0] +
global_param.forceoffset[0] + j;
get_scatter_nc_field_double(filenames.forcing[0],
get_scatter_nc_field_double(&(filenames.forcing[0]),
param_set.TYPE[PAR].varname,
d3start, d3count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
Expand All @@ -228,6 +243,15 @@ vic_force(void)
}
}

if (mpi_rank == VIC_MPI_ROOT) {
// Close forcing file if it is the last time step
if (current == global_param.nrecs) {
status = nc_close(filenames.forcing[0].nc_id);
check_nc_status(status, "Error closing %s",
filenames.forcing[0].nc_filename);
}
}

// Update the offset counter
global_param.forceoffset[0] += NF;

Expand Down Expand Up @@ -258,16 +282,31 @@ vic_force(void)
if (options.LAI_SRC == FROM_VEGHIST ||
options.FCAN_SRC == FROM_VEGHIST ||
options.ALB_SRC == FROM_VEGHIST) {
// for now forcing file is determined by the year
sprintf(filenames.forcing[1], "%s%4d.nc", filenames.f_path_pfx[1],
dmy[current].year);

// global_param.forceoffset[1] resets every year since the met file restarts
// every year
// global_param.forceskip[1] should also reset to 0 after the first year
if (current > 0 && (dmy[current].year != dmy[current - 1].year)) {
global_param.forceoffset[1] = 0;
global_param.forceskip[1] = 0;
// close the forcing file for the previous year and open the forcing
// file for the current new year
// (forcing file for the first year should already be open in
// get_global_param)
if (mpi_rank == VIC_MPI_ROOT) {
// close previous forcing file
status = nc_close(filenames.forcing[1].nc_id);
check_nc_status(status, "Error closing %s",
filenames.forcing[1].nc_filename);
// open new forcing file
sprintf(filenames.forcing[1].nc_filename, "%s%4d.nc",
filenames.f_path_pfx[1],
dmy[current].year);
status = nc_open(filenames.forcing[1].nc_filename, NC_NOWRITE,
&(filenames.forcing[1].nc_id));
check_nc_status(status, "Error opening %s",
filenames.forcing[1].nc_filename);
}
}

// only the time slice changes for the met file reads. The rest is constant
Expand All @@ -285,7 +324,7 @@ vic_force(void)
global_param.forceoffset[1] + j;
for (v = 0; v < options.NVEGTYPES; v++) {
d4start[1] = v;
get_scatter_nc_field_double(filenames.forcing[1], "lai",
get_scatter_nc_field_double(&(filenames.forcing[1]), "lai",
d4start, d4count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
vidx = veg_con_map[i].vidx[v];
Expand All @@ -304,7 +343,7 @@ vic_force(void)
global_param.forceoffset[1] + j;
for (v = 0; v < options.NVEGTYPES; v++) {
d4start[1] = v;
get_scatter_nc_field_double(filenames.forcing[1], "fcov",
get_scatter_nc_field_double(&(filenames.forcing[1]), "fcov",
d4start, d4count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
vidx = veg_con_map[i].vidx[v];
Expand All @@ -323,7 +362,7 @@ vic_force(void)
global_param.forceoffset[1] + j;
for (v = 0; v < options.NVEGTYPES; v++) {
d4start[1] = v;
get_scatter_nc_field_double(filenames.forcing[1], "alb",
get_scatter_nc_field_double(&(filenames.forcing[1]), "alb",
d4start, d4count, dvar);
for (i = 0; i < local_domain.ncells_active; i++) {
vidx = veg_con_map[i].vidx[v];
Expand All @@ -335,6 +374,15 @@ vic_force(void)
}
}

if (mpi_rank == VIC_MPI_ROOT) {
// Close forcing file if it is the last time step
if (current == global_param.nrecs) {
status = nc_close(filenames.forcing[1].nc_id);
check_nc_status(status, "Error closing %s",
filenames.forcing[1].nc_filename);
}
}

// Update the offset counter
global_param.forceoffset[1] += NF;
}
Expand Down Expand Up @@ -479,9 +527,9 @@ get_forcing_file_info(param_set_struct *param_set,
dmy_struct nc_start_dmy;

// read time info from netcdf file
get_nc_field_double(filenames.forcing[file_num], "time", &start, &count, nc_times);
get_nc_var_attr(filenames.forcing[file_num], "time", "units", &nc_unit_chars);
get_nc_var_attr(filenames.forcing[file_num], "time", "calendar", &calendar_char);
get_nc_field_double(&(filenames.forcing[file_num]), "time", &start, &count, nc_times);
get_nc_var_attr(&(filenames.forcing[file_num]), "time", "units", &nc_unit_chars);
get_nc_var_attr(&(filenames.forcing[file_num]), "time", "calendar", &calendar_char);

// parse the calendar string and check to make sure it matches the global clock
calendar = str_to_calendar(calendar_char);
Expand Down
6 changes: 3 additions & 3 deletions vic/drivers/image/src/vic_image_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
void
vic_image_start(void)
{
extern filep_struct filep;
extern filenames_struct filenames;
extern int mpi_rank;
extern filep_struct filep;
extern filenames_struct filenames;
extern int mpi_rank;

// Initialize structures
initialize_global_structures();
Expand Down
Loading