#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[ ] )
{
printf("Executing sample clSPARSE Norm1 C\n");
cl_int cl_status = CL_SUCCESS;
cl_platform_id* platforms = NULL;
cl_device_id* devices = NULL;
cl_uint num_platforms = 0;
cl_uint num_devices = 0;
cl_status = clGetPlatformIDs(0, NULL, &num_platforms);
if (num_platforms == 0)
{
printf ("No OpenCL platforms found. Exiting.\n");
return 0;
}
platforms = (cl_platform_id*) malloc (num_platforms * sizeof(cl_platform_id));
cl_status = clGetPlatformIDs(num_platforms, platforms, NULL);
if (cl_status != CL_SUCCESS)
{
printf("Problem with getting platform IDs. Err: %d\n", cl_status);
free(platforms);
return -1;
}
cl_status = clGetDeviceIDs(platforms[ 0 ], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
if (num_devices == 0)
{
printf("No OpenCL GPU devices found on platform 0. Exiting\n");
free(platforms);
return -2;
}
devices = (cl_device_id*) malloc( num_devices * sizeof(cl_device_id));
cl_status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, num_devices, devices, NULL);
if (cl_status != CL_SUCCESS)
{
printf("Problem with getting device id from platform. Exiting\n");
free(devices);
free(platforms);
return -3;
}
cl_context context = clCreateContext( NULL, 1, devices, NULL, NULL, NULL );
cl_command_queue queue = clCreateCommandQueue( context, devices[ 0 ], 0, NULL );
int N = 1024;
x.
values = clCreateBuffer(context, CL_MEM_READ_WRITE, N *
sizeof (
float),
NULL, &cl_status);
float one = 1.0f;
cl_status = clEnqueueFillBuffer(queue, x.
values, &one,
sizeof(
float),
0, N * sizeof(float), 0, NULL, NULL);
norm_x.
value = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
sizeof(
float),
NULL, &cl_status);
if (status != clsparseSuccess)
{
printf ("Problem with executing clsparseSetup()");
return -1;
}
float* host_norm_x =
clEnqueueMapBuffer(queue, norm_x.
value, CL_TRUE, CL_MAP_READ, 0,
sizeof(
float),
0, NULL, NULL, &cl_status);
printf ("\tResult : %f\n", *host_norm_x);
cl_status = clEnqueueUnmapMemObject(queue, norm_x.
value, host_norm_x,
0, NULL, NULL);
if (status != clsparseSuccess)
{
printf ("Problem with executing clsparseTeardown()");
return -1;
}
clReleaseMemObject(norm_x.
value);
clReleaseCommandQueue (queue);
clReleaseContext (context);
free (devices);
free (platforms);
printf ("Program completed\n");
return 0;
}