Use device index from CLI in OpenCL
This commit is contained in:
parent
77b521f5f0
commit
d7c17d72d5
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -144,7 +144,6 @@ void run()
|
||||
<< std::left << std::setw(12) << std::setprecision(5) << *minmax.second
|
||||
<< std::left << std::setw(12) << std::setprecision(5) << average
|
||||
<< std::endl;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user