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
|
|
|
// The BOINC client calls the project application with the arguments:
|
|
|
|
// --gpu_type TYPE --device N
|
|
|
|
// where TYPE is ATI or NVIDIA, and N is the GPU number of that type
|
|
|
|
// For example, for ATI GPU number 0, the arguments will be:
|
|
|
|
// --gpu_type ATI --device 0
|
|
|
|
//
|
|
|
|
// To get the cl_device_id and cl_platform_id for the OpenCL GPU
|
|
|
|
// asigned to your application call this function:
|
|
|
|
// int boinc_get_opencl_ids(int argc, char** argv, cl_device_id*, cl_platform_id*);
|
|
|
|
//
|
|
|
|
// NOTE: You should compile and link this function as part of your
|
|
|
|
// application; it is not included in the standard BOINC libraries.
|
|
|
|
//
|
|
|
|
|
2011-08-31 15:54:41 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "win_util.h"
|
|
|
|
#else
|
|
|
|
#ifdef __APPLE__
|
|
|
|
// Suppress obsolete warning when building for OS 10.3.9
|
|
|
|
#define DLOPEN_NO_WARN
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
#include "config.h"
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2011-09-01 08:31:55 +00:00
|
|
|
#include <string>
|
2011-08-31 15:54:41 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
|
|
|
|
#include "coproc.h"
|
|
|
|
|
|
|
|
#include "boinc_opencl.h"
|
|
|
|
|
2011-09-01 00:06:34 +00:00
|
|
|
#ifndef _WIN32
|
2011-08-31 15:54:41 +00:00
|
|
|
static jmp_buf resume;
|
|
|
|
static void segv_handler(int) {
|
|
|
|
longjmp(resume, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
int boinc_get_opencl_ids_aux(
|
|
|
|
char *type, int device_num, cl_device_id* device, cl_platform_id* platform
|
|
|
|
) {
|
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];
|
2011-08-31 16:12:14 +00:00
|
|
|
char vendor[256]; // Device vendor (NVIDIA, ATI, AMD, etc.)
|
|
|
|
int retval = 0;
|
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;
|
|
|
|
if (retval) 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-08-31 15:54:41 +00:00
|
|
|
platforms[platform_index], CL_DEVICE_TYPE_GPU, MAX_COPROC_INSTANCES, devices, &num_devices
|
|
|
|
);
|
|
|
|
|
|
|
|
if (num_devices > (cl_uint)(device_num + 1)) continue;
|
|
|
|
|
|
|
|
cl_device_id device_id = devices[device_num];
|
|
|
|
|
2011-08-31 17:36:16 +00:00
|
|
|
retval = clGetDeviceInfo(device_id, CL_DEVICE_VENDOR, sizeof(vendor), vendor, NULL);
|
|
|
|
if (retval || strlen(vendor)==0) continue;
|
2011-08-31 15:54:41 +00:00
|
|
|
|
|
|
|
if ((strstr(vendor, "AMD")) ||
|
|
|
|
(strstr(vendor, "Advanced Micro Devices, Inc."))
|
|
|
|
) {
|
|
|
|
strcpy(vendor, GPU_TYPE_ATI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(vendor, type)) {
|
2011-08-31 16:12:14 +00:00
|
|
|
*device = device_id;
|
|
|
|
*platform = platforms[platform_index];
|
2011-08-31 15:54:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:36:16 +00:00
|
|
|
if (device == NULL) return CL_DEVICE_NOT_FOUND;
|
|
|
|
return 0;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
int boinc_get_opencl_ids(
|
|
|
|
int argc, char** argv, cl_device_id* device, cl_platform_id* platform
|
|
|
|
) {
|
2011-08-31 15:54:41 +00:00
|
|
|
char type[256];
|
2011-08-31 16:12:14 +00:00
|
|
|
int device_num, retval=0;
|
2011-08-31 15:54:41 +00:00
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
strcpy(type, "");
|
2011-08-31 15:54:41 +00:00
|
|
|
device_num = -1;
|
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
for (int i=1; i<argc; i++) {
|
2011-08-31 15:54:41 +00:00
|
|
|
if (!strcmp(argv[i], "--gpu_type")) {
|
2011-09-01 08:31:55 +00:00
|
|
|
strcpy(type, argv[++i]);
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
if (!strcmp(argv[i], "--device")) {
|
|
|
|
device_num = atoi(argv[++i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
if (!strlen(type)) {
|
|
|
|
return CL_INVALID_DEVICE_TYPE;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (device_num < 0) {
|
2011-08-31 16:12:14 +00:00
|
|
|
return CL_INVALID_DEVICE;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
try {
|
2011-08-31 16:36:17 +00:00
|
|
|
retval = boinc_get_opencl_ids_aux(type, device_num, device, platform);
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2011-08-31 16:12:14 +00:00
|
|
|
return ERR_SIGNAL_CATCH;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void (*old_sig)(int) = signal(SIGSEGV, segv_handler);
|
|
|
|
if (setjmp(resume)) {
|
2011-08-31 16:12:14 +00:00
|
|
|
return ERR_SIGNAL_CATCH;
|
2011-08-31 15:54:41 +00:00
|
|
|
} else {
|
2011-08-31 16:12:14 +00:00
|
|
|
retval = boinc_get_opencl_ids_aux(type, device_num, device, platform);
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGSEGV, old_sig);
|
|
|
|
#endif
|
|
|
|
|
2011-08-31 16:12:14 +00:00
|
|
|
return retval;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|