Use device index from CLI in OpenCL

This commit is contained in:
Tom Deakin 2016-05-03 11:02:33 +01:00
parent 77b521f5f0
commit d7c17d72d5
3 changed files with 30 additions and 18 deletions

View File

@ -1,6 +1,11 @@
#include "OCLStream.h"
// Cache list of devices
bool cached = false;
std::vector<cl::Device> devices;
void getDeviceList(void);
std::string kernels{R"CLC(
constant TYPE scalar = 3.0;
@ -43,16 +48,27 @@ std::string kernels{R"CLC(
template <class T>
OCLStream<T>::OCLStream(const unsigned int ARRAY_SIZE)
OCLStream<T>::OCLStream(const unsigned int ARRAY_SIZE, const int device_index)
{
if (!cached)
getDeviceList();
array_size = ARRAY_SIZE;
// Setup default OpenCL GPU
context = cl::Context::getDefault();
queue = cl::CommandQueue::getDefault();
if (device_index >= devices.size())
throw std::runtime_error("Invalid device index");
device = devices[device_index];
// Print out device information
std::cout << "Using OpenCL device " << getDeviceName(device_index) << std::endl;
std::cout << "Driver: " << getDeviceDriver(device_index) << std::endl;
context = cl::Context(device);
queue = cl::CommandQueue(context);
// Create program
cl::Program program(kernels);
cl::Program program(context, kernels);
if (sizeof(T) == sizeof(double))
program.build("-DTYPE=double");
else if (sizeof(T) == sizeof(float))
@ -123,23 +139,19 @@ void OCLStream<T>::triad()
template <class T>
void OCLStream<T>::write_arrays(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c)
{
cl::copy(a.begin(), a.end(), d_a);
cl::copy(b.begin(), b.end(), d_b);
cl::copy(c.begin(), c.end(), d_c);
cl::copy(queue, a.begin(), a.end(), d_a);
cl::copy(queue, b.begin(), b.end(), d_b);
cl::copy(queue, c.begin(), c.end(), d_c);
}
template <class T>
void OCLStream<T>::read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{
cl::copy(d_a, a.begin(), a.end());
cl::copy(d_b, b.begin(), b.end());
cl::copy(d_c, c.begin(), c.end());
cl::copy(queue, d_a, a.begin(), a.end());
cl::copy(queue, d_b, b.begin(), b.end());
cl::copy(queue, d_c, c.begin(), c.end());
}
// Cache list of devices
bool cached = false;
std::vector<cl::Device> devices;
void getDeviceList(void)
{
// Get list of platforms

View File

@ -27,6 +27,7 @@ class OCLStream : public Stream<T>
cl::Buffer d_c;
// OpenCL objects
cl::Device device;
cl::Context context;
cl::CommandQueue queue;
@ -37,7 +38,7 @@ class OCLStream : public Stream<T>
public:
OCLStream(const unsigned int);
OCLStream(const unsigned int, const int);
~OCLStream();
virtual void copy() override;

View File

@ -61,11 +61,11 @@ void run()
#if defined(CUDA)
// Use the CUDA implementation
stream = new CUDAStream<T>(ARRAY_SIZE);
stream = new CUDAStream<T>(ARRAY_SIZE, deviceIndex);
#elif defined(OCL)
// Use the OpenCL implementation
stream = new OCLStream<T>(ARRAY_SIZE);
stream = new OCLStream<T>(ARRAY_SIZE, deviceIndex);
#endif
@ -145,7 +145,6 @@ void run()
<< std::left << std::setw(12) << std::setprecision(5) << average
<< std::endl;
}
delete[] stream;