diff --git a/omp_buckle.c b/omp_buckle.c new file mode 100644 index 0000000000000000000000000000000000000000..7c0273072849024dcfe5afc18b3e028af5f404eb --- /dev/null +++ b/omp_buckle.c @@ -0,0 +1,88 @@ +#include <stdio.h> +#include <omp.h> +#include <stdlib.h> + +int main(int argc, char** argv){ + int numtasks, rank, dest, source, rc, count, tag=1; + + numtasks = strtol(argv[1], NULL, 10); + + int bucketNEff; + int bucketsNEff[numtasks]; + int i; + int n; + int *arr; + int *input; + double sendtime=0.0 , sorttime=0.0; + int min,max; + + srand(time(NULL)); + printf("Input number of element"); + n = strtol(argv[2], NULL, 10); + input=(int*)malloc(sizeof(int)*n); + printf("%d number of elements\n", n); + + int c; + for (c = 0; c < n; c++) { + input[c] = abs(rand() % 1000000); + printf("%d. %d\n", c, input[c]); + } + + min = input[0]; + max = input[0]; + for (c = 0; c < n; c++) { + if(input[c] < min) { + min = input[c]; + } + if(input[c] > max) { + max = input[c]; + } + } + } + + printf("numtasks: %d\n", numtasks); + + + int j; + for(j=1; j<numtasks; j++){ + bucketsNEff[j] = 0; + } + + for(i=0; i<n; i++){ + int inc = max/(numtasks-1); + int iteration = 1; + int k; + int p = 0; //false + for(k=inc; k<=max; k+=inc){ + if(input[i] <= k){ + bucketsNEff[iteration]++; + p = 1; + break; + } + iteration++; + } + if(!p){ + bucketsNEff[iteration-1]++; + } + } + + #pragma omp parallel for num_threads(numtasks) + for(i=0; i<numtasks; i++){ + int c, d, t; + for (c = 1 ; c <= n; c++) { + d = c; + while ( d > 0 && input[d] < input[d-1]) { + t = input[d]; + input[d] = input[d-1]; + input[d-1] = t; + d--; + } + } + } + + printf("\nAvg send time = %lf\n", sendtime / numtasks); + printf("Avg sort time = %lf\n", sorttime / numtasks); + } + + return 0; +} diff --git a/omp_hello b/omp_hello new file mode 100755 index 0000000000000000000000000000000000000000..447837691ff1fd501bab86be97920ee2e83cf16a Binary files /dev/null and b/omp_hello differ diff --git a/omp_hello.c b/omp_hello.c new file mode 100644 index 0000000000000000000000000000000000000000..1f5dec6b5ad725ad619d87820c51698ced5dd9b8 --- /dev/null +++ b/omp_hello.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> +#include <omp.h> + +void Hello(void); /* Thread function */ + +int main(int argc, char *argv[]) { + int thread_count = strtol(argv[1], NULL, 10); + + #pragma omp parallel num_threads(thread_count) + Hello(); + + return 0; +} + +void Hello(void) { + int my_rank = omp_get_thread_num(); + int thread_count = omp_get_num_threads(); + + printf("Hello from thread %d of %d\n", + my_rank, thread_count); +} + diff --git a/omp_sort b/omp_sort new file mode 100755 index 0000000000000000000000000000000000000000..0dcae59f540ae44b6478a15ca22907d764da33b0 Binary files /dev/null and b/omp_sort differ diff --git a/omp_sort.c b/omp_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..ab404e55d8f26b811a1c1821ef2c1654f0b13e1a --- /dev/null +++ b/omp_sort.c @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <omp.h> +#include <stdlib.h> + +void insertionSort(int* arr, int size, int** sortedArr); +void bucketSort(int* arr, int size, int** sortedArr); + +int main(int argc, char** argv){ + int bucketNEff; + int thread_count = strtol(argv[1], NULL, 10); + int bucketsNEff[thread_count]; + + int* arr_final; + int* input; + //double sendtime=0.0 , sorttime=0.0; + //int min,max; + + srand(time(NULL)); + int n = strtol(argv[2], NULL, 10); + input = (int*) malloc (sizeof(int)*n); + printf("%d number of elements\n", n); + + int c; + for (c = 0; c < n; c++) { + input[c] = abs(rand() % 1000000); + printf("%d. %d\n", c, input[c]); + } + + # pragma omp parallel num_threads(thread_count) + bucketSort(input, n, &arr_final); + + for(c=0; c<n; c++){ + printf("%d ", arr_final[c]); + } + + /* + min = input[0]; + max = input[0]; + for (c = 0; c < n; c++) { + if(input[c] < min) { + min = input[c]; + } + if(input[c] > max) { + max = input[c]; + } + }*/ + return 0; +} + +void insertionSort(int* arr, int size, int** sortedArr){ + int c, d, t; + *sortedArr = (int*) malloc (sizeof(int) * size); + + for(c=0; c<size; c++){ + *sortedArr[c] = arr[c]; + } + + for (c = 1 ; c <= size - 1; c++) { + d = c; + + while ( d > 0 && arr[d] < arr[d-1]) { + t = *sortedArr[d]; + *sortedArr[d] = *sortedArr[d-1]; + *sortedArr[d-1] = t; + + d--; + } + } +} + +void bucketSort(int* arr, int size, int** sortedArr){ + int c; + *sortedArr = (int*) malloc (sizeof(int) * size); + + for(c=0; c<size; c++){ + sortedArr[c] = arr[c]; + } + + int my_rank = omp_get_thread_num(); + int thread_count = omp_get_num_threads(); + + double range = size/thread_count; + int i_awal, i_akhir; + i_awal = my_rank * range; + i_akhir = i_awal + range; + + int temp[range]; + int i; + for(i=0; i<range; i++){ + temp[i] = arr[ + } +}