Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / taskassignment / decidegpuusage.cpp
index 3e481bcd75c3a92a390fd2104320e4e4edfc794a..66b1a3cc53bb83787aaf1a31071c42ac53e7e89a 100644 (file)
@@ -78,43 +78,40 @@ namespace
 {
 
 //! Helper variable to localise the text of an often repeated message.
-const char * g_specifyEverythingFormatString =
-    "When you use mdrun -gputasks, %s must be set to non-default "
-    "values, so that the device IDs can be interpreted correctly."
+const char* g_specifyEverythingFormatString =
+        "When you use mdrun -gputasks, %s must be set to non-default "
+        "values, so that the device IDs can be interpreted correctly."
 #if GMX_GPU != GMX_GPU_NONE
-    " If you simply want to restrict which GPUs are used, then it is "
-    "better to use mdrun -gpu_id. Otherwise, setting the "
-#  if GMX_GPU == GMX_GPU_CUDA
-    "CUDA_VISIBLE_DEVICES"
-#  elif GMX_GPU == GMX_GPU_OPENCL
-    // Technically there is no portable way to do this offered by the
-    // OpenCL standard, but the only current relevant case for GROMACS
-    // is AMD OpenCL, which offers this variable.
-    "GPU_DEVICE_ORDINAL"
-#  else
-#  error "Unreachable branch"
-#  endif
-    " environment variable in your bash profile or job "
-    "script may be more convenient."
+        " If you simply want to restrict which GPUs are used, then it is "
+        "better to use mdrun -gpu_id. Otherwise, setting the "
+#    if GMX_GPU == GMX_GPU_CUDA
+        "CUDA_VISIBLE_DEVICES"
+#    elif GMX_GPU == GMX_GPU_OPENCL
+        // Technically there is no portable way to do this offered by the
+        // OpenCL standard, but the only current relevant case for GROMACS
+        // is AMD OpenCL, which offers this variable.
+        "GPU_DEVICE_ORDINAL"
+#    else
+#        error "Unreachable branch"
+#    endif
+        " environment variable in your bash profile or job "
+        "script may be more convenient."
 #endif
-;
-
-}   // namespace
-
-bool
-decideWhetherToUseGpusForNonbondedWithThreadMpi(const TaskTarget          nonbondedTarget,
-                                                const std::vector<int>   &gpuIdsToUse,
-                                                const std::vector<int>   &userGpuTaskAssignment,
-                                                const EmulateGpuNonbonded emulateGpuNonbonded,
-                                                const bool                buildSupportsNonbondedOnGpu,
-                                                const bool                nonbondedOnGpuIsUseful,
-                                                const int                 numRanksPerSimulation)
+        ;
+
+} // namespace
+
+bool decideWhetherToUseGpusForNonbondedWithThreadMpi(const TaskTarget        nonbondedTarget,
+                                                     const std::vector<int>& gpuIdsToUse,
+                                                     const std::vector<int>& userGpuTaskAssignment,
+                                                     const EmulateGpuNonbonded emulateGpuNonbonded,
+                                                     const bool buildSupportsNonbondedOnGpu,
+                                                     const bool nonbondedOnGpuIsUseful,
+                                                     const int  numRanksPerSimulation)
 {
     // First, exclude all cases where we can't run NB on GPUs.
-    if (nonbondedTarget == TaskTarget::Cpu ||
-        emulateGpuNonbonded == EmulateGpuNonbonded::Yes ||
-        !nonbondedOnGpuIsUseful ||
-        !buildSupportsNonbondedOnGpu)
+    if (nonbondedTarget == TaskTarget::Cpu || emulateGpuNonbonded == EmulateGpuNonbonded::Yes
+        || !nonbondedOnGpuIsUseful || !buildSupportsNonbondedOnGpu)
     {
         // If the user required NB on GPUs, we issue an error later.
         return false;
@@ -125,10 +122,10 @@ decideWhetherToUseGpusForNonbondedWithThreadMpi(const TaskTarget          nonbon
     if (!userGpuTaskAssignment.empty())
     {
         // Specifying -gputasks requires specifying everything.
-        if (nonbondedTarget == TaskTarget::Auto ||
-            numRanksPerSimulation < 1)
+        if (nonbondedTarget == TaskTarget::Auto || numRanksPerSimulation < 1)
         {
-            GMX_THROW(InconsistentInputError(formatString(g_specifyEverythingFormatString, "-nb and -ntmpi")));
+            GMX_THROW(InconsistentInputError(
+                    formatString(g_specifyEverythingFormatString, "-nb and -ntmpi")));
         }
         return true;
     }
@@ -147,23 +144,20 @@ decideWhetherToUseGpusForNonbondedWithThreadMpi(const TaskTarget          nonbon
     return haveGpus;
 }
 
-bool
-decideWhetherToUseGpusForPmeWithThreadMpi(const bool              useGpuForNonbonded,
-                                          const TaskTarget        pmeTarget,
-                                          const std::vector<int> &gpuIdsToUse,
-                                          const std::vector<int> &userGpuTaskAssignment,
-                                          const gmx_hw_info_t    &hardwareInfo,
-                                          const t_inputrec       &inputrec,
-                                          const gmx_mtop_t       &mtop,
-                                          const int               numRanksPerSimulation,
-                                          const int               numPmeRanksPerSimulation)
+bool decideWhetherToUseGpusForPmeWithThreadMpi(const bool              useGpuForNonbonded,
+                                               const TaskTarget        pmeTarget,
+                                               const std::vector<int>& gpuIdsToUse,
+                                               const std::vector<int>& userGpuTaskAssignment,
+                                               const gmx_hw_info_t&    hardwareInfo,
+                                               const t_inputrec&       inputrec,
+                                               const gmx_mtop_t&       mtop,
+                                               const int               numRanksPerSimulation,
+                                               const int               numPmeRanksPerSimulation)
 {
     // First, exclude all cases where we can't run PME on GPUs.
-    if ((pmeTarget == TaskTarget::Cpu) ||
-        !useGpuForNonbonded ||
-        !pme_gpu_supports_build(nullptr) ||
-        !pme_gpu_supports_hardware(hardwareInfo, nullptr) ||
-        !pme_gpu_supports_input(inputrec, mtop, nullptr))
+    if ((pmeTarget == TaskTarget::Cpu) || !useGpuForNonbonded || !pme_gpu_supports_build(nullptr)
+        || !pme_gpu_supports_hardware(hardwareInfo, nullptr)
+        || !pme_gpu_supports_input(inputrec, mtop, nullptr))
     {
         // PME can't run on a GPU. If the user required that, we issue
         // an error later.
@@ -179,20 +173,21 @@ decideWhetherToUseGpusForPmeWithThreadMpi(const bool              useGpuForNonbo
         // later.
 
         // Specifying -gputasks requires specifying everything.
-        if (pmeTarget == TaskTarget::Auto ||
-            numRanksPerSimulation < 1)
+        if (pmeTarget == TaskTarget::Auto || numRanksPerSimulation < 1)
         {
-            GMX_THROW(InconsistentInputError(formatString(g_specifyEverythingFormatString, "all of -nb, -pme, and -ntmpi")));
+            GMX_THROW(InconsistentInputError(
+                    formatString(g_specifyEverythingFormatString, "all of -nb, -pme, and -ntmpi")));
         }
 
         // PME on GPUs is only supported in a single case
         if (pmeTarget == TaskTarget::Gpu)
         {
-            if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0)) ||
-                (numPmeRanksPerSimulation > 1))
+            if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0))
+                || (numPmeRanksPerSimulation > 1))
             {
-                GMX_THROW(InconsistentInputError
-                              ("When you run mdrun -pme gpu -gputasks, you must supply a PME-enabled .tpr file and use a single PME rank."));
+                GMX_THROW(InconsistentInputError(
+                        "When you run mdrun -pme gpu -gputasks, you must supply a PME-enabled .tpr "
+                        "file and use a single PME rank."));
             }
             return true;
         }
