CL_BUILD_PROGRAM_FAILURE with valid kernel code?

#1

I have started to learn OpenCL, I was thinking if it was possible to parse the file content using a opencl device (R9 Fury) but when clBuildProgram is being called the function return -11 to the status

so i decided to look into clGetProgramBuildInfo and the output is gist .github . com/PontiacGTX/624500139263e69a54b11fb708967b47

C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:1:41: error: expected ')'
__kernel void Readf(__global const char*& pathFile,__global unsigned char*& content,__global cl_ulong& charArrSize)
                                        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:1:20: note: to match this '('
__kernel void Readf(__global const char*& pathFile,__global unsigned char*& content,__global cl_ulong& charArrSize)
                   ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:1:41: error: parameter name omitted
__kernel void Readf(__global const char*& pathFile,__global unsigned char*& content,__global cl_ulong& charArrSize)
                                        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:3:6: error: expected expression
        std::fstream* file = new std::fstream(pathFile, std::ios::binary);
            ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:4:2: error: use of undeclared identifier 'file'
        file.open(pathFile);
        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:4:12: error: use of undeclared identifier 'pathFile'
        file.open(pathFile);
                  ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:5:5: error: use of undeclared identifier 'file'; did you mean 'while'?
        if(file->is_open())
           ^~~~
           while
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:5:5: error: expected expression
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:7:3: error: use of undeclared identifier 'file'
                file->seekg(0,std::ios::end);
                ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:7:17: error: use of undeclared identifier 'std'
                file->seekg(0,std::ios::end);
                              ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:8:3: error: use of undeclared identifier 'charArrSize'
                charArrSize=file->tellg();
                ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:8:15: error: use of undeclared identifier 'file'; did you mean 'while'?
                charArrSize=file->tellg();
                            ^~~~
                            while
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:8:15: error: expected expression
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:9:3: error: use of undeclared identifier 'file'
                file->seekg(0,std::ios::beg);
                ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:9:17: error: use of undeclared identifier 'std'
                file->seekg(0,std::ios::beg);
                              ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:11:3: error: use of undeclared identifier 'content'
                content = new unsigned char[charArrSize+1];
                ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:11:13: error: use of undeclared identifier 'new'
                content = new unsigned char[charArrSize+1];
                          ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:14:11: error: use of undeclared identifier 'size'
                while(i<size)
                        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:16:11: error: use of undeclared identifier 'file'; did you mean 'while'?
                        buffer=file->get();
                               ^~~~
                               while
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:16:11: error: expected expression
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:17:4: error: use of undeclared identifier 'content'
                        content[i]=(unsigned char)buffer;
                        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:19:3: error: use of undeclared identifier 'content'
                content[charArrSize]='\0';
                ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:19:11: error: use of undeclared identifier 'charArrSize'
                content[charArrSize]='\0';
                        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:21:2: error: use of undeclared identifier 'file'
        file->close();
        ^
C:\Users\PONTIA~1\AppData\Local\Temp\\OCL15568T1.cl:22:2: error: use of undeclared identifier 'delete'
        delete file;
        ^
24 errors generated.

error: Clang front-end compilation failed!
Frontend phase failed compilation.
Error: Compiling CL to IR

the cl file contains this method : http://gist.github.com/PontiacGTX/852c0dabe350b0a079522f71c43849dd

__kernel void Readf(__global const char*& pathFile,__global unsigned char*& content,__global cl_ulong& charArrSize)
{
	std::fstream* file = new std::fstream(pathFile, std::ios::binary);
	file.open(pathFile);
	if(file->is_open())
	{
		file->seekg(0,std::ios::end);
		charArrSize=file->tellg();
		file->seekg(0,std::ios::beg);

		content = new unsigned char[charArrSize+1];
		size_t i=0;
		int buffer=0;
		while(i<size)
		{
			buffer=file->get();
			content[i]=(unsigned char)buffer;
		}
		content[charArrSize]='\0';
	}
	file->close();
	delete file;
}

the code for the program: http://gist.github.com/PontiacGTX/1dcf2990b27fd2c7c33d9d9d4d36ce42

#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#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);
	
}


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 ="";
	size_t sourceSize[1];
	cl_program program;

	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);
	cl_ulong 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,2*sizeof(cl_ulong), (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;
}

I dont understand what is trying to tell me the compile(clang shows errors) but that is valid c++ syntax

#2

Looks like you didn’t post the correct link for the output. You provided the same link for it as for the method code just below it.

Also, please prefer posting the output and code snippets inline in the thread so that it makes sense to readers (now but especially in the future). Just put 3 back-ticks (```) before and after those lines, like this:

#include <stdio.h>

int main()
{
    exit(1);
}
#3

Ok Thanks I edited the Original post