d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Implementing Matrix Multiplication Serially
Add Reply New Topic New Poll
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Oct 18 2013 03:30pm
Wondering if anybody could possibly help me to understand what I'm implementing into this code that I already have.

It's suppose to be pretty basic and the instructor gave us the majority of the skeleton file but looking at C, I'm just confused.

Thanks!
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Oct 18 2013 03:35pm
Like for instance here, this might not make any sense due to not being able to see anything else. (Will pm code if needed to understand) Don't want to post all the code tho

What am I doing for the c loop? - Since this is the answer of the A * B matrix

There are 3 matrix, A B C.

Matrix Multiplication is a frequently used operation that takes two matrices A (m x q) and matrix B
(q x n) and produces matrix C (m x n), where cij is the dot product of the ith row of A with the jth
column of B.

Code
/* initialize array A and B */
for( r=0; r<n; r++ ){
for( c=0; c<n; c++ ){
A[r][c] = rand() / (double) RAND_MAX;
// B[r][c] = rand() / (double) RAND_MAX;
} // for c
// printf("%d", );
} // end for r


This post was edited by Trev on Oct 18 2013 03:37pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 18 2013 07:06pm
of all the code you can show, you picked something useless?

write the multiplication out on paper and see how it works. pay attention to the indexes you use. then it should be straight forward to write the code once you understand it.

This post was edited by carteblanche on Oct 18 2013 07:07pm
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Oct 19 2013 05:38pm
Code
/* Calculate AND time sequential matrix-multiplication results */
// ADD CODE HERE TO PERFORM C = A X B MATRIX MULTIPLICATION WITH n X n ARRAYS

time(&endTime);
seqTime = endTime-startTime;
printf("Seq time = %ld\n",seqTime);


This is the important part that you were talking about, adding this code. Wow, completely failed to read this over.

Code
The sequential code for matrix multiplication is:
for i = 0 to m-1 do
for j = 0 to n-1 do
cij = 0
for k = 0 to q-1 do
cij = cij + aik * bkj
end for k
end for j
end for i


This post was edited by Trev on Oct 19 2013 05:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 19 2013 08:39pm
Quote (Trev @ Oct 19 2013 07:38pm)
Code
/* Calculate AND time sequential matrix-multiplication results */
  // ADD CODE HERE TO PERFORM C = A X B MATRIX MULTIPLICATION WITH n X n ARRAYS

  time(&endTime);
  seqTime = endTime-startTime;
  printf("Seq time = %ld\n",seqTime);


This is the important part that you were talking about, adding this code. Wow, completely failed to read this over.

Code
The sequential code for matrix multiplication is:
for i = 0 to m-1 do
  for j = 0 to n-1 do
      cij = 0
        for k = 0 to q-1 do
            cij = cij + aik * bkj
        end for k
    end for j
end for i

looks pretty simple to code. what's the problem?
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Oct 19 2013 09:05pm
Somewhere I've made a mistake, program isn't even working. But I've come up with
Code
for (i=0; i<n; i++){
for (j=0; j<n; j++){
C[i][j] = 0;
for (k=0; k<n; j++){
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 19 2013 09:09pm
Add logging statements. see if it's what you expect it to be. I did notice you're using n as all boundaries and you ignore m and q. so everything's a square matrix of the same size?
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Oct 20 2013 04:18am
Quote
for (k=0; k<n; j++)


k loop that increments j
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 20 2013 04:45am
Quote (flyinggoat @ Oct 20 2013 03:18am)
k loop that increments j


heh, yeah infinite loop me thinks :D
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll