]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix a file creation check. 5609/head
authorDavid Wells <wellsd2@rpi.edu>
Mon, 11 Dec 2017 00:19:26 +0000 (19:19 -0500)
committerDavid Wells <wellsd2@rpi.edu>
Mon, 11 Dec 2017 16:26:47 +0000 (11:26 -0500)
Commit 1cdbeecc858 is broken when step-42 is run with multiple MPI processes.

examples/step-42/step-42.cc

index 03b74f5b7ba91d487877247df75968e77e9574cd..da6eded48b118c04863773a715d2ec4a7d25e7ff 100644 (file)
 #include <fstream>
 #include <iostream>
 
-// This final include file provides the <code>mkdir</code> function
-// that we will use to create a directory for output files, if necessary:
+// Finally, we include two system headers that let us create a directory for
+// output files. The first header provides the <code>mkdir</code> function and
+// the second lets us determine what happened if <code>mkdir</code> fails.
 #include <sys/stat.h>
+#include <errno.h>
 
 namespace Step42
 {
@@ -876,8 +878,13 @@ namespace Step42
     output_dir = prm.get("output directory");
     if (output_dir != "" && *(output_dir.rbegin()) != '/')
       output_dir += "/";
-    const int ierr = mkdir(output_dir.c_str(), 0777);
-    AssertThrow(ierr == 0, ExcIO());
+
+    // If necessary, create a new directory for the output.
+    if (Utilities::MPI::this_mpi_process(mpi_communicator) == 0)
+      {
+        const int ierr = mkdir(output_dir.c_str(), 0777);
+        AssertThrow(ierr == 0 || errno == EEXIST, ExcIO());
+      }
 
     pcout << "    Using output directory '" << output_dir << "'" << std::endl;
     pcout << "    FE degree " << fe_degree << std::endl;

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.