C++ OpenCL - no instance of Constructor ?

F

Furtano

Gast
Hallo,

ich bekomme folgenden Fehler in Visual Studio 2012 angezeigt, versteh nicht woran es liegt :(.

buffer2jdqx.png

Danke und LG,
Furtano

PHP:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

#ifdef __APPLE__
	#include "OpenCL/opencl.h"
#else
	#include "CL/cl.hpp"
#endif

const int LIST_SIZE = 1000;


std::string GetPlatformName (cl_platform_id id)
{
	size_t size = 0;
	clGetPlatformInfo (id, CL_PLATFORM_NAME, 0, nullptr, &size);

	std::string result;
	result.resize (size);
	clGetPlatformInfo (id, CL_PLATFORM_NAME, size,
		const_cast<char*> (result.data ()), nullptr);

	return result;
}

std::string GetDeviceName (cl_device_id id)
{
	size_t size = 0;
	clGetDeviceInfo (id, CL_DEVICE_NAME, 0, nullptr, &size);

	std::string result;
	result.resize (size);
	clGetDeviceInfo (id, CL_DEVICE_NAME, size,
		const_cast<char*> (result.data ()), nullptr);

	return result;
}

void CheckError (cl_int error)
{
	if (error != CL_SUCCESS) {
		std::cerr << "OpenCL call failed with error " << error << std::endl;
		std::exit (1);
	}
}

std::string LoadKernel (const char* name)
{
	std::ifstream in (name);
	std::string result (
		(std::istreambuf_iterator<char> (in)),
		std::istreambuf_iterator<char> ());
	return result;
}

cl_program CreateProgram (const std::string& source,
	cl_context context)
{
	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateProgramWithSource.html
	size_t lengths [1] = { source.size () };
	const char* sources [1] = { source.data () };

	cl_int error = 0;
	cl_program program = clCreateProgramWithSource (context, 1, sources, lengths, &error);
	CheckError (error);

	return program;
}

int main ()
{


	/**
	GET DEVICE

	**/
	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html
	cl_uint platformIdCount = 0;
	clGetPlatformIDs (0, nullptr, &platformIdCount);

	if (platformIdCount == 0) {
		std::cerr << "No OpenCL platform found" << std::endl;
		return 1;
	} else {
		std::cout << "Found " << platformIdCount << " platform(s)" << std::endl;
	}

	std::vector<cl_platform_id> platformIds (platformIdCount);
	clGetPlatformIDs (platformIdCount, platformIds.data (), nullptr);

	for (cl_uint i = 0; i < platformIdCount; ++i) {
		std::cout << "\t (" << (i+1) << ") : " << GetPlatformName (platformIds [i]) << std::endl;
	}



	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceIDs.html
	cl_uint deviceIdCount = 0;
	clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, 0, nullptr,
		&deviceIdCount);

	if (deviceIdCount == 0) {
		std::cerr << "No OpenCL devices found" << std::endl;
		return 1;
	} else {
		std::cout << "Found " << deviceIdCount << " device(s)" << std::endl;
	}

	std::vector<cl_device_id> deviceIds (deviceIdCount);
	clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, deviceIdCount,
		deviceIds.data (), nullptr);

	for (cl_uint i = 0; i < deviceIdCount; ++i) {
		std::cout << "\t (" << (i+1) << ") : " << GetDeviceName (deviceIds [i]) << std::endl;
	}













	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html
	const cl_context_properties contextProperties [] =
	{
		CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties> (platformIds [0]),
		0, 0
	};

	cl_int error = CL_SUCCESS;
	cl_context context = clCreateContext (contextProperties, deviceIdCount,
		deviceIds.data (), nullptr, nullptr, &error);
	CheckError (error);

	std::cout << "Context created" << std::endl;
		
	int a;
	std::cin >> a;


	unsigned int n = 1000;





	double *h_a;
	double *h_b;
	double *h_c;

	// Device input buffers
    cl::Buffer d_a;
    cl::Buffer d_b;
    // Device output buffer
    cl::Buffer d_c;



	h_a = new double[n];
    h_b = new double[n];
    h_c = new double[n];

	 // Size, in bytes, of each vector
    size_t bytes = n*sizeof(double);



	 // Initialize vectors on host
    for(int i = 0; i < n; i++ )
    {
        h_a[i] = sinf(i)*sinf(i);
        h_b[i] = cosf(i)*cosf(i);
    }
    

	// Create a program from source
	cl_program program = CreateProgram (LoadKernel ("kernels/image.cl"),
		context);

	CheckError (clBuildProgram (program, deviceIdCount, deviceIds.data (), 
		"-D FILTER_SIZE=1", nullptr, nullptr));

	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateKernel.html
	cl_kernel kernel = clCreateKernel (program, "vec_add", &error);
	CheckError (error);


	// Create a buffer for the filter weights
	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateBuffer.html
	cl_mem filterWeightsBuffer = clCreateBuffer (context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
		sizeof (float) * 9, a, &error);
	CheckError (error);

	// Setup the kernel arguments
	clSetKernelArg (kernel, 0, sizeof (cl_mem), &d_a);
	clSetKernelArg (kernel, 1, sizeof (cl_mem),  &d_b);
	clSetKernelArg (kernel, 2, sizeof (cl_mem),  &d_c);
	
	// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateCommandQueue.html
	cl_command_queue queue = clCreateCommandQueue (context, deviceIds [0],
		0, &error);
	CheckError (error);

	cl::Buffer bufferA = cl::Buffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int));
	cl::Buffer bufferB = cl::Buffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int));
	cl::Buffer bufferC = cl::Buffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int));


}
 
