226 lines
4.6 KiB
C++
226 lines
4.6 KiB
C++
|
|
#include "OCLStream.h"
|
|
|
|
std::string kernels{R"CLC(
|
|
|
|
constant TYPE scalar = 3.0;
|
|
|
|
kernel void copy(
|
|
global const TYPE * restrict a,
|
|
global TYPE * restrict c)
|
|
{
|
|
const size_t i = get_global_id(0);
|
|
c[i] = a[i];
|
|
}
|
|
|
|
kernel void mul(
|
|
global TYPE * restrict b,
|
|
global const TYPE * restrict c)
|
|
{
|
|
const size_t i = get_global_id(0);
|
|
b[i] = scalar * c[i];
|
|
}
|
|
|
|
kernel void add(
|
|
global const TYPE * restrict a,
|
|
global const TYPE * restrict b,
|
|
global TYPE * restrict c)
|
|
{
|
|
const size_t i = get_global_id(0);
|
|
c[i] = a[i] + b[i];
|
|
}
|
|
|
|
kernel void triad(
|
|
global TYPE * restrict a,
|
|
global const TYPE * restrict b,
|
|
global const TYPE * restrict c)
|
|
{
|
|
const size_t i = get_global_id(0);
|
|
a[i] = b[i] + scalar * c[i];
|
|
}
|
|
|
|
)CLC"};
|
|
|
|
|
|
template <class T>
|
|
OCLStream<T>::OCLStream(const unsigned int ARRAY_SIZE)
|
|
{
|
|
array_size = ARRAY_SIZE;
|
|
|
|
// Setup default OpenCL GPU
|
|
context = cl::Context::getDefault();
|
|
queue = cl::CommandQueue::getDefault();
|
|
|
|
// Create program
|
|
cl::Program program(kernels);
|
|
if (sizeof(T) == sizeof(double))
|
|
program.build("-DTYPE=double");
|
|
else if (sizeof(T) == sizeof(float))
|
|
program.build("-DTYPE=float");
|
|
|
|
// Create kernels
|
|
copy_kernel = new cl::KernelFunctor<cl::Buffer, cl::Buffer>(program, "copy");
|
|
mul_kernel = new cl::KernelFunctor<cl::Buffer, cl::Buffer>(program, "mul");
|
|
add_kernel = new cl::KernelFunctor<cl::Buffer, cl::Buffer, cl::Buffer>(program, "add");
|
|
triad_kernel = new cl::KernelFunctor<cl::Buffer, cl::Buffer, cl::Buffer>(program, "triad");
|
|
|
|
// Create buffers
|
|
d_a = cl::Buffer(context, CL_MEM_READ_WRITE, sizeof(T) * ARRAY_SIZE);
|
|
d_b = cl::Buffer(context, CL_MEM_READ_WRITE, sizeof(T) * ARRAY_SIZE);
|
|
d_c = cl::Buffer(context, CL_MEM_READ_WRITE, sizeof(T) * ARRAY_SIZE);
|
|
|
|
}
|
|
|
|
template <class T>
|
|
OCLStream<T>::~OCLStream()
|
|
{
|
|
delete[] copy_kernel;
|
|
delete[] mul_kernel;
|
|
delete[] add_kernel;
|
|
delete[] triad_kernel;
|
|
}
|
|
|
|
template <class T>
|
|
void OCLStream<T>::copy()
|
|
{
|
|
(*copy_kernel)(
|
|
cl::EnqueueArgs(queue, cl::NDRange(array_size)),
|
|
d_a, d_c
|
|
);
|
|
queue.finish();
|
|
}
|
|
|
|
template <class T>
|
|
void OCLStream<T>::mul()
|
|
{
|
|
(*mul_kernel)(
|
|
cl::EnqueueArgs(queue, cl::NDRange(array_size)),
|
|
d_b, d_c
|
|
);
|
|
queue.finish();
|
|
}
|
|
|
|
template <class T>
|
|
void OCLStream<T>::add()
|
|
{
|
|
(*add_kernel)(
|
|
cl::EnqueueArgs(queue, cl::NDRange(array_size)),
|
|
d_a, d_b, d_c
|
|
);
|
|
queue.finish();
|
|
}
|
|
|
|
template <class T>
|
|
void OCLStream<T>::triad()
|
|
{
|
|
(*triad_kernel)(
|
|
cl::EnqueueArgs(queue, cl::NDRange(array_size)),
|
|
d_a, d_b, d_c
|
|
);
|
|
queue.finish();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
// Cache list of devices
|
|
bool cached = false;
|
|
std::vector<cl::Device> devices;
|
|
|
|
void getDeviceList(void)
|
|
{
|
|
// Get list of platforms
|
|
std::vector<cl::Platform> platforms;
|
|
cl::Platform::get(&platforms);
|
|
|
|
// Enumerate devices
|
|
for (unsigned i = 0; i < platforms.size(); i++)
|
|
{
|
|
std::vector<cl::Device> plat_devices;
|
|
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &plat_devices);
|
|
devices.insert(devices.end(), plat_devices.begin(), plat_devices.end());
|
|
}
|
|
cached = true;
|
|
}
|
|
|
|
void listDevices(void)
|
|
{
|
|
getDeviceList();
|
|
|
|
// Print device names
|
|
if (devices.size() == 0)
|
|
{
|
|
std::cerr << "No devices found." << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << std::endl;
|
|
std::cout << "Devices:" << std::endl;
|
|
for (int i = 0; i < devices.size(); i++)
|
|
{
|
|
std::cout << i << ": " << getDeviceName(i) << std::endl;
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
std::string getDeviceName(const int device)
|
|
{
|
|
if (!cached)
|
|
getDeviceList();
|
|
|
|
std::string name;
|
|
cl_device_info info = CL_DEVICE_NAME;
|
|
|
|
if (device < devices.size())
|
|
{
|
|
devices[device].getInfo(info, &name);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Error asking for name for non-existant device");
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
std::string getDeviceDriver(const int device)
|
|
{
|
|
if (!cached)
|
|
getDeviceList();
|
|
|
|
std::string driver;
|
|
|
|
if (device < devices.size())
|
|
{
|
|
devices[device].getInfo(CL_DRIVER_VERSION, &driver);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Error asking for driver for non-existant device");
|
|
}
|
|
|
|
return driver;
|
|
}
|
|
|
|
|
|
template class OCLStream<float>;
|
|
template class OCLStream<double>;
|
|
|