1 Reply Latest reply on May 7, 2009 11:01 AM by gaurav.garg

    Merging two (MxN-1) matrices to one (MxN) matrix

    Peterp

      Hi.

      i've two (MxN-1) matrices and i want to merge them to one matrix For Example

      matrix A: 0 1       matrix B: 1 2       result matrix:  0 2 2

                     3 4                       4 5                             3 8 5

                     6 7                      7  8                             6 14 8

      The first column of the result matrix is the first column of the matrix A and the last column of the result matrix ist the last column of the matrix B and and the columns in the middle are the sum of both A and B.  Here is my kernel:

      kernel void merge(int dim1, float matrix1[][], float matrix2[][], out float res_matrix[][])

      {

      int2 index = instance().xy;

      if(index.x == 0)

      {

      res_matrix[index.y][index.x] = matrix1[index.y][index.x+1];

      }

      else if(index.x+1 == (dim1-1))

      {

      res_matrix[index.y][index.x+1] = matrix2[index.y][index.x+1];

      }

      else

      {

      res_matrix[index.y][index.x] = matrix1[index.y][index.x]+matrix2[index.y][index.x];

      }

      }

      Here is my C++ Code:

      const int rank = 2;

      unsigned int dim[] = { MAX, MAX-1 };

      ::brook::Stream<float>m1(rank,dim);

      m1.read(matrix2);

      ::brook::Stream<float>m2(rank,dim);

      m2.read(matrix3);

      unsigned int dimF[] = { MAX, MAX };

      ::brook::Stream<float>m3(rank,dimF);

      merge(MAX,m1,m2,m3);

      But i get the wrong results. But i'm doing wrong? 

        • Merging two (MxN-1) matrices to one (MxN) matrix
          gaurav.garg

          The number of kernel instance is going to be same as output dimensions. Also, I would suggest you to avoid use of scatter output, instaed use regular output stream. Scatter has some performace overhead. So, you need to make the folloing change-

          kernel void merge(int dim1, float matrix1[][], float matrix2[][], out float res_matrix<> // use regular output stream)

          {

          int2 index = instance().xy;

          if(index.x == 0)

          {

          res_matrix = matrix1[index.y][index.x];

          }

          else if(index.x == (dim1-1))

          {

          res_matrix = matrix2[index.y][index.x-1];

          }

          else

          {

          res_matrix = matrix1[index.y][index.x]+matrix2[index.y][index.x-1];

          }

          }

          ===========================

          Also, the dimension array while declaring stream requires width to be the first member of array. You should declare input stream like this -

          unsigned int dim[] = { MAX-1, MAX }; //{width, height}

          ::brook::Stream<float>m1(rank,dim);

           

          Let me know if it helps.