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());
}
int main(int argc, char *argv[])
{
@ -43,19 +44,13 @@ int main(int argc, char *argv[])
<< "Version: " << VERSION_STRING << std::endl
<< "Implementation: OpenCL" << std::endl;
parseArguments(argc, argv);
std::string status;
try
{
parseArguments(argc, argv);
if (NTIMES < 2) throw badntimes();
}
catch (std::exception& e)
{
std::cerr
<< "Error: "
<< e.what()
<< std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Precision: ";
if (useFloat) std::cout << "float";
@ -100,20 +95,12 @@ int main(int argc, char *argv[])
// Open the Kernel source
std::string kernels;
try
{
std::ifstream in("ocl-stream-kernels.cl");
if (!in.is_open()) throw badfile();
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
@ -122,50 +109,18 @@ int main(int argc, char *argv[])
getDeviceList(devices);
// Check device index is in range
try
{
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::Context context;
cl::CommandQueue queue;
cl::Program program;
try
{
context = cl::Context(device);
}
catch (cl::Error& e)
{
die("Creating context", e);
}
status = "Creating context";
cl::Context context(device);
try
{
queue = cl::CommandQueue(context);
}
catch (cl::Error &e)
{
die("Creating queue", e);
}
status = "Creating queue";
cl::CommandQueue queue(context);
try
{
program = cl::Program(context, kernels);
}
catch (cl::Error &e)
{
die("Creating program", e);
}
status = "Creating program";
cl::Program program(context, kernels);
// Print out device name
std::string name = getDeviceName(device);
@ -186,12 +141,16 @@ int main(int argc, char *argv[])
<< "Build error:"
<< buildlog
<< std::endl;
exit(e.err());
throw e;
}
status = "Making kernel 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");
status = "Making kernel 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");
// Create host vectors
@ -217,39 +176,21 @@ int main(int argc, char *argv[])
}
// Create device buffers
cl::Buffer d_a, d_b, d_c;
try
{
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);
}
status = "Creating buffers";
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);
cl::Buffer d_c(context, CL_MEM_READ_WRITE, DATATYPE_SIZE * ARRAY_SIZE);
// 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_b, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_b);
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
try
{
queue.finish();
}
catch (cl::Error &e)
{
die("Queue finish", e);
}
// List of times
std::vector< std::vector<double> > timings;
@ -260,75 +201,51 @@ int main(int argc, char *argv[])
// Main loop
for (unsigned int k = 0; k < NTIMES; k++)
{
status = "Executing copy";
std::vector<double> times;
t1 = std::chrono::high_resolution_clock::now();
try
{
copy(
cl::EnqueueArgs(
queue,
cl::NDRange(ARRAY_SIZE)),
d_a, d_c);
queue.finish();
}
catch (cl::Error &e)
{
die("Executing copy", e);
}
t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing mul";
t1 = std::chrono::high_resolution_clock::now();
try
{
mul(
cl::EnqueueArgs(
queue,
cl::NDRange(ARRAY_SIZE)),
d_b, d_c);
queue.finish();
}
catch (cl::Error &e)
{
die("Executing mul", e);
}
t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing add";
t1 = std::chrono::high_resolution_clock::now();
try
{
add(
cl::EnqueueArgs(
queue,
cl::NDRange(ARRAY_SIZE)),
d_a, d_b, d_c);
queue.finish();
}
catch (cl::Error &e)
{
die("Executing add", e);
}
t2 = std::chrono::high_resolution_clock::now();
times.push_back(std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count());
status = "Executing triad";
t1 = std::chrono::high_resolution_clock::now();
try
{
triad(
cl::EnqueueArgs(
queue,
cl::NDRange(ARRAY_SIZE)),
d_a, d_b, d_c);
queue.finish();
}
catch (cl::Error &e)
{
die("Executing triad", e);
}
t2 = std::chrono::high_resolution_clock::now();
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
try
{
status = "Copying back buffers";
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_c, CL_FALSE, 0, ARRAY_SIZE*DATATYPE_SIZE, h_c);
queue.finish();
}
catch (cl::Error &e)
{
die("Copying back buffers", e);
}
if (useFloat)
{
@ -401,11 +313,23 @@ int main(int argc, char *argv[])
<< std::endl;
}
}
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)
{
// Get list of platforms