1 /*
2 * Copyright (C) 2013, Northwestern University and Argonne National Laboratory
3 * See COPYRIGHT notice in top-level directory.
4 *
5 * $Id: noclobber.c 2744 2016-12-28 16:25:22Z wkliao $
6 */
7
8 /*
9 * This program tests if PnetCDF can return the right error code NC_EEXIST
10 * when create mode NC_NOCLOBBER is used and the file exists.
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <libgen.h> /* basename() */
17 #include <mpi.h>
18 #include <pnetcdf.h>
19
20 #include <testutils.h>
21
22 #define ERR if (err!=NC_NOERR) {printf("Error at line %d: %s\n", __LINE__,ncmpi_strerror(err)); exit(-1);}
23
main(int argc,char ** argv)24 int main(int argc, char **argv) {
25 char filename[256];
26 int err, nerrs=0, ncid, cmode, rank, nprocs;
27
28 MPI_Init(&argc, &argv);
29 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
30 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31
32 if (argc > 2) {
33 if (!rank) printf("Usage: %s [filename]\n",argv[0]);
34 MPI_Finalize();
35 return 0;
36 }
37 if (argc == 2) snprintf(filename, 256, "%s", argv[1]);
38 else strcpy(filename, "testfile.nc");
39 MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD);
40
41 if (rank == 0) {
42 char *cmd_str = (char*)malloc(strlen(argv[0]) + 256);
43 sprintf(cmd_str, "*** TESTING C %s for NC_NOCLOBBER and NC_EEXIST ", basename(argv[0]));
44 printf("%-66s ------ ", cmd_str); fflush(stdout);
45 free(cmd_str);
46 }
47
48 /* create a file if it does not exist */
49 cmode = NC_CLOBBER;
50 err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid);
51 ERR
52 err = ncmpi_close(ncid); ERR
53
54 /* now the file exists, test if PnetCDF can return correct error code */
55 cmode = NC_NOCLOBBER;
56 err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid);
57 if (err != NC_EEXIST) /* err == NC_EOFILE */
58 nerrs++;
59
60 /* check if PnetCDF freed all internal malloc */
61 MPI_Offset malloc_size, sum_size;
62 err = ncmpi_inq_malloc_size(&malloc_size);
63 if (err == NC_NOERR) {
64 MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD);
65 if (rank == 0 && sum_size > 0)
66 printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n",
67 sum_size);
68 }
69
70 MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
71 if (rank == 0) {
72 if (nerrs) printf(FAIL_STR,nerrs);
73 else printf(PASS_STR);
74 }
75
76 MPI_Finalize();
77 return 0;
78 }
79