Du hast keinen gültigen Konstruktortypen aufgerufen. Vermutung: Das dritte Argument ist vom Typ integer. Muss aber unsigned integer sein. Also hier solltest du eine Konvertierung einfügen, oder LIST_SIZE direkt als const unsigned int definieren
 
Benötigt man für die Buffererstellung in OpenCL nicht auch immer noch einen Pointer für Bufferinhalt (NULL falls nicht gebraucht) und nen Error als Funktionsargument (wie du es bereits bei deinen oberen Buffern gemacht hast)? Ich weiß leider gerade nicht, was für konstrukturen die hpp Bindung von OpenCL für die Buffererstellung so unterstützt.

P.S. Die Cpp Bindung von OpenCL ist ein Graus, würde eher die reinen C Funktionen verwenden.
 
Zuletzt bearbeitet:
LIST_SIZE als const usigned int definieren bringt leider nix :(

würds schon gern in C++ haben, wie säh es in c aus?
 
Hast du doch schon stehen dort? Dein übriges Zeug verwendet ja alles die C-Bindung . . . . .
Code:
cl_mem filterWeightsBuffer = clCreateBuffer (context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof (float) * 9, a, &error);
 
Hmm, schon seltsam. Laut der Referenz für die Buffer-Klasse ...

cl::Buffer::Buffer ( const Context & context,
cl_mem_flags flags,
::size_t size,
void * host_ptr = NULL,
cl_int * err = NULL
) [inline]

Create a buffer object.

Parameters:
context is a valid OpenCL context used to create the buffer object.
flags is a bit-field that is used to specify allocation and usage information such as the memory arena that should be used to allocate the buffer object and how it will be used.
size is the size in bytes of the buffer memory object to be allocated.
host_ptr is a pointer to the buffer data that may already be allocated by the application. The size of the buffer that host_ptr points to must be >= size bytes. Passing in a pointer to an already allocated buffer on the host and using it as a buffer object allows applications to share data efficiently with kernels and the host.
err will return an appropriate error code. If err is NULL, no error code is returned.

Returns:
A valid non-zero buffer object and err is set to CL_SUCCESS if the buffer object is created successfully or a NULL value with one of the following error values returned in err:

CL_INVALID_CONTEXT if context is not a valid context.
CL_INVALID_VALUE if values specified in flags are not valid.
CL_INVALID_BUFFER_SIZE if size is 0 or is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE value.
CL_INVALID_HOST_PTR if host_ptr is NULL and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags or if host_ptr is not NULL but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags.
CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for buffer object.
CL_INVALID_OPERATION if the buffer object cannot be created for all devices in context.
CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the runtime.

Note:
In the case that exceptions are enabled and error value other than CL_SUCCESS is generated, then cl::Error exception is generated.


erwartet der Konstruktor als 2. Argument ein Bitfeld. Bitfelder sollen eigentlich immer unsigned sein; deine Compilerfehlermeldung zeigt als 2. Argument aber int (also signed) an. Aber selbst dann sollte eigentlich implizit eine Typkonvertierung erfolgen. Verstehe ich nicht ...
Ergänzung ()

Ist das auch wirklich die ERSTE Fehlermeldung, die der Compiler ausspuckt?
 
Der erste Parameter in cl::Buffer muss vom Typ cl::Context sein. Du benutzt aber die c-Entsprechung cl_context. Ausserdem müsste/sollte in filterWeightsBuffer der vorletzte Parameter ein Zeiger sein (OpenCL 1.2).
 

Ähnliche Themen

F
2
Antworten
21
Aufrufe
2.978
F
Zurück
Oben