2011-08-31 15:54:41 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2011 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// BOINC API for OpenCL
|
2011-09-01 00:06:34 +00:00
|
|
|
//
|
|
|
|
// To get the cl_device_id and cl_platform_id for the OpenCL GPU
|
2011-11-03 08:28:55 +00:00
|
|
|
// assigned to your application call this function:
|
2012-12-04 11:57:18 +00:00
|
|
|
// int boinc_get_opencl_ids(int argc, char** argv, char *type, cl_device_id* device, cl_platform_id* platform);
|
2011-09-01 00:06:34 +00:00
|
|
|
//
|
2011-11-03 08:28:55 +00:00
|
|
|
// To use this function, link your application with libboinc_opencl.a
|
2011-09-01 00:06:34 +00:00
|
|
|
//
|
|
|
|
|
2011-08-31 15:54:41 +00:00
|
|
|
#ifdef _WIN32
|
2020-06-04 08:24:49 +00:00
|
|
|
#include "boinc_win.h"
|
2011-08-31 15:54:41 +00:00
|
|
|
#include "win_util.h"
|
2013-01-11 15:33:05 +00:00
|
|
|
#else
|
2011-09-01 08:31:55 +00:00
|
|
|
#include <string>
|
2013-01-11 15:33:05 +00:00
|
|
|
#endif
|
2011-08-31 15:54:41 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "coproc.h"
|
2012-05-09 20:03:15 +00:00
|
|
|
#include "str_replace.h"
|
|
|
|
#include "boinc_api.h"
|
|
|
|
|
2011-08-31 15:54:41 +00:00
|
|
|
#include "boinc_opencl.h"
|
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
static int compareBOINCVersionTo(int toMajor, int toMinor, int toRelease);
|
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
// A few complicating factors:
|
|
|
|
// Windows & Linux have a separate OpenCL platform for each vendor
|
|
|
|
// (NVIDIA, AMD, Intel).
|
|
|
|
// Mac has only one platform (Apple) which reports GPUs from all vendors.
|
|
|
|
//
|
|
|
|
// In all systems, opencl_device_indexes start at 0 for each platform
|
|
|
|
// and device_nums start at 0 for each vendor.
|
|
|
|
//
|
2012-12-04 12:13:21 +00:00
|
|
|
// On Macs, OpenCL does not always recognize all GPU models detected by
|
|
|
|
// CUDA, so a device_num may not correspond to its opencl_device_index
|
|
|
|
// even if all GPUs are from NVIDIA.
|
2012-12-04 11:57:18 +00:00
|
|
|
//
|
2013-01-11 01:16:32 +00:00
|
|
|
int get_vendor(cl_device_id device_id, char* vendor, int len) {
|
2012-12-04 11:57:18 +00:00
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
retval = clGetDeviceInfo(
|
2013-01-11 01:16:32 +00:00
|
|
|
device_id, CL_DEVICE_VENDOR, len, vendor, NULL
|
2012-12-04 11:57:18 +00:00
|
|
|
);
|
2013-01-22 08:44:24 +00:00
|
|
|
if (retval != CL_SUCCESS) return retval;
|
|
|
|
if (!strlen(vendor)) return CL_INVALID_DEVICE_TYPE;
|
|
|
|
|
2013-01-11 01:16:32 +00:00
|
|
|
if ((strstr(vendor, "AMD")) ||
|
2012-12-04 11:57:18 +00:00
|
|
|
(strstr(vendor, "Advanced Micro Devices, Inc."))
|
|
|
|
) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(vendor, GPU_TYPE_ATI, len); // "ATI"
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasestr(vendor, "nvidia")) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(vendor, GPU_TYPE_NVIDIA, len); // "NVIDIA"
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 09:44:57 +00:00
|
|
|
if (strcasestr(vendor, "intel")) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(vendor, GPU_TYPE_INTEL, len); // "intel_gpu"
|
2012-12-05 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-24 17:53:09 +00:00
|
|
|
// returns an OpenCL error num or zero
|
|
|
|
//
|
2011-08-31 16:12:14 +00:00
|
|
|
int boinc_get_opencl_ids_aux(
|
2012-12-04 19:17:26 +00:00
|
|
|
char* type, int opencl_device_index, int device_num,
|
|
|
|
cl_device_id* device, cl_platform_id* platform
|
2011-08-31 16:12:14 +00:00
|
|
|
) {
|
2011-08-31 15:54:41 +00:00
|
|
|
cl_platform_id platforms[MAX_OPENCL_PLATFORMS];
|
|
|
|
cl_uint num_platforms, platform_index, num_devices;
|
|
|
|
cl_device_id devices[MAX_COPROC_INSTANCES];
|
2012-12-04 11:57:18 +00:00
|
|
|
char vendor[256]; // Device vendor (NVIDIA, ATI, AMD, etc.)
|
2011-08-31 16:12:14 +00:00
|
|
|
int retval = 0;
|
2012-12-04 11:57:18 +00:00
|
|
|
cl_device_id device_id;
|
|
|
|
int device_num_for_type = -1;
|
|
|
|
int device_index;
|
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
if ((!type) || (!strlen(type))) return CL_INVALID_DEVICE_TYPE;
|
2011-08-31 15:54:41 +00:00
|
|
|
|
2011-08-31 17:36:16 +00:00
|
|
|
retval = clGetPlatformIDs(MAX_OPENCL_PLATFORMS, platforms, &num_platforms);
|
|
|
|
if (num_platforms == 0) return CL_DEVICE_NOT_FOUND;
|
2011-11-09 21:24:47 +00:00
|
|
|
if (retval != CL_SUCCESS) return retval;
|
2011-08-31 15:54:41 +00:00
|
|
|
|
|
|
|
for (platform_index=0; platform_index<num_platforms; ++platform_index) {
|
2011-08-31 17:36:16 +00:00
|
|
|
retval = clGetDeviceIDs(
|
2011-10-24 17:53:09 +00:00
|
|
|
platforms[platform_index], CL_DEVICE_TYPE_GPU,
|
|
|
|
MAX_COPROC_INSTANCES, devices, &num_devices
|
2011-08-31 15:54:41 +00:00
|
|
|
);
|
2011-11-09 21:24:47 +00:00
|
|
|
if (retval != CL_SUCCESS) continue;
|
2011-08-31 15:54:41 +00:00
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
// Use gpu_opencl_dev_index if available
|
|
|
|
if (opencl_device_index >= 0) {
|
2014-09-22 11:57:23 +00:00
|
|
|
if (opencl_device_index < (int)num_devices) {
|
|
|
|
device_id = devices[opencl_device_index];
|
|
|
|
retval = get_vendor(device_id, vendor, sizeof(vendor));
|
|
|
|
if (retval != CL_SUCCESS) continue;
|
2012-12-04 11:57:18 +00:00
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
if (!strcmp(vendor, type)) {
|
|
|
|
*device = device_id;
|
|
|
|
*platform = platforms[platform_index];
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
2014-09-22 11:57:23 +00:00
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
continue;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
// Older versions of init_data.xml don't have the gpu_opencl_dev_index
|
|
|
|
// field so use the value of gpu_device_num.
|
2012-12-04 11:57:18 +00:00
|
|
|
// NOTE: This may return the wrong device on older versions of BOINC if
|
2014-05-01 09:16:28 +00:00
|
|
|
// OpenCL does not recognize all GPU models detected by CUDA or CAL
|
2012-12-04 11:57:18 +00:00
|
|
|
for (device_index=0; device_index<(int)num_devices; ++device_index) {
|
|
|
|
device_id = devices[device_index];
|
|
|
|
|
2013-01-11 01:16:32 +00:00
|
|
|
retval = get_vendor(device_id, vendor, sizeof(vendor));
|
2012-12-04 11:57:18 +00:00
|
|
|
if (retval != CL_SUCCESS) continue;
|
|
|
|
|
|
|
|
if (!strcmp(vendor, type)) {
|
|
|
|
if(++device_num_for_type == device_num) {
|
|
|
|
*device = device_id;
|
|
|
|
*platform = platforms[platform_index];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
fprintf(stderr, "GPU not found: type=%s, opencl_device_index=%d, device_num=%d\n",
|
|
|
|
type, opencl_device_index, device_num);
|
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
return CL_DEVICE_NOT_FOUND;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
|
|
|
|
// This version is compatible with older clients.
|
|
|
|
// Usage:
|
|
|
|
// Pass the argc and argv received from the BOINC client
|
2012-12-05 09:44:57 +00:00
|
|
|
// type: may be PROC_TYPE_NVIDIA_GPU, PROC_TYPE_AMD_GPU or PROC_TYPE_INTEL_GPU
|
|
|
|
// (it may also be 0, but then it will fail on older clients.)
|
|
|
|
//
|
2014-09-22 11:57:23 +00:00
|
|
|
// The argc, argv and type arguments are ignored for 6.13.3 or later clients.
|
2012-12-04 11:57:18 +00:00
|
|
|
//
|
2011-10-24 17:53:09 +00:00
|
|
|
// returns
|
|
|
|
// - 0 if success
|
|
|
|
// - ERR_FOPEN if init_data.xml missing
|
|
|
|
// - ERR_XML_PARSE if can't parse init_data.xml
|
2012-12-05 09:44:57 +00:00
|
|
|
// - CL_INVALID_DEVICE_TYPE if unable to get gpu_type information
|
2014-09-22 11:57:23 +00:00
|
|
|
// - CL_INVALID_DEVICE if unable to get opencl_device_index or gpu device_num
|
|
|
|
// - CL_DEVICE_NOT_FOUND if the requested device was not found
|
2011-10-24 17:53:09 +00:00
|
|
|
// - an OpenCL error number if OpenCL error
|
|
|
|
//
|
2012-12-04 19:17:26 +00:00
|
|
|
int boinc_get_opencl_ids(
|
2012-12-05 09:44:57 +00:00
|
|
|
int argc, char** argv, int type,
|
2012-12-04 19:17:26 +00:00
|
|
|
cl_device_id* device, cl_platform_id* platform
|
|
|
|
){
|
2011-10-24 17:53:09 +00:00
|
|
|
int retval;
|
2011-09-12 03:48:16 +00:00
|
|
|
APP_INIT_DATA aid;
|
2012-12-05 09:44:57 +00:00
|
|
|
char *gpu_type = NULL;
|
2012-12-04 11:57:18 +00:00
|
|
|
int gpu_device_num = -1;
|
|
|
|
int i;
|
2011-09-12 03:48:16 +00:00
|
|
|
|
2011-10-24 17:53:09 +00:00
|
|
|
retval = boinc_parse_init_data_file();
|
|
|
|
if (retval) return retval;
|
2011-09-12 03:48:16 +00:00
|
|
|
boinc_get_init_data(aid);
|
2011-08-31 15:54:41 +00:00
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
if (strlen(aid.gpu_type)) {
|
|
|
|
gpu_type = aid.gpu_type;
|
|
|
|
} else {
|
2012-12-05 09:44:57 +00:00
|
|
|
switch (type) {
|
|
|
|
case PROC_TYPE_NVIDIA_GPU:
|
|
|
|
gpu_type = (char *)GPU_TYPE_NVIDIA;
|
|
|
|
break;
|
|
|
|
case PROC_TYPE_AMD_GPU:
|
|
|
|
gpu_type = (char *)GPU_TYPE_ATI;
|
|
|
|
break;
|
|
|
|
case PROC_TYPE_INTEL_GPU:
|
|
|
|
gpu_type = (char *)GPU_TYPE_INTEL;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((!gpu_type) || !strlen(gpu_type)) {
|
2011-10-24 17:53:09 +00:00
|
|
|
fprintf(stderr, "GPU type not found in %s\n", INIT_DATA_FILE);
|
2012-12-05 09:44:57 +00:00
|
|
|
return CL_INVALID_DEVICE_TYPE;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 11:57:18 +00:00
|
|
|
if (aid.gpu_opencl_dev_index < 0) {
|
2014-09-22 11:57:23 +00:00
|
|
|
if (compareBOINCVersionTo(7,0,12) >= 0) {
|
|
|
|
// gpu_opencl_dev_index was added in BOINC version 7.0.12.
|
|
|
|
// A gpu_opencl_dev_index value of -1 in version 7.0.12 or later
|
|
|
|
// means BOINC client did not assign an OpenCL GPU to this task.
|
|
|
|
fprintf(stderr, "Illegal value for gpu_opencl_dev_index: %d in BOINC Client %d.%d.%d\n",
|
|
|
|
aid.gpu_opencl_dev_index, aid.major_version, aid.minor_version, aid.release);
|
|
|
|
return CL_INVALID_DEVICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Older versions of init_data.xml don't have the gpu_opencl_dev_index
|
|
|
|
// field so use the value of gpu_device_num if available.
|
2012-12-04 11:57:18 +00:00
|
|
|
gpu_device_num = aid.gpu_device_num;
|
|
|
|
if (gpu_device_num < 0) {
|
2014-09-22 11:57:23 +00:00
|
|
|
if (compareBOINCVersionTo(6,13,3) < 0) {
|
|
|
|
// gpu_device_num and gpu_type fields were added in BOINC version 6.13.3.
|
|
|
|
// Very old versions of init_data.xml don't have gpu_device_num field
|
|
|
|
// but instead pass the device number as a command-line argument.
|
|
|
|
for (i=0; i<argc-1; i++) {
|
|
|
|
if ((!strcmp(argv[i], "--device")) || (!strcmp(argv[i], "-device"))) {
|
|
|
|
gpu_device_num = atoi(argv[i+1]);
|
|
|
|
break;
|
|
|
|
}
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 11:57:23 +00:00
|
|
|
|
|
|
|
if (gpu_device_num < 0) {
|
|
|
|
// BOINC client apparently did not assign a GPU to this task.
|
|
|
|
fprintf(stderr, "Illegal value for gpu_device_num: %d in BOINC Client %d.%d.%d\n",
|
|
|
|
aid.gpu_device_num, aid.major_version, aid.minor_version, aid.release);
|
|
|
|
return CL_INVALID_DEVICE;
|
|
|
|
}
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
2014-09-22 11:57:23 +00:00
|
|
|
} // End if (aid.gpu_opencl_dev_index < 0)
|
2012-12-04 11:57:18 +00:00
|
|
|
|
|
|
|
retval = boinc_get_opencl_ids_aux(
|
|
|
|
gpu_type, aid.gpu_opencl_dev_index, gpu_device_num, device, platform
|
|
|
|
);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Deprecated: use the version above instead
|
|
|
|
//
|
|
|
|
// returns
|
|
|
|
// - 0 if success
|
|
|
|
// - ERR_FOPEN if init_data.xml missing
|
|
|
|
// - ERR_XML_PARSE if can't parse init_data.xml
|
2014-09-22 11:57:23 +00:00
|
|
|
// - CL_INVALID_DEVICE_TYPE if unable to get gpu_type information
|
|
|
|
// - CL_INVALID_DEVICE if unable to get opencl_device_index or gpu device_num
|
|
|
|
// - CL_DEVICE_NOT_FOUND if the requested device was not found
|
2012-12-04 11:57:18 +00:00
|
|
|
// - an OpenCL error number if OpenCL error
|
|
|
|
//
|
|
|
|
int boinc_get_opencl_ids(cl_device_id* device, cl_platform_id* platform) {
|
|
|
|
int retval;
|
|
|
|
APP_INIT_DATA aid;
|
|
|
|
|
|
|
|
retval = boinc_parse_init_data_file();
|
|
|
|
if (retval) return retval;
|
|
|
|
boinc_get_init_data(aid);
|
|
|
|
|
|
|
|
if (!strlen(aid.gpu_type)) {
|
|
|
|
fprintf(stderr, "GPU type not found in %s\n", INIT_DATA_FILE);
|
2014-09-22 11:57:23 +00:00
|
|
|
return CL_INVALID_DEVICE_TYPE;
|
2012-12-04 11:57:18 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 11:57:23 +00:00
|
|
|
if (aid.gpu_opencl_dev_index < 0) {
|
|
|
|
if (compareBOINCVersionTo(7,0,12) >= 0) {
|
|
|
|
// gpu_opencl_dev_index was added in BOINC version 7.0.12.
|
|
|
|
// A gpu_opencl_dev_index value of -1 in version 7.0.12 or
|
|
|
|
// later means BOINC did not assign an OpenCL GPU to this task.
|
|
|
|
fprintf(stderr, "Illegal value for gpu_opencl_dev_index: %d in BOINC Client %d.%d.%d\n",
|
|
|
|
aid.gpu_opencl_dev_index, aid.major_version, aid.minor_version, aid.release);
|
|
|
|
return CL_INVALID_DEVICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aid.gpu_device_num < 0) {
|
|
|
|
if (compareBOINCVersionTo(6,13,3) >= 0) {
|
|
|
|
// gpu_device_num and gpu_type fields were added in BOINC version 6.13.3.
|
|
|
|
// A gpu_device_num value of -1 in version 6.13.3 or later means
|
|
|
|
// BOINC did not assign a GPU to this task.
|
|
|
|
fprintf(stderr, "Illegal value for gpu_device_num: %d in BOINC Client %d.%d.%d\n",
|
|
|
|
aid.gpu_device_num, aid.major_version, aid.minor_version, aid.release);
|
|
|
|
return CL_INVALID_DEVICE;
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 15:12:44 +00:00
|
|
|
retval = boinc_get_opencl_ids_aux(
|
2012-12-04 11:57:18 +00:00
|
|
|
aid.gpu_type, aid.gpu_opencl_dev_index, aid.gpu_device_num, device, platform
|
2011-11-08 15:12:44 +00:00
|
|
|
);
|
2011-08-31 15:54:41 +00:00
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
return retval;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
2014-09-22 11:57:23 +00:00
|
|
|
|
|
|
|
static int compareBOINCVersionTo(int toMajor, int toMinor, int toRelease) {
|
|
|
|
APP_INIT_DATA aid;
|
|
|
|
|
|
|
|
boinc_get_init_data(aid);
|
|
|
|
|
|
|
|
if (aid.major_version < toMajor) return -1;
|
|
|
|
if (aid.major_version > toMajor) return 1;
|
|
|
|
if (aid.minor_version < toMinor) return -1;
|
|
|
|
if (aid.minor_version > toMinor) return 1;
|
|
|
|
if (aid.release < toRelease) return -1;
|
|
|
|
if (aid.release > toRelease) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|