Advertisement
Thiff

Podivat se2

Nov 6th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <sys/param.h>
  6. #include <pthread.h>
  7.  
  8. #define TYPE int
  9.  
  10. struct thread_argument {
  11.   int id;                 // user identification
  12.   int from, length, start;       // data range
  13.   TYPE *data;             // array
  14.   TYPE max;               // result
  15.   TYPE sort;              // sort
  16.   TYPE ssort;              // sort
  17. };
  18.  
  19. // function search_max search the largest number in part of array
  20. // from the left (included) up to the right element
  21. TYPE search_max( int left, int length, TYPE *array ) {
  22.   TYPE max_elem = array[ left ];
  23.   for ( int i = 1; i < length; i++ )
  24.     if ( max_elem < array[ left + i ] )
  25.       max_elem = array[ left + i ];
  26.   return max_elem;
  27. }
  28.  
  29. //function bubble_sort is sorting thread arrays
  30. TYPE bubble_sort( int from, int length, TYPE *array ) {
  31.   for (int i = *array; i < *array + length - 1; i++) {
  32.     for (int j = *array; j < *array + length - 1; j++) {
  33.       if(array[j+1] < array[j]) {
  34.         int tmp = array[j + 1];
  35.         array[j + 1] = array[j];
  36.         array[j] = tmp;
  37.       }
  38.     }
  39.   }
  40. }
  41.  
  42. TYPE selection_sort( int from, int length, TYPE *array) {
  43.  for (int i = from; i < length - 1; i++) {
  44.    int maxIndex = i;
  45.    for (int j = i + 1; j < length; j++) {
  46.       if (array[j] > array[maxIndex])
  47.         maxIndex = j;
  48.    }
  49.    int tmp = array[i];
  50.    array[i] = array[maxIndex];
  51.    array[maxIndex] = tmp;
  52.  }
  53. }
  54.  
  55. void print_array(TYPE *array, int *length) {
  56.   for(int i = 0; i < *length; i++) {
  57.     printf( "%d ", array[i] );
  58.   }
  59. }
  60.  
  61. // Thread will search the largest element in array
  62. // from element arg->from with length of arg->length.
  63. // Result will be stored to arg->max.
  64. void *my_thread( void *void_arg ) {
  65.   thread_argument *ptr_data = ( thread_argument * ) void_arg;
  66.  
  67.   printf( "Thread %d started from %d with length %d...\n",
  68.     ptr_data->id, ptr_data->from, ptr_data->length );
  69.  
  70.   ptr_data->max = search_max( ptr_data->from, ptr_data->length, ptr_data->data );
  71.  
  72.   printf( "Found maximum in thread %d is %d\n", ptr_data->id, ptr_data->max );
  73.  
  74.   return NULL;
  75. }
  76.  
  77. // Thread will bubble sort the array
  78. // from element arg->from with length of arg->length.
  79. // Result will be stored to arg->sort.
  80. void *my_thread2( void *void_arg ) {
  81.   thread_argument *ptr_data = ( thread_argument * ) void_arg;
  82.  
  83.   printf( "Thread %d started from %d with length %d...\n",
  84.     ptr_data->id, ptr_data->from, ptr_data->length );
  85.  
  86.   ptr_data->sort = bubble_sort( ptr_data->from, ptr_data->length, ptr_data->data );
  87.  
  88.   return NULL;
  89. }
  90.  
  91. void *my_thread3( void *void_arg ) {
  92.   thread_argument *ptr_data = ( thread_argument * ) void_arg;
  93.  
  94.   printf( "Thread %d started from %d with length %d...\n",
  95.     ptr_data->id, ptr_data->from, ptr_data->length );
  96.  
  97.   ptr_data->ssort = selection_sort( ptr_data->from, ptr_data->length, ptr_data->data );
  98.  
  99.   return NULL;
  100. }
  101.  
  102. // Time interval between two measurements
  103. int timeval_to_ms( timeval *before, timeval *after ) {
  104.   timeval res;
  105.   timersub( after, before, &res );
  106.   return 1000 * res.tv_sec + res.tv_usec / 1000;
  107. }
  108.  
  109. #define LENGTH_LIMIT 10
  110.  
  111. int main( int na, char **arg ) {
  112.   // The number of elements must be used as program argument
  113.   if ( na < 2 ) {
  114.     printf( "Specify number of elements, at least %d.\n", LENGTH_LIMIT );
  115.     return 0;
  116.   }
  117.  
  118.   int my_length = atoi( arg[ 1 ] );
  119.   if ( my_length < LENGTH_LIMIT ) {
  120.     printf( "The number of elements must be at least %d.\n", LENGTH_LIMIT );
  121.     return 0;
  122.   }
  123.  
  124.   // array allocation
  125.   TYPE *my_array = new TYPE [ my_length ];
  126.   if ( !my_array ) {
  127.     printf( "Not enought memory for array!\n" );
  128.     return 1;
  129.   }
  130.  
  131.   // Initialization of random number generator
  132.   srand( ( int ) time( NULL ) );
  133.  
  134.   printf( "Random numbers generetion started..." );
  135.   for ( int i = 0; i < my_length; i++ ) {
  136.     my_array[ i ] = rand() % ( my_length * 10 );
  137.     if ( !( i % LENGTH_LIMIT ) ) {
  138.       printf( "." );
  139.       fflush( stdout );
  140.     }
  141.   }
  142.   int *length = &my_length;
  143.   // Initialization of thread arguments
  144.   int n = 8;
  145.   printf( "%d",na );
  146.   if (na > 2 )
  147.     n = atoi( arg[ 2 ] );
  148.    //pocet vlaken
  149.   int start = 0;
  150.   pthread_t* pt = new pthread_t[n];
  151.   thread_argument* ta = new thread_argument[n];
  152.   timeval time_before, time_after;
  153.  
  154.   for(int i = 0; i < n; i++) {
  155.     ta[i].id = i;
  156.     ta[i].from = start;
  157.     ta[i].start = start;
  158.     ta[i].length = my_length / n;
  159.     ta[i].data = my_array;
  160.     start = start + (my_length / n);
  161.   }
  162.  
  163.   // Time recording before searching
  164.   gettimeofday( &time_before, NULL );
  165.  
  166.   // Threads starting
  167.    if (na > 3 ) {
  168.     int sort = atoi( arg[ 3 ] );
  169.     if(sort == 1){
  170.       printf("bubble_sort" );
  171.       for(int i = 0; i < n; i++) {
  172.         pthread_create( &pt[i], NULL, my_thread2, &ta[i] );
  173.       }
  174.     }
  175.     else if (sort == 2 ) {
  176.       printf("selection_sort" );
  177.       for(int i = 0; i < n; i++) {
  178.         pthread_create( &pt[i], NULL, my_thread3, &ta[i] );
  179.       }
  180.     }
  181.   }
  182.   else {
  183.     printf("bubble_sort" );
  184.     for(int i = 0; i < n; i++) {
  185.       pthread_create( &pt[i], NULL, my_thread2, &ta[i] );
  186.     }
  187.   }
  188.   // Waiting for threads completion
  189.   for(int i = 0; i < n; i++) {
  190.     pthread_join( pt[i], NULL );
  191.   }
  192.  
  193.   // Time recording after searching
  194.   gettimeofday( &time_after, NULL );
  195.  
  196.   //Starting function on threads
  197.   //printf( "The found maximum: %d\n", MAX( ta1.max, ta2.max ) );
  198.   TYPE *vysledek = new TYPE [ my_length ];
  199.   int min = 0;
  200.   for(int i = 0; i < my_length; i++) {
  201.     bool first = true;
  202.     for(int j = 0; j < n; j++) {
  203.       if (ta[j].length == 0) continue;
  204.       //printf("%d%d\n", my_array[ta[j].from],j);
  205.       if(first){
  206.         first = false;
  207.        min = my_array[ta[j].from];
  208.      }
  209.       if (min > my_array[ta[j].from]) min = my_array[ta[j].from];
  210.     }
  211.     for(int j = 0; j < n; j++) {
  212.       if(min == my_array[ta[j].from]) {
  213.         ta[j].from++;
  214.         ta[j].length--;
  215.         break;
  216.       }
  217.     }
  218.     vysledek[i] = min;
  219.   }
  220.  
  221.   print_array(vysledek, length);
  222.  
  223.   printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  224.  
  225.  
  226.  
  227.   //printf( "\nMaximum number search using one thread...\n" );
  228.   printf( "\nSorting array using one thread...\n" );
  229.  
  230.   gettimeofday( &time_before, NULL );
  231.  
  232.   //Sorting in single thread
  233.   TYPE M = bubble_sort( 0, my_length, my_array );
  234.  
  235.   gettimeofday( &time_after, NULL );
  236.  
  237.   /*for(int i = 0; i < my_length; i++) {
  238.       printf( "%d ", my_array[i] );
  239.   }*/
  240.  
  241.   printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement