Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <iostream>
  5. #include <pthread.h>
  6.  
  7. const int N = 500;
  8. const int step = 10;
  9. int n;
  10. int A[N][N];
  11. int B[N][N];
  12. int C[N][N];
  13. double t1, t2, t3, t4;
  14. #define num_threads 8
  15.  
  16. void* task_code(void* param)
  17. {
  18.     int num, i, j;
  19.     num = (*((int*)param));
  20.  
  21.     for (i = num * n / num_threads; i < (num + 1) * n / num_threads; i++)
  22.         for (j = 0; j < n; j++)
  23.             C[i][j] = A[i][j] * B[i][j];
  24.     return NULL;
  25. }
  26.  
  27. double mtime()
  28. {
  29.     struct timeval t;
  30.     gettimeofday(&t, NULL);
  31.     double mt = (double)t.tv_usec;
  32.     return mt / 1000000;
  33. }
  34.  
  35. int main(void)
  36. {
  37.     for (int i = 0; i < N; i++) {
  38.         for (int j = 0; j < N; j++) {
  39.             A[i][j] = rand() % 1000;
  40.             B[i][j] = rand() % 1000;
  41.             C[i][j] = 0;
  42.         }
  43.     }
  44.  
  45.     for (n = step; n <= N; n += step) {
  46.         pthread_t threads[num_threads];
  47.         int thread_args[num_threads];
  48.         int rc, i;
  49.  
  50.         // ||
  51.         t3 = mtime();
  52.         for (i = 0; i < num_threads; ++i) {
  53.             thread_args[i] = i;
  54.             rc = pthread_create(&threads[i], NULL, task_code, (void*)&thread_args[i]);
  55.             assert(0 == rc);
  56.         }
  57.         for (i = 0; i < num_threads; ++i) {
  58.             rc = pthread_join(threads[i], NULL);
  59.             assert(0 == rc);
  60.         }
  61.         t4 = mtime();
  62.  
  63.         // default
  64.         t1 = mtime();
  65.         for (int i = 0; i < n; i++) {
  66.             for (int j = 0; j < n; j++) {
  67.                 C[i][j] = A[i][j] * B[i][j];
  68.             }
  69.         }
  70.         t2 = mtime();
  71.  
  72.         printf("--------------------------------------------\n");
  73.         printf("n = %d\n", n);
  74.         printf("p = %d\n", num_threads);
  75.         printf("T1 = %lf(s)\n", (t4 - t3));
  76.         printf("Tp = %lf(s)\n", (t2 - t1));
  77.         printf("S = %lf\n", (t2 - t1) / (t4 - t3));
  78.         printf("E = %lf \n", (t2 - t1) / (t4 - t3) / num_threads);
  79.         printf("t1 = %f \n", t1);
  80.         printf("t2 = %f \n", t2);
  81.         printf("t3 = %f \n", t3);
  82.         printf("t4 = %f \n", t4);
  83.     }
  84.     return EXIT_SUCCESS;
  85. }
  86.  
  87.  
  88.  
  89. #define WIN32_LEAN_AND_MEAN
  90. #include <Windows.h>
  91. #include <stdint.h> // portable: uint64_t   MSVC: __int64
  92.  
  93. // MSVC defines this in winsock2.h!?
  94. typedef struct timeval {
  95.     long tv_sec;
  96.     long tv_usec;
  97. } timeval;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement