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_Thread = 2;
- const int my_length = 100;
- struct thread_argument
- {
- int id;
- int from, to;
- TYPE *data;
- };
- int timeval_to_ms( timeval *before, timeval *after )
- {
- timeval res;
- timersub( after, before, &res );
- return 1000 * res.tv_sec + res.tv_usec / 1000;
- }
- void merge(TYPE *arr1, TYPE *arr2, int start1, int end1, int start2, int end2, TYPE *arr3)
- {
- int i = start1, j = start2;
- int 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++];
- }
- void vypis(TYPE *arr) {
- for (int i = 0; i < my_length; i++)
- {
- cout << arr[i] << endl;
- }
- }
- 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)
- { TYPE temp;
- int minR = -100.0;
- int maxR = 100.0;
- for (int i = start; i < end; i++)
- {
- temp = randomFloat(minR,maxR);
- arr[i] = roundf(temp * 100) / 100.0;
- //arr[i] = randomInt(minR, maxR);
- }
- }
- void vypis(TYPE *arr, int start, int end){
- for( int i = start; i < end ; i++ ){
- cout << arr[i];}
- cout << endl;
- }
- 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;
- }
- void *thread_filling( void *void_arg )
- { int minR = -100.0;
- int maxR = 100.0;
- thread_argument *ptr_data = ( thread_argument * ) void_arg;
- int length = ptr_data->to - ptr_data->from;
- cout << ptr_data->id << \" \" << ptr_data->from << \" \" << ptr_data->to;
- fill_array(ptr_data->data, ptr_data->from, ptr_data->to);
- vypis(ptr_data->data, ptr_data->from, ptr_data->to );
- return NULL;
- }
- int main()
- { srand( ( int ) time( NULL ) );
- TYPE *my_array = new TYPE [my_length];
- TYPE *merged = new TYPE [2*my_length];
- timeval time_before,time_after;
- pthread_t pt[count_Thread];
- thread_argument ta[count_Thread];
- int minR = -200.00;
- int maxR = 100.00;
- for (int i = 0; i < count_Thread; i++)
- {
- ta[i].id = i;
- ta[i].from = i * (my_length/count_Thread);
- ta[i].to = ta[i].from + (my_length/count_Thread);
- ta[i].data = my_array;
- }
- for(int i = 0; i < count_Thread; i++){
- pthread_create(&pt[i], NULL, thread_filling, &my_array[i] );}
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_join(pt[i], NULL );
- }
- gettimeofday(&time_before,NULL);
- for(int i = 0; i < count_Thread; i++){
- pthread_create(&pt[i], NULL, thread_sorting, &my_array[i]);
- }
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_join(pt[i], NULL );
- }
- gettimeofday(&time_after,NULL);
- merge(ta[0].data,ta[1].data,ta[0].from,ta[0].to,ta[1].from,ta[1].to,merged);
- vypis(merged,0,my_length);
- cout << \"Time of sorting in ms: \" << timeval_to_ms(&time_before,&time_after) << \" using \" << count_Thread << \" Threads \" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement