Computer Organization and Design assignements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

155 lines
4.7 KiB

  1. /*!
  2. \file matmul.c
  3. \brief Matrix multiplication implementation.
  4. \author Nikos Pitsianis
  5. \author Dimitris Floros
  6. \author Christos Choutouridis 8997 <cchoutou@ece.auth.gr>
  7. \date 2020-05-05
  8. */
  9. #include "matmul.h"
  10. /*!
  11. * Square Matrix multiplication - ijk
  12. * \param C pointer to output matrix
  13. * \param A pointer to input matrix A
  14. * \param B pointer to input matrix B
  15. * \param n Size of matrices (both sizes)
  16. * \return none
  17. */
  18. void matrixMult_ijk(float * const C, float const * const A, float const * const B, int const n) {
  19. for (int i = 0; i < n; ++i)
  20. for (int j = 0; j < n; ++j) {
  21. int k =0;
  22. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  23. for (k = 1; k < n; ++k)
  24. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  25. }
  26. }
  27. /*!
  28. * Square Matrix multiplication - ikj
  29. * \param C pointer to output matrix
  30. * \param A pointer to input matrix A
  31. * \param B pointer to input matrix B
  32. * \param n Size of matrices (both sizes)
  33. * \return none
  34. */
  35. void matrixMult_ikj(float * const C, float const * const A, float const * const B, int const n) {
  36. for (int i = 0; i < n; ++i)
  37. for (int k = 0; k < n; ++k) {
  38. if (!k) {
  39. for (int j = 0; j < n; ++j)
  40. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  41. } else {
  42. for (int j = 0; j < n; ++j)
  43. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  44. }
  45. }
  46. }
  47. /*!
  48. * Square Matrix multiplication - jik
  49. * \param C pointer to output matrix
  50. * \param A pointer to input matrix A
  51. * \param B pointer to input matrix B
  52. * \param n Size of matrices (both sizes)
  53. * \return none
  54. */
  55. void matrixMult_jik(float * const C, float const * const A, float const * const B, int const n) {
  56. for (int j = 0; j < n; j++)
  57. for (int i = 0; i < n; i++) {
  58. int k =0;
  59. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  60. for (k = 1; k < n; k++)
  61. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  62. }
  63. }
  64. /*!
  65. * Square Matrix multiplication - jki
  66. * \param C pointer to output matrix
  67. * \param A pointer to input matrix A
  68. * \param B pointer to input matrix B
  69. * \param n Size of matrices (both sizes)
  70. * \return none
  71. */
  72. void matrixMult_jki(float * const C, float const * const A, float const * const B, int const n) {
  73. for (int j = 0; j < n; ++j)
  74. for (int k = 0; k < n; ++k) {
  75. if (!k) {
  76. for (int i = 0; i < n; ++i)
  77. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  78. } else {
  79. for (int i = 0; i < n; ++i)
  80. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  81. }
  82. }
  83. }
  84. /*!
  85. * Square Matrix multiplication - kij
  86. * \param C pointer to output matrix
  87. * \param A pointer to input matrix A
  88. * \param B pointer to input matrix B
  89. * \param n Size of matrices (both sizes)
  90. * \return none
  91. */
  92. void matrixMult_kij(float * const C, float const * const A, float const * const B, int const n) {
  93. for (int k = 0; k < n; ++k) {
  94. if (!k) {
  95. for (int i = 0; i < n; ++i)
  96. for (int j = 0; j < n; ++j)
  97. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  98. } else {
  99. for (int i = 0; i < n; ++i)
  100. for (int j = 0; j < n; ++j)
  101. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  102. }
  103. }
  104. }
  105. /*!
  106. * Square Matrix multiplication - kji
  107. * \param C pointer to output matrix
  108. * \param A pointer to input matrix A
  109. * \param B pointer to input matrix B
  110. * \param n Size of matrices (both sizes)
  111. * \return none
  112. * xxx
  113. */
  114. void matrixMult_kji(float * const C, float const * const A, float const * const B, int const n) {
  115. for (int k = 0; k < n; ++k) {
  116. if (!k) {
  117. for (int j = 0; j < n; ++j)
  118. for (int i = 0; i < n; ++i)
  119. C[ sub2ind(i,j,n) ] = A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  120. } else {
  121. for (int j = 0; j < n; ++j)
  122. for (int i = 0; i < n; ++i)
  123. C[ sub2ind(i,j,n) ] += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  124. }
  125. }
  126. }
  127. /*!
  128. * Initialize matrix with random indices and return the matrix pointer.
  129. *
  130. * \param n The size of the matrix (both of them)
  131. * \return Pointer to allocated and initialized matrix
  132. */
  133. float* matrixInit(int const n) {
  134. float *M = (float *) malloc( n*n*sizeof(float) );
  135. for (int i = 0; i < n; i++) /* rows */
  136. for (int j = 0; j < n; j++) /* cols */
  137. M[ sub2ind(i,j,n) ] = (float)rand()/(float)(RAND_MAX);
  138. return M;
  139. }