2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Define functions for detection and initialization for OpenCL devices.
38 * \author Anca Hamuraru <anca@streamcomputing.eu>
39 * \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
40 * \author Teemu Virolainen <teemu@streamcomputing.eu>
50 # include <sys/sysctl.h>
55 #include "gromacs/gpu_utils/gpu_utils.h"
56 #include "gromacs/gpu_utils/ocl_compiler.h"
57 #include "gromacs/gpu_utils/oclutils.h"
58 #include "gromacs/hardware/hw_info.h"
59 #include "gromacs/mdtypes/md_enums.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/fatalerror.h"
63 #include "gromacs/utility/smalloc.h"
64 #include "gromacs/utility/stringutil.h"
66 /*! \brief Return true if executing on compatible OS for AMD OpenCL.
68 * This is assumed to be true for OS X version of at least 10.10.4 and
69 * all other OS flavors.
71 * Uses the BSD sysctl() interfaces to extract the kernel version.
73 * \return true if version is 14.4 or later (= OS X version 10.10.4),
74 * or OS is not Darwin.
77 runningOnCompatibleOSForAmd()
81 char kernelVersion[256];
82 size_t len = sizeof(kernelVersion);
85 mib[1] = KERN_OSRELEASE;
87 sysctl(mib, sizeof(mib)/sizeof(mib[0]), kernelVersion, &len, NULL, 0);
89 int major = strtod(kernelVersion, NULL);
90 int minor = strtod(strchr(kernelVersion, '.')+1, NULL);
92 // Kernel 14.4 corresponds to OS X 10.10.4
93 return (major > 14 || (major == 14 && minor >= 4));
99 /*! \brief Returns true if the gpu characterized by the device properties is
100 * supported by the native gpu acceleration.
101 * \returns true if the GPU properties passed indicate a compatible
102 * GPU, otherwise false.
104 static int is_gmx_supported_gpu_id(struct gmx_device_info_t *ocl_gpu_device)
106 if ((getenv("GMX_OCL_DISABLE_COMPATIBILITY_CHECK")) != NULL)
108 return egpuCompatible;
111 /* Only AMD and NVIDIA GPUs are supported for now */
112 switch (ocl_gpu_device->vendor_e)
114 case OCL_VENDOR_NVIDIA:
115 return egpuCompatible;
117 return runningOnCompatibleOSForAmd() ? egpuCompatible : egpuIncompatible;
119 return egpuIncompatible;
124 /*! \brief Returns an ocl_vendor_id_t value corresponding to the input OpenCL vendor name.
126 * \param[in] vendor_name String with OpenCL vendor name.
127 * \returns ocl_vendor_id_t value for the input vendor_name
129 static ocl_vendor_id_t get_vendor_id(char *vendor_name)
133 if (strstr(vendor_name, "NVIDIA"))
135 return OCL_VENDOR_NVIDIA;
138 if (strstr(vendor_name, "AMD") ||
139 strstr(vendor_name, "Advanced Micro Devices"))
141 return OCL_VENDOR_AMD;
144 if (strstr(vendor_name, "Intel"))
146 return OCL_VENDOR_INTEL;
149 return OCL_VENDOR_UNKNOWN;
153 //! This function is documented in the header file
154 bool canDetectGpus(std::string *errorMessage)
156 cl_uint numPlatforms;
157 cl_int status = clGetPlatformIDs(0, nullptr, &numPlatforms);
158 GMX_ASSERT(status != CL_INVALID_VALUE, "Incorrect call of clGetPlatformIDs detected");
159 if (status == CL_PLATFORM_NOT_FOUND_KHR)
161 // No valid ICDs found
162 if (errorMessage != nullptr)
164 errorMessage->assign("No valid OpenCL driver found");
168 GMX_RELEASE_ASSERT(status == CL_SUCCESS,
169 gmx::formatString("An unexpected value was returned from clGetPlatformIDs %u: %s",
170 status, ocl_get_error_string(status).c_str()).c_str());
171 bool foundPlatform = (numPlatforms > 0);
172 if (!foundPlatform && errorMessage != nullptr)
174 errorMessage->assign("No OpenCL platforms found even though the driver was valid");
176 return foundPlatform;
179 //! This function is documented in the header file
180 void findGpus(gmx_gpu_info_t *gpu_info)
182 cl_uint ocl_platform_count;
183 cl_platform_id *ocl_platform_ids;
184 cl_device_type req_dev_type = CL_DEVICE_TYPE_GPU;
186 ocl_platform_ids = NULL;
188 if (getenv("GMX_OCL_FORCE_CPU") != NULL)
190 req_dev_type = CL_DEVICE_TYPE_CPU;
195 cl_int status = clGetPlatformIDs(0, NULL, &ocl_platform_count);
196 if (CL_SUCCESS != status)
198 GMX_THROW(gmx::InternalError(gmx::formatString("An unexpected value %u was returned from clGetPlatformIDs: ",
199 status) + ocl_get_error_string(status)));
202 if (1 > ocl_platform_count)
204 // TODO this should have a descriptive error message that we only support one OpenCL platform
208 snew(ocl_platform_ids, ocl_platform_count);
210 status = clGetPlatformIDs(ocl_platform_count, ocl_platform_ids, NULL);
211 if (CL_SUCCESS != status)
213 GMX_THROW(gmx::InternalError(gmx::formatString("An unexpected value %u was returned from clGetPlatformIDs: ",
214 status) + ocl_get_error_string(status)));
217 for (unsigned int i = 0; i < ocl_platform_count; i++)
219 cl_uint ocl_device_count;
221 /* If requesting req_dev_type devices fails, just go to the next platform */
222 if (CL_SUCCESS != clGetDeviceIDs(ocl_platform_ids[i], req_dev_type, 0, NULL, &ocl_device_count))
227 if (1 <= ocl_device_count)
229 gpu_info->n_dev += ocl_device_count;
233 if (1 > gpu_info->n_dev)
238 snew(gpu_info->gpu_dev, gpu_info->n_dev);
242 cl_device_id *ocl_device_ids;
244 snew(ocl_device_ids, gpu_info->n_dev);
247 for (unsigned int i = 0; i < ocl_platform_count; i++)
249 cl_uint ocl_device_count;
251 /* If requesting req_dev_type devices fails, just go to the next platform */
252 if (CL_SUCCESS != clGetDeviceIDs(ocl_platform_ids[i], req_dev_type, gpu_info->n_dev, ocl_device_ids, &ocl_device_count))
257 if (1 > ocl_device_count)
262 for (unsigned int j = 0; j < ocl_device_count; j++)
264 gpu_info->gpu_dev[device_index].ocl_gpu_id.ocl_platform_id = ocl_platform_ids[i];
265 gpu_info->gpu_dev[device_index].ocl_gpu_id.ocl_device_id = ocl_device_ids[j];
267 gpu_info->gpu_dev[device_index].device_name[0] = 0;
268 clGetDeviceInfo(ocl_device_ids[j], CL_DEVICE_NAME, sizeof(gpu_info->gpu_dev[device_index].device_name), gpu_info->gpu_dev[device_index].device_name, NULL);
270 gpu_info->gpu_dev[device_index].device_version[0] = 0;
271 clGetDeviceInfo(ocl_device_ids[j], CL_DEVICE_VERSION, sizeof(gpu_info->gpu_dev[device_index].device_version), gpu_info->gpu_dev[device_index].device_version, NULL);
273 gpu_info->gpu_dev[device_index].device_vendor[0] = 0;
274 clGetDeviceInfo(ocl_device_ids[j], CL_DEVICE_VENDOR, sizeof(gpu_info->gpu_dev[device_index].device_vendor), gpu_info->gpu_dev[device_index].device_vendor, NULL);
276 gpu_info->gpu_dev[device_index].compute_units = 0;
277 clGetDeviceInfo(ocl_device_ids[j], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(gpu_info->gpu_dev[device_index].compute_units), &(gpu_info->gpu_dev[device_index].compute_units), NULL);
279 gpu_info->gpu_dev[device_index].adress_bits = 0;
280 clGetDeviceInfo(ocl_device_ids[j], CL_DEVICE_ADDRESS_BITS, sizeof(gpu_info->gpu_dev[device_index].adress_bits), &(gpu_info->gpu_dev[device_index].adress_bits), NULL);
282 gpu_info->gpu_dev[device_index].vendor_e = get_vendor_id(gpu_info->gpu_dev[device_index].device_vendor);
284 gpu_info->gpu_dev[device_index].stat = is_gmx_supported_gpu_id(gpu_info->gpu_dev + device_index);
286 if (egpuCompatible == gpu_info->gpu_dev[device_index].stat)
288 gpu_info->n_dev_compatible++;
295 gpu_info->n_dev = device_index;
297 /* Dummy sort of devices - AMD first, then NVIDIA, then Intel */
298 // TODO: Sort devices based on performance.
299 if (0 < gpu_info->n_dev)
302 for (int i = 0; i < gpu_info->n_dev; i++)
304 if (OCL_VENDOR_AMD == gpu_info->gpu_dev[i].vendor_e)
310 gmx_device_info_t ocl_gpu_info;
311 ocl_gpu_info = gpu_info->gpu_dev[i];
312 gpu_info->gpu_dev[i] = gpu_info->gpu_dev[last];
313 gpu_info->gpu_dev[last] = ocl_gpu_info;
318 /* if more than 1 device left to be sorted */
319 if ((gpu_info->n_dev - 1 - last) > 1)
321 for (int i = 0; i < gpu_info->n_dev; i++)
323 if (OCL_VENDOR_NVIDIA == gpu_info->gpu_dev[i].vendor_e)
329 gmx_device_info_t ocl_gpu_info;
330 ocl_gpu_info = gpu_info->gpu_dev[i];
331 gpu_info->gpu_dev[i] = gpu_info->gpu_dev[last];
332 gpu_info->gpu_dev[last] = ocl_gpu_info;
339 sfree(ocl_device_ids);
345 sfree(ocl_platform_ids);
348 //! This function is documented in the header file
349 void free_gpu_info(const gmx_gpu_info_t gmx_unused *gpu_info)
351 if (gpu_info == NULL)
356 sfree(gpu_info->gpu_dev);
359 //! This function is documented in the header file
360 std::vector<int> getCompatibleGpus(const gmx_gpu_info_t &gpu_info)
362 // Possible minor over-allocation here, but not important for anything
363 std::vector<int> compatibleGpus;
364 compatibleGpus.reserve(gpu_info.n_dev);
365 for (int i = 0; i < gpu_info.n_dev; i++)
367 assert(gpu_info.gpu_dev);
368 if (gpu_info.gpu_dev[i].stat == egpuCompatible)
370 compatibleGpus.push_back(i);
373 return compatibleGpus;
376 //! This function is documented in the header file
377 const char *getGpuCompatibilityDescription(const gmx_gpu_info_t &gpu_info,
380 return (index >= gpu_info.n_dev ?
381 gpu_detect_res_str[egpuNonexistent] :
382 gpu_detect_res_str[gpu_info.gpu_dev[index].stat]);
385 //! This function is documented in the header file
386 void get_gpu_device_info_string(char *s, const gmx_gpu_info_t &gpu_info, int index)
390 if (index < 0 && index >= gpu_info.n_dev)
395 gmx_device_info_t *dinfo = &gpu_info.gpu_dev[index];
398 dinfo->stat == egpuCompatible ||
399 dinfo->stat == egpuIncompatible;
403 sprintf(s, "#%d: %s, stat: %s",
405 gpu_detect_res_str[dinfo->stat]);
409 sprintf(s, "#%d: name: %s, vendor: %s, device version: %s, stat: %s",
410 index, dinfo->device_name, dinfo->device_vendor,
411 dinfo->device_version,
412 gpu_detect_res_str[dinfo->stat]);
416 //! This function is documented in the header file
417 void init_gpu(const gmx::MDLogger & /*mdlog*/,
418 gmx_device_info_t *deviceInfo)
422 // If the device is NVIDIA, for safety reasons we disable the JIT
423 // caching as this is known to be broken at least until driver 364.19;
424 // the cache does not always get regenerated when the source code changes,
425 // e.g. if the path to the kernel sources remains the same
427 if (deviceInfo->vendor_e == OCL_VENDOR_NVIDIA)
429 // Ignore return values, failing to set the variable does not mean
430 // that something will go wrong later.
432 _putenv("CUDA_CACHE_DISABLE=1");
434 // Don't override, maybe a dev is testing.
435 setenv("CUDA_CACHE_DISABLE", "1", 0);
440 //! This function is documented in the header file
441 gmx_device_info_t *getDeviceInfo(const gmx_gpu_info_t &gpu_info,
444 if (deviceId < 0 || deviceId >= gpu_info.n_dev)
446 gmx_incons("Invalid GPU deviceId requested");
448 return &gpu_info.gpu_dev[deviceId];
451 //! This function is documented in the header file
452 size_t sizeof_gpu_dev_info(void)
454 return sizeof(gmx_device_info_t);
457 void gpu_set_host_malloc_and_free(bool bUseGpuKernels,
458 gmx_host_alloc_t **nb_alloc,
459 gmx_host_free_t **nb_free)
463 *nb_alloc = &ocl_pmalloc;
464 *nb_free = &ocl_pfree;