Advertisement
Thiff

apps

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