Error Reading Kernel Source

Hello. I am just beginning to learn OpenCL and I am going through the exercises from the Introduction_to_OpenCL_Programming (exercise files can be found here: http://developer.amd.com/zones/openclzo … -2010.aspx ). I am having trouble getting the code from the multiply matrix problem to run. It compiles, but when I run the program it gives me the message that there was an error reading file.

This is the portion of code I am referring to:

// read the kernel
	fp = fopen("multMatrix_kernel.cl","r");
	fseek(fp,0L, SEEK_END);
	filelen = ftell(fp);
	rewind(fp);

	kernel_src = (char*) malloc(sizeof(char)*(filelen+1));
	readlen = fread(kernel_src,1,filelen,fp);
	if(readlen!= filelen)
	{
	   printf("error reading file
");
	   printf(kernel_src);
	   std::cin.get();
	   exit(1);
	}

Anyway, I began printing out the kernel source and it seems there is a bunch of characters at the end.

Anyway, if anyone has run into a similar problem I would greatly appreciate any pointers you might have. I am using VS2010. Thank you for your time.

It’s possible that was corrupted and that caused the text-mode read operation to return less amount of data. Try opening the kernel file with notepad, see if it looks odd, and then save it again anyway. Notepad may automatically correct some simple issues.

Just in case, how did you print the kernel source? If you printed it with printf(), what you see at the end was simply caused because the string is not null-terminated.

Try this instead:


   // read the kernel
   const char* filename = "multMatrix_kernel.cl";
   fp = fopen(filename,"rb");
   if(!fp)
   {
       printf("Error: could not open %s for reading
", filename);
       exit(1);
   }
   fseek(fp,0L, SEEK_END);
   filelen = ftell(fp);
   rewind(fp);

   kernel_src = (char*) calloc(sizeof(char), filelen+1);
   if(!kernel_src)
   {
      printf("Error: Out of memory");
      exit(-1);
   }

   readlen = fread(kernel_src, 1, filelen, fp);
   if(readlen != filelen)
   {
      printf("Error reading file
");
      printf("%*s", readlen, kernel_src);
      std::cin.get();
      exit(1);
   }

Opening files in text mode only produces headaches in my experience.