Facebook
From Sweet Iguana, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 44
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <vector>
  4. #include <thread>
  5.  
  6. int ** A, ** B, ** BT, ** C;
  7. const int size = 5000;
  8.  
  9.  
  10. void transpose()
  11. {
  12.     for (int i=0; i < size; i++)
  13.     {
  14.         for (int j=0; j < size; j++)
  15.         {
  16.             BT[j][i] = B[i][j];
  17.         }
  18.     }
  19. }
  20.  
  21.  
  22. void multiply(int start, int stop)
  23. {
  24.     for (int i = start; i < stop; i++)
  25.     {
  26.         for (int j = start; j < stop; j++)
  27.         {
  28.             int tmp = 0;
  29.             for (int k = start; k < stop; k++)
  30.             {
  31.                 tmp += A[i][k] * B[k][j];
  32.                
  33.             }
  34.             C[i][j]=tmp;
  35.         }
  36.     }
  37. }
  38.  
  39.  
  40. void multiply_transposed(int start, int stop)
  41. {
  42.     for (int i = start; i < stop; i++)
  43.     {
  44.         for (int j = start; j < stop; j++)
  45.         {
  46.             int tmp = 0;
  47.             for (int k = start; k < stop; k++)
  48.             {
  49.                 tmp += A[i][k] * BT[j][k];
  50.                
  51.             }
  52.             C[i][j]=tmp;
  53.         }
  54.     }
  55. }
  56.  
  57. void execute(int size_th = 4, int method = 0)
  58. {
  59.     if (size_th > 1)
  60.     {
  61.         std::vector < std::thread >  threads;
  62.         int stop = 0, start = 0;
  63.  
  64.         auto start_time = std::chrono::steady_clock::now();
  65.         while (stop < size)
  66.         {
  67.             start = stop;
  68.             stop += (int) size / size_th;
  69.             std::cout << stop << std::endl;
  70.             if (method == 0)
  71.                 threads.push_back(std::thread(multiply, start, stop - 1));      
  72.             else
  73.                 threads.push_back(std::thread(multiply_transposed, start, stop - 1));
  74.         }
  75.  
  76.         for(int i=0; i < size_th; i++)
  77.         {
  78.             threads[i].join();
  79.         }
  80.         auto end_time = std::chrono::steady_clock::now();
  81.         std::chrono::duration <double> elapsed_seconds = end_time - start_time;
  82.         std::cout << "Elapsed time: " << elapsed_seconds.count() << std::endl;
  83.     }
  84.     else
  85.     {
  86.         auto start_time = std::chrono::steady_clock::now();
  87.  
  88.         if (method == 0)
  89.             multiply(0, size - 1);
  90.         else
  91.             multiply_transposed(0, size - 1);
  92.        
  93.         auto end_time = std::chrono::steady_clock::now();
  94.         std::chrono::duration <double> elapsed_seconds = end_time - start_time;
  95.         std::cout << "Elapsed time: " << elapsed_seconds.count() << std::endl;
  96.     }
  97.    
  98.    
  99. }
  100.  
  101.  
  102.  
  103. int main()
  104. {
  105.     A = new int *[size];
  106.     B = new int *[size];
  107.     BT = new int *[size];
  108.     C = new int *[size];
  109.  
  110.     for(int i=0; i < size; i++)
  111.     {
  112.         A[i] = new int[size];
  113.         B[i] = new int[size];
  114.         BT[i] = new int[size];
  115.         C[i] = new int[size];
  116.     }
  117.    
  118.     for (int i = 0; i < size; i++)
  119.     {
  120.         for (int j = 0; j < size; j++)
  121.         {
  122.             A[i][j] = rand() % 100 + 1;
  123.             B[i][j] = rand() % 100 + 1;
  124.             C[i][j] = 0;
  125.         }
  126.        
  127.     }
  128.  
  129.     transpose();
  130.  
  131.     // For normal multiply
  132.     // execute(1, 0);
  133.     // execute(2, 0);
  134.     // execute(4, 0);
  135.  
  136.     // For transposed multiply
  137.     // execute(1, 1);
  138.     // execute(2, 1);
  139.     // execute(4, 1);
  140.  
  141.     return 0;
  142. }