@@ -207,13 +202,13 @@ decideWhetherToUseGpusForPmeWithThreadMpi(const bool              useGpuForNonbo
 
     if (pmeTarget == TaskTarget::Gpu)
     {
-        if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0)) ||
-            (numPmeRanksPerSimulation > 1))
+        if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0))
+            || (numPmeRanksPerSimulation > 1))
         {
-            GMX_THROW(NotImplementedError
-                          ("PME tasks were required to run on GPUs, but that is not implemented with "
-                          "more than one PME rank. Use a single rank simulation, or a separate PME rank, "
-                          "or permit PME tasks to be assigned to the CPU."));
+            GMX_THROW(NotImplementedError(
+                    "PME tasks were required to run on GPUs, but that is not implemented with "
+                    "more than one PME rank. Use a single rank simulation, or a separate PME rank, "
+                    "or permit PME tasks to be assigned to the CPU."));
         }
         return true;
     }
@@ -237,20 +232,20 @@ decideWhetherToUseGpusForPmeWithThreadMpi(const bool              useGpuForNonbo
     return false;
 }
 
-bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarget,
-                                        const std::vector<int>    &userGpuTaskAssignment,
-                                        const EmulateGpuNonbonded  emulateGpuNonbonded,
-                                        const bool                 buildSupportsNonbondedOnGpu,
-                                        const bool                 nonbondedOnGpuIsUseful,
-                                        const bool                 gpusWereDetected)
+bool decideWhetherToUseGpusForNonbonded(const TaskTarget          nonbondedTarget,
+                                        const std::vector<int>&   userGpuTaskAssignment,
+                                        const EmulateGpuNonbonded emulateGpuNonbonded,
+                                        const bool                buildSupportsNonbondedOnGpu,
+                                        const bool                nonbondedOnGpuIsUseful,
+                                        const bool                gpusWereDetected)
 {
     if (nonbondedTarget == TaskTarget::Cpu)
     {
         if (!userGpuTaskAssignment.empty())
         {
-            GMX_THROW(InconsistentInputError
-                          ("A GPU task assignment was specified, but nonbonded interactions were "
-                          "assigned to the CPU. Make no more than one of these choices."));
+            GMX_THROW(InconsistentInputError(
+                    "A GPU task assignment was specified, but nonbonded interactions were "
+                    "assigned to the CPU. Make no more than one of these choices."));
         }
 
         return false;
@@ -258,11 +253,11 @@ bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarg
 
     if (!buildSupportsNonbondedOnGpu && nonbondedTarget == TaskTarget::Gpu)
     {
-        GMX_THROW(InconsistentInputError
-                      ("Nonbonded interactions on the GPU were requested with -nb gpu, "
-                      "but the GROMACS binary has been built without GPU support. "
-                      "Either run without selecting GPU options, or recompile GROMACS "
-                      "with GPU support enabled"));
+        GMX_THROW(InconsistentInputError(
+                "Nonbonded interactions on the GPU were requested with -nb gpu, "
+                "but the GROMACS binary has been built without GPU support. "
+                "Either run without selecting GPU options, or recompile GROMACS "
+                "with GPU support enabled"));
     }
 
     // TODO refactor all these TaskTarget::Gpu checks into one place?
@@ -272,14 +267,15 @@ bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarg
     {
         if (nonbondedTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(InconsistentInputError
-                          ("Nonbonded interactions on the GPU were required, which is inconsistent "
-                          "with choosing emulation. Make no more than one of these choices."));
+            GMX_THROW(InconsistentInputError(
+                    "Nonbonded interactions on the GPU were required, which is inconsistent "
+                    "with choosing emulation. Make no more than one of these choices."));
         }
         if (!userGpuTaskAssignment.empty())
         {
-            GMX_THROW(InconsistentInputError
-                          ("GPU ID usage was specified, as was GPU emulation. Make no more than one of these choices."));
+            GMX_THROW(
+                    InconsistentInputError("GPU ID usage was specified, as was GPU emulation. Make "
+                                           "no more than one of these choices."));
         }
 
         return false;
@@ -289,9 +285,9 @@ bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarg
     {
         if (nonbondedTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(InconsistentInputError
-                          ("Nonbonded interactions on the GPU were required, but not supported for these "
-                          "simulation settings. Change your settings, or do not require using GPUs."));
+            GMX_THROW(InconsistentInputError(
+                    "Nonbonded interactions on the GPU were required, but not supported for these "
+                    "simulation settings. Change your settings, or do not require using GPUs."));
         }
 
         return false;
@@ -302,7 +298,8 @@ bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarg
         // Specifying -gputasks requires specifying everything.
         if (nonbondedTarget == TaskTarget::Auto)
         {
-            GMX_THROW(InconsistentInputError(formatString(g_specifyEverythingFormatString, "-nb and -ntmpi")));
+            GMX_THROW(InconsistentInputError(
+                    formatString(g_specifyEverythingFormatString, "-nb and -ntmpi")));
         }
 
         return true;
@@ -324,10 +321,10 @@ bool decideWhetherToUseGpusForNonbonded(const TaskTarget           nonbondedTarg
 
 bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
                                   const TaskTarget        pmeTarget,
-                                  const std::vector<int> &userGpuTaskAssignment,
-                                  const gmx_hw_info_t    &hardwareInfo,
-                                  const t_inputrec       &inputrec,
-                                  const gmx_mtop_t       &mtop,
+                                  const std::vector<int>userGpuTaskAssignment,
+                                  const gmx_hw_info_t&    hardwareInfo,
+                                  const t_inputrec&       inputrec,
+                                  const gmx_mtop_t&       mtop,
                                   const int               numRanksPerSimulation,
                                   const int               numPmeRanksPerSimulation,
                                   const bool              gpusWereDetected)
@@ -341,8 +338,8 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
     {
         if (pmeTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(NotImplementedError
-                          ("PME on GPUs is only supported when nonbonded interactions run on GPUs also."));
+            GMX_THROW(NotImplementedError(
+                    "PME on GPUs is only supported when nonbonded interactions run on GPUs also."));
         }
         return false;
     }
@@ -352,8 +349,7 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
     {
         if (pmeTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(NotImplementedError
-                          ("Cannot compute PME interactions on a GPU, because " + message));
+            GMX_THROW(NotImplementedError("Cannot compute PME interactions on a GPU, because " + message));
         }
         return false;
     }
@@ -361,8 +357,7 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
     {
         if (pmeTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(NotImplementedError
-                          ("Cannot compute PME interactions on a GPU, because " + message));
+            GMX_THROW(NotImplementedError("Cannot compute PME interactions on a GPU, because " + message));
         }
         return false;
     }
@@ -370,8 +365,7 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
     {
         if (pmeTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(NotImplementedError
-                          ("Cannot compute PME interactions on a GPU, because " + message));
+            GMX_THROW(NotImplementedError("Cannot compute PME interactions on a GPU, because " + message));
         }
         return false;
     }
@@ -380,9 +374,9 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
     {
         if (!userGpuTaskAssignment.empty())
         {
-            GMX_THROW(InconsistentInputError
-                          ("A GPU task assignment was specified, but PME interactions were "
-                          "assigned to the CPU. Make no more than one of these choices."));
+            GMX_THROW(InconsistentInputError(
+                    "A GPU task assignment was specified, but PME interactions were "
+                    "assigned to the CPU. Make no more than one of these choices."));
         }
 
         return false;
@@ -393,7 +387,8 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
         // Specifying -gputasks requires specifying everything.
         if (pmeTarget == TaskTarget::Auto)
         {
-            GMX_THROW(InconsistentInputError(formatString(g_specifyEverythingFormatString, "all of -nb, -pme, and -ntmpi"))); // TODO ntmpi?
+            GMX_THROW(InconsistentInputError(formatString(
+                    g_specifyEverythingFormatString, "all of -nb, -pme, and -ntmpi"))); // TODO ntmpi?
         }
 
         return true;
@@ -406,13 +401,13 @@ bool decideWhetherToUseGpusForPme(const bool              useGpuForNonbonded,
 
     if (pmeTarget == TaskTarget::Gpu)
     {
-        if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0)) ||
-            (numPmeRanksPerSimulation > 1))
+        if (((numRanksPerSimulation > 1) && (numPmeRanksPerSimulation == 0))
+            || (numPmeRanksPerSimulation > 1))
         {
-            GMX_THROW(NotImplementedError
-                          ("PME tasks were required to run on GPUs, but that is not implemented with "
-                          "more than one PME rank. Use a single rank simulation, or a separate PME rank, "
-                          "or permit PME tasks to be assigned to the CPU."));
+            GMX_THROW(NotImplementedError(
+                    "PME tasks were required to run on GPUs, but that is not implemented with "
+                    "more than one PME rank. Use a single rank simulation, or a separate PME rank, "
+                    "or permit PME tasks to be assigned to the CPU."));
         }
         return true;
     }
@@ -448,9 +443,9 @@ bool decideWhetherToUseGpusForBonded(const bool       useGpuForNonbonded,
     {
         if (bondedTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(InconsistentInputError
-                          ("Bonded interactions on the GPU were required, but not supported for these "
-                          "simulation settings. Change your settings, or do not require using GPUs."));
+            GMX_THROW(InconsistentInputError(
+                    "Bonded interactions on the GPU were required, but not supported for these "
+                    "simulation settings. Change your settings, or do not require using GPUs."));
         }
 
         return false;
@@ -460,10 +455,10 @@ bool decideWhetherToUseGpusForBonded(const bool       useGpuForNonbonded,
     {
         if (bondedTarget == TaskTarget::Gpu)
         {
-            GMX_THROW(InconsistentInputError
-                          ("Bonded interactions on the GPU were required, but this requires that "
-                          "short-ranged non-bonded interactions are also run on the GPU. Change "
-                          "your settings, or do not require using GPUs."));
+            GMX_THROW(InconsistentInputError(
+                    "Bonded interactions on the GPU were required, but this requires that "
+                    "short-ranged non-bonded interactions are also run on the GPU. Change "
+                    "your settings, or do not require using GPUs."));
         }
 
         return false;
@@ -485,7 +480,8 @@ bool decideWhetherToUseGpusForBonded(const bool       useGpuForNonbonded,
     // (It would be better to dynamically assign bondeds based on timings)
     // Note that here we assume that the auto setting of PME ranks will not
     // choose seperate PME ranks when nonBonded are assigned to the GPU.
-    bool usingOurCpuForPmeOrEwald = (usingLJPme || (usingElecPmeOrEwald && !useGpuForPme && numPmeRanksPerSimulation <= 0));
+    bool usingOurCpuForPmeOrEwald =
+            (usingLJPme || (usingElecPmeOrEwald && !useGpuForPme && numPmeRanksPerSimulation <= 0));
 
     return gpusWereDetected && usingOurCpuForPmeOrEwald;
 }
@@ -496,7 +492,7 @@ bool decideWhetherToUseGpuForUpdate(const bool        forceGpuUpdateDefaultOn,
                                     const bool        useGpuForNonbonded,
                                     const TaskTarget  updateTarget,
                                     const bool        gpusWereDetected,
-                                    const t_inputrec &inputrec,
+                                    const t_inputrecinputrec,
                                     const bool        haveVSites,
                                     const bool        useEssentialDynamics,
                                     const bool        doOrientationRestraints,
@@ -519,7 +515,8 @@ bool decideWhetherToUseGpuForUpdate(const bool        forceGpuUpdateDefaultOn,
     // 2. Non-bonded interactions are on the GPU.
     if (!(useGpuForPme || useGpuForNonbonded))
     {
-        errorMessage += "Either PME or short-ranged non-bonded interaction tasks must run on the GPU.\n";
+        errorMessage +=
+                "Either PME or short-ranged non-bonded interaction tasks must run on the GPU.\n";
     }
     if (!gpusWereDetected)
     {
@@ -582,14 +579,16 @@ bool decideWhetherToUseGpuForUpdate(const bool        forceGpuUpdateDefaultOn,
     {
         if (updateTarget == TaskTarget::Gpu)
         {
-            std::string prefix = gmx::formatString("Update task on the GPU was required,\n"
-                                                   "but the following condition(s) were not satisfied:\n");
+            std::string prefix = gmx::formatString(
+                    "Update task on the GPU was required,\n"
+                    "but the following condition(s) were not satisfied:\n");
             GMX_THROW(InconsistentInputError((prefix + errorMessage).c_str()));
         }
         return false;
     }
 
-    return ((forceGpuUpdateDefaultOn && updateTarget == TaskTarget::Auto) || (updateTarget == TaskTarget::Gpu));
+    return ((forceGpuUpdateDefaultOn && updateTarget == TaskTarget::Auto)
+            || (updateTarget == TaskTarget::Gpu));
 }
 
-}  // namespace gmx
+} // namespace gmx