8 Replies Latest reply on Dec 6, 2013 3:59 AM by himanshu.gautam

    matrix multiplication failed

    arunad

      Hello,

      I am trying to do matrix multiplication using OpenCL with c in visual studio 2010 and the host code is not reading the Kernel file.I have attached code please explain me whats the problem.

      Your help will be appreciated.

        • Re: matrix multiplication failed
          himanshu.gautam

          Hi

          sorry for the late reply.

           

          there are some mistakes in your code

          1. You allocated memory as 1D and trying to access it as 2D. thats good but you are using the expression y * Hieght + x is wrong. always it should be (y * width + x)

          2.  change fread(kernel_src,1,filelen,fp); to fread(kernel_src,filelen,1,fp);

          I think rest will be fine.

           

          try these changes and compile your code.

          • Re: matrix multiplication failed
            arvin99

            First, check if you insert the kernel code in vs 2010 project with correct way. If you already do that ant the host still can't read kernel file then try change your code:

             

            #define MAX_SOURCE_SIZE (0x100000)

             

            int main(int argc, char **argv)

            {

             

                 ///Your host code

             

                 //Load Kernel Source

                 FILE *fp;

                 const char fileName[] = "./parallel.cl";

             

                fp = fopen(fileName, "r");

               if (!fp)

               {

              fprintf(stderr, "Failed to load kernel.\n");

              exit(1);

              }

              char *source_str     = source_str = (char *)malloc(MAX_SOURCE_SIZE);

              size_t source_size  = source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);

              fclose(fp);

             

              program = clCreateProgramWithSource(context, 1, (const char **) &source_str, (const size_t *) &source_size, &err);

              err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);

             

              ///Your host code

             

               return 0;

            }

             

            Hope it works.......

              • Re: matrix multiplication failed
                arunad

                Hi arvin,

                Thank you for reply,

                now its reading the kernel file and when it compiles its displaying the value of Matrix A and Matrix B but output is wrong.

                this is the portion of the code i am referring to:

                /* Display Results */

                  printf("\n Matrix A\n");

                  for ( y=0; y< height; y++)

                  {

                 

                  for ( x=0; x< width; x++)

                  {

                 

                  printf("%.2f,",inputMatrix1[y*height+x]);

                  }

                  printf("\n");

                  }

                 

                  printf("\n Matrix B\n");

                  for ( y=0; y< height; y++)

                  {

                 

                  for ( x=0; x< width; x++)

                  {

                 

                  printf("%.2f,",inputMatrix2[y*width+x]);

                  }

                  printf("\n");

                  }

                 

                 

                 

                 

                  printf("\n Matrix A + Mattrix B\n");

                  for ( y=0; y< height; y++)

                  {

                 

                  for ( x=0; x< width; x++)

                  {

                 

                  printf("%.2f,",results[y*height+x]);

                  }

                  printf("\n");

                  }