This commit is contained in:
Blaise Tine
2019-11-24 00:43:03 -05:00
parent cec065b6fd
commit f04c2e0be8

View File

@@ -7,10 +7,11 @@
#define _CL_HELPER_ #define _CL_HELPER_
#include <CL/cl.h> #include <CL/cl.h>
#include <vector>
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
#include <string> #include <string>
#include <vector>
using std::string; using std::string;
using std::ifstream; using std::ifstream;
@@ -20,8 +21,7 @@ using std::cout;
//#pragma OPENCL EXTENSION cl_nv_compiler_options:enable //#pragma OPENCL EXTENSION cl_nv_compiler_options:enable
#define WORK_DIM 2 // work-items dimensions #define WORK_DIM 2 // work-items dimensions
struct oclHandleStruct struct oclHandleStruct {
{
cl_context context; cl_context context;
cl_device_id *devices; cl_device_id *devices;
cl_command_queue queue; cl_command_queue queue;
@@ -42,25 +42,23 @@ int device_id_inused = 0; //deviced id used (default : 0)
/* /*
* Converts the contents of a file into a string * Converts the contents of a file into a string
*/ */
string FileToString(const string fileName) string FileToString(const string fileName) {
{
ifstream f(fileName.c_str(), ifstream::in | ifstream::binary); ifstream f(fileName.c_str(), ifstream::in | ifstream::binary);
try try {
{
size_t size; size_t size;
char *str; char *str;
string s; string s;
if(f.is_open()) if (f.is_open()) {
{
size_t fileSize; size_t fileSize;
f.seekg(0, ifstream::end); f.seekg(0, ifstream::end);
size = fileSize = f.tellg(); size = fileSize = f.tellg();
f.seekg(0, ifstream::beg); f.seekg(0, ifstream::beg);
str = new char[size + 1]; str = new char[size + 1];
if (!str) throw(string("Could not allocate memory")); if (!str)
throw(string("Could not allocate memory"));
f.read(str, fileSize); f.read(str, fileSize);
f.close(); f.close();
@@ -70,58 +68,45 @@ string FileToString(const string fileName)
delete[] str; delete[] str;
return s; return s;
} }
} } catch (std::string msg) {
catch(std::string msg)
{
cerr << "Exception caught in FileToString(): " << msg << endl; cerr << "Exception caught in FileToString(): " << msg << endl;
if (f.is_open()) if (f.is_open())
f.close(); f.close();
} } catch (...) {
catch(...)
{
cerr << "Exception caught in FileToString()" << endl; cerr << "Exception caught in FileToString()" << endl;
if (f.is_open()) if (f.is_open())
f.close(); f.close();
} }
string errorMsg = "FileToString()::Error: Unable to open file " string errorMsg = "FileToString()::Error: Unable to open file " + fileName;
+ fileName;
throw(errorMsg); throw(errorMsg);
} }
//--------------------------------------- //---------------------------------------
// Read command line parameters // Read command line parameters
// //
void _clCmdParams(int argc, char *argv[]) { void _clCmdParams(int argc, char *argv[]) {
for (int i =0; i < argc; ++i) for (int i = 0; i < argc; ++i) {
{ switch (argv[i][1]) {
switch (argv[i][1])
{
case 'g': //--g stands for size of work group case 'g': //--g stands for size of work group
if (++i < argc) if (++i < argc) {
{
sscanf(argv[i], "%u", &work_group_size); sscanf(argv[i], "%u", &work_group_size);
} } else {
else std::cerr << "Could not read argument after option " << argv[i - 1]
{ << std::endl;
std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
throw; throw;
} }
break; break;
case 'd': //--d stands for device id used in computaion case 'd': //--d stands for device id used in computaion
if (++i < argc) if (++i < argc) {
{
sscanf(argv[i], "%u", &device_id_inused); sscanf(argv[i], "%u", &device_id_inused);
} } else {
else std::cerr << "Could not read argument after option " << argv[i - 1]
{ << std::endl;
std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
throw; throw;
} }
break; break;
default: default:;
;
} }
} }
} }
//--------------------------------------- //---------------------------------------
@@ -129,9 +114,9 @@ void _clCmdParams(int argc, char* argv[]){
//--description: there are 5 steps to initialize all the OpenCL objects needed //--description: there are 5 steps to initialize all the OpenCL objects needed
//--revised on 04/01/2011: get the number of devices and //--revised on 04/01/2011: get the number of devices and
// devices have no relationship with context // devices have no relationship with context
void _clInit() void _clInit() {
{
printf("_clInit()\n"); printf("_clInit()\n");
int DEVICE_ID_INUSED = device_id_inused; int DEVICE_ID_INUSED = device_id_inused;
cl_int resultCL; cl_int resultCL;
@@ -145,26 +130,20 @@ void _clInit()
//----------------------------------------------- //-----------------------------------------------
//--cambine-1: find the available platforms and select one //--cambine-1: find the available platforms and select one
cl_uint numPlatforms; cl_uint numPlatforms = 1;
cl_platform_id targetPlatform = NULL; cl_platform_id targetPlatform = NULL;
resultCL = clGetPlatformIDs(0, NULL, &numPlatforms); cl_platform_id *allPlatforms =
if (resultCL != CL_SUCCESS) (cl_platform_id *)malloc(numPlatforms * sizeof(cl_platform_id));
throw (string("InitCL()::Error: Getting number of platforms (clGetPlatformIDs)"));
//printf("number of platforms:%d\n",numPlatforms); //by cambine
if (!(numPlatforms > 0))
throw (string("InitCL()::Error: No platforms found (clGetPlatformIDs)"));
cl_platform_id* allPlatforms = (cl_platform_id*) malloc(numPlatforms * sizeof(cl_platform_id));
resultCL = clGetPlatformIDs(numPlatforms, allPlatforms, NULL); resultCL = clGetPlatformIDs(numPlatforms, allPlatforms, NULL);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS)
throw(string("InitCL()::Error: Getting platform ids (clGetPlatformIDs)")); throw(string("InitCL()::Error: Getting platform ids (clGetPlatformIDs)"));
/* Select the target platform. Default: first platform */ // Select the target platform. Default: first platform
targetPlatform = allPlatforms[0]; targetPlatform = allPlatforms[0];
for (int i = 0; i < numPlatforms; i++)
/*for (int i = 0; i < numPlatforms; i++)
{ {
char pbuff[128]; char pbuff[128];
resultCL = clGetPlatformInfo( allPlatforms[i], resultCL = clGetPlatformInfo( allPlatforms[i],
@@ -178,11 +157,12 @@ void _clInit()
//printf("vedor is %s\n",pbuff); //printf("vedor is %s\n",pbuff);
} }
free(allPlatforms); free(allPlatforms);*/
//----------------------------------------------- //-----------------------------------------------
//--cambine-2: create an OpenCL context //--cambine-2: create an OpenCL context
cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)targetPlatform, 0 }; /*cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM,
(cl_context_properties)targetPlatform, 0 };
oclHandles.context = clCreateContextFromType(cprops, oclHandles.context = clCreateContextFromType(cprops,
CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_GPU,
NULL, NULL,
@@ -190,37 +170,49 @@ void _clInit()
&resultCL); &resultCL);
if ((resultCL != CL_SUCCESS) || (oclHandles.context == NULL)) if ((resultCL != CL_SUCCESS) || (oclHandles.context == NULL))
throw (string("InitCL()::Error: Creating Context (clCreateContextFromType)")); throw (string("InitCL()::Error: Creating Context
(clCreateContextFromType)"));
//----------------------------------------------- //-----------------------------------------------
//--cambine-3: detect OpenCL devices //--cambine-3: detect OpenCL devices
/* First, get the size of device list */ // First, get the size of device list
oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &deviceListSize); oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, 0,
NULL, &deviceListSize);
if(oclHandles.cl_status!=CL_SUCCESS){ if(oclHandles.cl_status!=CL_SUCCESS){
throw(string("exception in _clInit -> clGetDeviceIDs")); throw(string("exception in _clInit -> clGetDeviceIDs"));
} }
if (deviceListSize == 0) if (deviceListSize == 0)
throw(string("InitCL()::Error: No devices found.")); throw(string("InitCL()::Error: No devices found."));
//std::cout<<"device number:"<<deviceListSize<<std::endl; printf("OK1()\n");
/* Now, allocate the device list */ //std::cout<<"device number:"<<deviceListSize<<std::endl;*/
oclHandles.devices = (cl_device_id *)malloc(deviceListSize * sizeof(cl_device_id));
// Now, allocate the device list
deviceListSize = 1;
oclHandles.devices =
(cl_device_id *)malloc(deviceListSize * sizeof(cl_device_id));
if (oclHandles.devices == 0) if (oclHandles.devices == 0)
throw(string("InitCL()::Error: Could not allocate memory.")); throw(string("InitCL()::Error: Could not allocate memory."));
/* Next, get the device list data */ //* Next, get the device list data
oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, deviceListSize, \ oclHandles.cl_status =
clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_DEFAULT, deviceListSize,
oclHandles.devices, NULL); oclHandles.devices, NULL);
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(string("exception in _clInit -> clGetDeviceIDs-2")); throw(string("exception in _clInit -> clGetDeviceIDs-2"));
} }
oclHandles.context = clCreateContext(NULL, deviceListSize, oclHandles.devices,
NULL, NULL, &resultCL);
if ((resultCL != CL_SUCCESS) || (oclHandles.context == NULL))
throw(string("InitCL()::Error: Creating Context (clCreateContext)"));
//----------------------------------------------- //-----------------------------------------------
//--cambine-4: Create an OpenCL command queue //--cambine-4: Create an OpenCL command queue
oclHandles.queue = clCreateCommandQueue(oclHandles.context, oclHandles.queue = clCreateCommandQueue(
oclHandles.devices[DEVICE_ID_INUSED], oclHandles.context, oclHandles.devices[DEVICE_ID_INUSED], 0, &resultCL);
0, printf("resultCL=%d, queue=0x%x\n", resultCL, oclHandles.queue);
&resultCL);
if ((resultCL != CL_SUCCESS) || (oclHandles.queue == NULL)) if ((resultCL != CL_SUCCESS) || (oclHandles.queue == NULL))
throw(string("InitCL()::Creating Command Queue. (clCreateCommandQueue)")); throw(string("InitCL()::Creating Command Queue. (clCreateCommandQueue)"));
@@ -230,45 +222,41 @@ void _clInit()
const char * source = source_str.c_str(); const char * source = source_str.c_str();
size_t sourceSize[] = { source_str.length() };*/ size_t sourceSize[] = { source_str.length() };*/
oclHandles.program = oclHandles.program = clCreateProgramWithBuiltInKernels(
clCreateProgramWithBuiltInKernels(oclHandles.context, 1, &oclHandles.devices[DEVICE_ID_INUSED], "BFS_1, BFS_2", &resultCL); oclHandles.context, 1, &oclHandles.devices[DEVICE_ID_INUSED],
"BFS_1;BFS_2", &resultCL);
/*oclHandles.program = clCreateProgramWithSource(oclHandles.context, /*oclHandles.program = clCreateProgramWithSource(oclHandles.context,
1, 1,
&source, &source,
sourceSize, sourceSize,
&resultCL);*/ &resultCL);*/
if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL)) if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL))
throw(string("InitCL()::Error: Loading Binary into cl_program. (clCreateProgramWithBinary)")); throw(string("InitCL()::Error: Loading Binary into cl_program. "
"(clCreateProgramWithBinary)"));
// insert debug information // insert debug information
// std::string options= "-cl-nv-verbose"; //Doesn't work on AMD machines // std::string options= "-cl-nv-verbose"; //Doesn't work on AMD machines
// options += " -cl-nv-opt-level=3"; // options += " -cl-nv-opt-level=3";
resultCL = clBuildProgram(oclHandles.program, deviceListSize, oclHandles.devices, NULL, NULL,NULL); resultCL = clBuildProgram(oclHandles.program, deviceListSize,
oclHandles.devices, NULL, NULL, NULL);
if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL)) if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL)) {
{
cerr << "InitCL()::Error: In clBuildProgram" << endl; cerr << "InitCL()::Error: In clBuildProgram" << endl;
size_t length; size_t length;
resultCL = clGetProgramBuildInfo(oclHandles.program, resultCL = clGetProgramBuildInfo(oclHandles.program,
oclHandles.devices[DEVICE_ID_INUSED], oclHandles.devices[DEVICE_ID_INUSED],
CL_PROGRAM_BUILD_LOG, CL_PROGRAM_BUILD_LOG, 0, NULL, &length);
0,
NULL,
&length);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS)
throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)")); throw(string("InitCL()::Error: Getting Program build "
"info(clGetProgramBuildInfo)"));
char *buffer = (char *)malloc(length); char *buffer = (char *)malloc(length);
resultCL = clGetProgramBuildInfo(oclHandles.program, resultCL = clGetProgramBuildInfo(
oclHandles.devices[DEVICE_ID_INUSED], oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED],
CL_PROGRAM_BUILD_LOG, CL_PROGRAM_BUILD_LOG, length, buffer, NULL);
length,
buffer,
NULL);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS)
throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)")); throw(string("InitCL()::Error: Getting Program build "
"info(clGetProgramBuildInfo)"));
cerr << buffer << endl; cerr << buffer << endl;
free(buffer); free(buffer);
@@ -281,7 +269,9 @@ void _clInit()
size_t binary_sizes[deviceListSize]; size_t binary_sizes[deviceListSize];
char *binaries[deviceListSize]; char *binaries[deviceListSize];
// figure out number of devices and the sizes of the binary for each device. // figure out number of devices and the sizes of the binary for each device.
oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*deviceListSize, &binary_sizes, NULL ); oclHandles.cl_status =
clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARY_SIZES,
sizeof(size_t) * deviceListSize, &binary_sizes, NULL);
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-2")); throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-2"));
} }
@@ -290,7 +280,9 @@ void _clInit()
// copy over all of the generated binaries. // copy over all of the generated binaries.
for (int i = 0; i < deviceListSize; i++) for (int i = 0; i < deviceListSize; i++)
binaries[i] = (char *)malloc(sizeof(char) * (binary_sizes[i] + 1)); binaries[i] = (char *)malloc(sizeof(char) * (binary_sizes[i] + 1));
oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARIES, sizeof(char *)*deviceListSize, binaries, NULL ); oclHandles.cl_status =
clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARIES,
sizeof(char *) * deviceListSize, binaries, NULL);
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-3")); throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-3"));
} }
@@ -308,16 +300,14 @@ void _clInit()
free(binaries[i]); free(binaries[i]);
#endif #endif
for (int nKernel = 0; nKernel < total_kernels; nKernel++) for (int nKernel = 0; nKernel < total_kernels; nKernel++) {
{
/* get a kernel object handle for a kernel with the given name */ /* get a kernel object handle for a kernel with the given name */
cl_kernel kernel = clCreateKernel(oclHandles.program, cl_kernel kernel = clCreateKernel(
(kernel_names[nKernel]).c_str(), oclHandles.program, (kernel_names[nKernel]).c_str(), &resultCL);
&resultCL);
if ((resultCL != CL_SUCCESS) || (kernel == NULL)) if ((resultCL != CL_SUCCESS) || (kernel == NULL)) {
{ string errorMsg = "InitCL()::Error: Creating Kernel (clCreateKernel) \"" +
string errorMsg = "InitCL()::Error: Creating Kernel (clCreateKernel) \"" + kernel_names[nKernel] + "\""; kernel_names[nKernel] + "\"";
throw(errorMsg); throw(errorMsg);
} }
@@ -327,15 +317,20 @@ void _clInit()
#ifdef RES_MSG #ifdef RES_MSG
char *build_log; char *build_log;
size_t ret_val_size; size_t ret_val_size;
oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size); oclHandles.cl_status = clGetProgramBuildInfo(
oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED],
CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(string("exceptions in _InitCL -> getting resource information")); throw(string("exceptions in _InitCL -> getting resource information"));
} }
build_log = (char *)malloc(ret_val_size + 1); build_log = (char *)malloc(ret_val_size + 1);
oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL); oclHandles.cl_status = clGetProgramBuildInfo(
oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED],
CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(string("exceptions in _InitCL -> getting resources allocation information-2")); throw(string(
"exceptions in _InitCL -> getting resources allocation information-2"));
} }
build_log[ret_val_size] = '\0'; build_log[ret_val_size] = '\0';
std::cout << "--cambine:" << build_log << std::endl; std::cout << "--cambine:" << build_log << std::endl;
@@ -345,17 +340,13 @@ void _clInit()
//--------------------------------------- //---------------------------------------
// release CL objects // release CL objects
void _clRelease() void _clRelease() {
{
char errorFlag = false; char errorFlag = false;
for (int nKernel = 0; nKernel < oclHandles.kernel.size(); nKernel++) for (int nKernel = 0; nKernel < oclHandles.kernel.size(); nKernel++) {
{ if (oclHandles.kernel[nKernel] != NULL) {
if (oclHandles.kernel[nKernel] != NULL)
{
cl_int resultCL = clReleaseKernel(oclHandles.kernel[nKernel]); cl_int resultCL = clReleaseKernel(oclHandles.kernel[nKernel]);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS) {
{
cerr << "ReleaseCL()::Error: In clReleaseKernel" << endl; cerr << "ReleaseCL()::Error: In clReleaseKernel" << endl;
errorFlag = true; errorFlag = true;
} }
@@ -364,22 +355,18 @@ void _clRelease()
oclHandles.kernel.clear(); oclHandles.kernel.clear();
} }
if (oclHandles.program != NULL) if (oclHandles.program != NULL) {
{
cl_int resultCL = clReleaseProgram(oclHandles.program); cl_int resultCL = clReleaseProgram(oclHandles.program);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS) {
{
cerr << "ReleaseCL()::Error: In clReleaseProgram" << endl; cerr << "ReleaseCL()::Error: In clReleaseProgram" << endl;
errorFlag = true; errorFlag = true;
} }
oclHandles.program = NULL; oclHandles.program = NULL;
} }
if (oclHandles.queue != NULL) if (oclHandles.queue != NULL) {
{
cl_int resultCL = clReleaseCommandQueue(oclHandles.queue); cl_int resultCL = clReleaseCommandQueue(oclHandles.queue);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS) {
{
cerr << "ReleaseCL()::Error: In clReleaseCommandQueue" << endl; cerr << "ReleaseCL()::Error: In clReleaseCommandQueue" << endl;
errorFlag = true; errorFlag = true;
} }
@@ -388,25 +375,25 @@ void _clRelease()
free(oclHandles.devices); free(oclHandles.devices);
if (oclHandles.context != NULL) if (oclHandles.context != NULL) {
{
cl_int resultCL = clReleaseContext(oclHandles.context); cl_int resultCL = clReleaseContext(oclHandles.context);
if (resultCL != CL_SUCCESS) if (resultCL != CL_SUCCESS) {
{
cerr << "ReleaseCL()::Error: In clReleaseContext" << endl; cerr << "ReleaseCL()::Error: In clReleaseContext" << endl;
errorFlag = true; errorFlag = true;
} }
oclHandles.context = NULL; oclHandles.context = NULL;
} }
if (errorFlag) throw(string("ReleaseCL()::Error encountered.")); if (errorFlag)
throw(string("ReleaseCL()::Error encountered."));
} }
//-------------------------------------------------------- //--------------------------------------------------------
//--cambine:create buffer and then copy data from host to device //--cambine:create buffer and then copy data from host to device
cl_mem _clCreateAndCpyMem(int size, void *h_mem_source) throw(string) { cl_mem _clCreateAndCpyMem(int size, void *h_mem_source) throw(string) {
cl_mem d_mem; cl_mem d_mem;
d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, \ d_mem = clCreateBuffer(oclHandles.context,
size, h_mem_source, &oclHandles.cl_status); CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, size,
h_mem_source, &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clCreateAndCpyMem()")); throw(string("excpetion in _clCreateAndCpyMem()"));
@@ -418,7 +405,9 @@ cl_mem _clCreateAndCpyMem(int size, void * h_mem_source) throw(string){
//--date: 17/01/2011 //--date: 17/01/2011
cl_mem _clMallocRW(int size, void *h_mem_ptr) throw(string) { cl_mem _clMallocRW(int size, void *h_mem_ptr) throw(string) {
cl_mem d_mem; cl_mem d_mem;
d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status); d_mem = clCreateBuffer(oclHandles.context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size,
h_mem_ptr, &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clMallocRW")); throw(string("excpetion in _clMallocRW"));
@@ -430,7 +419,9 @@ cl_mem _clMallocRW(int size, void * h_mem_ptr) throw(string){
//--date: 17/01/2011 //--date: 17/01/2011
cl_mem _clMalloc(int size, void *h_mem_ptr) throw(string) { cl_mem _clMalloc(int size, void *h_mem_ptr) throw(string) {
cl_mem d_mem; cl_mem d_mem;
d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status); d_mem = clCreateBuffer(oclHandles.context,
CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size,
h_mem_ptr, &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clMalloc")); throw(string("excpetion in _clMalloc"));
@@ -442,7 +433,8 @@ cl_mem _clMalloc(int size, void * h_mem_ptr) throw(string){
//--cambine: transfer data from host to device //--cambine: transfer data from host to device
//--date: 17/01/2011 //--date: 17/01/2011
void _clMemcpyH2D(cl_mem d_mem, int size, const void *h_mem_ptr) throw(string) { void _clMemcpyH2D(cl_mem d_mem, int size, const void *h_mem_ptr) throw(string) {
oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL); oclHandles.cl_status = clEnqueueWriteBuffer(
oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clMemcpyH2D")); throw(string("excpetion in _clMemcpyH2D"));
@@ -454,22 +446,23 @@ void _clMemcpyH2D(cl_mem d_mem, int size, const void *h_mem_ptr) throw(string){
cl_mem _clCreateAndCpyPinnedMem(int size, float *h_mem_source) throw(string) { cl_mem _clCreateAndCpyPinnedMem(int size, float *h_mem_source) throw(string) {
cl_mem d_mem, d_mem_pinned; cl_mem d_mem, d_mem_pinned;
float *h_mem_pinned = NULL; float *h_mem_pinned = NULL;
d_mem_pinned = clCreateBuffer(oclHandles.context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, \ d_mem_pinned = clCreateBuffer(oclHandles.context,
size, NULL, &oclHandles.cl_status); CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, size,
NULL, &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned")); throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned"));
#endif #endif
//------------ //------------
d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_ONLY, \ d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_ONLY, size, NULL,
size, NULL, &oclHandles.cl_status); &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clCreateAndCpyMem() -> d_mem ")); throw(string("excpetion in _clCreateAndCpyMem() -> d_mem "));
#endif #endif
//---------- //----------
h_mem_pinned = (cl_float *)clEnqueueMapBuffer(oclHandles.queue, d_mem_pinned, CL_TRUE, \ h_mem_pinned = (cl_float *)clEnqueueMapBuffer(
CL_MAP_WRITE, 0, size, 0, NULL, \ oclHandles.queue, d_mem_pinned, CL_TRUE, CL_MAP_WRITE, 0, size, 0, NULL,
NULL, &oclHandles.cl_status); NULL, &oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
@@ -481,9 +474,8 @@ cl_mem _clCreateAndCpyPinnedMem(int size, float* h_mem_source) throw(string){
h_mem_pinned[i] = h_mem_source[i]; h_mem_pinned[i] = h_mem_source[i];
} }
//---------- //----------
oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, \ oclHandles.cl_status = clEnqueueWriteBuffer(
CL_TRUE, 0, size, h_mem_pinned, \ oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_pinned, 0, NULL, NULL);
0, NULL, NULL);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer")); throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer"));
@@ -492,12 +484,12 @@ cl_mem _clCreateAndCpyPinnedMem(int size, float* h_mem_source) throw(string){
return d_mem; return d_mem;
} }
//-------------------------------------------------------- //--------------------------------------------------------
//--cambine:create write only buffer on device //--cambine:create write only buffer on device
cl_mem _clMallocWO(int size) throw(string) { cl_mem _clMallocWO(int size) throw(string) {
cl_mem d_mem; cl_mem d_mem;
d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0, &oclHandles.cl_status); d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0,
&oclHandles.cl_status);
#ifdef ERRMSG #ifdef ERRMSG
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(string("excpetion in _clCreateMem()")); throw(string("excpetion in _clCreateMem()"));
@@ -508,7 +500,8 @@ cl_mem _clMallocWO(int size) throw(string){
//-------------------------------------------------------- //--------------------------------------------------------
// transfer data from device to host // transfer data from device to host
void _clMemcpyD2H(cl_mem d_mem, int size, void *h_mem) throw(string) { void _clMemcpyD2H(cl_mem d_mem, int size, void *h_mem) throw(string) {
oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem, 0,0,0); oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE,
0, size, h_mem, 0, 0, 0);
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clCpyMemD2H -> "; oclHandles.error_str = "excpetion in _clCpyMemD2H -> ";
switch (oclHandles.cl_status) { switch (oclHandles.cl_status) {
@@ -544,9 +537,11 @@ void _clMemcpyD2H(cl_mem d_mem, int size, void * h_mem) throw(string){
//-------------------------------------------------------- //--------------------------------------------------------
// set kernel arguments // set kernel arguments
void _clSetArgs(int kernel_id, int arg_idx, void * d_mem, int size = 0) throw(string){ void _clSetArgs(int kernel_id, int arg_idx, void *d_mem,
int size = 0) throw(string) {
if (!size) { if (!size) {
oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem); oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx,
sizeof(d_mem), &d_mem);
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clSetKernelArg() "; oclHandles.error_str = "excpetion in _clSetKernelArg() ";
switch (oclHandles.cl_status) { switch (oclHandles.cl_status) {
@@ -581,9 +576,9 @@ void _clSetArgs(int kernel_id, int arg_idx, void * d_mem, int size = 0) throw(st
if (oclHandles.cl_status != CL_SUCCESS) if (oclHandles.cl_status != CL_SUCCESS)
throw(oclHandles.error_str); throw(oclHandles.error_str);
#endif #endif
} } else {
else{ oclHandles.cl_status =
oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem); clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clSetKernelArg() "; oclHandles.error_str = "excpetion in _clSetKernelArg() ";
switch (oclHandles.cl_status) { switch (oclHandles.cl_status) {
@@ -637,7 +632,6 @@ void _clFinish() throw(string){
default: default:
oclHandles.error_str += "Unknown reasons"; oclHandles.error_str += "Unknown reasons";
break; break;
} }
if (oclHandles.cl_status != CL_SUCCESS) { if (oclHandles.cl_status != CL_SUCCESS) {
throw(oclHandles.error_str); throw(oclHandles.error_str);
@@ -646,19 +640,22 @@ void _clFinish() throw(string){
} }
//-------------------------------------------------------- //--------------------------------------------------------
//--cambine:enqueue kernel //--cambine:enqueue kernel
void _clInvokeKernel(int kernel_id, int work_items, int work_group_size) throw(string){ void _clInvokeKernel(int kernel_id, int work_items,
int work_group_size) throw(string) {
cl_uint work_dim = WORK_DIM; cl_uint work_dim = WORK_DIM;
cl_event e[1]; cl_event e[1];
if(work_items%work_group_size != 0) //process situations that work_items cannot be divided by work_group_size if (work_items % work_group_size != 0) // process situations that work_items
work_items = work_items + (work_group_size-(work_items%work_group_size)); // cannot be divided by work_group_size
work_items =
work_items + (work_group_size - (work_items % work_group_size));
size_t local_work_size[] = {work_group_size, 1}; size_t local_work_size[] = {work_group_size, 1};
size_t global_work_size[] = {work_items, 1}; size_t global_work_size[] = {work_items, 1};
oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \ oclHandles.cl_status = clEnqueueNDRangeKernel(
oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0,
global_work_size, local_work_size, 0, 0, &(e[0])); global_work_size, local_work_size, 0, 0, &(e[0]));
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clInvokeKernel() -> "; oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
switch(oclHandles.cl_status) switch (oclHandles.cl_status) {
{
case CL_INVALID_PROGRAM_EXECUTABLE: case CL_INVALID_PROGRAM_EXECUTABLE:
oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE"; oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
break; break;
@@ -715,19 +712,21 @@ void _clInvokeKernel(int kernel_id, int work_items, int work_group_size) throw(s
// throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents")); // throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents"));
// #endif // #endif
} }
void _clInvokeKernel2D(int kernel_id, int range_x, int range_y, int group_x, int group_y) throw(string){ void _clInvokeKernel2D(int kernel_id, int range_x, int range_y, int group_x,
int group_y) throw(string) {
cl_uint work_dim = WORK_DIM; cl_uint work_dim = WORK_DIM;
size_t local_work_size[] = {group_x, group_y}; size_t local_work_size[] = {group_x, group_y};
size_t global_work_size[] = {range_x, range_y}; size_t global_work_size[] = {range_x, range_y};
cl_event e[1]; cl_event e[1];
/*if(work_items%work_group_size != 0) //process situations that work_items cannot be divided by work_group_size /*if(work_items%work_group_size != 0) //process situations that work_items
cannot be divided by work_group_size
work_items = work_items + (work_group_size-(work_items%work_group_size));*/ work_items = work_items + (work_group_size-(work_items%work_group_size));*/
oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \ oclHandles.cl_status = clEnqueueNDRangeKernel(
oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0,
global_work_size, local_work_size, 0, 0, &(e[0])); global_work_size, local_work_size, 0, 0, &(e[0]));
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clInvokeKernel() -> "; oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
switch(oclHandles.cl_status) switch (oclHandles.cl_status) {
{
case CL_INVALID_PROGRAM_EXECUTABLE: case CL_INVALID_PROGRAM_EXECUTABLE:
oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE"; oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
break; break;
@@ -796,8 +795,7 @@ void _clFree(cl_mem ob) throw(string){
oclHandles.cl_status = clReleaseMemObject(ob); oclHandles.cl_status = clReleaseMemObject(ob);
#ifdef ERRMSG #ifdef ERRMSG
oclHandles.error_str = "excpetion in _clFree() ->"; oclHandles.error_str = "excpetion in _clFree() ->";
switch(oclHandles.cl_status) switch (oclHandles.cl_status) {
{
case CL_INVALID_MEM_OBJECT: case CL_INVALID_MEM_OBJECT:
oclHandles.error_str += "CL_INVALID_MEM_OBJECT"; oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
break; break;