From 628529d0d74cef673f0361f226fa0916d1d48535 Mon Sep 17 00:00:00 2001 From: Erik Lindahl Date: Fri, 29 Dec 2017 23:18:38 +0100 Subject: [PATCH] Fix OpenCL compiles on Mac OS 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 | 9 +++++++- src/gromacs/gpu_utils/gpu_utils_ocl.cpp | 4 +++- src/gromacs/gpu_utils/ocl_compiler.cpp | 22 ++++++++++++++++++- .../mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp | 4 ++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/config.h.cmakein b/src/config.h.cmakein index dfdb3f10d7..bd7e658901 100644 --- a/src/config.h.cmakein +++ b/src/config.h.cmakein @@ -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. @@ -315,6 +315,13 @@ /* Define to 1 if yo have the 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 header file. */ #cmakedefine01 HAVE_PWD_H diff --git a/src/gromacs/gpu_utils/gpu_utils_ocl.cpp b/src/gromacs/gpu_utils/gpu_utils_ocl.cpp index ee7683db84..0d78643d61 100644 --- a/src/gromacs/gpu_utils/gpu_utils_ocl.cpp +++ b/src/gromacs/gpu_utils/gpu_utils_ocl.cpp @@ -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()); diff --git a/src/gromacs/gpu_utils/ocl_compiler.cpp b/src/gromacs/gpu_utils/ocl_compiler.cpp index 76f439e1bd..950c323fab 100644 --- a/src/gromacs/gpu_utils/ocl_compiler.cpp +++ b/src/gromacs/gpu_utils/ocl_compiler.cpp @@ -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 +#include #include #include @@ -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; } diff --git a/src/gromacs/mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp b/src/gromacs/mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp index 6ce4891b1b..4b83058434 100644 --- a/src/gromacs/mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp +++ b/src/gromacs/mdlib/nbnxn_ocl/nbnxn_ocl_jit_support.cpp @@ -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", -- 2.22.0