Remove large try/catch block in OpenCL - wrap around each OCL call

This commit is contained in:
Tom Deakin 2015-07-28 12:03:44 +01:00
parent e605b056a6
commit 2b00245e63

View File

@ -38,6 +38,18 @@ struct badntimes : public std::exception
}; };
// Print error and exit
void die(std::string msg, cl::Error& e)
{
std::cerr
<< "Error: "
<< msg
<< ": " << e.what()
<< "(" << e.err() << ")"
<< std::endl;
exit(e.err());
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -48,8 +60,18 @@ int main(int argc, char *argv[])
<< "Implementation: OpenCL" << std::endl; << "Implementation: OpenCL" << std::endl;
parseArguments(argc, argv); parseArguments(argc, argv);
try
{
if (NTIMES < 2) throw badntimes(); if (NTIMES < 2) throw badntimes();
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Precision: "; std::cout << "Precision: ";
if (useFloat) std::cout << "float"; if (useFloat) std::cout << "float";
@ -94,12 +116,20 @@ int main(int argc, char *argv[])
// Open the Kernel source // Open the Kernel source
std::string kernels; std::string kernels;
try
{ {
std::ifstream in("ocl-stream-kernels.cl"); std::ifstream in("ocl-stream-kernels.cl");
if (!in.is_open()) throw badfile(); if (!in.is_open()) throw badfile();
kernels = std::string (std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>())); kernels = std::string (std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
} }
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
exit(EXIT_FAILURE);
}
// Setup OpenCL // Setup OpenCL
@ -108,13 +138,50 @@ int main(int argc, char *argv[])
getDeviceList(devices); getDeviceList(devices);
// Check device index is in range // Check device index is in range
try
{
if (deviceIndex >= devices.size()) throw invaliddevice(); if (deviceIndex >= devices.size()) throw invaliddevice();
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
exit(EXIT_FAILURE);
}
cl::Device device = devices[deviceIndex]; cl::Device device = devices[deviceIndex];
cl::Context context;
cl::CommandQueue queue;
cl::Program program;
cl::Context context(device); try
cl::CommandQueue queue(context); {
cl::Program program(context, kernels); context = cl::Context(device);
}
catch (cl::Error& e)
{
die("Creating context", e);
}
try
{
queue = cl::CommandQueue(context);
}
catch (cl::Error &e)
{
die("Creating queue", e);
}
try
{
program = cl::Program(context, kernels);
}
catch (cl::Error &e)
{
die("Creating program", e);
}
// Print out device name // Print out device name
std::string name = getDeviceName(device); std::string name = getDeviceName(device);
@ -135,13 +202,13 @@ int main(int argc, char *argv[])
<< "Build error:" << "Build error:"
<< buildlog << buildlog
<< std::endl; << std::endl;
throw e; exit(e.err());
} }
cl::make_kernel<cl::Buffer, cl::Buffer> copy(program, "copy"); cl::make_kernel<cl::Buffer&, cl::Buffer&> copy(program, "copy");
cl::make_kernel<cl::Buffer, cl::Buffer> mul(program, "mul"); cl::make_kernel<cl::Buffer&, cl::Buffer&> mul(program, "mul");
cl::make_kernel<cl::Buffer, cl::Buffer, cl::Buffer> add(program, "add"); cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&> add(program, "add");
cl::make_kernel<cl::Buffer, cl::Buffer, cl::Buffer> triad(program, "triad"); cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&> triad(program, "triad");
// Create host vectors // Create host vectors
void *h_a = malloc(ARRAY_SIZE * DATATYPE_SIZE); void *h_a = malloc(ARRAY_SIZE * DATATYPE_SIZE);
@ -166,17 +233,39 @@ int main(int argc, char *argv[])
} }
// Create device buffers // Create device buffers
cl::Buffer d_a(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE); cl::Buffer d_a, d_b, d_c;
cl::Buffer d_b(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE); try
cl::Buffer d_c(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE); {
d_a = cl::Buffer(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
d_b = cl::Buffer(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
d_c = cl::Buffer(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
}
catch (cl::Error &e)
{
die("Creating buffers", e);
}
// Copy host memory to device // Copy host memory to device
try
{
queue.enqueueWriteBuffer(d_a, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_a); queue.enqueueWriteBuffer(d_a, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_a);
queue.enqueueWriteBuffer(d_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b); queue.enqueueWriteBuffer(d_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b);
queue.enqueueWriteBuffer(d_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c); queue.enqueueWriteBuffer(d_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c);
}
catch (cl::Error &e)
{
die("Copying buffers to device", e);
}
// Make sure the copies are finished // Make sure the copies are finished
try
{
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Queue finish", e);
}
// List of times // List of times
std::vector< std::vector<double> > timings; std::vector< std::vector<double> > timings;
@ -236,10 +325,17 @@ int main(int argc, char *argv[])
} }
// Check solutions // Check solutions
try
{
queue.enqueueReadBuffer(d_a, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_a); queue.enqueueReadBuffer(d_a, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_a);
queue.enqueueReadBuffer(d_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b); queue.enqueueReadBuffer(d_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b);
queue.enqueueReadBuffer(d_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c); queue.enqueueReadBuffer(d_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c);
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Copying back buffers", e);
}
if (useFloat) if (useFloat)
{ {
@ -260,7 +356,6 @@ int main(int argc, char *argv[])
double min[4] = {DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX}; double min[4] = {DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX};
double max[4] = {0.0, 0.0, 0.0, 0.0}; double max[4] = {0.0, 0.0, 0.0, 0.0};
double avg[4] = {0.0, 0.0, 0.0, 0.0}; double avg[4] = {0.0, 0.0, 0.0, 0.0};
// Ignore first result // Ignore first result
for (unsigned int i = 1; i < NTIMES; i++) for (unsigned int i = 1; i < NTIMES; i++)
{ {
@ -271,7 +366,6 @@ int main(int argc, char *argv[])
max[j] = std::max(max[j], timings[i][j]); max[j] = std::max(max[j], timings[i][j]);
} }
} }
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
avg[j] /= (double)(NTIMES-1); avg[j] /= (double)(NTIMES-1);
@ -284,7 +378,6 @@ int main(int argc, char *argv[])
<< std::left << std::setw(12) << "Max" << std::left << std::setw(12) << "Max"
<< std::left << std::setw(12) << "Average" << std::left << std::setw(12) << "Average"
<< std::endl; << std::endl;
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
{ {
std::cout std::cout
@ -295,30 +388,52 @@ int main(int argc, char *argv[])
<< std::left << std::setw(12) << std::setprecision(5) << avg[j] << std::left << std::setw(12) << std::setprecision(5) << avg[j]
<< std::endl; << std::endl;
} }
} }
unsigned getDeviceList(std::vector<cl::Device>& devices) unsigned getDeviceList(std::vector<cl::Device>& devices)
{ {
// Get list of platforms // Get list of platforms
std::vector<cl::Platform> platforms; std::vector<cl::Platform> platforms;
try
{
cl::Platform::get(&platforms); cl::Platform::get(&platforms);
}
catch (cl::Error &e)
{
die("Getting platforms", e);
}
// Enumerate devices // Enumerate devices
for (unsigned int i = 0; i < platforms.size(); i++) for (unsigned int i = 0; i < platforms.size(); i++)
{ {
std::vector<cl::Device> plat_devices; std::vector<cl::Device> plat_devices;
try
{
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &plat_devices); platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &plat_devices);
}
catch (cl::Error &e)
{
die("Getting devices", e);
}
devices.insert(devices.end(), plat_devices.begin(), plat_devices.end()); devices.insert(devices.end(), plat_devices.begin(), plat_devices.end());
} }
return devices.size(); return devices.size();
} }
std::string getDeviceName(const cl::Device& device) std::string getDeviceName(const cl::Device& device)
{ {
std::string name; std::string name;
cl_device_info info = CL_DEVICE_NAME; cl_device_info info = CL_DEVICE_NAME;
try
{
// Special case for AMD // Special case for AMD
#ifdef CL_DEVICE_BOARD_NAME_AMD #ifdef CL_DEVICE_BOARD_NAME_AMD
device.getInfo(CL_DEVICE_VENDOR, &name); device.getInfo(CL_DEVICE_VENDOR, &name);
@ -327,6 +442,12 @@ std::string getDeviceName(const cl::Device& device)
#endif #endif
device.getInfo(info, &name); device.getInfo(info, &name);
}
catch (cl::Error &e)
{
die("Getting device name", e);
}
return name; return name;
} }