Be consistent with indenting

This commit is contained in:
Tom Deakin 2015-07-16 16:36:30 +01:00
parent 97c179feb3
commit e5bd4fcb19

View File

@ -27,83 +27,83 @@ unsigned getDeviceList(std::vector<cl::Device>& devices);
struct badfile : public std::exception struct badfile : public std::exception
{ {
virtual const char * what () const throw () virtual const char * what () const throw ()
{ {
return "Cannot open kernel file"; return "Cannot open kernel file";
} }
}; };
struct badtype : public std::exception struct badtype : public std::exception
{ {
virtual const char * what () const throw () virtual const char * what () const throw ()
{ {
return "Datatype is not 4 or 8"; return "Datatype is not 4 or 8";
} }
}; };
struct invaliddevice : public std::exception struct invaliddevice : public std::exception
{ {
virtual const char * what () const throw () virtual const char * what () const throw ()
{ {
return "Chosen device index is invalid"; return "Chosen device index is invalid";
} }
}; };
size_t sizes[4] = { size_t sizes[4] = {
2 * sizeof(DATATYPE) * ARRAY_SIZE, 2 * sizeof(DATATYPE) * ARRAY_SIZE,
2 * sizeof(DATATYPE) * ARRAY_SIZE, 2 * sizeof(DATATYPE) * ARRAY_SIZE,
3 * sizeof(DATATYPE) * ARRAY_SIZE, 3 * sizeof(DATATYPE) * ARRAY_SIZE,
3 * sizeof(DATATYPE) * ARRAY_SIZE 3 * sizeof(DATATYPE) * ARRAY_SIZE
}; };
void check_solution(std::vector<DATATYPE>& a, std::vector<DATATYPE>& b, std::vector<DATATYPE>& c) void check_solution(std::vector<DATATYPE>& a, std::vector<DATATYPE>& b, std::vector<DATATYPE>& c)
{ {
// Generate correct solution // Generate correct solution
DATATYPE golda = 1.0; DATATYPE golda = 1.0;
DATATYPE goldb = 2.0; DATATYPE goldb = 2.0;
DATATYPE goldc = 0.0; DATATYPE goldc = 0.0;
const DATATYPE scalar = 3.0; const DATATYPE scalar = 3.0;
for (unsigned int i = 0; i < NTIMES; i++) for (unsigned int i = 0; i < NTIMES; i++)
{ {
goldc = golda; goldc = golda;
goldb = scalar * goldc; goldb = scalar * goldc;
goldc = golda + goldb; goldc = golda + goldb;
golda = goldb + scalar * goldc; golda = goldb + scalar * goldc;
} }
// Calculate average error // Calculate average error
double erra = 0.0; double erra = 0.0;
double errb = 0.0; double errb = 0.0;
double errc = 0.0; double errc = 0.0;
for (unsigned int i = 0; i < ARRAY_SIZE; i++) for (unsigned int i = 0; i < ARRAY_SIZE; i++)
{ {
erra += fabs(a[i] - golda); erra += fabs(a[i] - golda);
errb += fabs(b[i] - goldb); errb += fabs(b[i] - goldb);
errc += fabs(c[i] - goldc); errc += fabs(c[i] - goldc);
} }
erra /= (double)ARRAY_SIZE; erra /= (double)ARRAY_SIZE;
errb /= (double)ARRAY_SIZE; errb /= (double)ARRAY_SIZE;
errc /= (double)ARRAY_SIZE; errc /= (double)ARRAY_SIZE;
double epsi; double epsi;
if (sizeof(DATATYPE) == 4) epsi = 1.0E-6; if (sizeof(DATATYPE) == 4) epsi = 1.0E-6;
else if (sizeof(DATATYPE) == 8) epsi = 1.0E-13; else if (sizeof(DATATYPE) == 8) epsi = 1.0E-13;
else throw badtype(); else throw badtype();
if (erra > epsi) if (erra > epsi)
std::cout std::cout
<< "Validation failed on a[]. Average error " << erra << "Validation failed on a[]. Average error " << erra
<< std::endl; << std::endl;
if (errb > epsi) if (errb > epsi)
std::cout std::cout
<< "Validation failed on b[]. Average error " << errb << "Validation failed on b[]. Average error " << errb
<< std::endl; << std::endl;
if (errc > epsi) if (errc > epsi)
std::cout std::cout
<< "Validation failed on c[]. Average error " << errc << "Validation failed on c[]. Average error " << errc
<< std::endl; << std::endl;
} }
cl_uint deviceIndex = 0; cl_uint deviceIndex = 0;
@ -111,195 +111,195 @@ cl_uint deviceIndex = 0;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// Print out run information // Print out run information
std::cout std::cout
<< "GPU-STREAM" << std::endl << "GPU-STREAM" << std::endl
<< "Version: " << VERSION_STRING << std::endl << "Version: " << VERSION_STRING << std::endl
<< "Implementation: OpenCL" << std::endl << std::endl; << "Implementation: OpenCL" << std::endl << std::endl;
try try
{ {
parseArguments(argc, argv); parseArguments(argc, argv);
// Open the Kernel source // Open the Kernel source
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();
std::string kernels(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>())); std::string kernels(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>()));
// Setup OpenCL // Setup OpenCL
// Get list of devices // Get list of devices
std::vector<cl::Device> devices; std::vector<cl::Device> devices;
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 invaliddevice();
cl::Device device = devices[deviceIndex]; cl::Device device = devices[deviceIndex];
cl::Context context(device); cl::Context context(device);
cl::CommandQueue queue(context); cl::CommandQueue queue(context);
cl::Program program(context, kernels); cl::Program program(context, kernels);
// Print out device name // Print out device name
std::string name = getDeviceName(device); std::string name = getDeviceName(device);
std::cout << "Using OpenCL device " << name << std::endl; std::cout << "Using OpenCL device " << name << std::endl;
try try
{ {
program.build(); program.build();
} }
catch (cl::Error& e) catch (cl::Error& e)
{ {
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>(); std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
std::string buildlog = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]); std::string buildlog = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
std::cerr std::cerr
<< "Build error:" << "Build error:"
<< buildlog << buildlog
<< std::endl; << std::endl;
throw e; throw e;
} }
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
std::vector<DATATYPE> h_a(ARRAY_SIZE, 1.0); std::vector<DATATYPE> h_a(ARRAY_SIZE, 1.0);
std::vector<DATATYPE> h_b(ARRAY_SIZE, 2.0); std::vector<DATATYPE> h_b(ARRAY_SIZE, 2.0);
std::vector<DATATYPE> h_c(ARRAY_SIZE, 0.0); std::vector<DATATYPE> h_c(ARRAY_SIZE, 0.0);
// Create device buffers // Create device buffers
cl::Buffer d_a(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); cl::Buffer d_a(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE);
cl::Buffer d_b(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); cl::Buffer d_b(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE);
cl::Buffer d_c(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); cl::Buffer d_c(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE);
// Copy host memory to device // Copy host memory to device
cl::copy(queue, h_a.begin(), h_a.end(), d_a); cl::copy(queue, h_a.begin(), h_a.end(), d_a);
cl::copy(queue, h_b.begin(), h_b.end(), d_b); cl::copy(queue, h_b.begin(), h_b.end(), d_b);
cl::copy(queue, h_c.begin(), h_c.end(), d_c); cl::copy(queue, h_c.begin(), h_c.end(), d_c);
// Make sure the copies are finished // Make sure the copies are finished
queue.finish(); queue.finish();
// List of times // List of times
std::vector< std::vector<double> > timings; std::vector< std::vector<double> > timings;
// Declare timers // Declare timers
std::chrono::high_resolution_clock::time_point t1, t2; std::chrono::high_resolution_clock::time_point t1, t2;
// Main loop // Main loop
for (unsigned int k = 0; k < NTIMES; k++) for (unsigned int k = 0; k < NTIMES; k++)
{ {
std::vector<double> times; std::vector<double> times;
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
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();
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());
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
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();
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());
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
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();
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());
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
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();
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());
timings.push_back(times); timings.push_back(times);
} }
// Check solutions // Check solutions
cl::copy(queue, d_a, h_a.begin(), h_a.end()); cl::copy(queue, d_a, h_a.begin(), h_a.end());
cl::copy(queue, d_b, h_b.begin(), h_b.end()); cl::copy(queue, d_b, h_b.begin(), h_b.end());
cl::copy(queue, d_c, h_c.begin(), h_c.end()); cl::copy(queue, d_c, h_c.begin(), h_c.end());
check_solution(h_a, h_b, h_c); check_solution(h_a, h_b, h_c);
// Crunch results // Crunch results
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++)
{ {
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
{ {
avg[j] += timings[i][j]; avg[j] += timings[i][j];
min[j] = MIN(min[j], timings[i][j]); min[j] = MIN(min[j], timings[i][j]);
max[j] = MAX(max[j], timings[i][j]); max[j] = 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);
// Display results // Display results
std::string labels[] = {"Copy", "Mul", "Add", "Triad"}; std::string labels[] = {"Copy", "Mul", "Add", "Triad"};
std::cout std::cout
<< std::left << std::setw(12) << "Function" << std::left << std::setw(12) << "Function"
<< std::left << std::setw(12) << "MBytes/sec" << std::left << std::setw(12) << "MBytes/sec"
<< std::left << std::setw(12) << "Min (sec)" << std::left << std::setw(12) << "Min (sec)"
<< 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
<< std::left << std::setw(12) << labels[j] << std::left << std::setw(12) << labels[j]
<< std::left << std::setw(12) << 1.0E-06 * sizes[j]/min[j] << std::left << std::setw(12) << 1.0E-06 * sizes[j]/min[j]
<< std::left << std::setw(12) << min[j] << std::left << std::setw(12) << min[j]
<< std::left << std::setw(12) << max[j] << std::left << std::setw(12) << max[j]
<< std::left << std::setw(12) << avg[j] << std::left << std::setw(12) << avg[j]
<< std::endl; << std::endl;
} }
} }
// Catch OpenCL Errors and display information // Catch OpenCL Errors and display information
catch (cl::Error& e) catch (cl::Error& e)
{ {
std::cerr std::cerr
<< "Error: " << "Error: "
<< e.what() << e.what()
<< "(" << e.err() << ")" << "(" << e.err() << ")"
<< std::endl; << std::endl;
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr std::cerr
<< "Error: " << "Error: "
<< e.what() << e.what()
<< std::endl; << std::endl;
} }
} }
unsigned getDeviceList(std::vector<cl::Device>& devices) unsigned getDeviceList(std::vector<cl::Device>& devices)
@ -321,93 +321,93 @@ unsigned getDeviceList(std::vector<cl::Device>& devices)
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;
// 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);
if (strstr(name.c_str(), "Advanced Micro Devices")) if (strstr(name.c_str(), "Advanced Micro Devices"))
info = CL_DEVICE_BOARD_NAME_AMD; info = CL_DEVICE_BOARD_NAME_AMD;
#endif #endif
device.getInfo(info, &name); device.getInfo(info, &name);
return name; return name;
} }
int parseUInt(const char *str, cl_uint *output) int parseUInt(const char *str, cl_uint *output)
{ {
char *next; char *next;
*output = strtoul(str, &next, 10); *output = strtoul(str, &next, 10);
return !strlen(next); return !strlen(next);
} }
void parseArguments(int argc, char *argv[]) void parseArguments(int argc, char *argv[])
{ {
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--list"))
{ {
// Get list of devices if (!strcmp(argv[i], "--list"))
std::vector<cl::Device> devices;
getDeviceList(devices);
// Print device names
if (devices.size() == 0)
{
std::cout << "No devices found." << std::endl;
}
else
{
std::cout << std::endl;
std::cout << "Devices:" << std::endl;
for (unsigned i = 0; i < devices.size(); i++)
{ {
std::cout << i << ": " << getDeviceName(devices[i]) << std::endl; // Get list of devices
std::vector<cl::Device> devices;
getDeviceList(devices);
// Print device names
if (devices.size() == 0)
{
std::cout << "No devices found." << std::endl;
}
else
{
std::cout << std::endl;
std::cout << "Devices:" << std::endl;
for (unsigned i = 0; i < devices.size(); i++)
{
std::cout << i << ": " << getDeviceName(devices[i]) << std::endl;
}
std::cout << std::endl;
}
exit(0);
} }
std::cout << std::endl; else if (!strcmp(argv[i], "--device"))
} {
exit(0); if (++i >= argc || !parseUInt(argv[i], &deviceIndex))
} {
else if (!strcmp(argv[i], "--device")) std::cout << "Invalid device index" << std::endl;
{ exit(1);
if (++i >= argc || !parseUInt(argv[i], &deviceIndex)) }
{ }
std::cout << "Invalid device index" << std::endl; else if (!strcmp(argv[i], "--arraysize") || !strcmp(argv[i], "-s"))
exit(1); {
} if (++i >= argc || !parseUInt(argv[i], &ARRAY_SIZE))
} {
else if (!strcmp(argv[i], "--arraysize") || !strcmp(argv[i], "-s")) std::cout << "Invalid array size" << std::endl;
{ exit(1);
if (++i >= argc || !parseUInt(argv[i], &ARRAY_SIZE)) }
{ }
std::cout << "Invalid array size" << std::endl; else if (!strcmp(argv[i], "--float"))
exit(1); {
} useFloat = true;
} }
else if (!strcmp(argv[i], "--float")) else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{ {
useFloat = true; std::cout << std::endl;
} std::cout << "Usage: ./gpu-stream-ocl [OPTIONS]" << std::endl << std::endl;
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) std::cout << "Options:" << std::endl;
{ std::cout << " -h --help Print the message" << std::endl;
std::cout << std::endl; std::cout << " --list List available devices" << std::endl;
std::cout << "Usage: ./gpu-stream-ocl [OPTIONS]" << std::endl << std::endl; std::cout << " --device INDEX Select device at INDEX" << std::endl;
std::cout << "Options:" << std::endl; std::cout << " -s --arraysize SIZE Use SIZE elements in the array" << std::endl;
std::cout << " -h --help Print the message" << std::endl; std::cout << " --float Enable use of floats instead of doubles" << std::endl;
std::cout << " --list List available devices" << std::endl; std::cout << std::endl;
std::cout << " --device INDEX Select device at INDEX" << std::endl; exit(0);
std::cout << " -s --arraysize SIZE Use SIZE elements in the array" << std::endl; }
std::cout << " --float Enable use of floats instead of doubles" << std::endl; else
std::cout << std::endl; {
exit(0); std::cout << "Unrecognized argument '" << argv[i] << "' (try '--help')"
}
else
{
std::cout << "Unrecognized argument '" << argv[i] << "' (try '--help')"
<< std::endl; << std::endl;
exit(1); exit(1);
}
} }
}
} }