Computer Organization and Design assignements
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 3.0 KiB

4 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. \file main.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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include <sys/time.h>
  14. #include <assert.h>
  15. #include "matmul.h"
  16. extern double sqrt (double);
  17. int cmpfunc (const void * a, const void * b) {
  18. double v =*(double*)a - *(double*)b;
  19. return (v < 0) ? -1 : (v > 0) ? 1 : 0;
  20. }
  21. double median (double* t, size_t n) {
  22. qsort ((void*)t, n, sizeof(t[0]), cmpfunc);
  23. return (n % 2) ? t[n/2] : (t[n/2] + t[n/2 -1]) /2;
  24. }
  25. double std_deviation (double* t, size_t n) {
  26. double av =0;
  27. for (size_t i=0 ; i<n ; ++i) {
  28. av += t[i];
  29. }
  30. av /= n;
  31. double s =0;
  32. for (size_t i=0 ; i<n ; ++i) {
  33. s += (t[i]-av)*(t[i]-av);
  34. }
  35. return sqrt (s/n);
  36. }
  37. mMult_ft multSelect (char* order) {
  38. if (! strcmp ((const char*)order, "ijk")) return matrixMult_ijk;
  39. else if (! strcmp ((const char*)order, "ikj")) return matrixMult_ikj;
  40. else if (! strcmp ((const char*)order, "jik")) return matrixMult_jik;
  41. else if (! strcmp ((const char*)order, "jki")) return matrixMult_jki;
  42. else if (! strcmp ((const char*)order, "kij")) return matrixMult_kij;
  43. else if (! strcmp ((const char*)order, "kji")) return matrixMult_kji;
  44. else return matrixMult_ijk;
  45. }
  46. /*!
  47. * A unit testing like main function to profile our code
  48. */
  49. int main(int argc, char **argv) {
  50. struct timeval start, end; /* time structs */
  51. double time[MAX_ITER] = {0.0}; /* execution time array in ms */
  52. float *A, *B, *C; /* matrix declarations */
  53. /* read matrix size (or use default) */
  54. if (argc != 3){
  55. fprintf( stderr,
  56. "Usage:\n"
  57. "%s n order, where \n"
  58. " n: is the matrix size.\n"
  59. " order: the loop order ex: ijk , jik, ...\n",
  60. argv[0]);
  61. exit(1);
  62. }
  63. int n = atoi( argv[1] );
  64. mMult_ft mMult = multSelect(argv[2]);
  65. /* initialize matrices */
  66. A = matrixInit( n );
  67. B = matrixInit( n );
  68. C = (float *) malloc( n*n*sizeof(float) );
  69. /* compute matrix multiplication */
  70. for (int it = 0; it < MAX_ITER; it++) {
  71. gettimeofday(&start, NULL);
  72. mMult( C, A, B, n );
  73. gettimeofday(&end, NULL);
  74. time[it] = (end.tv_sec - start.tv_sec) * 1000.0 + /* sec to ms */
  75. (end.tv_usec - start.tv_usec) / 1000.0; /* us to ms */
  76. printf("Iter: %d Time: %f ms\n", it, time[it]);
  77. }
  78. /* we need to use the result -- verify it */
  79. for (int i = 0; i < n; i++) { /* rows */
  80. for (int j = 0; j < n; j++) { /* cols */
  81. float gold = 0;
  82. for (int k = 0; k < n; k++) { /* accumulate products */
  83. gold += A[ sub2ind(i,k,n) ] * B[ sub2ind(k,j,n) ];
  84. }
  85. assert( (gold - C[sub2ind(i,j,n)]) < 1e-3 );
  86. }
  87. }
  88. // statistical data
  89. printf ("Median: %f [msec]\n", median (time, MAX_ITER));
  90. printf ("Std.Dev: %f [msec]\n", std_deviation(time, MAX_ITER));
  91. }