Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 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. //
  6. // Threads programming example for Linux (10/2016)
  7. // For the propper testing is necessary to have at least 2 cores CPU
  8. //
  9. // ***********************************************************************
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <sys/time.h>
  15. #include <sys/param.h>
  16. #include <pthread.h>
  17.  
  18. #define TYPE int
  19.  
  20. class Sort{
  21. public:
  22. /**
  23. * Razeni vkladanim (od nejvyssiho)
  24. * @param array pole k serazeni
  25. * @param size velikost pole
  26. */
  27. void sort(TYPE * array, int from, int to,bool desc = true);
  28. void sortAsc(TYPE * array, int from, int to);
  29. void sortDesc(TYPE * array, int from, int to);
  30.  
  31. };
  32.  
  33. class InsertSort:Sort{
  34.  
  35. void sort(TYPE * array, int from, int to,bool desc = true){
  36. if (desc) this->sortDesc(array, from, to);
  37. else this->sortAsc(array, from, to);
  38. }
  39.  
  40. void sortAsc(TYPE * array, int from, int to){
  41. for (int i = from; i < to - 1; i++) {
  42. int j = i + 1;
  43. TYPE tmp = array[j];
  44. while (j > from && tmp < array[j-1]) {
  45. array[j] = array[j-1];
  46. j--;
  47. }
  48. array[j] = tmp;
  49. }
  50. }
  51.  
  52. void sortDesc(TYPE * array, int from, int to){
  53. for (int i = from; i < to - 1; i++) {
  54. int j = i + 1;
  55. TYPE tmp = array[j];
  56. while (j > from && tmp > array[j-1]) {
  57. array[j] = array[j-1];
  58. j--;
  59. }
  60. array[j] = tmp;
  61. }
  62. }
  63. };
  64.  
  65.  
  66. class task_part
  67. {
  68. public:
  69. int id; // user identification
  70. int from, length; // data range
  71. TYPE *data; // array
  72. TYPE max; // result
  73.  
  74. task_part( int myid, int first, int num, TYPE *ptr ) :
  75. id( myid ), from( first ), length( num ), data( ptr ) {
  76.  
  77. }
  78.  
  79. TYPE get_result() { return max; }
  80.  
  81. // function search_max search the largest number in part of array
  82. // from the left (included) up to the right element
  83. TYPE search_max()
  84. {
  85. TYPE max_elem = data[ from ];
  86. for ( int i = 1; i < length; i++ )
  87. if ( max_elem < data[ from + i ] )
  88. max_elem = data[ from + i ];
  89. return max_elem;
  90. }
  91. };
  92.  
  93. // Thread will search the largest element in array
  94. // from element arg->from with length of arg->length.
  95. // Result will be stored to arg->max.
  96. void *my_thread( void *void_arg )
  97. {
  98. task_part *ptr_task = ( task_part * ) void_arg;
  99.  
  100. printf( "Thread %d started from %d with length %d...\n",
  101. ptr_task->id, ptr_task->from, ptr_task->length );
  102.  
  103. ptr_task->max = ptr_task->search_max();
  104.  
  105. printf( "Found maximum in thread %d is %d\n", ptr_task->id, ptr_task->max );
  106.  
  107. return NULL;
  108. }
  109.  
  110. // Time interval between two measurements
  111. int timeval_to_ms( timeval *before, timeval *after )
  112. {
  113. timeval res;
  114. timersub( after, before, &res );
  115. return 1000 * res.tv_sec + res.tv_usec / 1000;
  116. }
  117.  
  118. #define LENGTH_LIMIT 10000000
  119.  
  120. int main( int na, char **arg )
  121. {
  122. // The number of elements must be used as program argument
  123. if ( na != 2 )
  124. {
  125. printf( "Specify number of elements, at least %d.\n", LENGTH_LIMIT );
  126. return 0;
  127. }
  128. int my_length = atoi( arg[ 1 ] );
  129. if ( my_length < LENGTH_LIMIT )
  130. {
  131. printf( "The number of elements must be at least %d.\n", LENGTH_LIMIT );
  132. return 0;
  133. }
  134.  
  135. // array allocation
  136. TYPE *my_array = new TYPE [ my_length ];
  137. if ( !my_array )
  138. {
  139. printf( "Not enough memory for array!\n" );
  140. return 1;
  141. }
  142.  
  143. // Initialization of random number generator
  144. srand( ( int ) time( NULL ) );
  145.  
  146. printf( "Random numbers generation started..." );
  147. for ( int i = 0; i < my_length; i++ )
  148. {
  149. my_array[ i ] = rand() % ( my_length * 10 );
  150. if ( !( i % LENGTH_LIMIT ) )
  151. {
  152. printf( "." );
  153. fflush( stdout );
  154. }
  155. }
  156.  
  157. printf( "\nMaximum number search using two threads...\n" );
  158. pthread_t pt1, pt2;
  159. task_part tp1( 1, 0, my_length / 2, my_array );
  160. task_part tp2( 2, my_length / 2, my_length - my_length / 2, my_array );
  161. timeval time_before, time_after;
  162.  
  163. // Time recording before searching
  164. gettimeofday( &time_before, NULL );
  165.  
  166.  
  167. // Threads starting
  168. pthread_create( &pt1, NULL, my_thread, &tp1 );
  169. pthread_create( &pt2, NULL, my_thread, &tp2 );
  170.  
  171. // Waiting for threads completion
  172. pthread_join( pt1, NULL );
  173. pthread_join( pt2, NULL );
  174.  
  175. // Time recording after searching
  176. gettimeofday( &time_after, NULL );
  177.  
  178. printf( "The found maximum: %d\n", MAX( tp1.get_result(), tp2.get_result() ) );
  179. printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  180.  
  181. printf( "\nMaximum number search using one thread...\n" );
  182.  
  183. gettimeofday( &time_before, NULL );
  184.  
  185. // Searching in single thread
  186. task_part single( 333, 0, my_length, my_array );
  187. TYPE res = single.search_max();
  188.  
  189. gettimeofday( &time_after, NULL );
  190.  
  191. printf( "The found maximum: %d\n", res );
  192. printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement