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:
|
2011-09-01 00:06:34 +00:00
|
|
|
// int boinc_get_opencl_ids(int argc, char** argv, cl_device_id*, cl_platform_id*);
|
|
|
|
//
|
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
|
|
|
|
#include "win_util.h"
|
|
|
|
#endif
|
2011-09-01 08:31:55 +00:00
|
|
|
#include <string>
|
2011-08-31 15:54:41 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
2011-09-11 22:22:55 +00:00
|
|
|
#include "boinc_api.h"
|
2011-08-31 15:54:41 +00:00
|
|
|
#include "coproc.h"
|
|
|
|
#include "boinc_opencl.h"
|
|
|
|
|
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(
|
|
|
|
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-11-03 03:29:07 +00:00
|
|
|
bool found = false;
|
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-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
|
|
|
);
|
|
|
|
|
|
|
|
if (num_devices > (cl_uint)(device_num + 1)) continue;
|
|
|
|
|
|
|
|
cl_device_id device_id = devices[device_num];
|
|
|
|
|
2011-10-24 17:53:09 +00:00
|
|
|
retval = clGetDeviceInfo(
|
|
|
|
device_id, CL_DEVICE_VENDOR, sizeof(vendor), vendor, NULL
|
|
|
|
);
|
2011-08-31 17:36:16 +00:00
|
|
|
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-11-03 03:29:07 +00:00
|
|
|
found = true;
|
2011-08-31 15:54:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 03:29:07 +00:00
|
|
|
if (!found) return CL_DEVICE_NOT_FOUND;
|
2011-08-31 17:36:16 +00:00
|
|
|
return 0;
|
2011-08-31 15:54:41 +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
|
|
|
|
// - ERR_NOT_FOUND if missing <gpu_type> or <gpu_device_num> fields
|
|
|
|
// - an OpenCL error number if OpenCL error
|
|
|
|
//
|
2011-09-11 22:22:55 +00:00
|
|
|
int boinc_get_opencl_ids(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;
|
|
|
|
|
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
|
|
|
|
2011-09-11 22:22:55 +00:00
|
|
|
if (!strlen(aid.gpu_type)) {
|
2011-10-24 17:53:09 +00:00
|
|
|
fprintf(stderr, "GPU type not found in %s\n", INIT_DATA_FILE);
|
|
|
|
return ERR_NOT_FOUND;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-11 22:22:55 +00:00
|
|
|
if (aid.gpu_device_num < 0) {
|
2011-10-24 17:53:09 +00:00
|
|
|
fprintf(stderr, "GPU device # not found in %s\n", INIT_DATA_FILE);
|
|
|
|
return ERR_NOT_FOUND;
|
2011-08-31 15:54:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 15:12:44 +00:00
|
|
|
retval = boinc_get_opencl_ids_aux(
|
|
|
|
aid.gpu_type, aid.gpu_device_num, device, platform
|
|
|
|
);
|
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
|
|
|
}
|