diff --git a/src/MPI_pointtopoint.c b/src/MPI_pointtopoint.c new file mode 100644 index 0000000000000000000000000000000000000000..b3eb1b082b32d9c0457e47b606675359f7df30a4 --- /dev/null +++ b/src/MPI_pointtopoint.c @@ -0,0 +1,27 @@ +#include <mpi.h> +#include <stdio.h> + +int main() +{ + int id, x; + + MPI_Init(NULL, NULL); + 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); + MPI_Recv(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, NULL); + } + if (id == 1) + MPI_Recv(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, NULL); + x =7; + MPI_Send(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); + + printf("P[%d] x: %d\n", id, x); + + MPI_Finalize(); + return 0; +} \ No newline at end of file diff --git a/src/MST_OpenMP.c b/src/MST_OpenMP.c index 757d567f3cffdcd4e6796db0587833ede7bd2267..f71ec19e094fe5c73956db1bc167eb9316a922f4 100644 --- a/src/MST_OpenMP.c +++ b/src/MST_OpenMP.c @@ -147,15 +147,21 @@ int find(Subset subsets[], int i) // find root and make root as parent of i // (path compression) if (subsets[i].parent != i) + #pragma omp single subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(Subset subsets[], int x, int y) { + #pragma omp task int xroot = find(subsets, x); + + #pragma omp task int yroot = find(subsets, y); + #pragma omp taskwait + // Attach smaller rank tree under root of high // rank tree (Union by Rank) if (subsets[xroot].rank < subsets[yroot].rank) @@ -205,11 +211,16 @@ void KruskalMST(Graph *graph, Edge result[], int *e) // Pick the smallest edge of the graph Edge next_edge = graph->edge[i++]; + #pragma omp task int x = find(subsets, next_edge.src); + #pragma omp task int y = find(subsets, next_edge.dest); + #pragma omp taskwait + if (x != y) { result[(*e)++] = next_edge; + Union(subsets, x, y); } } diff --git a/src/MST_OpenMPI b/src/MST_OpenMPI new file mode 100755 index 0000000000000000000000000000000000000000..556099513aad8e81d4c4db15ab75e054b12b7459 Binary files /dev/null and b/src/MST_OpenMPI differ diff --git a/src/MST_OpenMPI.c b/src/MST_OpenMPI.c index ed877dbf584f19035e5b9c881c214da4960e1660..5ac42e2dd9ee1e58fe21f6417df8e757673249ca 100644 --- a/src/MST_OpenMPI.c +++ b/src/MST_OpenMPI.c @@ -24,6 +24,7 @@ typedef struct Edge *edge; } Graph; + // Create new Graph based on V vertices and E edges Graph *CreateGraph(int V, int E) { @@ -33,7 +34,95 @@ Graph *CreateGraph(int V, int E) graph->edge = (Edge *)(malloc(E * sizeof(Edge))); return graph; } +void PartisiSrc(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].src < pivot.src) + { + p++; + } + while (T[q - 1].src > pivot.src) + { + q--; + } + if (p < q) + { + Edge temp = T[p - 1]; + T[p - 1] = T[q - 1]; + T[q - 1] = Edge; + p++; + q--; + } + else if (p == q) + { + p++; + } + } + *k = q; +} + +// Mengurutkan array dengan menggunakan Quick Sort +void QuickSortSrc(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiSrc(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortSrc(T, i, k); + QuickSortSrc(T, k + 1, j); + } +} +void PartisiWeight(Edge T[], int i, int j, int *k) +{ + Edge pivot = T[i - 1]; + int p = i; + int q = j; + while (p <= q) + { + while (T[p - 1].weight < pivot.weight) + { + p++; + } + while (T[q - 1].weight > pivot.weight) + { + q--; + } + if (p < q) + { + Edge 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 QuickSortWeight(Edge T[], int i, int j) +{ + int k; + if (i < j) + { + PartisiWeight(T, i, j, &k); + // PrintArray(T, i, k); + // PrintArray(T, k + 1, j); + QuickSortWeight(T, i, k); + QuickSortWeight(T, k + 1, j); + } +} // Print all graph component to the CLI void PrintGraph(Graph *graph) { @@ -202,7 +291,7 @@ void KruskalMST(Graph *graph, Edge result[], int *e) { // Pick the smallest edge of the graph Edge next_edge = graph->edge[i++]; - + MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) @@ -218,8 +307,9 @@ void KruskalMST(Graph *graph, Edge result[], int *e) } void printResult(Edge *result, int e) -{ - // printf("%d\n", e); +{ // Get the number of processes + + int minimumCost = 0; // #pragma omp parallel for for (int i = 0; i < e; i++) @@ -228,15 +318,19 @@ void printResult(Edge *result, int e) minimumCost += result[i].weight; } printf("%d\n", minimumCost); - for (int i = 0; i < e; i++) + + + for (int i = start; i < numPartition; i++) { printf("%d-%d\n", result[i].src, result[i].dest); } } // Driver program to test above functions -int main(int argc, char** argv) +int main() { + + Graph *graph= ReadArguments(); MPI_Init(NULL, NULL); // Get the number of processes int world_size; @@ -247,13 +341,13 @@ int main(int argc, char** argv) MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); - Graph *graph = ReadArguments(); Edge result[graph->V]; int e = 0; int t = clock(); KruskalMST(graph, result, &e); t = clock() - t; - printResult(result, e); + // printResult(result, e); printf("Waktu Eksekusi: %f ms \n", ((double)t) / CLOCKS_PER_SEC * 1000); MPI_Finalize(); + return 0; } \ No newline at end of file diff --git a/src/MST_Serial.c b/src/MST_Serial.c index 78971ddaf750a8a6556e6d4a3ec91f77bbfa82c6..6f4a6437722c513d3cb808e7b96f1ef2c73d979e 100644 --- a/src/MST_Serial.c +++ b/src/MST_Serial.c @@ -185,10 +185,6 @@ 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/a b/src/a new file mode 100755 index 0000000000000000000000000000000000000000..80bbe4ed5ce9bfdf04b0344f38f98032aabeaa95 Binary files /dev/null and b/src/a differ