Home > Enterprise >  Why is my matrix multiplication code not working?
Why is my matrix multiplication code not working?

Time:05-05

I am new to C and I have written a C OpenMp Matrix Multiplication code that multiplies two 1000x1000 matrices. So far its not running and I am having a hard time finding out where the bugs are. I tried to figure it out for a few days but I'm stuck.

Here is my code:

#include <iostream>
#include <time.h>
#include <omp.h>

using namespace std;
int N;

void Multiply()
 {
    //initialize matrices with random numbers
    //#pragma omp for 
    int aMatrix[N][N], i, j;
  for( i = 0; i < N;   i)
  {for( j = 0;  j < N;   j)
     {aMatrix[i][j] = rand();}
  }
    int bMatrix[N][N], i1, j2;
  for( i1 = 0; i1 < N;   i1)
  {for( j2 = 0;  j2 < N;   j2)
     {bMatrix[i1][j2] = rand();}
  }
  //Result Matrix
    int product[N][N]  = {0};

    //Transpose Matrix;
    int BTransposed[j][i];
    BTransposed[j][i] = bMatrix[i1][j2];

    for (int row = 0; row < N; row  ) {
        for (int col = 0; col < N; col  ) {
            // Multiply the row of A by the column of B to get the row, column of product.
            for (int inner = 0; inner < N; inner  ) {
                product[row][col]  = aMatrix[row][inner] * BTransposed[col][inner];
            }

        }
    
    }
}

int main() {
    
   time_t begin, end;
    time(&begin);

    Multiply();

  time(&end);
  time_t elapsed = end - begin;

  cout << ("Time measured: ") << endl;
    cout << elapsed << endl;

    return 0;

}```

CodePudding user response:

You created a matrix

 int BTransposed[j][i];
    BTransposed[j][i] = bMatrix[i1][j2];

that has the size j x i and than u make the element at [j][i] equal to the element in bMatrix[i1][j2], you should have an error since u cant accses the index j and i since it goes from 0 to j-1 and i-1

CodePudding user response:

The transposed matrix (BTransposed) is not correctly constructed. You can solve this in the following ways:

First Option: use a for loop to create the correct BTransposed matrix.

for (int i = 0; i != N; i  )
  for (int j = 0; j != N; j  )
    BTransposed[i][j] = bMatrix[j][i]

Second Option (better one): completely delete BTransposed matrix. when needed just use the original bMatrix with indexes i,j exchanged! for example instead of BTransposed[col][inner] you can use BMatrix[inner][col].

CodePudding user response:

There's no need to explicitly create transposed matrices, you can calculate the product via appropriate index calculations directly; actually you can even handle non-square matrices that way, e.g.

template <size_t X, size_t Y>
struct Matrix // minimalistic matrix type only for illustration...
{
    int data[X][Y];
};

template<size_t I, size_t K, size_t J>
Matrix<I, J> operator*(Matrix<I, K> const& x, Matrix<K, J>const& y)
{
    Matrix<I, J> r; // decltype(x*y) r; to avoid code repetition...
    for(size_t i = 0; i < I;   i)
    {
        for(size_t j = 0; j < J;   j)
        {
            r.data[i][j] = 0;
            for(size_t k = 0; k < K;   k)
            {
                // appropriate index access:
                r.data[i][j]  = x.data[i][k] * y.data[k][j];
            }
        }
    }
    return r;
}

It's not an efficient algorithm, though, there are better ones. Strassen is among the most implemented ones, good overall runtime bounds (but not the best) while still not too complex to implement...

  • Related