diff --git a/.bucketsort.cpp.swp b/.bucketsort.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..b828b4773b3f54e385b27438f22a5793557f5b17 Binary files /dev/null and b/.bucketsort.cpp.swp differ diff --git a/a b/a new file mode 100755 index 0000000000000000000000000000000000000000..2975acab392005f434add98478c91027396f60e1 Binary files /dev/null and b/a differ diff --git a/bucketsort.c b/bucketsort.c new file mode 100644 index 0000000000000000000000000000000000000000..143997605f1ecc02104f36bafea8c7dfec7323c5 --- /dev/null +++ b/bucketsort.c @@ -0,0 +1,75 @@ +for (int i=0; i<SIZE; i++){ + if (array[i] < min){ + min = array[i]; + } + if (array[i] > max){ + max = array[i]; + } + } + + int *num_bucket = malloc (sizeof(int) *num_process); + for(d=1; d < num_process; d++){ + num_bucket[d] = 0; + } + if (num_process != 1){ + for(d=0; d < SIZE; d++){ + int increaseOf = max/(num_process-1); + int iteration = 1; + int annexed = 0; + for(j=increaseOf; j<=max; j=j+increaseOf){ + if(array[d] <= j){ + num_bucket[iteration]++; + annexed = 1; + break; + } + iteration++; + } + if (annexed == 0){ + num_bucket[iteration-1]++; + } + } + for(i=1; i<num_process; i++){ + MPI_Send(&num_bucket[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD); + } + for(d=0; d < SIZE; d++){ + int increaseOf = max/(num_process-1); + int iteration = 1; + int message = 0; + for (j = increaseOf; j <= max; j = j + increaseOf) { + if(array[d] <= j) { + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration, 1, MPI_COMM_WORLD); + message = 1; + break; + } + iteration++; + } + if (message == 0) + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration-1, 1, MPI_COMM_WORLD); + } + + int lastIndex = 0; + int current_index = 0; + for(i=1; i<num_process; i++){ + unsigned int * recvArray = malloc(sizeof(unsigned int)*num_bucket[i]); + MPI_Recv(&recvArray[0], num_bucket[i], MPI_UNSIGNED, i, 2, MPI_COMM_WORLD, &stat); + if(lastIndex == 0){ + lastIndex = num_bucket[i]; + } + for(j=0; j<num_bucket[i]; j++){ + array[current_index] = recvArray[j]; + current_index++; + } + } + } + + endtime = MPI_Wtime(); + + int c; + for(c=0; c<SIZE; c++){ + printf("%d, ", array[c]); + } + + printf("\nIt took %f seconds \n", endtime-starttime); + printf("Numbers: %d \n", SIZE); + printf("Processes: %d \n", num_process); + diff --git a/bucketsort.cpp b/bucketsort.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd79272f115626a76ed4b391739bfed2d65c12ff --- /dev/null +++ b/bucketsort.cpp @@ -0,0 +1,69 @@ +#include <omp.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <omp.h> +#include <assert.h> +#include <vector> +#include <algorithm> +using namespace std; + +int n, maxn; +int m[1000100]; +vector<int> init[100]; +vector<int> sorted; +int d,i,j,t; +int numberThread; + +void bucketSort(){ + int currentThread = omp_get_thread_num(); + int buck_size = init[currentThread].size(); + sort(init[currentThread].begin(), init[currentThread].end()); +} + +int main(int argc, char *argv[]){ + numberThread = strtol(argv[1], NULL, 10); + srand(time(NULL)); + scanf("%d", &n); + int width = n / numberThread; + + for (int i = 0; i < n; i++) { + m[i] = rand() % n; + } + + for (int i = 0; i < n; i++) { + if (m[i]!= 0) { + m[i]--; + } + init[m[i]/width].push_back(m[i]); + } + + double start; + double end; + start = omp_get_wtime(); + for (int i = 0;i<numberThread;i++){ + int currBuckSize = init[i].size(); + for(int j = 0;j<currBuckSize;j++){ + printf("%d ",init[i][j]); + } + printf("\n"); + } + #pragma omp parallel num_threads(numberThread) shared(init) + bucketSort(); + #pragma omp barrier + for (int i = 0;i<numberThread;i++){ + int currBuckSize = init[i].size(); + for (int j = 0;j<currBuckSize;j++){ + sorted.push_back(init[i][j]); + } + } + end = omp_get_wtime(); + for (int i = 0;i<numberThread;i++){ + int currBuckSize = init[i].size(); + for(int j = 0;j<currBuckSize;j++){ + printf("%d ",init[i][j]); + } + printf("\n"); + } + printf("Work took %f sec. time.\n", end-start); +} diff --git a/laporan.txt b/laporan.txt new file mode 100644 index 0000000000000000000000000000000000000000..c7b961f5682206ce0ee5605f866ecf55f2500814 --- /dev/null +++ b/laporan.txt @@ -0,0 +1,31 @@ +ta 50.000 +Thread 1 : 0.066235 +Thread 8 : 0.025905 +Thread 16 : 0.016285 +Thread 32 : 0.027269 +Thread 64 : 0.035011 +Thread 128 : + +Data 100.000 +Thread 1 : 0.068898 +Thread 8 : 0.045690 +Thread 16 : 0.040743 +Thread 32 : 0.042587 +Thread 64 : 0.050379 +Thread 128 : + +Data 200.000 +Thread 1 : 0.250969 +Thread 8 : 0.202466 +Thread 16 : 0.137942 +Thread 32 : 0.119361 +Thread 64 : 0.133145 +Thread 128 : + +Data 400.000 +Thread 1 : 0.582148 +Thread 8 : 0.420642 +Thread 16 : 0.584418 +Thread 32 : 0.390922 +Thread 64 : 0.480704 +Thread 128 :