1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// 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
//
// To get the cl_device_id and cl_platform_id for the OpenCL GPU
// assigned to your application call this function:
// int boinc_get_opencl_ids(int argc, char** argv, char *type, cl_device_id* device, cl_platform_id* platform);
//
// To use this function, link your application with libboinc_opencl.a
//
#ifdef _WIN32
#include "boinc_win.h"
#include "win_util.h"
#else
#include <string>
#endif
#include "error_numbers.h"
#include "coproc.h"
#include "str_replace.h"
#include "boinc_api.h"
#include "boinc_opencl.h"
static int compareBOINCVersionTo(int toMajor, int toMinor, int toRelease);
// 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.
//
// 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.
//
int get_vendor(cl_device_id device_id, char* vendor, int len) {
int retval = 0;
retval = clGetDeviceInfo(
device_id, CL_DEVICE_VENDOR, len, vendor, NULL
);
if (retval != CL_SUCCESS) return retval;
if (!strlen(vendor)) return CL_INVALID_DEVICE_TYPE;
if ((strstr(vendor, "AMD")) ||
(strstr(vendor, "Advanced Micro Devices, Inc."))
) {
strlcpy(vendor, GPU_TYPE_ATI, len); // "ATI"
}
if (strcasestr(vendor, "nvidia")) {
strlcpy(vendor, GPU_TYPE_NVIDIA, len); // "NVIDIA"
}
if (strcasestr(vendor, "intel")) {
strlcpy(vendor, GPU_TYPE_INTEL, len); // "intel_gpu"
}
return 0;
}
// returns an OpenCL error num or zero
//
int boinc_get_opencl_ids_aux(
char* type, int opencl_device_index, int device_num,
cl_device_id* device, cl_platform_id* platform
) {
cl_platform_id platforms[MAX_OPENCL_PLATFORMS];
cl_uint num_platforms, platform_index, num_devices;
cl_device_id devices[MAX_COPROC_INSTANCES];
char vendor[256]; // Device vendor (NVIDIA, ATI, AMD, etc.)
int retval = 0;
cl_device_id device_id;
int device_num_for_type = -1;
int device_index;
if ((!type) || (!strlen(type))) return CL_INVALID_DEVICE_TYPE;
retval = clGetPlatformIDs(MAX_OPENCL_PLATFORMS, platforms, &num_platforms);
if (num_platforms == 0) return CL_DEVICE_NOT_FOUND;
if (retval != CL_SUCCESS) return retval;
for (platform_index=0; platform_index<num_platforms; ++platform_index) {
retval = clGetDeviceIDs(
platforms[platform_index], CL_DEVICE_TYPE_GPU,
MAX_COPROC_INSTANCES, devices, &num_devices
);
if (retval != CL_SUCCESS) continue;
// Use gpu_opencl_dev_index if available
if (opencl_device_index >= 0) {
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;
if (!strcmp(vendor, type)) {
*device = device_id;
*platform = platforms[platform_index];
return 0;
}
}
continue;
}
// Older versions of init_data.xml don't have the gpu_opencl_dev_index
// field so use the value of gpu_device_num.
// NOTE: This may return the wrong device on older versions of BOINC if
// OpenCL does not recognize all GPU models detected by CUDA or CAL
for (device_index=0; device_index<(int)num_devices; ++device_index) {
device_id = devices[device_index];
retval = get_vendor(device_id, vendor, sizeof(vendor));
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;
}
}
}
}
fprintf(stderr, "GPU not found: type=%s, opencl_device_index=%d, device_num=%d\n",
type, opencl_device_index, device_num);
return CL_DEVICE_NOT_FOUND;
}
// This version is compatible with older clients.
// Usage:
// Pass the argc and argv received from the BOINC client
// 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.)
//
// The argc, argv and type arguments are ignored for 6.13.3 or later clients.
//
// returns
// - 0 if success
// - ERR_FOPEN if init_data.xml missing
// - ERR_XML_PARSE if can't parse init_data.xml
// - 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
// - an OpenCL error number if OpenCL error
//
int boinc_get_opencl_ids(
int argc, char** argv, int type,
cl_device_id* device, cl_platform_id* platform
){
int retval;
APP_INIT_DATA aid;
char *gpu_type = NULL;
int gpu_device_num = -1;
int i;
retval = boinc_parse_init_data_file();
if (retval) return retval;
boinc_get_init_data(aid);
if (strlen(aid.gpu_type)) {
gpu_type = aid.gpu_type;
} else {
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;
}
}
if ((!gpu_type) || !strlen(gpu_type)) {
fprintf(stderr, "GPU type not found in %s\n", INIT_DATA_FILE);
return CL_INVALID_DEVICE_TYPE;
}
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 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.
gpu_device_num = aid.gpu_device_num;
if (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.
// 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;
}
}
}
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;
}
}
} // End if (aid.gpu_opencl_dev_index < 0)
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
// - 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
// - 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);
return CL_INVALID_DEVICE_TYPE;
}
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;
}
}
}
retval = boinc_get_opencl_ids_aux(
aid.gpu_type, aid.gpu_opencl_dev_index, aid.gpu_device_num, device, platform
);
return retval;
}
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;
}
|