Skip to content

Instantly share code, notes, and snippets.

@PontiacGTX
Last active June 12, 2019 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PontiacGTX/1dcf2990b27fd2c7c33d9d9d4d36ce42 to your computer and use it in GitHub Desktop.
Save PontiacGTX/1dcf2990b27fd2c7c33d9d9d4d36ce42 to your computer and use it in GitHub Desktop.
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <fstream>
#include <CL/cl.h>
#include <sstream>
void GetPlatform(cl_platform_id& platform,cl_int& status)
{
cl_uint platformCount = 0;
status = clGetPlatformIDs(NULL, 0, &platformCount);
if (status != 0)
{
std::cout << "couldnt find platforms" << std::endl;
}
if (platformCount > 0)
{
cl_platform_id* platforms = new cl_platform_id[platformCount * sizeof(cl_platform_id)];
status = clGetPlatformIDs(platformCount, platforms, &platformCount);
platform = platforms[0];
if (platform == nullptr)
{
std::cout << "couldnt assign a platform " << std::endl;
}
delete[] platforms;
}
}
void GetDevicesIDs(cl_device_id*& devices,cl_platform_id& platform)
{
cl_uint deviceCount = 0;
cl_int status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,0,NULL,&deviceCount);
if (deviceCount == 0)
{
std::cout << "No GPU device available." << std::endl;
std::cout << "Choose CPU as default device." << std::endl;
status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_CPU,0,NULL,&deviceCount);
devices = new cl_device_id[deviceCount * sizeof(cl_device_id)];
status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_CPU,deviceCount,devices,NULL);
}
else
{
devices = new cl_device_id[deviceCount * sizeof(cl_device_id)];
status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,deviceCount,devices,NULL);
}
}
void GetContext(cl_context& context,cl_device_id* &devices,cl_platform_id& platform)
{
cl_int status = 0;
cl_context_properties contextProperties[] = {CL_CONTEXT_PLATFORM,reinterpret_cast<cl_context_properties>(platform),0};
context = clCreateContext(contextProperties,1,devices,NULL,NULL,&status);
if (status != 0)
{
std::cout << "Couldnt create context" << std:: endl;
}
}
void GetCommandQueue(cl_command_queue& commandQueue, cl_context& context, cl_device_id*& devices)
{
const cl_command_queue_properties queueProperties =
(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_ON_DEVICE | CL_QUEUE_ON_DEVICE_DEFAULT);
commandQueue = clCreateCommandQueue(context,devices[0],0/*properties*/,NULL);
}
bool FileParse(std::string& _Content,const std::string& filePath)
{
std::fstream* file = new std::fstream(filePath, std::ios::binary);
size_t fileSize = 0;
file->open(filePath);
if (file->is_open())
{
file->seekg(0, std::ios::end);
fileSize = file->tellg();
file->seekg(0, std::ios::beg);
}
_Content.reserve(fileSize);
_Content.insert(_Content.begin(),std::istreambuf_iterator<char>(*file),std::istreambuf_iterator<char>());
file->close();
delete file;
if (_Content.size() > 0)
return 0;
return 1;
}
/*//void CreateProgram(cl_program& program,cl_context& context,const char* filePath,const char*& SourceCode,size_t (&fileSize)[size],cl_int& status)
//{
// std::string Source;
// size_t sz = 0;
// status = FileParse(Source,filePath,sz);
// fileSize = sz;
// if (status == 1)
// {
// std::cout << "the file couldnt be read" << std::endl;
// }
// SourceCode = Source.c_str();
// fileSize = { strlen(SourceCode) };
// program = clCreateProgramWithSource(context,1,&SourceCode,fileSize,NULL);
// sz = NULL;
//}*/
template <size_t size >
void CreateProgramWithSource(const char*& filePath, std::string& SourceStr, size_t(&sourceSize)[size], cl_context& context, cl_program& program)
{
bool parsedFile = FileParse(SourceStr,filePath);
if (parsedFile == 0)
{
std::cout << "File Parsed" << std::endl;
}
const char* Source = SourceStr.c_str();
sourceSize[0] = { strlen(Source) };
cl_int status = 0;
program = clCreateProgramWithSource(context, 1, &Source, sourceSize, NULL);
}
void readfilec(const char* filePath,unsigned char* content[],unsigned long* filesize)
{
FILE* fp;
fp = fopen(filePath, "rb");
fseek(fp, 0, SEEK_END); // seek to end of file
*filesize = ftell(fp); // get current file pointer
fseek(fp, 0, SEEK_SET);
content = (unsigned char**)malloc(*filesize * sizeof(unsigned char));
unsigned char* buff = (unsigned char*)malloc(*filesize*sizeof(unsigned char));
/* checking the fread return value is not necessary but recommended */
if ((fread(*content, sizeof(unsigned char), *filesize, fp)) != *filesize)
exit(EXIT_FAILURE);
content = &buff;
fclose(fp);
//free(buffer); /* remember to free the memory */
}
void UCharToStr(std::string* str,unsigned char* charArrPtr[],size_t size)
{
str->reserve(size);
size_t index = 0; unsigned char* c = NULL; char ch = NULL;
while (size>0)
{
c = *(charArrPtr++);
auto j = *charArrPtr;
ch = *c;
*str->insert(str->begin(),ch);
size--;
}
}
int main()
{
cl_int status = 0;
size_t length = 0;
cl_platform_id platform=nullptr;
GetPlatform(platform,status);
cl_device_id* devices = nullptr;
GetDevicesIDs(devices,platform);
cl_context context;
GetContext(context,devices,platform);
cl_command_queue commandQueue;
GetCommandQueue(commandQueue,context,devices);
const char* filePath = "kernelCL.cl";
std::string sourceCode = "";
cl_program program;
size_t sourceSize[1];
CreateProgramWithSource(filePath,sourceCode,sourceSize,context,program);
status = clBuildProgram(program,1,devices,NULL,NULL,NULL);
if (status != 0)
{
std::cout << "couldnt create program"<<std::endl;
status = clGetProgramBuildInfo(program,devices[0], CL_PROGRAM_BUILD_LOG,0,NULL,&length);
size_t total = length * sizeof(char);
char* buffer = new char[total];
status = clGetProgramBuildInfo(program,devices[0],CL_PROGRAM_BUILD_LOG,length,buffer,NULL);
std::string message(buffer,total);
std::cout << message<<std::endl;
}
unsigned char *str = NULL;
std::fstream file(filePath, std::ios::binary);
file.open(filePath);
file.seekg(0,std::ios::end);
unsigned long strSize = file.tellg();
file.seekg(0, std::ios::beg);
cl_mem filePathmem = clCreateBuffer(context,CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,strlen(filePath)*sizeof(char),(void*)filePath,NULL);
cl_mem sizeFile = clCreateBuffer(context,CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,sizeof(unsigned long), (void*)strSize,NULL);
cl_mem output = clCreateBuffer(context,CL_MEM_WRITE_ONLY ,strSize*sizeof(unsigned char),(void*)str,NULL);
file.close();
strSize = 0;
cl_kernel kernel = clCreateKernel(program,"Readf",&status);
if (status != 0)
{
std::cout << "couldnt create Kernel for program, code: " << status << std::endl;
}
status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&filePathmem);
if (status != 0)
{
std::cout << "couldnt create argument for argument 0" << std::endl;
}
status = clSetKernelArg(kernel,1, sizeof(cl_mem), (void*)&output);
if (status != 0)
{
std::cout << "couldnt create argument for argument 1" << std::endl;
}
status = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&sizeFile);
if (status != 0)
{
std::cout << "couldnt create argument for argument 2" << std::endl;
}
size_t globalWorkSize[256] = {strSize};
status = clEnqueueNDRangeKernel(commandQueue,kernel,1,NULL,globalWorkSize,NULL,0,NULL,NULL);
if (status != 0)
{
std::cout << "couldnt create execute the queue 0:" << std::endl;
}
status = clEnqueueReadBuffer(commandQueue, output,1,0, strSize * sizeof(unsigned char),str,0,NULL,NULL);
if (status != 0)
{
std::cout << "couldnt output the elements for the kernel 0:" << std::endl;
}
std::cout << str << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment