Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/param.h>
- #include <pthread.h>
- #define TYPE int
- struct thread_argument {
- int id; // user identification
- int from, length, start; // data range
- TYPE *data; // array
- TYPE max; // result
- TYPE sort; // sort
- TYPE ssort; // sort
- };
- // function search_max search the largest number in part of array
- // from the left (included) up to the right element
- TYPE search_max( int left, int length, TYPE *array ) {
- TYPE max_elem = array[ left ];
- for ( int i = 1; i < length; i++ )
- if ( max_elem < array[ left + i ] )
- max_elem = array[ left + i ];
- return max_elem;
- }
- //function bubble_sort is sorting thread arrays
- TYPE bubble_sort( int from, int length, TYPE *array ) {
- for (int i = *array; i < *array + length - 1; i++) {
- for (int j = *array; j < *array + length - 1; j++) {
- if(array[j+1] < array[j]) {
- int tmp = array[j + 1];
- array[j + 1] = array[j];
- array[j] = tmp;
- }
- }
- }
- }
- TYPE selection_sort( int from, int length, TYPE *array) {
- for (int i = from; i < length - 1; i++) {
- int maxIndex = i;
- for (int j = i + 1; j < length; j++) {
- if (array[j] > array[maxIndex])
- maxIndex = j;
- }
- int tmp = array[i];
- array[i] = array[maxIndex];
- array[maxIndex] = tmp;
- }
- }
- void print_array(TYPE *array, int *length) {
- for(int i = 0; i < *length; i++) {
- printf( "%d ", array[i] );
- }
- }
- // Thread will search the largest element in array
- // from element arg->from with length of arg->length.
- // Result will be stored to arg->max.
- void *my_thread( void *void_arg ) {
- thread_argument *ptr_data = ( thread_argument * ) void_arg;
- printf( "Thread %d started from %d with length %d...\n",
- ptr_data->id, ptr_data->from, ptr_data->length );
- ptr_data->max = search_max( ptr_data->from, ptr_data->length, ptr_data->data );
- printf( "Found maximum in thread %d is %d\n", ptr_data->id, ptr_data->max );
- return NULL;
- }
- // Thread will bubble sort the array
- // from element arg->from with length of arg->length.
- // Result will be stored to arg->sort.
- void *my_thread2( void *void_arg ) {
- thread_argument *ptr_data = ( thread_argument * ) void_arg;
- printf( "Thread %d started from %d with length %d...\n",
- ptr_data->id, ptr_data->from, ptr_data->length );
- ptr_data->sort = bubble_sort( ptr_data->from, ptr_data->length, ptr_data->data );
- return NULL;
- }
- void *my_thread3( void *void_arg ) {
- thread_argument *ptr_data = ( thread_argument * ) void_arg;
- printf( "Thread %d started from %d with length %d...\n",
- ptr_data->id, ptr_data->from, ptr_data->length );
- ptr_data->ssort = selection_sort( ptr_data->from, ptr_data->length, ptr_data->data );
- return NULL;
- }
- // Time interval between two measurements
- int timeval_to_ms( timeval *before, timeval *after ) {
- timeval res;
- timersub( after, before, &res );
- return 1000 * res.tv_sec + res.tv_usec / 1000;
- }
- #define LENGTH_LIMIT 10
- int main( int na, char **arg ) {
- // The number of elements must be used as program argument
- if ( na < 2 ) {
- printf( "Specify number of elements, at least %d.\n", LENGTH_LIMIT );
- return 0;
- }
- int my_length = atoi( arg[ 1 ] );
- if ( my_length < LENGTH_LIMIT ) {
- printf( "The number of elements must be at least %d.\n", LENGTH_LIMIT );
- return 0;
- }
- // array allocation
- TYPE *my_array = new TYPE [ my_length ];
- if ( !my_array ) {
- printf( "Not enought memory for array!\n" );
- return 1;
- }
- // Initialization of random number generator
- srand( ( int ) time( NULL ) );
- printf( "Random numbers generetion started..." );
- for ( int i = 0; i < my_length; i++ ) {
- my_array[ i ] = rand() % ( my_length * 10 );
- if ( !( i % LENGTH_LIMIT ) ) {
- printf( "." );
- fflush( stdout );
- }
- }
- int *length = &my_length;
- // Initialization of thread arguments
- int n = 8;
- printf( "%d",na );
- if (na > 2 )
- n = atoi( arg[ 2 ] );
- //pocet vlaken
- int start = 0;
- pthread_t* pt = new pthread_t[n];
- thread_argument* ta = new thread_argument[n];
- timeval time_before, time_after;
- for(int i = 0; i < n; i++) {
- ta[i].id = i;
- ta[i].from = start;
- ta[i].start = start;
- ta[i].length = my_length / n;
- ta[i].data = my_array;
- start = start + (my_length / n);
- }
- // Time recording before searching
- gettimeofday( &time_before, NULL );
- // Threads starting
- if (na > 3 ) {
- int sort = atoi( arg[ 3 ] );
- if(sort == 1){
- printf("bubble_sort" );
- for(int i = 0; i < n; i++) {
- pthread_create( &pt[i], NULL, my_thread2, &ta[i] );
- }
- }
- else if (sort == 2 ) {
- printf("selection_sort" );
- for(int i = 0; i < n; i++) {
- pthread_create( &pt[i], NULL, my_thread3, &ta[i] );
- }
- }
- }
- else {
- printf("bubble_sort" );
- for(int i = 0; i < n; i++) {
- pthread_create( &pt[i], NULL, my_thread2, &ta[i] );
- }
- }
- // Waiting for threads completion
- for(int i = 0; i < n; i++) {
- pthread_join( pt[i], NULL );
- }
- // Time recording after searching
- gettimeofday( &time_after, NULL );
- //Starting function on threads
- //printf( "The found maximum: %d\n", MAX( ta1.max, ta2.max ) );
- TYPE *vysledek = new TYPE [ my_length ];
- int min = 0;
- for(int i = 0; i < my_length; i++) {
- bool first = true;
- for(int j = 0; j < n; j++) {
- if (ta[j].length == 0) continue;
- //printf("%d%d\n", my_array[ta[j].from],j);
- if(first){
- first = false;
- min = my_array[ta[j].from];
- }
- if (min > my_array[ta[j].from]) min = my_array[ta[j].from];
- }
- for(int j = 0; j < n; j++) {
- if(min == my_array[ta[j].from]) {
- ta[j].from++;
- ta[j].length--;
- break;
- }
- }
- vysledek[i] = min;
- }
- print_array(vysledek, length);
- printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
- //printf( "\nMaximum number search using one thread...\n" );
- printf( "\nSorting array using one thread...\n" );
- gettimeofday( &time_before, NULL );
- //Sorting in single thread
- TYPE M = bubble_sort( 0, my_length, my_array );
- gettimeofday( &time_after, NULL );
- /*for(int i = 0; i < my_length; i++) {
- printf( "%d ", my_array[i] );
- }*/
- printf( "The search time: %d [ms]\n", timeval_to_ms( &time_before, &time_after ) );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement