#include <iostream>
#include <vector>
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.hpp>
#else
#include <CL/cl.hpp>
#endif
int main(int argc, char* argv[])
{
std::cout << "Executing sample clSPARSE AXPY (y = alpha * x + y) C++" << std::endl;
cl_int cl_status;
std::vector<cl::Platform> platforms;
cl_status = cl::Platform::get(&platforms);
if (cl_status != CL_SUCCESS)
{
std::cout << "Problem with getting OpenCL platforms"
<< " [" << cl_status << "]" << std::endl;
return -1;
}
int platform_id = 0;
for (const auto& p : platforms)
{
std::cout << "Platform ID " << platform_id++ << " : "
<< p.getInfo<CL_PLATFORM_NAME>() << std::endl;
}
platform_id = 0;
cl::Platform platform = platforms[platform_id];
std::vector<cl::Device> devices;
cl_status = platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
if (cl_status != CL_SUCCESS)
{
std::cout << "Problem with getting devices from platform"
<< " [" << platform_id << "] " << platform.getInfo<CL_PLATFORM_NAME>()
<< " error: [" << cl_status << "]" << std::endl;
}
std::cout << std::endl
<< "Getting devices from platform " << platform_id << std::endl;
cl_int device_id = 0;
for (const auto& device : devices)
{
std::cout << "Device ID " << device_id++ << " : "
<< device.getInfo<CL_DEVICE_NAME>() << std::endl;
}
device_id = 0;
cl::Device device = devices[device_id];
cl::Context context (device);
cl::CommandQueue queue(context, device);
int N = 1024;
float alpha = 2.0f;
std::vector<float> y(N, 2.0f);
std::vector<float> x(N, 1.0f);
gpuAlpha.
value = ::clCreateBuffer (context(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(float), &alpha, &cl_status);
if (cl_status != CL_SUCCESS )
{
std::cout << "Problem with allocating alpha buffer on GPU\n " << std::endl;
}
gpuY.
values = ::clCreateBuffer (context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
N * sizeof(float), y.data(), &cl_status);
if (cl_status != CL_SUCCESS )
{
std::cout << "Problem with allocating Y buffer on GPU\n " << std::endl;
}
gpuX.
values = ::clCreateBuffer (context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
N * sizeof(float), x.data(), &cl_status);
if (cl_status != CL_SUCCESS )
{
std::cout << "Problem with allocating X buffer on GPU\n " << std::endl;
}
if (status != clsparseSuccess)
{
std::cout << "Problem with executing clsparseSetup()" << std::endl;
return -1;
}
if (status != clsparseSuccess)
{
std::cout << "Problem with execution of clsparse AXPY algorithm"
<< " error: [" << status << "]" << std::endl;
}
if (status != clsparseSuccess)
{
std::cerr << "Problem with executing clsparseTeardown()" << std::endl;
return -2;
}
::clEnqueueReadBuffer(queue(), gpuY.
values, CL_TRUE, 0, N *
sizeof(
float),
y.data(), 0, nullptr, nullptr);
std::cout << "Result data: " << std::endl;
for ( int i = 0; i < 5; i++)
{
std::cout << "\t" << i << " = " << y[i] << std::endl;
}
::clReleaseMemObject(gpuAlpha.
value);
::clReleaseMemObject(gpuY.
values);
::clReleaseMemObject(gpuX.
values);
std::cout << "Program completed" << std::endl;
return 0;
}