Advertisement
Thiff

ThreadsFillingSorting_notmerged

Nov 13th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <sys/time.h>
  6. #include <sys/param.h>
  7. #include <pthread.h>
  8. #include <math.h>
  9. #define TYPE float
  10.  
  11. using namespace std;
  12.  
  13. const int count_Threadfill = 2;
  14. const int count_Threadsort = 2;
  15. const int my_length = 10000;
  16.  
  17.  
  18. struct thread_argument
  19. {
  20.     int id;
  21.     int from, to;
  22.     TYPE *data;
  23. };
  24.  
  25. void mergeArrays(TYPE *arr1, TYPE *arr2, int start1,int end1, int start2, int end2, TYPE *arr3)
  26. {
  27.     int i = start1, j = start2, k = 0;
  28.     while (i<end1 && j<end2)
  29.     {
  30.         if (arr1[i] < arr2[j])
  31.             arr3[k++] = arr1[i++];
  32.         else
  33.             arr3[k++] = arr2[j++];
  34.     }
  35.  
  36.     while (i < end1)
  37.         arr3[k++] = arr1[i++];
  38.  
  39.     while (j < end2)
  40.         arr3[k++] = arr2[j++];
  41. }
  42.  
  43. int timeval_to_ms( timeval *before, timeval *after )
  44. {
  45.     timeval res;
  46.     timersub( after, before, &res );
  47.     return 1000 * res.tv_sec + res.tv_usec / 1000;
  48. }
  49. float randomFloat(float minR,float maxR){
  50.     return (minR + 1) + (((float) rand()) / (float) RAND_MAX) * (maxR - (minR + 1));
  51.  
  52. }
  53.  
  54. int randomInt(int minR, int maxR){
  55.     return rand()%(maxR-minR + 1) + minR;
  56.  
  57. }
  58. void BubbleSort_dsc(TYPE *arr, int start, int end){
  59.     TYPE tmp;
  60.     for(int i = start; i < end; i++)
  61.     {
  62.         for(int j = start; j < end - 1; j++)
  63.         {
  64.             if(arr[j+1] > arr[j])
  65.             {
  66.                 tmp = arr[j + 1];
  67.                 arr[j + 1] = arr[j];
  68.                 arr[j] = tmp;
  69.             }
  70.         }
  71.     }
  72.  
  73. }
  74.  
  75. void BubbleSort_asc(TYPE *arr, int start, int end){
  76.     TYPE tmp;
  77.     for(int i = start; i < end; i++)
  78.     {
  79.         for(int j = start; j < end - 1; j++)
  80.         {
  81.             if(arr[j+1] < arr[j])
  82.             {
  83.                 tmp = arr[j + 1];
  84.                 arr[j + 1] = arr[j];
  85.                 arr[j] = tmp;
  86.             }
  87.         }
  88.     }
  89.  
  90. }
  91. void fill_array(TYPE *arr, int start, int end, int minR, int maxR)
  92. {
  93.     TYPE temp;
  94.     for (int i = start; i < end; i++)
  95.     {
  96.         temp = randomFloat(minR,maxR);
  97.         arr[i] = roundf(temp * 100) / 100.0;
  98.         //arr[i] = randomInt(minR, maxR);
  99.  
  100.     }
  101. }
  102.  
  103. void *thread_filling( void *void_arg )
  104. {   int minR = -100.00;
  105.     int maxR = 100.00;
  106.     thread_argument *ptr_data = (thread_argument*) void_arg;
  107.     fill_array(ptr_data->data, ptr_data->from, ptr_data->to,minR,maxR);
  108.     return NULL;
  109. }
  110. void *thread_sorting( void *void_arg )
  111. {
  112.     thread_argument *ptr_data = (thread_argument*) void_arg;
  113.     BubbleSort_asc(ptr_data->data, ptr_data->from, ptr_data->to);
  114.     return NULL;
  115. }
  116.  
  117. int main()
  118. {
  119.     srand( ( int ) time( NULL ) );
  120.     TYPE *my_array = new TYPE [my_length];
  121.  
  122.     pthread_t ptfill[count_Threadfill];
  123.     pthread_t ptsort[count_Threadsort];
  124.     thread_argument tafill[count_Threadfill];
  125.     thread_argument tasort[count_Threadsort];
  126.     timeval time_before,time_after;
  127.     timeval t1,t2;
  128.     int minR = -100;
  129.     int maxR = 100;
  130.  
  131.     for (int i = 0; i < count_Threadfill; i++)
  132.     {
  133.         tafill[i].id = i;
  134.         tafill[i].from = i * (my_length/count_Threadfill);
  135.         tafill[i].to = tafill[i].from + (my_length/count_Threadfill);
  136.         tafill[i].data = my_array;
  137.     }
  138.     for (int i = 0; i < count_Threadsort; i++)
  139.     {
  140.         tasort[i].id = i;
  141.         tasort[i].from = i * (my_length/count_Threadsort);
  142.         tasort[i].to = tasort[i].from + (my_length/count_Threadsort);
  143.         tasort[i].data = my_array;
  144.     }
  145.     gettimeofday(&t1,NULL);
  146.     for (int i = 0; i < count_Threadfill; i++)
  147.      {
  148.         pthread_create( &ptfill[i], NULL, thread_filling, &tafill[i] );
  149.      }
  150.     for (int i = 0; i < count_Threadfill; i++)
  151.     {
  152.         pthread_join(ptfill[i], NULL);
  153.     }
  154.     gettimeofday(&t2,NULL);
  155.     gettimeofday(&time_before,NULL);
  156.  
  157.     for (int i = 0; i < count_Threadsort; i++)
  158.     {
  159.         pthread_create( &ptsort[i], NULL, thread_sorting, &tasort[i] );
  160.     }
  161.     for (int i = 0; i < count_Threadsort; i++)
  162.     {
  163.         pthread_join(ptsort[i], NULL);
  164.     }
  165.     gettimeofday(&time_after,NULL);
  166.  
  167.     mergeArrays(my_array,my_array,tasort[0].from,tasort[0].to,tasort[1].from,tasort[1].to,my_array);
  168.  
  169.     for (int i = 0; i < my_length; i++)
  170.     {
  171.         cout << my_array[i] << endl;
  172.     }
  173.  
  174.     cout << "Time of sorting in ms: " << timeval_to_ms(&time_before,&time_after) << " using " << count_Threadsort << " Threads " << endl;
  175.     cout << "Time of filling in ms: " << timeval_to_ms(&t1,&t2) << " using " << count_Threadfill << " Threads " << endl;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement