Throw std::runtime_error(string) rather than define own exception classes just for strings!

This commit is contained in:
Tom Deakin 2015-07-31 17:02:48 +01:00
parent 3bb92747ba
commit 6dcc0ffc5c
3 changed files with 22 additions and 67 deletions

View File

@ -52,54 +52,6 @@ extern bool useFloat;
extern int deviceIndex; extern int deviceIndex;
// Exceptions
struct invaliddevice : public std::exception
{
virtual const char * what () const throw ()
{
return "Chosen device index is invalid";
}
};
struct badntimes : public std::exception
{
virtual const char * what () const throw ()
{
return "Chosen number of times is invalid, must be >= 2";
}
};
struct badarraysize : public std::exception
{
virtual const char * what () const throw ()
{
return "Array size must be >= 1024";
}
};
struct badbuffersize : public std::exception
{
virtual const char * what () const throw ()
{
return "Device cannot allocate a buffer big enough";
}
};
struct badmemsize : public std::exception
{
virtual const char * what () const throw ()
{
return "Device does not have enough memory for all 3 buffers";
}
};
struct nodouble : public std::exception
{
virtual const char * what () const throw ()
{
return "Device does not support double precision, please use --float";
}
};
template < typename T > template < typename T >
void check_solution(void* a_in, void* b_in, void* c_in) void check_solution(void* a_in, void* b_in, void* c_in)

View File

@ -102,7 +102,8 @@ int main(int argc, char *argv[])
parseArguments(argc, argv); parseArguments(argc, argv);
if (NTIMES < 2) throw badntimes(); if (NTIMES < 2)
throw std::runtime_error("Chosen number of times is invalid, must be >= 2");
std::cout << "Precision: "; std::cout << "Precision: ";
if (useFloat) std::cout << "float"; if (useFloat) std::cout << "float";
@ -119,7 +120,8 @@ int main(int argc, char *argv[])
<< "Warning: array size must divide 1024" << std::endl << "Warning: array size must divide 1024" << std::endl
<< "Resizing array from " << OLD_ARRAY_SIZE << "Resizing array from " << OLD_ARRAY_SIZE
<< " to " << ARRAY_SIZE << std::endl; << " to " << ARRAY_SIZE << std::endl;
if (ARRAY_SIZE == 0) throw badarraysize(); if (ARRAY_SIZE == 0)
throw std::runtime_error("Array size must be >= 1024");
} }
// Get precision (used to reset later) // Get precision (used to reset later)
@ -152,7 +154,8 @@ int main(int argc, char *argv[])
int count; int count;
cudaGetDeviceCount(&count); cudaGetDeviceCount(&count);
check_cuda_error(); check_cuda_error();
if (deviceIndex >= count) throw invaliddevice(); if (deviceIndex >= count)
throw std::runtime_error("Chosen device index is invalid");
cudaSetDevice(deviceIndex); cudaSetDevice(deviceIndex);
check_cuda_error(); check_cuda_error();
@ -162,7 +165,8 @@ int main(int argc, char *argv[])
// Check buffers fit on the device // Check buffers fit on the device
cudaDeviceProp props; cudaDeviceProp props;
cudaGetDeviceProperties(&props, deviceIndex); cudaGetDeviceProperties(&props, deviceIndex);
if (props.totalGlobalMem < 3*DATATYPE_SIZE*ARRAY_SIZE) throw badmemsize(); if (props.totalGlobalMem < 3*DATATYPE_SIZE*ARRAY_SIZE)
throw std::runtime_error("Device does not have enough memory for all 3 buffers");
// Create host vectors // Create host vectors
void * h_a = malloc(ARRAY_SIZE*DATATYPE_SIZE); void * h_a = malloc(ARRAY_SIZE*DATATYPE_SIZE);

View File

@ -49,14 +49,6 @@
std::string getDeviceName(const cl::Device& device); std::string getDeviceName(const cl::Device& device);
unsigned getDeviceList(std::vector<cl::Device>& devices); unsigned getDeviceList(std::vector<cl::Device>& devices);
struct badfile : public std::exception
{
virtual const char * what () const throw ()
{
return "Cannot open kernel file";
}
};
// Print error and exit // Print error and exit
void die(std::string msg, cl::Error& e) void die(std::string msg, cl::Error& e)
@ -85,7 +77,8 @@ int main(int argc, char *argv[])
try try
{ {
parseArguments(argc, argv); parseArguments(argc, argv);
if (NTIMES < 2) throw badntimes(); if (NTIMES < 2)
throw std::runtime_error("Chosen number of times is invalid, must be >= 2");
std::cout << "Precision: "; std::cout << "Precision: ";
@ -103,7 +96,8 @@ int main(int argc, char *argv[])
<< "Warning: array size must divide 1024" << std::endl << "Warning: array size must divide 1024" << std::endl
<< "Resizing array from " << OLD_ARRAY_SIZE << "Resizing array from " << OLD_ARRAY_SIZE
<< " to " << ARRAY_SIZE << std::endl; << " to " << ARRAY_SIZE << std::endl;
if (ARRAY_SIZE == 0) throw badarraysize(); if (ARRAY_SIZE == 0)
throw std::runtime_error("Array size must be >= 1024");
} }
// Get precision (used to reset later) // Get precision (used to reset later)
@ -136,7 +130,8 @@ int main(int argc, char *argv[])
std::string kernels; std::string kernels;
{ {
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 std::runtime_error("Cannot open kernel file");
kernels = std::string (std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>())); kernels = std::string (std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
} }
@ -148,7 +143,8 @@ int main(int argc, char *argv[])
getDeviceList(devices); getDeviceList(devices);
// Check device index is in range // Check device index is in range
if (deviceIndex >= devices.size()) throw invaliddevice(); if (deviceIndex >= devices.size())
throw std::runtime_error("Chosen device index is invalid");
cl::Device device = devices[deviceIndex]; cl::Device device = devices[deviceIndex];
@ -166,14 +162,17 @@ int main(int argc, char *argv[])
std::cout << "Using OpenCL device " << name << std::endl; std::cout << "Using OpenCL device " << name << std::endl;
// Check device can do double precision if requested // Check device can do double precision if requested
if (!useFloat && !device.getInfo<CL_DEVICE_DOUBLE_FP_CONFIG>()) throw nodouble(); if (!useFloat && !device.getInfo<CL_DEVICE_DOUBLE_FP_CONFIG>())
throw std::runtime_error("Device does not support double precision, please use --float");
// Check buffers fit on the device // Check buffers fit on the device
status = "Getting device memory sizes"; status = "Getting device memory sizes";
cl_ulong totalmem = device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>(); cl_ulong totalmem = device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>();
cl_ulong maxbuffer = device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>(); cl_ulong maxbuffer = device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>();
if (maxbuffer < DATATYPE_SIZE*ARRAY_SIZE) throw badbuffersize(); if (maxbuffer < DATATYPE_SIZE*ARRAY_SIZE)
if (totalmem < 3*DATATYPE_SIZE*ARRAY_SIZE) throw badmemsize(); throw std::runtime_error("Device cannot allocate a buffer big enough");
if (totalmem < 3*DATATYPE_SIZE*ARRAY_SIZE)
throw std::runtime_error("Device does not have enough memory for all 3 buffers");
try try
{ {