diff --git a/src/BubbleSort.c b/src/BubbleSort.c new file mode 100644 index 0000000000000000000000000000000000000000..8b752e4425245c4e1235d1e0e95fe6160a9b1662 --- /dev/null +++ b/src/BubbleSort.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +// Bubble Sort +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n - 1; i++){ + for (j = 0; j < n - i - 1; j++){ + if (arr[j] > arr[j + 1]) + { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +// Menuliskan array ke layar +void PrintArray(int T[], int a, int b) +{ + for (int i = a; i <= b - 1; i++) + { + printf("%d ", T[i - 1]); + } + printf("%d\n", T[b - 1]); +} + +int main() +{ + int T[5000]; + int N; + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", &T[i]); + } + + PrintArray(T, 1, N); + int t = clock(); + bubbleSort(T, N); + t = clock() - t; + printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000); + return 0; +} \ No newline at end of file diff --git a/src/Coba.c b/src/Coba.c new file mode 100644 index 0000000000000000000000000000000000000000..3a98195636abc7846703dfeae6c84e819b317312 --- /dev/null +++ b/src/Coba.c @@ -0,0 +1,95 @@ +#include <mpi.h> +#include <stdio.h> +#include <string.h> +#include <time.h> + +// Menuliskan array ke layar +void PrintArray(int T[], int a, int b) +{ + for (int i = a; i <= b - 1; i++) + { + printf("%d ", T[i - 1]); + } + printf("%d\n", T[b - 1]); +} + +// Melakukan partisi sembari menukar value dengan pivot ke-i +void Partisi(int T[], int i, int j, int *k) +{ + int pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1] < pivot) + { + p++; + } + while (T[q - 1] > pivot) + { + q--; + } + if (p < q) + { + int temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = temp; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void SequentialQuickSort(int T[], int i, int j) +{ + int k; + if (i < j) + { + Partisi(T, i, j, &k); + SequentialQuickSort(T, i, k); + SequentialQuickSort(T, k + 1, j); + } +} + +int main() +{ + int my_rank, comm_sz, n; + int T[5000]; + int N; + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); + MPI_Comm_size(MPI_COMM_WORLD, &comm_sz); + + if (my_rank == 0){ + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", &T[i]); + } + printf("Hello World"); + double t = MPI_Wtime(); + // QuickSort(T, 1, N); + // MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); + // MPI_Bcast(&T, 5000, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Send(&T, 5000, MPI_INT, 0, 0, MPI_COMM_WORLD); + printf("Hello World"); + t = MPI_Wtime() - t; + printf("Waktu Eksekusi: %.4f ms\n", t / CLOCKS_PER_SEC * 1000); + + } else { + // MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); + // MPI_Bcast(&T, 5000, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Recv(&T, 5000, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + // printf("Process %d: After MPI_Bcast, value is %d\n", my_rank, N); + } + + PrintArray(T, 1, N); + return 0; +} \ No newline at end of file diff --git a/src/MPI_Communication.c b/src/MPI_Communication.c new file mode 100644 index 0000000000000000000000000000000000000000..70dc805455f1901f9aa25133fd66326a73c97e4c --- /dev/null +++ b/src/MPI_Communication.c @@ -0,0 +1,25 @@ +#include <mpi.h> +#include <stdio.h> + +int main() +{ + int id, size, x; + + MPI_Init(NULL, NULL); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &id); + + // P0 mengirim pesan x ke P1 + // P1 menerima pesan dari P0, disimpan ke x + if (id == 0) { + x = 6; + MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); + } + if (id == 1) + MPI_Recv(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, NULL); + + printf("P[%d] x: %d\n", id, x); + + MPI_Finalize(); + return 0; +} \ No newline at end of file diff --git a/src/MPI_HelloWorld.c b/src/MPI_HelloWorld.c index 7146ffd8262acd40764effde180fc1a3e93ded66..22c55b9e040f89ceb3cde7cf40e205db325e8a3d 100644 --- a/src/MPI_HelloWorld.c +++ b/src/MPI_HelloWorld.c @@ -1,27 +1,17 @@ #include <mpi.h> #include <stdio.h> -int main(int argc, char** argv) { - // Initialize the MPI environment - MPI_Init(NULL, NULL); +int main(int argc, char **argv) +{ + int size, id; - // Get the number of processes - int world_size; - MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &id); - // Get the rank of the process - int world_rank; - MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); - - // Get the name of the processor - char processor_name[MPI_MAX_PROCESSOR_NAME]; - int name_len; - MPI_Get_processor_name(processor_name, &name_len); - - // Print off a hello world message - printf("Hello world from processor %s, rank %d out of %d processors\n", - processor_name, world_rank, world_size); - - // Finalize the MPI environment. + if (id > 0){ + printf("Hello world!(%d)\n", id); + } MPI_Finalize(); + return 0; } \ No newline at end of file diff --git a/src/MPI_QuickSort.c b/src/MPI_QuickSort.c new file mode 100644 index 0000000000000000000000000000000000000000..b8fcceb597c97cac6a2ba76d74f77f4fda40f8a3 --- /dev/null +++ b/src/MPI_QuickSort.c @@ -0,0 +1,129 @@ +#include "mpi.h" +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +// #include <omp.h> + +void initArray(int *arr, int n) { + int i; + for (i = 0; i < n; ++i) + { + arr[i] = rand() % n; + } +} + +void printArray(int *arr, int n) { + int i; + for (i = 0; i < n; ++i) + { + printf("%d\n", arr[i]); + } +} + +void swap(int *arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +void quicksortNormal(int *arr, int lo, int hi) { + if (lo < hi) { + int x = arr[lo]; + int pivot = lo; + for (int i = lo + 1; i < hi; ++i) + { + if (arr[i] <= x) { + pivot++; + swap(arr, pivot, i); + } + } + swap(arr, lo, pivot); + + quicksortNormal(arr, lo, pivot); + quicksortNormal(arr, pivot + 1, hi); + } +} + +void quicksort(int *arr, int lo, int hi, int rank, int numtasks, int rank_index) { + int dest = rank + (1 << rank_index); + + if (dest >= numtasks) { + quicksortNormal(arr,lo,hi); + } else if (lo < hi) { + int x = arr[lo]; + int pivot = lo; + + for (int i = lo + 1; i < hi; ++i) + { + if (arr[i] <= x) { + pivot++; + swap(arr, pivot, i); + } + } + swap(arr, lo, pivot); + + if (pivot - lo > hi - pivot - 1) { + MPI_Send(&arr[pivot + 1], hi - pivot - 1, MPI_INT, dest, 1, MPI_COMM_WORLD); + quicksort(arr, lo, pivot, rank, numtasks, rank_index + 1); + MPI_Recv(&arr[pivot + 1], hi - pivot - 1, MPI_INT, dest, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } else { + MPI_Send(&arr[lo], pivot - lo, MPI_INT, dest, 1, MPI_COMM_WORLD); + quicksort(arr, pivot + 1, hi, rank, numtasks, rank_index + 1); + MPI_Recv(&arr[lo], pivot - lo, MPI_INT, dest, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + } +} + +int main(int argc, char** argv) { + srand(1707); + int numtasks, rank; + int *tempArr; + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD, &numtasks); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Status msg; + + printf("%d rank\n", rank); + if (rank == 0) { + int n; + int arr[n]; + + scanf("%d", &n); + // for (int i = 0; i < n; i++) + // { + // scanf("%d", &arr[i]); + // } + + initArray(arr, n); + + double start = MPI_Wtime(); + printf("%d rank\n", rank); + quicksort(arr, 0, n, rank, numtasks, 0); + double finish = MPI_Wtime(); + + printArray(arr, n); + printf("N: %d\n", n); + printf("Time: %.4f seconds\n", finish-start); + + } else { + int rc; + int arrSize; + int source; + int index_count = 0; + while(1 << index_count <= rank) + index_count ++; + + rc = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &msg); + rc = MPI_Get_count(&msg, MPI_INT, &arrSize); + source = msg.MPI_SOURCE; + + tempArr = (int *) malloc(sizeof(int)*arrSize); + rc = MPI_Recv(tempArr, arrSize, MPI_INT, source, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + quicksort(tempArr, 0, arrSize, rank, numtasks, index_count); + rc = MPI_Send(tempArr, arrSize, MPI_INT, source, 1, MPI_COMM_WORLD); + free(tempArr); + } + printf("LOL %d\n", rank); + MPI_Finalize(); + return 0; +} diff --git a/src/MST_Serial.c b/src/MST_Serial.c index 78971ddaf750a8a6556e6d4a3ec91f77bbfa82c6..1da7b06e21492c765bb36be6bacaf092bff58b9d 100644 --- a/src/MST_Serial.c +++ b/src/MST_Serial.c @@ -185,10 +185,7 @@ void KruskalMST(Graph *graph, Edge result[], int *e) bubbleSort(graph->edge, graph->E, 1); // Step 2: Allocate memory for creating V subsets Subset *subsets = CreateSubset(V); -<<<<<<< HEAD -======= ->>>>>>> fd8239d3b13d39b3a279991ba4a69031b1378216 while (((*e) < V - 1) && (i < graph->E)) { // Pick the smallest edge of the graph diff --git a/src/QuickSort.c b/src/QuickSort.c index 0074171c7575e069927ab8c6e8a1054c53e83d84..58e61cc1f120c01a03ea1464bcc5c277fa868945 100644 --- a/src/QuickSort.c +++ b/src/QuickSort.c @@ -1,4 +1,7 @@ #include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> // Menuliskan array ke layar void PrintArray(int T[], int a, int b) @@ -67,7 +70,9 @@ int main() } PrintArray(T, 1, N); + int t = clock(); QuickSort(T, 1, N); - PrintArray(T, 1, N); + t = clock() - t; + printf("Waktu Eksekusi: %f ms\n", ((double)t) / CLOCKS_PER_SEC * 1000); return 0; } \ No newline at end of file diff --git a/src/Test.c b/src/Test.c new file mode 100644 index 0000000000000000000000000000000000000000..bf7e5e134b0ba4326f8ae614d3385a6ca8804149 --- /dev/null +++ b/src/Test.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include "mpi.h" + + +//////////// +//MPI_Bcast +//////////// +// +// int MPI_Bcast( void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm ) +// +// This example simply uses MPI_Bcast to broadcast a read in value to all other processes from root process +// +// example usage: +// compile: mpicc -o mpi_bcast mpi_bcast.c +// run: mpirun -n 4 mpi_bcast +// +int main(argc, argv) +int argc; +char **argv; +{ + int rank, value; + + MPI_Init(&argc, &argv); + + MPI_Comm_rank(MPI_COMM_WORLD, &rank); //what rank is the current processor + + if (rank == 0) { + // if root process we read the value to broadcast + printf("Enter a number to broadcast:\n"); + scanf("%d", &value); + } else { + printf("process %d: Before MPI_Bcast, value is %d\n", rank, value); + } + + // each processor calls MPI_Bcast, data is broadcast from root processor and ends up in everyone value variable + // root process uses MPI_Bcast to broadcast the value, each other process uses MPI_Bcast to receive the broadcast value + MPI_Bcast(&value, 1, MPI_INT, 0, MPI_COMM_WORLD); + + printf("process %d: After MPI_Bcast, value is %d\n", rank, value); + + MPI_Finalize(); + + return 0; +} \ No newline at end of file diff --git a/src/hello b/src/hello deleted file mode 100755 index 1de23036653bc3c34fc555ce1c6d1e0e66f45edb..0000000000000000000000000000000000000000 Binary files a/src/hello and /dev/null differ