Use large try/catch in main function for clarity

This commit is contained in:
Tom Deakin 2015-07-28 13:37:15 +01:00
parent 3c248195ea
commit a9a087622d

View File

@ -34,6 +34,7 @@ void die(std::string msg, cl::Error& e)
exit(e.err()); exit(e.err());
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -43,19 +44,13 @@ int main(int argc, char *argv[])
<< "Version: " << VERSION_STRING << std::endl << "Version: " << VERSION_STRING << std::endl
<< "Implementation: OpenCL" << std::endl; << "Implementation: OpenCL" << std::endl;
parseArguments(argc, argv); std::string status;
try try
{ {
parseArguments(argc, argv);
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";
@ -100,20 +95,12 @@ 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
@ -122,50 +109,18 @@ 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;
try status = "Creating context";
{ cl::Context context(device);
context = cl::Context(device);
}
catch (cl::Error& e)
{
die("Creating context", e);
}
try status = "Creating queue";
{ cl::CommandQueue queue(context);
queue = cl::CommandQueue(context);
}
catch (cl::Error &e)
{
die("Creating queue", e);
}
try status = "Creating program";
{ cl::Program program(context, kernels);
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);
@ -186,12 +141,16 @@ int main(int argc, char *argv[])
<< "Build error:" << "Build error:"
<< buildlog << buildlog
<< std::endl; << std::endl;
exit(e.err()); throw e;
} }
status = "Making kernel copy";
cl::make_kernel<cl::Buffer&, cl::Buffer&> copy(program, "copy"); cl::make_kernel<cl::Buffer&, cl::Buffer&> copy(program, "copy");
status = "Making kernel mul";
cl::make_kernel<cl::Buffer&, cl::Buffer&> mul(program, "mul"); cl::make_kernel<cl::Buffer&, cl::Buffer&> mul(program, "mul");
status = "Making kernel add";
cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&> add(program, "add"); cl::make_kernel<cl::Buffer&, cl::Buffer&, cl::Buffer&> add(program, "add");
status = "Making kernel triad";
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
@ -217,39 +176,21 @@ int main(int argc, char *argv[])
} }
// Create device buffers // Create device buffers
cl::Buffer d_a, d_b, d_c; status = "Creating buffers";
try cl::Buffer d_a(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
{ cl::Buffer d_b(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
d_a = cl::Buffer(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE); cl::Buffer d_c(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 status = "Copying buffers";
{
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;
@ -260,75 +201,51 @@ int main(int argc, char *argv[])
// Main loop // Main loop
for (unsigned int k = 0; k < NTIMES; k++) for (unsigned int k = 0; k < NTIMES; k++)
{ {
status = "Executing copy";
std::vector<double> times; std::vector<double> times;
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
try
{
copy( copy(
cl::EnqueueArgs( cl::EnqueueArgs(
queue, queue,
cl::NDRange(ARRAY_SIZE)), cl::NDRange(ARRAY_SIZE)),
d_a, d_c); d_a, d_c);
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Executing copy", e);
}
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count()); times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing mul";
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
try
{
mul( mul(
cl::EnqueueArgs( cl::EnqueueArgs(
queue, queue,
cl::NDRange(ARRAY_SIZE)), cl::NDRange(ARRAY_SIZE)),
d_b, d_c); d_b, d_c);
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Executing mul", e);
}
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count()); times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing add";
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
try
{
add( add(
cl::EnqueueArgs( cl::EnqueueArgs(
queue, queue,
cl::NDRange(ARRAY_SIZE)), cl::NDRange(ARRAY_SIZE)),
d_a, d_b, d_c); d_a, d_b, d_c);
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Executing add", e);
}
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count()); times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing triad";
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
try
{
triad( triad(
cl::EnqueueArgs( cl::EnqueueArgs(
queue, queue,
cl::NDRange(ARRAY_SIZE)), cl::NDRange(ARRAY_SIZE)),
d_a, d_b, d_c); d_a, d_b, d_c);
queue.finish(); queue.finish();
}
catch (cl::Error &e)
{
die("Executing triad", e);
}
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count()); times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
@ -337,17 +254,12 @@ int main(int argc, char *argv[])
} }
// Check solutions // Check solutions
try status = "Copying back buffers";
{
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)
{ {
@ -402,8 +314,20 @@ int main(int argc, char *argv[])
} }
} }
catch (cl::Error &e)
{
die(status, e);
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
exit(EXIT_FAILURE);
}
}
unsigned getDeviceList(std::vector<cl::Device>& devices) unsigned getDeviceList(std::vector<cl::Device>& devices)