Skip to content

Commit 18d456c

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

File tree

1 file changed

+105
-19
lines changed

1 file changed

+105
-19
lines changed

src/e3sm_io.c

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

372448
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-
385449
decom.blocklens[i] = NULL;
386450
decom.disps[i] = NULL;
387451
decom.raw_offsets[i] = NULL;
388452
decom.w_starts[i] = NULL;
389453
decom.max_nreqs[i] = 0;
390454
}
455+
391456
ffreq = 1;
392457

393458
/* command-line arguments */
@@ -449,11 +514,11 @@ int main (int argc, char **argv) {
449514
break;
450515

451516
case 'o':
452-
strncpy(cfg.out_path, optarg, E3SM_IO_MAX_PATH);
517+
strncpy(cfg.out_path, optarg, E3SM_IO_MAX_PATH-1);
453518
cfg.wr = 1;
454519
break;
455520
case 'i':
456-
strncpy(cfg.in_path, optarg, E3SM_IO_MAX_PATH);
521+
strncpy(cfg.in_path, optarg, E3SM_IO_MAX_PATH-1);
457522
cfg.rd = 1;
458523
break;
459524
case 'm':
@@ -508,7 +573,7 @@ int main (int argc, char **argv) {
508573
if (!cfg.rank) usage(argv[0]);
509574
ERR_OUT("Decomposition file not provided")
510575
}
511-
strncpy(cfg.decomp_path, argv[optind], E3SM_IO_MAX_PATH);
576+
strncpy(cfg.decomp_path, argv[optind], E3SM_IO_MAX_PATH-1);
512577

513578
cfg.F_case_h0.nrecs = 1; /* force only one record for F h0 case */
514579
cfg.F_case_h1.nrecs = nrecs;
@@ -678,7 +743,8 @@ int main (int argc, char **argv) {
678743

679744
char *hint_lines[64];
680745
int num_hint_lines;
681-
parse_hint_file(&cfg, "e3sm_io_hints.txt", &num_hint_lines, hint_lines);
746+
// parse_hint_file(&cfg, "e3sm_io_hints.txt", &num_hint_lines, hint_lines);
747+
parse_hint_file(&cfg, "/pscratch/sd/w/wkliao/e3sm_io_hints.txt", &num_hint_lines, hint_lines);
682748

683749
timing[1] = MPI_Wtime() - timing[1];
684750
MPI_Barrier(MPI_COMM_WORLD);
@@ -708,7 +774,27 @@ int main (int argc, char **argv) {
708774

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

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

713799
/* set MPI-IO and PnetCDF hints */
714800
err = set_info(&cfg, hint_str);
@@ -743,7 +829,7 @@ if (cfg.rank == 0) printf("\nHINTS: %s\n\n", (hint_str) ? hint_str : "");
743829
}
744830
}
745831

746-
if (cfg.info != MPI_INFO_NULL) MPI_Info_free (&(cfg.info));
832+
if (cfg.info != MPI_INFO_NULL) MPI_Info_free(&cfg.info);
747833

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

0 commit comments

Comments
 (0)