Advertisement
Guest User

ccccc

a guest
Nov 23rd, 2017
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. // ***********************************************************************
  2. //
  3. // Demo program for subject Computer Architectures and Paralel systems
  4. // Petr Olivka, Dept. of Computer Science, FEECS, VSB-TU Ostrava
  5. // email:petr.olivka@vsb.cz
  6. //
  7. // Threads programming example for Linux (10/2016)
  8. // For the propper testing is necessary to have at least 2 cores CPU
  9. //
  10. // ***********************************************************************
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <sys/time.h>
  16. #include <sys/param.h>
  17. #include <pthread.h>
  18.  
  19. #define TYPE int
  20.  
  21. struct thread_argument
  22. {
  23.     int id;                 // user identification
  24.     int from, length;       // data range
  25.     TYPE *data;             // array
  26.     TYPE max;               // result
  27. };
  28.  
  29. // function search_max search the largest number in part of array
  30. // from the left (included) up to the right element
  31. TYPE search_max( int left, int length, TYPE *array )
  32. {
  33.     TYPE max_elem = array[ left ];
  34.     for ( int i = 1; i < length; i++ )
  35.         if ( max_elem < array[ left + i ] )
  36.             max_elem = array[ left + i ];
  37.     return max_elem;
  38. }
  39.  
  40. // Thread will search the largest element in array
  41. // from element arg->from with length of arg->length.
  42. // Result will be stored to arg->max.
  43. void *my_thread( void *void_arg )
  44. {
  45.     thread_argument *ptr_data = ( thread_argument * ) void_arg;
  46.  
  47.     printf( "Thread %d started from %d with length %d...\n",
  48.         ptr_data->id, ptr_data->from, ptr_data->length );
  49.  
  50.     ptr_data->max = search_max( ptr_data->from, ptr_data->length, ptr_data->data );
  51.  
  52.     printf( "Found maximum in thread %d is %d\n", ptr_data->id, ptr_data->max );
  53.  
  54.     return NULL;
  55. }
  56.  
  57. // Time interval between two measurements
  58. int timeval_to_ms( timeval *before, timeval *after )
  59. {
  60.     timeval res;
  61.     timersub( after, before, &res );
  62.     return 1000 * res.tv_sec + res.tv_usec / 1000;
  63. }
  64.  
  65. #define LENGTH_LIMIT 10000000
  66.  
  67. int main( int na, char **arg )
  68. {
  69.     // The number of elements must be used as program argument
  70.     if ( na != 2 )
  71.     {
  72.         printf( "Specify number of elements, at least %d.\n", LENGTH_LIMIT );
  73.         return 0;
  74.     }
  75.     int my_length = atoi( arg[ 1 ] );
  76.     if ( my_length < LENGTH_LIMIT )
  77.     {
  78.         printf( "The number of elements must be at least %d.\n", LENGTH_LIMIT );
  79.         return 0;
  80.     }
  81.  
  82.     // array allocation
  83.     TYPE *my_array = new TYPE [ my_length ];
  84.     if ( !my_array )
  85.     {
  86.         printf( "Not enought memory for array!\n" );
  87.         return 1;
  88.     }
  89.  
  90.     // Initialization of random number generator
  91.     srand( ( int ) time( NULL ) );
  92.  
  93.     printf( "Random numbers generetion started..." );
  94.     for ( int i = 0; i < my_length; i++ )
  95.     {
  96.             my_array[ i ] = rand() % ( my_length * 10 );
  97.             if ( !( i % LENGTH_LIMIT ) )
  98.             {
  99.                 printf( "." );
  100.                 fflush( stdout );
  101.             }
  102.     }
  103.  
  104.     printf( "\nMaximum number search using two threads...\n" );
  105.     pthread_t pt1, pt2;
  106.     thread_argument ta1, ta2;
  107.     timeval time_before, time_after;
  108.  
  109.     // Initialization of thread arguments
  110.     ta1.id = 1;
  111.     ta1.from = 0;
  112.     ta1.length = my_length / 2;
  113.     ta1.data = my_array;
  114.  
  115.     ta2.id = 2;
  116.     ta2.from = ta1.length;
  117.     ta2.length = my_length - ta1.length;
  118.     ta2.data = my_array;
  119.  
  120.     // Time recording before searching
  121.     gettimeofday( &time_before, NULL );
  122.  
  123.  
  124.     // Threads starting
  125.     pthread_create( &pt1, NULL, my_thread, &ta1 );
  126.     pthread_create( &pt2, NULL, my_thread, &ta2 );
  127.  
  128.     // Waiting for threads completion
  129.     pthread_join( pt1, NULL );
  130.     pthread_join( pt2, NULL );
  131.  
  132.     // Time recording after searching
  133.     gettimeofday( &time_after, NULL );
  134.  
  135.     printf( "The found maximum: %d\n", MAX( ta1.max, ta2.max ) );
  136.     printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  137.  
  138.     printf( "\nMaximum number search using one thread...\n" );
  139.  
  140.     gettimeofday( &time_before, NULL );
  141.  
  142.     // Searching in single thread
  143.     TYPE M = search_max( 0, my_length, my_array );
  144.  
  145.     gettimeofday( &time_after, NULL );
  146.  
  147.     printf( "The found maximum: %d\n", M );
  148.     printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement