Fix OpenCL compiles on Mac OS
authorErik Lindahl <erik@kth.se>
Fri, 29 Dec 2017 22:18:38 +0000 (23:18 +0100)
committerMark Abraham <mark.j.abraham@gmail.com>
Tue, 2 Jan 2018 17:08:08 +0000 (18:08 +0100)
Confirmed to work on Mac OS 10.13.2 running
on a Macbook Pro with Radeon Pro 560.

Fixes #2369.

Change-Id: I0b0056075ecbaf7c11e0022ed13e43cfe2e7484c

src/config.h.cmakein
src/gromacs/gpu_utils/gpu_utils_ocl.cpp
src/gromacs/gpu_utils/ocl_compiler.cpp
src/gromacs/mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp

index dfdb3f10d74db1cd9bcc4936cc99928ef922cfe6..bd7e658901cbf87274cd3e1881a21602a9224d79 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 
 /* Define to 1 if yo have the <unistd.h> header file. */
 #cmakedefine HAVE_UNISTD_H
+#  ifdef __APPLE__
+// Mac OS 13.x has a bug where dispatch.h generates an error for OpenCL builds if
+// HAVE_UNISTD_H is merely defined, but not set to 1. Since unistd.h should always
+// be available on this platform we simply undefine and redefine it to 1 for now
+#    undef  HAVE_UNISTD_H
+#    define HAVE_UNISTD_H 1
+#endif
 
 /* Define to 1 if yo have the <pwd.h> header file. */
 #cmakedefine01 HAVE_PWD_H
index ee7683db84c7c7e8a3a9e945176525cf4c9f07a9..0d78643d61e5d29cd691f6b599d3180caf03fe33 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -156,6 +156,7 @@ bool canDetectGpus(std::string *errorMessage)
     cl_uint numPlatforms;
     cl_int  status       = clGetPlatformIDs(0, nullptr, &numPlatforms);
     GMX_ASSERT(status != CL_INVALID_VALUE, "Incorrect call of clGetPlatformIDs detected");
+#ifdef cl_khr_icd
     if (status == CL_PLATFORM_NOT_FOUND_KHR)
     {
         // No valid ICDs found
@@ -165,6 +166,7 @@ bool canDetectGpus(std::string *errorMessage)
         }
         return false;
     }
+#endif
     GMX_RELEASE_ASSERT(status == CL_SUCCESS,
                        gmx::formatString("An unexpected value was returned from clGetPlatformIDs %u: %s",
                                          status, ocl_get_error_string(status).c_str()).c_str());
index 76f439e1bd6eaa4b62b1ec574e6b13835be41305..950c323fab7c79e87a71ece156046b987a36e117 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -49,6 +49,7 @@
 
 #include <cstdio>
 
+#include <algorithm>
 #include <string>
 #include <vector>
 
@@ -363,6 +364,22 @@ static std::string makeKernelIncludePathOption(const std::string &unescapedKerne
     return includePathOption;
 }
 
+/*! \brief Replace duplicated spaces with a single one in string
+ *
+ * Only the first character will be kept for multiple adjacent characters that
+ * are both identical and where the first one returns true for isspace().
+ *
+ * \param str String that will be modified.
+ */
+static void
+removeExtraSpaces(std::string *str)
+{
+    GMX_RELEASE_ASSERT(str != nullptr, "A pointer to an actual string must be provided");
+    std::string::iterator newEnd =
+        std::unique( str->begin(), str->end(), [ = ](char a, char b){ return isspace(a) && (a == b); } );
+    str->erase(newEnd, str->end());
+}
+
 /*! \brief Builds a string with build options for the OpenCL kernels
  *
  * \throws std::bad_alloc  if out of memory. */
@@ -385,6 +402,9 @@ makePreprocessorOptions(const std::string   &kernelRootPath,
     preprocessorOptions += ' ';
     preprocessorOptions += makeKernelIncludePathOption(kernelRootPath);
 
+    // Mac OS (and maybe some other implementations) does not accept double spaces in options
+    removeExtraSpaces(&preprocessorOptions);
+
     return preprocessorOptions;
 }
 
index 6ce4891b1b5f7bb776326ec6f9e78ba0ce6cb7cd..4b83058434b03bd53ce1e3bf8087d53963a7b13c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015,2016,2017,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -92,7 +92,7 @@ static const char * kernel_VdW_family_definitions[] =
 {
     " -DVDWNAME=_VdwLJ",
     " -DLJ_COMB_GEOM -DVDWNAME=_VdwLJCombGeom",
-    " -DLJ_COMB_LB  -DVDWNAME=_VdwLJCombLB",
+    " -DLJ_COMB_LB -DVDWNAME=_VdwLJCombLB",
     " -DLJ_FORCE_SWITCH -DVDWNAME=_VdwLJFsw",
     " -DLJ_POT_SWITCH -DVDWNAME=_VdwLJPsw",
     " -DLJ_EWALD_COMB_GEOM -DVDWNAME=_VdwLJEwCombGeom",