#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::string matrix_path;
if (argc < 2)
{
std::cout << "Not enough parameters. "
<< "Please specify path to matrix in mtx format as parameter"
<< std::endl;
return -1;
}
else
{
matrix_path = std::string(argv[1]);
}
std::cout << "Executing sample clSPARSE CG Solver (A*x = b) C++" << std::endl;
std::cout << "Matrix will be read from: " << matrix_path << 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 -2;
}
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);
if (status != clsparseSuccess)
{
std::cout << "Problem with executing clsparseSetup()" << std::endl;
return -3;
}
if( fileError != clsparseSuccess )
{
std::cout << "Could not read matrix market header from disk" << std::endl;
return -5;
}
A.
values = ::clCreateBuffer( context(), CL_MEM_READ_ONLY,
A.
col_indices = ::clCreateBuffer( context(), CL_MEM_READ_ONLY,
A.
row_pointer = ::clCreateBuffer( context(), CL_MEM_READ_ONLY,
if (fileError != clsparseSuccess)
{
std::cout << "Problem with reading matrix from " << matrix_path
<< " Error: " << status << std::endl;
return -6;
}
float one = 1.0f;
float zero = 0.0f;
x.
values = clCreateBuffer(context(), CL_MEM_READ_ONLY, x.
num_values *
sizeof(
float),
NULL, &cl_status);
cl_status = clEnqueueFillBuffer(queue(), x.
values, &zero,
sizeof(
float),
0, x.
num_values *
sizeof(
float), 0,
nullptr,
nullptr);
b.
values = clCreateBuffer(context(), CL_MEM_READ_WRITE, b.
num_values *
sizeof(
float),
NULL, &cl_status);
cl_status = clEnqueueFillBuffer(queue(), b.
values, &one,
sizeof(
float),
0, b.
num_values *
sizeof(
float), 0,
nullptr,
nullptr);
if (status != clsparseSuccess)
{
std::cout << "Problem with releasing control object."
<< " Error: " << status << std::endl;
}
if (status != clsparseSuccess)
{
std::cout << "Problem with closing clSPARSE library."
<< " Error: " << status << std::endl;
}
clReleaseMemObject ( A.
values );
clReleaseMemObject ( x.
values );
clReleaseMemObject ( b.
values );
std::cout << "Program completed successfully." << std::endl;
return 0;
}