/* opencl_test. CPP */
//newTutorial1. CPP: Defines the entrypoint for the console application.
//
//# include "stdafx. H" (MFC header files, here don't need to)
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
Int convertToString (const char * filename, STD: : string& S)
{
Size_t size;
Char * STR;
STD: : fstream f (filename, (STD: : fstream: : | in STD: : fstream: : binary));
If (f.i s_open ())
{
Size_t fileSize.
F.s eekg (0, STD: : fstream: : end);
Size=fileSize=(size_t) f.t ellg ();
F.s eekg (0, STD: : fstream: : beg);
STR=new char [size + 1];
if(! STR)
{
f.close();
return 0;
}
F.r ead (STR, fileSize);
f.close();
STR [size]='\ 0';
S=STR;
The delete [] STR;
return 0;
}
Cout<& lt;" Error: failed to open the file \ n: "& lt;
}
Int main (int arg c, char * argv [])
{
//1
Cl_uint numPlatforms;
Cl_platform_id platform=NULL;
Cl_int status=clGetPlatformIDs (0, NULL, & amp; NumPlatforms);
If (the status!=CL_SUCCESS)
{
Cout<& lt;" Error: Getting platforms!"
}
If (numPlatforms> 0)
{
Cl_platform_id * platforms=(cl_platform_id *) malloc (numPlatforms *
Sizeof (cl_platform_id));
Status=clGetPlatformIDs (numPlatforms, platforms, NULL);
Platform=platforms [0];
Free (platforms);
}
//2
Cl_uint numDevices=0;
Cl_device_id * devices;
Status=clGetDeviceIDs (platform, CL_DEVICE_TYPE_CPU, 0,
NULL, & amp; NumDevices);
If (numDevices==0)
{
Cout<& lt;" No GPU device available. "& lt;
Status=clGetDeviceIDs (platform, CL_DEVICE_TYPE_ACCELERATOR numDevices, NULL, & amp; NumDevices);
Devices=(cl_device_id *) malloc (numDevices * sizeof (cl_device_id));
Status=clGetDeviceIDs (platform, CL_DEVICE_TYPE_ACCELERATOR numDevices, devices, NULL);
}
The else
{
Devices=(cl_device_id *) malloc (numDevices * sizeof (cl_device_id));
Status=clGetDeviceIDs (platform, CL_DEVICE_TYPE_ACCELERATOR,
NumDevices, devices, NULL);
}
//3
Cl_context context=clCreateContext (NULL, 1, devices, NULL, NULL, NULL);
//4
Cl_command_queue commandQueue=clCreateCommandQueue (context, devices [0], 0, NULL);
//5
Const char * filename="HelloWorld_Kernel. Cl";
String sourceStr;
Status=convertToString (filename, sourceStr);
Const char * source=sourceStr. C_str ();
Size_t sourceSize []={strlen (source)};
Cl_program program=clCreateProgramWithSource (context, 1, & amp; The source, sourceSize, NULL);
//6
Status=clBuildProgram (program, 1, devices, NULL, NULL, NULL);
//7
Const char * input="Gdkkn Vnqkc";
Size_t strlength=strlen (input);
Cout<& lt;" The input string: "& lt;
Cl_mem inputBuffer=clCreateBuffer (context, CL_MEM_READ_ONLY |
CL_MEM_COPY_HOST_PTR, (strlength + 1) * sizeof (char),
(void *) input, NULL);
Cl_mem outputBuffer=clCreateBuffer (context, CL_MEM_WRITE_ONLY, (strlength + 1) * sizeof (char), NULL, NULL);
//8
Cl_kernel kernel=clCreateKernel (program, "helloworld", NULL);
//9
Status=clSetKernelArg (kernel, 0, sizeof (cl_mem), (void *) & amp; The inputBuffer);
Status=clSetKernelArg (kernel, 1, sizeof (cl_mem), (void *) & amp; OutputBuffer);
//10
Size_t global_work_size [1]={strlength};
Status=clEnqueueNDRangeKernel (commandQueue, the kernel, 1, NULL,
Global_work_size, NULL, 0, NULL, NULL);
//11
Status=clEnqueueReadBuffer (commandQueue, outputBuffer CL_TRUE, 0,
Strlength * sizeof (char), the output, 0, NULL, NULL);
The output [strlength]='\ 0';
Cout<& lt;" The output string: "& lt;
//12
Status=clReleaseKernel (kernel);
Status=clReleaseProgram (program);
Status=clReleaseMemObject (inputBuffer);
Status=clReleaseMemObject (outputBuffer);
Status=clReleaseCommandQueue (commandQueue);
Status=clReleaseContext (context);
If (the output!=NULL)
{
Free (the output);
The output=NULL;
}
If (devices!=NULL)
{
Free (devices);
Devices=NULL;
}
return 0;
}
The other is:
/* the kernel in cl */
_kernel void helloworld (_global char *, in _global char * out)
{
Int num=get_global_id (0);
Out [num]=[in num] + 1;
}
But it has been an error:
Opencl. Obj: error LNK2019: cannot resolve the external symbol of _clGetPlatformIDs @ 12, the symbol referenced in the function _main
1> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull