Advertisement
Guest User

Matrix multiplication

a guest
Sep 11th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1.  
  2. /**
  3. matrix multiplication
  4. */
  5. #include <omp.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #ifndef DEBUG
  10. #define printf(fmt,...) (0)
  11. #endif
  12.  
  13.  
  14. #ifndef MROW
  15. #define MROW 300
  16. #endif
  17.  
  18. #ifndef MCOL
  19. #define MCOL 300
  20. #endif
  21.  
  22. int A[MROW][MCOL];
  23. int B[MROW][MCOL];
  24. int C[MROW][MCOL];
  25.  
  26. void printMatrix(int m[MROW][MCOL])
  27. {
  28. printf("\n");
  29. for (int i = 0; i < MROW; ++i)
  30. {
  31. printf("|");
  32. for (int j = 0; j < MCOL; ++j)
  33. {
  34. printf("%d|", m[i][j] );
  35. }
  36. printf("\n");
  37. }
  38. }
  39.  
  40.  
  41. void fillMatrix(int matrix[MROW][MCOL])
  42. {
  43.  
  44. for(int i=0; i<MROW; i++)
  45. {
  46. for (int j = 0; j < MCOL; j++)
  47. {
  48. matrix[i][j]= rand()%(MCOL);
  49. matrix[i][j]= rand()%(MROW);
  50. }
  51.  
  52. }
  53. }
  54.  
  55. int main(int argc, char const *argv[])
  56. {
  57. printf("Starting with %d,%d\n",MROW,MCOL);
  58. fillMatrix(A);
  59. fillMatrix(B);
  60. printf("Starting multiplication\n");
  61. #pragma omp parallel for schedule(dynamic) collapse(2)
  62. for (int i = 0; i < MROW; i++)
  63. {
  64. for (int j = 0; j < MCOL; j++)
  65. {
  66. int cellResult=0;
  67. int k,h;
  68. for (k=0; k<MCOL; k++)
  69. {
  70. for(h=0; h < MROW; h++)
  71. {
  72. cellResult = cellResult + (A[i][k] * B[h][j]) ;
  73. }
  74. }
  75. C[i][j] = cellResult;
  76. }
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement