Hi,
i have a Problem with OpenCL on Visual Studio 2010.
I saw an example code for Vector Addition and wanted to trie it out.
So I’ve creatad a DLL to use the Code in an other Programming Language.
But always I’ll get this Error-Message:
“CL_BUILD_PROGRAM_FAILURE”
I hope you can help me.
Oh and here are the Sourcecode’s:
main.cpp:
[codebox]#include “OpenCL for Blitz Basic-DLL.h”
extern “C” _declspec( dllexport ) int OCL_TEST();
int OCL_TEST(void)
{
// Get OpenCL platform count
cl_uint NumPlatforms;
clGetPlatformIDs(0, NULL, &NumPlatforms);
// Get all OpenCL platform IDs
cl_platform_id* PlatformIDs;
PlatformIDs = new cl_platform_id[NumPlatforms];
clGetPlatformIDs(NumPlatforms, PlatformIDs, NULL);
// Select NVIDIA platform (this example assumes it IS present)
char cBuffer[1024];
cl_uint NvPlatform;
for(cl_uint i = 0; i < NumPlatforms; ++i)
{
clGetPlatformInfo(PlatformIDs[i], CL_PLATFORM_NAME, 1024, cBuffer, NULL);
if(strstr(cBuffer, "NVIDIA") != NULL)
{
NvPlatform = i;
break;
}
}
// Get a GPU device on Platform (this example assumes one IS present)
cl_device_id cdDevice;
clGetDeviceIDs(PlatformIDs[NvPlatform], CL_DEVICE_TYPE_GPU, 1, &cdDevice, NULL);
// Create a context
cl_context hContext;
hContext = clCreateContext(0, 1, &cdDevice, NULL, NULL, NULL);
// Create a command queue for the device in the context
cl_command_queue hCmdQueue;
cl_int a;
hCmdQueue = clCreateCommandQueue(hContext, cdDevice, 0, NULL);
// Create & compile program
cl_program hProgram;
hProgram = clCreateProgramWithSource(hContext, 1, OpenCLSource, 0, 0);
a = clBuildProgram(hProgram, 0, 0, 0, 0, 0);
if(a != CL_SUCCESS)
{
return a;
}
// Create kernel instance
cl_kernel hKernel;
hKernel = clCreateKernel(hProgram, "vectorAdd", 0);
// Allocate host vectors
float * pA = new float[cnDimension];
float * pB = new float[cnDimension];
float * pC = new float[cnDimension];
// Initalize host memory (using helper C function called “randomInit”)
//randomInit(pA, cnDimension);
//randomInit(pB, cnDimension);
for(int i = 0; i < cnDimension; i++)
{
pA[i] = float(rand()/1000.0);
pB[i] = float(rand()/1000.0);
}
// Allocate device memory (and init hDeviceMemA and hDeviceMemB)
cl_mem hDeviceMemA, hDeviceMemB, hDeviceMemC;
hDeviceMemA = clCreateBuffer(hContext,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
cnDimension * sizeof(cl_float), pA, 0);
hDeviceMemB = clCreateBuffer(hContext,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
cnDimension * sizeof(cl_float), pB, 0);
hDeviceMemC = clCreateBuffer(hContext,
CL_MEM_WRITE_ONLY,
cnDimension * sizeof(cl_float), 0, 0);
// Setup parameter values
clSetKernelArg(hKernel, 0, sizeof(cl_mem), (void *)&hDeviceMemA);
clSetKernelArg(hKernel, 1, sizeof(cl_mem), (void *)&hDeviceMemB);
clSetKernelArg(hKernel, 2, sizeof(cl_mem), (void *)&hDeviceMemC);
// Launch kernel
clEnqueueNDRangeKernel(hCmdQueue, hKernel, 1, 0, &cnDimension, 0, 0, 0, 0);
// Copy result from device back to host; block until complete
clEnqueueReadBuffer(hCmdQueue, hDeviceMemC, CL_TRUE, 0,
cnDimension * sizeof(cl_float), pC, 0, 0, 0);
std::ofstream file;
file.open("G:\\file.txt");
for(int i = 0; i < cnDimension; i++)
{
file << pA[i] << " + " << pB[i] << " = " << pC[i] << std::endl;
}
file.close();
// Cleanup
delete[] pA;
delete[] pB;
delete[] pC;
delete[] PlatformIDs;
clReleaseKernel(hKernel);
clReleaseProgram(hProgram);
clReleaseMemObject(hDeviceMemA);
clReleaseMemObject(hDeviceMemB);
clReleaseMemObject(hDeviceMemC);
clReleaseCommandQueue(hCmdQueue);
clReleaseContext(hContext);
//return 0;
}[/codebox]
OpenCL for Blitz Basic-DLL.h:
[codebox]#include <Windows.h>
#include <CL\cl.h>
#include <CL\opencl.h>
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#pragma comment(lib, “user32.lib”)
// CONSTANTS
// Kernel launch configuration
const unsigned int cnBlockSize = 512;
const unsigned int cnBlocks = 3;
const unsigned int cnDimension = cnBlocks * cnBlockSize;
#include “OpenCL-Source.h”[/codebox]
OpenCL-Source.h:
[codebox]// OpenCL source Code
const char* OpenCLSource = {
"__kernel void",
"vectorAdd(__global const float * a, ",
" __global const float * b, ",
" __global float * c) ",
"{ ",
" // Vector element index ",
" int nIndex = get_global_id(0); ",
" c[nIndex] = a[nIndex] + b[nIndex];",
"} "
};[/codebox]
Thanks