Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- #include <sys/time.h>
- #include <sys/param.h>
- #include <pthread.h>
- #include <math.h>
- #define TYPE float
- using namespace std;
- const int count_Threadfill = 2;
- const int count_Threadsort = 2;
- const int my_length = 10000;
- struct thread_argument
- {
- int id;
- int from, to;
- TYPE *data;
- };
- void mergeArrays(TYPE *arr1, TYPE *arr2, int start1,int end1, int start2, int end2, TYPE *arr3)
- {
- int i = start1, j = start2, k = 0;
- while (i<end1 && j<end2)
- {
- if (arr1[i] < arr2[j])
- arr3[k++] = arr1[i++];
- else
- arr3[k++] = arr2[j++];
- }
- while (i < end1)
- arr3[k++] = arr1[i++];
- while (j < end2)
- arr3[k++] = arr2[j++];
- }
- int timeval_to_ms( timeval *before, timeval *after )
- {
- timeval res;
- timersub( after, before, &res );
- return 1000 * res.tv_sec + res.tv_usec / 1000;
- }
- float randomFloat(float minR,float maxR){
- return (minR + 1) + (((float) rand()) / (float) RAND_MAX) * (maxR - (minR + 1));
- }
- int randomInt(int minR, int maxR){
- return rand()%(maxR-minR + 1) + minR;
- }
- void BubbleSort_dsc(TYPE *arr, int start, int end){
- TYPE tmp;
- for(int i = start; i < end; i++)
- {
- for(int j = start; j < end - 1; j++)
- {
- if(arr[j+1] > arr[j])
- {
- tmp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = tmp;
- }
- }
- }
- }
- void BubbleSort_asc(TYPE *arr, int start, int end){
- TYPE tmp;
- for(int i = start; i < end; i++)
- {
- for(int j = start; j < end - 1; j++)
- {
- if(arr[j+1] < arr[j])
- {
- tmp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = tmp;
- }
- }
- }
- }
- void fill_array(TYPE *arr, int start, int end, int minR, int maxR)
- {
- TYPE temp;
- for (int i = start; i < end; i++)
- {
- temp = randomFloat(minR,maxR);
- arr[i] = roundf(temp * 100) / 100.0;
- //arr[i] = randomInt(minR, maxR);
- }
- }
- void *thread_filling( void *void_arg )
- { int minR = -100.00;
- int maxR = 100.00;
- thread_argument *ptr_data = (thread_argument*) void_arg;
- fill_array(ptr_data->data, ptr_data->from, ptr_data->to,minR,maxR);
- return NULL;
- }
- void *thread_sorting( void *void_arg )
- {
- thread_argument *ptr_data = (thread_argument*) void_arg;
- BubbleSort_asc(ptr_data->data, ptr_data->from, ptr_data->to);
- return NULL;
- }
- int main()
- {
- srand( ( int ) time( NULL ) );
- TYPE *my_array = new TYPE [my_length];
- pthread_t ptfill[count_Threadfill];
- pthread_t ptsort[count_Threadsort];
- thread_argument tafill[count_Threadfill];
- thread_argument tasort[count_Threadsort];
- timeval time_before,time_after;
- timeval t1,t2;
- int minR = -100;
- int maxR = 100;
- for (int i = 0; i < count_Threadfill; i++)
- {
- tafill[i].id = i;
- tafill[i].from = i * (my_length/count_Threadfill);
- tafill[i].to = tafill[i].from + (my_length/count_Threadfill);
- tafill[i].data = my_array;
- }
- for (int i = 0; i < count_Threadsort; i++)
- {
- tasort[i].id = i;
- tasort[i].from = i * (my_length/count_Threadsort);
- tasort[i].to = tasort[i].from + (my_length/count_Threadsort);
- tasort[i].data = my_array;
- }
- gettimeofday(&t1,NULL);
- for (int i = 0; i < count_Threadfill; i++)
- {
- pthread_create( &ptfill[i], NULL, thread_filling, &tafill[i] );
- }
- for (int i = 0; i < count_Threadfill; i++)
- {
- pthread_join(ptfill[i], NULL);
- }
- gettimeofday(&t2,NULL);
- gettimeofday(&time_before,NULL);
- for (int i = 0; i < count_Threadsort; i++)
- {
- pthread_create( &ptsort[i], NULL, thread_sorting, &tasort[i] );
- }
- for (int i = 0; i < count_Threadsort; i++)
- {
- pthread_join(ptsort[i], NULL);
- }
- gettimeofday(&time_after,NULL);
- mergeArrays(my_array,my_array,tasort[0].from,tasort[0].to,tasort[1].from,tasort[1].to,my_array);
- for (int i = 0; i < my_length; i++)
- {
- cout << my_array[i] << endl;
- }
- cout << "Time of sorting in ms: " << timeval_to_ms(&time_before,&time_after) << " using " << count_Threadsort << " Threads " << endl;
- cout << "Time of filling in ms: " << timeval_to_ms(&t1,&t2) << " using " << count_Threadfill << " Threads " << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement