Merge branch release-2016
[gromacs.git] / src / gromacs / gpu_utils / gpu_utils_ocl.cpp
blob4d4d6d6d2390317bcd2899233470b6ae4c9e10bd
1 /*
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.
35 /*! \internal \file
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>
43 #include "gmxpre.h"
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #ifdef __APPLE__
50 # include <sys/sysctl.h>
51 #endif
53 #include <memory.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/fatalerror.h"
62 #include "gromacs/utility/smalloc.h"
64 /*! \brief Helper macro for error handling */
65 #define CALLOCLFUNC_LOGERROR(func, err_str, retval) { \
66 cl_int opencl_ret = func; \
67 if (CL_SUCCESS != opencl_ret) \
68 { \
69 sprintf(err_str, "OpenCL error %d", opencl_ret); \
70 retval = -1; \
71 } \
72 else{ \
73 retval = 0; } \
77 /*! \brief Return true if executing on compatible OS for AMD OpenCL.
79 * This is assumed to be true for OS X version of at least 10.10.4 and
80 * all other OS flavors.
82 * Uses the BSD sysctl() interfaces to extract the kernel version.
84 * \return true if version is 14.4 or later (= OS X version 10.10.4),
85 * or OS is not Darwin.
87 static bool
88 runningOnCompatibleOSForAmd()
90 #ifdef __APPLE__
91 int mib[2];
92 char kernelVersion[256];
93 size_t len = sizeof(kernelVersion);
95 mib[0] = CTL_KERN;
96 mib[1] = KERN_OSRELEASE;
98 sysctl(mib, sizeof(mib)/sizeof(mib[0]), kernelVersion, &len, NULL, 0);
100 int major = strtod(kernelVersion, NULL);
101 int minor = strtod(strchr(kernelVersion, '.')+1, NULL);
103 // Kernel 14.4 corresponds to OS X 10.10.4
104 return (major > 14 || (major == 14 && minor >= 4));
105 #else
106 return true;
107 #endif
110 /*! \brief Returns true if the gpu characterized by the device properties is
111 * supported by the native gpu acceleration.
112 * \returns true if the GPU properties passed indicate a compatible
113 * GPU, otherwise false.
115 static int is_gmx_supported_gpu_id(struct gmx_device_info_t *ocl_gpu_device)
117 /* Only AMD and NVIDIA GPUs are supported for now */
118 switch (ocl_gpu_device->vendor_e)
120 case OCL_VENDOR_NVIDIA:
121 return egpuCompatible;
122 case OCL_VENDOR_AMD:
123 return runningOnCompatibleOSForAmd() ? egpuCompatible : egpuIncompatible;
124 default:
125 return egpuIncompatible;
130 /*! \brief Returns an ocl_vendor_id_t value corresponding to the input OpenCL vendor name.
132 * \param[in] vendor_name String with OpenCL vendor name.
133 * \returns ocl_vendor_id_t value for the input vendor_name
135 static ocl_vendor_id_t get_vendor_id(char *vendor_name)
137 if (vendor_name)
139 if (strstr(vendor_name, "NVIDIA"))
141 return OCL_VENDOR_NVIDIA;
143 else
144 if (strstr(vendor_name, "AMD") ||
145 strstr(vendor_name, "Advanced Micro Devices"))
147 return OCL_VENDOR_AMD;
149 else
150 if (strstr(vendor_name, "Intel"))
152 return OCL_VENDOR_INTEL;
155 return OCL_VENDOR_UNKNOWN;
159 //! This function is documented in the header file
160 int detect_gpus(gmx_gpu_info_t *gpu_info, char *err_str)
162 int retval;
163 cl_uint ocl_platform_count;
164 cl_platform_id *ocl_platform_ids;
165 cl_device_type req_dev_type = CL_DEVICE_TYPE_GPU;
167 retval = 0;
168 ocl_platform_ids = NULL;
170 if (getenv("GMX_OCL_FORCE_CPU") != NULL)
172 req_dev_type = CL_DEVICE_TYPE_CPU;
175 while (1)
177 CALLOCLFUNC_LOGERROR(clGetPlatformIDs(0, NULL, &ocl_platform_count), err_str, retval)
178 if (0 != retval)
180 break;
183 if (1 > ocl_platform_count)
185 break;
188 snew(ocl_platform_ids, ocl_platform_count);
190 CALLOCLFUNC_LOGERROR(clGetPlatformIDs(ocl_platform_count, ocl_platform_ids, NULL), err_str, retval)
191 if (0 != retval)
193 break;
196 for (unsigned int i = 0; i < ocl_platform_count; i++)
198 cl_uint ocl_device_count;
200 /* If requesting req_dev_type devices fails, just go to the next platform */
201 if (CL_SUCCESS != clGetDeviceIDs(ocl_platform_ids[i], req_dev_type, 0, NULL, &ocl_device_count))
203 continue;
206 if (1 <= ocl_device_count)
208 gpu_info->n_dev += ocl_device_count;
212 if (1 > gpu_info->n_dev)
214 break;
217 snew(gpu_info->gpu_dev, gpu_info->n_dev);
220 int device_index;
221 cl_device_id *ocl_device_ids;
223 snew(ocl_device_ids, gpu_info->n_dev);
224 device_index = 0;
226 for (unsigned int i = 0; i < ocl_platform_count; i++)
228 cl_uint ocl_device_count;
230 /* If requesting req_dev_type devices fails, just go to the next platform */
231 if (CL_SUCCESS != clGetDeviceIDs(ocl_platform_ids[i], req_dev_type, gpu_info->n_dev, ocl_device_ids, &ocl_device_count))
233 continue;
236 if (1 > ocl_device_count)
238 break;
241 for (unsigned int j = 0; j < ocl_device_count; j++)
243 gpu_info->gpu_dev[device_index].ocl_gpu_id.ocl_platform_id = ocl_platform_ids[i];
244 gpu_info->gpu_dev[device_index].ocl_gpu_id.ocl_device_id = ocl_device_ids[j];
246 gpu_info->gpu_dev[device_index].device_name[0] = 0;
247 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);
249 gpu_info->gpu_dev[device_index].device_version[0] = 0;
250 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);
252 gpu_info->gpu_dev[device_index].device_vendor[0] = 0;
253 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);
255 gpu_info->gpu_dev[device_index].compute_units = 0;
256 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);
258 gpu_info->gpu_dev[device_index].adress_bits = 0;
259 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);
261 gpu_info->gpu_dev[device_index].vendor_e = get_vendor_id(gpu_info->gpu_dev[device_index].device_vendor);
263 gpu_info->gpu_dev[device_index].stat = is_gmx_supported_gpu_id(gpu_info->gpu_dev + device_index);
265 if (egpuCompatible == gpu_info->gpu_dev[device_index].stat)
267 gpu_info->n_dev_compatible++;
270 device_index++;
274 gpu_info->n_dev = device_index;
276 /* Dummy sort of devices - AMD first, then NVIDIA, then Intel */
277 // TODO: Sort devices based on performance.
278 if (0 < gpu_info->n_dev)
280 int last = -1;
281 for (int i = 0; i < gpu_info->n_dev; i++)
283 if (OCL_VENDOR_AMD == gpu_info->gpu_dev[i].vendor_e)
285 last++;
287 if (last < i)
289 gmx_device_info_t ocl_gpu_info;
290 ocl_gpu_info = gpu_info->gpu_dev[i];
291 gpu_info->gpu_dev[i] = gpu_info->gpu_dev[last];
292 gpu_info->gpu_dev[last] = ocl_gpu_info;
297 /* if more than 1 device left to be sorted */
298 if ((gpu_info->n_dev - 1 - last) > 1)
300 for (int i = 0; i < gpu_info->n_dev; i++)
302 if (OCL_VENDOR_NVIDIA == gpu_info->gpu_dev[i].vendor_e)
304 last++;
306 if (last < i)
308 gmx_device_info_t ocl_gpu_info;
309 ocl_gpu_info = gpu_info->gpu_dev[i];
310 gpu_info->gpu_dev[i] = gpu_info->gpu_dev[last];
311 gpu_info->gpu_dev[last] = ocl_gpu_info;
318 sfree(ocl_device_ids);
321 break;
324 sfree(ocl_platform_ids);
326 return retval;
329 //! This function is documented in the header file
330 void free_gpu_info(const gmx_gpu_info_t gmx_unused *gpu_info)
332 if (gpu_info == NULL)
334 return;
337 sfree(gpu_info->gpu_dev);
340 //! This function is documented in the header file
341 int getGpuCompatibilityStatus(const gmx_gpu_info_t *gpu_info,
342 int index)
344 assert(gpu_info);
346 return (index >= gpu_info->n_dev) ? egpuNonexistent : gpu_info->gpu_dev[index].stat;
349 //! This function is documented in the header file
350 void get_gpu_device_info_string(char gmx_unused *s, const gmx_gpu_info_t gmx_unused *gpu_info, int gmx_unused index)
352 assert(s);
353 assert(gpu_info);
355 if (index < 0 && index >= gpu_info->n_dev)
357 return;
360 gmx_device_info_t *dinfo = &gpu_info->gpu_dev[index];
362 bool bGpuExists =
363 dinfo->stat == egpuCompatible ||
364 dinfo->stat == egpuIncompatible;
366 if (!bGpuExists)
368 sprintf(s, "#%d: %s, stat: %s",
369 index, "N/A",
370 gpu_detect_res_str[dinfo->stat]);
372 else
374 sprintf(s, "#%d: name: %s, vendor: %s, device version: %s, stat: %s",
375 index, dinfo->device_name, dinfo->device_vendor,
376 dinfo->device_version,
377 gpu_detect_res_str[dinfo->stat]);
381 //! This function is documented in the header file
382 gmx_bool init_gpu(const gmx::MDLogger & /*mdlog*/,
383 int mygpu,
384 char *result_str,
385 const gmx_gpu_info_t gmx_unused *gpu_info,
386 const gmx_gpu_opt_t *gpu_opt
389 assert(result_str);
391 result_str[0] = 0;
393 if (mygpu < 0 || mygpu >= gpu_opt->n_dev_use)
395 char sbuf[STRLEN];
396 sprintf(sbuf, "Trying to initialize an non-existent GPU: "
397 "there are %d %s-selected GPU(s), but #%d was requested.",
398 gpu_opt->n_dev_use, gpu_opt->bUserSet ? "user" : "auto", mygpu);
399 gmx_incons(sbuf);
402 // If the device is NVIDIA, for safety reasons we disable the JIT
403 // caching as this is known to be broken at least until driver 364.19;
404 // the cache does not always get regenerated when the source code changes,
405 // e.g. if the path to the kernel sources remains the same
407 if (gpu_info->gpu_dev[mygpu].vendor_e == OCL_VENDOR_NVIDIA)
409 // Ignore return values, failing to set the variable does not mean
410 // that something will go wrong later.
411 #ifdef _MSC_VER
412 _putenv("CUDA_CACHE_DISABLE=1");
413 #else
414 // Don't override, maybe a dev is testing.
415 setenv("CUDA_CACHE_DISABLE", "1", 0);
416 #endif
419 return TRUE;
422 //! This function is documented in the header file
423 int get_gpu_device_id(const gmx_gpu_info_t *,
424 const gmx_gpu_opt_t *gpu_opt,
425 int idx)
427 assert(gpu_opt);
428 assert(idx >= 0 && idx < gpu_opt->n_dev_use);
430 return gpu_opt->dev_use[idx];
433 //! This function is documented in the header file
434 char* get_ocl_gpu_device_name(const gmx_gpu_info_t *gpu_info,
435 const gmx_gpu_opt_t *gpu_opt,
436 int idx)
438 assert(gpu_info);
439 assert(gpu_opt);
440 assert(idx >= 0 && idx < gpu_opt->n_dev_use);
442 return gpu_info->gpu_dev[gpu_opt->dev_use[idx]].device_name;
445 //! This function is documented in the header file
446 size_t sizeof_gpu_dev_info(void)
448 return sizeof(gmx_device_info_t);
451 /*! \brief Prints the name of a kernel function pointer.
453 * \param[in] kernel OpenCL kernel
454 * \returns CL_SUCCESS if the operation was successful, an OpenCL error otherwise.
456 cl_int dbg_ocl_kernel_name(const cl_kernel kernel)
458 cl_int cl_error;
459 char kernel_name[256];
460 cl_error = clGetKernelInfo(kernel, CL_KERNEL_FUNCTION_NAME,
461 sizeof(kernel_name), &kernel_name, NULL);
462 if (cl_error)
464 printf("No kernel found!\n");
466 else
468 printf("%s\n", kernel_name);
470 return cl_error;
473 /*! \brief Prints the name of a kernel function pointer.
475 * \param[in] kernel OpenCL kernel
476 * \returns CL_SUCCESS if the operation was successful, an OpenCL error otherwise.
478 cl_int dbg_ocl_kernel_name_address(void* kernel)
480 cl_int cl_error;
481 char kernel_name[256];
482 cl_error = clGetKernelInfo((cl_kernel)kernel, CL_KERNEL_FUNCTION_NAME,
483 sizeof(kernel_name), &kernel_name, NULL);
484 if (cl_error)
486 printf("No kernel found!\n");
488 else
490 printf("%s\n", kernel_name);
492 return cl_error;
495 void gpu_set_host_malloc_and_free(bool bUseGpuKernels,
496 gmx_host_alloc_t **nb_alloc,
497 gmx_host_free_t **nb_free)
499 if (bUseGpuKernels)
501 *nb_alloc = &ocl_pmalloc;
502 *nb_free = &ocl_pfree;
504 else
506 *nb_alloc = NULL;
507 *nb_free = NULL;