From 838ba4b7afb4a15162a3de72b7565a6cc9c26141 Mon Sep 17 00:00:00 2001
From: Lukas Werner <lks.werner@fau.de>
Date: Tue, 13 Mar 2018 00:14:46 +0100
Subject: [PATCH] Fix non-MPI builds

---
 src/pe/raytracing/Raytracer.cpp | 27 +++++++++++++++++++--------
 src/pe/raytracing/Raytracer.h   | 26 ++++++++++++++++----------
 2 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/src/pe/raytracing/Raytracer.cpp b/src/pe/raytracing/Raytracer.cpp
index 7f95db2fb..21f19d9c3 100644
--- a/src/pe/raytracing/Raytracer.cpp
+++ b/src/pe/raytracing/Raytracer.cpp
@@ -31,6 +31,7 @@ namespace walberla {
 namespace pe {
 namespace raytracing {
    
+#ifdef WALBERLA_BUILD_WITH_MPI
 void BodyIntersectionInfo_Comparator_MPI_OP( BodyIntersectionInfo *in, BodyIntersectionInfo *inout, int *len, MPI_Datatype *dptr) {
    WALBERLA_UNUSED(dptr);
    for (int i = 0; i < *len; ++i) {
@@ -53,7 +54,8 @@ void BodyIntersectionInfo_Comparator_MPI_OP( BodyIntersectionInfo *in, BodyInter
       inout++;
    }
 }
-   
+#endif
+
 /*!\brief Instantiation constructor for the Raytracer class.
  *
  * \param forest BlockForest the raytracer operates on.
@@ -201,11 +203,14 @@ void Raytracer::setupView_() {
 /*!\brief Utility function for initializing the attribute filenameRankWidth.
  */
 void Raytracer::setupFilenameRankWidth_() {
-   int numProcesses = mpi::MPIManager::instance()->numProcesses();
-   filenameRankWidth_ = uint8_c(log10(numProcesses)+1);
+   WALBERLA_MPI_SECTION() {
+      int numProcesses = mpi::MPIManager::instance()->numProcesses();
+      filenameRankWidth_ = uint8_c(log10(numProcesses)+1);
+   }
 }
 
 void Raytracer::setupMPI_() {
+#ifdef WALBERLA_BUILD_WITH_MPI
    MPI_Op_create((MPI_User_function *)BodyIntersectionInfo_Comparator_MPI_OP, true, &bodyIntersectionInfo_reduction_op);
    
    const int nblocks = 7;
@@ -236,6 +241,7 @@ void Raytracer::setupMPI_() {
    MPI_Type_create_resized( tmp_type, lb, extent, &bodyIntersectionInfo_mpi_type );
    
    MPI_Type_commit(&bodyIntersectionInfo_mpi_type);
+#endif
 }
    
 /*!\brief Generates the filename for output files.
@@ -250,11 +256,14 @@ std::string Raytracer::getOutputFilename(const std::string& base, size_t timeste
    std::stringstream fileNameStream;
    fileNameStream << base << "_";
    fileNameStream << std::setfill('0') << std::setw(int_c(filenameTimestepWidth_)) << timestep; // add timestep
-   fileNameStream << "+";
-   if (isGlobalImage) {
-      fileNameStream << "global";
-   } else {
-      fileNameStream << std::setfill('0') << std::setw(int_c(filenameRankWidth_)) << std::to_string(rank); // add rank
+   WALBERLA_MPI_SECTION() {
+      // Appending the rank to the filename only makes sense if actually using MPI.
+      fileNameStream << "+";
+      if (isGlobalImage) {
+         fileNameStream << "global";
+      } else {
+         fileNameStream << std::setfill('0') << std::setw(int_c(filenameRankWidth_)) << std::to_string(rank); // add rank
+      }
    }
    fileNameStream << ".png"; // add extension
    return fileNameStream.str();
@@ -369,6 +378,7 @@ void Raytracer::writeImageBufferToFile(const std::vector<Color>& imageBuffer, co
 }
    
 void Raytracer::syncImageUsingMPIReduce(std::vector<BodyIntersectionInfo>& intersectionsBuffer, WcTimingTree* tt) {
+#ifdef WALBERLA_BUILD_WITH_MPI
    WALBERLA_MPI_BARRIER();
    if (tt != NULL) tt->start("Reduction");
    int rank = mpi::MPIManager::instance()->rank();
@@ -387,6 +397,7 @@ void Raytracer::syncImageUsingMPIReduce(std::vector<BodyIntersectionInfo>& inter
    
    WALBERLA_MPI_BARRIER();
    if (tt != NULL) tt->stop("Reduction");
+#endif
 }
    
 void Raytracer::syncImageUsingMPIGather(std::vector<BodyIntersectionInfo>& intersections, std::vector<BodyIntersectionInfo>& intersectionsBuffer, WcTimingTree* tt) {
diff --git a/src/pe/raytracing/Raytracer.h b/src/pe/raytracing/Raytracer.h
index d6a3c31a9..87632f2f7 100644
--- a/src/pe/raytracing/Raytracer.h
+++ b/src/pe/raytracing/Raytracer.h
@@ -136,8 +136,10 @@ private:
    real_t pixelHeight_;       //!< The height of a pixel of the generated image in the viewing plane.
    //@}
    
+#ifdef WALBERLA_BUILD_WITH_MPI
    MPI_Op bodyIntersectionInfo_reduction_op;
    MPI_Datatype bodyIntersectionInfo_mpi_type;
+#endif
    
 public:
    /*!\name Get functions */
@@ -542,9 +544,6 @@ void Raytracer::generateImage(const size_t timestep, WcTimingTree* tt) {
          Vec3 direction = (pixelLocation - cameraPosition_).getNormalized();
          ray.setDirection(direction);
          
-         //if (x != 110) continue;
-         //if (y != 80 && y != 150) continue;
-         
          n.reset();
          t_closest = inf;
          body_closest = NULL;
@@ -629,13 +628,20 @@ void Raytracer::generateImage(const size_t timestep, WcTimingTree* tt) {
 #endif
    }
    
-   switch(reductionMethod_) {
-      case MPI_REDUCE:
-         syncImageUsingMPIReduce(intersectionsBuffer, tt);
-         break;
-      case MPI_GATHER:
-         syncImageUsingMPIGather(intersections, intersectionsBuffer, tt);
-         break;
+   // Reduction with different methods only makes sense if actually using MPI.
+   // Besides that, the MPI reduce routine does not compile without MPI.
+   WALBERLA_MPI_SECTION() {
+      switch(reductionMethod_) {
+         case MPI_REDUCE:
+            syncImageUsingMPIReduce(intersectionsBuffer, tt);
+            break;
+         case MPI_GATHER:
+            syncImageUsingMPIGather(intersections, intersectionsBuffer, tt);
+            break;
+      }
+   }
+   WALBERLA_NON_MPI_SECTION() {
+      syncImageUsingMPIGather(intersections, intersectionsBuffer, tt);
    }
    
    if (tt != NULL) tt->start("Output");
-- 
GitLab