Problem with simple Vector Manipulation

Hello I am studying IT in Germany and I want to write my Bachelor Thesis in OpenCL programming.

So I start reading about OpenCl and my first Program should a matrix inverter.

So this is my kernel:

__kernel void invertieren(__global double matrix, __global const int dim)
{
int i = get_global_id(0);
matrix[i] = 1.0
dim;
}

Because first thing i wanna do was to get access to the matrix.

This is my host program:

#include <CL/cl.h>

#define DIM 2
#define MAX_SOURCE_SIZE (0x100000)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
/* Variablen anlegen */
cl_platform_id platform_id = NULL;
cl_uint ret_num_platforms;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_mem memobj = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_int ret;

FILE *file;
char filename[] = "./kernel.cl";
char *source_str;
size_t source_size;
int dim = DIM;
double matrix[DIM*DIM];
int i;

/* lese den Kernel ein */
if( !(file = fopen(filename,"r")))
{
	fprintf(stderr,"Kernel konnte nicht geladen werden.\n");
	getchar();
	exit(1);
}
source_str = (char *) malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str,1,MAX_SOURCE_SIZE,file);

/* datei schließen */
fclose(file);

/* Eingabewerte setzen und ausgeben */
for(i=0;i<dim*dim;++i)
	matrix[i]=i*1.0;
for(i=0;i<dim*dim;++i)
{
	printf("[%2lf]",matrix[i]);
	if((i+1)%DIM==0)
		printf("\n");
}
getchar();

/* hole Liste der Plattformen */
clGetPlatformIDs(1,&platform_id,&ret_num_platforms);

/* wähle GPU Gerät */
clGetDeviceIDs(platform_id,CL_DEVICE_TYPE_DEFAULT,1,&device_id,&ret_num_devices);

/* erzeuge Kontext */
context = clCreateContext(NULL,1,&device_id,NULL,NULL,&ret);

/* Erzeuge Anweisungs-Warteschlange */
command_queue = clCreateCommandQueue(context,device_id,0,&ret);

/* Erzeuge Speicherobjekt */
memobj = clCreateBuffer(context,CL_MEM_READ_WRITE,sizeof(cl_mem),NULL,&ret);

/* schreibe die Daten auf den Device-Speicher */
clEnqueueWriteBuffer(command_queue,memobj,CL_TRUE,0,dim*dim*sizeof(double),matrix,0,NULL,NULL);
	
/* Erzeuge Programmobjekt */
program = clCreateProgramWithSource(context,1,(const char **)&source_str,(const size_t *)&source_size,&ret);

/* Kompilieren */
clBuildProgram(program,1,&device_id,NULL,NULL,NULL);

/* erzeuge kernel objekt */
kernel = clCreateKernel(program,"invert",&ret);

/* setze die Kernel Argumente */
clSetKernelArg(kernel,0,sizeof(cl_mem),(void *) &memobj);
clSetKernelArg(kernel,1,sizeof(int),(void *) &dim);

/* führe den Kernel aus */
clEnqueueTask(command_queue,kernel,0,NULL,NULL);

/* aus dem Speicherobjekt lesen */
clEnqueueReadBuffer(command_queue,memobj,CL_TRUE,0,dim*dim*sizeof(double),(void *) matrix,0,NULL,NULL);

/* Objekte freigeben */
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseMemObject(memobj);
clReleaseCommandQueue(command_queue);
clReleaseContext(context);

free(source_str);

/* Ergebnis ausgeben */
for(i=0;i<dim*dim;++i)
{
	printf("[%2lf]",matrix[i]);
	if((i+1)%DIM==0)
		printf("\n");
}
getchar();


return 0;

}

So as far I can see it I did it all like in teh examples you can find on WWW.

But it does not have an effect on my matrix. The data of the matrix after the calculation
ist the same as before the very calculation.

Can somebody tell me if I overlooked something.

I would appreciate this.

A bit harder to follow with all the comments and German, but I noticed two things I want to point out. The first is that in the kernel code, the kernel is named “invertieren”, but your host code creates a kernel named “invert”. Second, you use clEnqueueTask to launch the kernel, when the kernel source indicates that you should use clEnqueueNDRange.

Is there a second kernel that you didn’t post?

As Martin has already pointed out, you’ve called your kernel “invertieren” in the OpenCL source file, but call it “invert” in clCreateKernel, which will fail, but as you don’t check any return codes for errors, you’ll never know. Also, I’m not sure if it is valid to qualify the parameter “dim” with __global, as “dim” is not a pointer, so your program might already fail in clBuildProgram.