Advertisement
Guest User

main.c

a guest
Nov 29th, 2010
1,730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. // INCLUDES --------------------------------------------------------------------
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5. #include <time.h>
  6.  
  7. // DEFINES ---------------------------------------------------------------------
  8. #define MAXDIM 500
  9. #define STEP 1
  10. #define NUMTHREADS 8
  11. #define RUNS 10
  12.  
  13. // VARIABLES -------------------------------------------------------------------
  14. int matrix_a[MAXDIM][MAXDIM], matrix_b[MAXDIM][MAXDIM];
  15. int matrix_c[MAXDIM][MAXDIM];
  16. int dim;
  17. int threads;
  18.  
  19. // MATRIX FUNCTIONS ------------------------------------------------------------
  20. void print_matrix(int matrix[][MAXDIM]) {
  21.     for (int r = 0; r < dim; r++) {
  22.         for (int c = 0; c < dim; c++)
  23.             printf("%7d ", matrix[r][c]);
  24.         printf("\n");
  25.     }
  26.     printf("\n");
  27. }
  28.  
  29. void random_matrix(int matrix[][MAXDIM]) {
  30.     for (int r = 0; r < dim; r++)
  31.         for (int c = 0; c < dim; c++)
  32.             matrix[r][c] = rand() % 100;
  33. }
  34.  
  35.  
  36. float run(void (*f)()) {
  37.     float time = 0;
  38.    
  39.     for (int i = 0; i < RUNS; i++) {
  40.         struct timeval start, end;
  41.         gettimeofday(&start, NULL);
  42.         (*f)();
  43.         gettimeofday(&end, NULL);
  44.  
  45.         long s = start.tv_sec * 1000 + start.tv_usec / 1000;
  46.         long e = end.tv_sec * 1000 + end.tv_usec / 1000;
  47.    
  48.         time += e - s;
  49.     }
  50.     return time / RUNS;
  51. }
  52.  
  53. // SEQ MULTIPLICATION ----------------------------------------------------------
  54. void mult_seq() {
  55.     for (int r = 0; r < dim; r++) {
  56.         for (int c = 0; c < dim; c++) {
  57.             matrix_c[r][c] = 0;
  58.             for (int i = 0; i < dim; i++)
  59.                 matrix_c[r][c] += matrix_a[r][i] * matrix_b[i][c];
  60.         }
  61.     }
  62. }
  63.  
  64. // MAIN ------------------------------------------------------------------------
  65. int main(int argc, char* argv[]) {
  66.     srand(time(NULL));
  67.    
  68.     dim = 512;
  69.     random_matrix(matrix_a);
  70.     random_matrix(matrix_b);
  71.     printf("%d ", dim);
  72.     printf("%f \n", run(mult_seq));
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement