Advertisement
Guest User

Untitled

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