Skip to content

Commit e870517

Browse files
committed
delete output files before runs
1 parent 9e83572 commit e870517

1 file changed

Lines changed: 106 additions & 18 deletions

File tree

src/e3sm_io.c

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <string.h> /* strcpy(), strncpy(), strstr() */
16-
#include <unistd.h> /* getopt() */
16+
#include <unistd.h> /* getopt(), unlink() */
1717

1818
#include <mpi.h>
1919

@@ -235,6 +235,85 @@ int set_info(e3sm_io_config *cfg,
235235
return err;
236236
}
237237

238+
/*----< delete_output_files() >----------------------------------------------*/
239+
static
240+
void delete_output_files(e3sm_io_config *cfg)
241+
{
242+
char *filename, *base, *ext;
243+
char *out_path, *out_path_h0, *out_path_h1;
244+
char *out_path_subfile, *out_path_subfile_h0, *out_path_subfile_h1;
245+
size_t len;
246+
247+
if (cfg->rank > 0) goto fnc_exit;
248+
249+
/* remove file system type prefix, if there is any */
250+
filename = strchr(cfg->out_path, ':');
251+
if (filename == NULL)
252+
filename = cfg->out_path;
253+
else
254+
filename++;
255+
256+
len = strlen(filename) + 8;
257+
258+
out_path_subfile = (char*) malloc(len);
259+
out_path_subfile_h0 = (char*) malloc(len);
260+
out_path_subfile_h1 = (char*) malloc(len);
261+
out_path_h0 = (char*) malloc(len);
262+
out_path_h1 = (char*) malloc(len);
263+
264+
base = strdup(filename);
265+
ext = strrchr(base, '.');
266+
if (ext != NULL) {
267+
*ext = '\0';
268+
ext++;
269+
}
270+
271+
out_path = filename;
272+
273+
if (ext == NULL || (strcmp(ext, "nc") && strcmp(ext, "h5") && strcmp(ext, "nc4") && strcmp(ext, "bp"))) {
274+
sprintf(out_path_h0, "%s_h0", filename);
275+
sprintf(out_path_h1, "%s_h1", filename);
276+
} else {
277+
sprintf(out_path_h0, "%s_h0.%s", base, ext);
278+
sprintf(out_path_h1, "%s_h1.%s", base, ext);
279+
}
280+
281+
/* append subfile ID */
282+
if (cfg->strategy == blob && cfg->api != adios) {
283+
sprintf(out_path_subfile_h0, "%s.%04d", out_path_h0, cfg->subfile_ID);
284+
sprintf(out_path_subfile_h1, "%s.%04d", out_path_h1, cfg->subfile_ID);
285+
sprintf(out_path_subfile, "%s.%04d", filename, cfg->subfile_ID);
286+
}
287+
288+
/* delete the output file and ignore the error */
289+
if (cfg->run_case == G) {
290+
unlink(out_path);
291+
if (cfg->strategy == blob && cfg->api != adios)
292+
unlink(out_path_subfile);
293+
} else {
294+
if (cfg->hx == 0 || cfg->hx == -1) { /* h0 file */
295+
unlink(out_path_h0);
296+
if (cfg->strategy == blob && cfg->api != adios)
297+
unlink(out_path_subfile_h0);
298+
}
299+
if (cfg->hx == 1 || cfg->hx == -1) { /* h1 file */
300+
unlink(out_path_h1);
301+
if (cfg->strategy == blob && cfg->api != adios)
302+
unlink(out_path_subfile_h1);
303+
}
304+
}
305+
306+
free(out_path_subfile);
307+
free(out_path_subfile_h0);
308+
free(out_path_subfile_h1);
309+
free(out_path_h0);
310+
free(out_path_h1);
311+
free(base);
312+
313+
fnc_exit:
314+
MPI_Barrier(cfg->io_comm);
315+
}
316+
238317
/*----< print_info() >------------------------------------------------------*/
239318
void print_info (MPI_Info *info_used) {
240319
int i, nkeys;
@@ -370,24 +449,13 @@ int main (int argc, char **argv) {
370449
cfg.factor = 1;
371450

372451
for (i = 0; i < MAX_NUM_DECOMP; i++) {
373-
cfg.G_case.nvars_D[i] = 0;
374-
cfg.F_case_h0.nvars_D[i] = 0;
375-
cfg.F_case_h1.nvars_D[i] = 0;
376-
cfg.I_case_h0.nvars_D[i] = 0;
377-
cfg.I_case_h1.nvars_D[i] = 0;
378-
379-
cfg.G_case.num_attrs = 0;
380-
cfg.F_case_h0.num_attrs = 0;
381-
cfg.F_case_h1.num_attrs = 0;
382-
cfg.I_case_h0.num_attrs = 0;
383-
cfg.I_case_h1.num_attrs = 0;
384-
385452
decom.blocklens[i] = NULL;
386453
decom.disps[i] = NULL;
387454
decom.raw_offsets[i] = NULL;
388455
decom.w_starts[i] = NULL;
389456
decom.max_nreqs[i] = 0;
390457
}
458+
391459
ffreq = 1;
392460

393461
/* command-line arguments */
@@ -449,11 +517,11 @@ int main (int argc, char **argv) {
449517
break;
450518

451519
case 'o':
452-
strncpy(cfg.out_path, optarg, E3SM_IO_MAX_PATH);
520+
strncpy(cfg.out_path, optarg, E3SM_IO_MAX_PATH-1);
453521
cfg.wr = 1;
454522
break;
455523
case 'i':
456-
strncpy(cfg.in_path, optarg, E3SM_IO_MAX_PATH);
524+
strncpy(cfg.in_path, optarg, E3SM_IO_MAX_PATH-1);
457525
cfg.rd = 1;
458526
break;
459527
case 'm':
@@ -508,7 +576,7 @@ int main (int argc, char **argv) {
508576
if (!cfg.rank) usage(argv[0]);
509577
ERR_OUT("Decomposition file not provided")
510578
}
511-
strncpy(cfg.decomp_path, argv[optind], E3SM_IO_MAX_PATH);
579+
strncpy(cfg.decomp_path, argv[optind], E3SM_IO_MAX_PATH-1);
512580

513581
cfg.F_case_h0.nrecs = 1; /* force only one record for F h0 case */
514582
cfg.F_case_h1.nrecs = nrecs;
@@ -708,7 +776,27 @@ int main (int argc, char **argv) {
708776

709777
char *hint_str = (num_hint_lines == 0) ? NULL : hint_lines[j];
710778

711-
if (cfg.rank == 0) printf("\nHINTS: %s\n\n", (hint_str) ? hint_str : "");
779+
if (cfg.rank == 0) {
780+
printf("\nHINTS: %s\n\n", (hint_str) ? hint_str : "");
781+
fflush(stdout);
782+
}
783+
784+
/* reset counters */
785+
for (i = 0; i < MAX_NUM_DECOMP; i++) {
786+
cfg.G_case.nvars_D[i] = 0;
787+
cfg.F_case_h0.nvars_D[i] = 0;
788+
cfg.F_case_h1.nvars_D[i] = 0;
789+
cfg.I_case_h0.nvars_D[i] = 0;
790+
cfg.I_case_h1.nvars_D[i] = 0;
791+
}
792+
cfg.G_case.num_attrs = 0;
793+
cfg.F_case_h0.num_attrs = 0;
794+
cfg.F_case_h1.num_attrs = 0;
795+
cfg.I_case_h0.num_attrs = 0;
796+
cfg.I_case_h1.num_attrs = 0;
797+
798+
/* delete output files */
799+
delete_output_files(&cfg);
712800

713801
/* set MPI-IO and PnetCDF hints */
714802
err = set_info(&cfg, hint_str);
@@ -743,7 +831,7 @@ if (cfg.rank == 0) printf("\nHINTS: %s\n\n", (hint_str) ? hint_str : "");
743831
}
744832
}
745833

746-
if (cfg.info != MPI_INFO_NULL) MPI_Info_free (&(cfg.info));
834+
if (cfg.info != MPI_INFO_NULL) MPI_Info_free(&cfg.info);
747835

748836
timing[0] = timing[1] + timing[2] + timing[3];
749837
MPI_Reduce(timing, max_t, 4, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

0 commit comments

Comments
 (0)