diff --git a/src/MST_OpenMPI.c b/src/MST_OpenMPI.c index 2c03b56006c20da1f41d2c23e90ba51032c70166..32a401d5c64bad6c80e35a948cdf7947cf3b4c5f 100644 --- a/src/MST_OpenMPI.c +++ b/src/MST_OpenMPI.c @@ -260,11 +260,14 @@ void SequentialQuickSort(Edge arr[], int i, int j) // Mengurutkan array dengan metode Quick Sort Parallel void QuickSort(Edge arr[], int i, int j, int rank, int max_rank, int rank_index){ + MPI_Datatype mpi_edge_type; + makeStruct(&mpi_edge_type); + int distributed_process = rank + pow2(rank_index); rank_index++; // If the rank of distributed process is higher than max rank - if (distributed_process > max_rank) { + if (distributed_process >= max_rank) { SequentialQuickSort(arr, i, j); } else { @@ -273,13 +276,13 @@ void QuickSort(Edge arr[], int i, int j, int rank, int max_rank, int rank_index) // Upper half of array is larger than lower half of array if (pivot-i > j-pivot-1){ - MPI_Send(&arr[pivot + 1], j - pivot - 1, MPI_INT, distributed_process, 1, MPI_COMM_WORLD); + MPI_Send(&arr[pivot + 1], j - pivot - 1, mpi_edge_type, distributed_process, 1, MPI_COMM_WORLD); QuickSort(arr, i, pivot, rank, max_rank, rank_index + 1); - MPI_Recv(&arr[pivot + 1], j - pivot - 1, MPI_INT, distributed_process, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&arr[pivot + 1], j - pivot - 1, mpi_edge_type, distributed_process, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } else { - MPI_Send(&arr[i], pivot - i, MPI_INT, distributed_process, 1, MPI_COMM_WORLD); + MPI_Send(&arr[i], pivot - i, mpi_edge_type, distributed_process, 1, MPI_COMM_WORLD); QuickSort(arr, pivot + 1, j, rank, max_rank, rank_index + 1); - MPI_Recv(&arr[i], pivot - i, MPI_INT, distributed_process, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&arr[i], pivot - i, mpi_edge_type, distributed_process, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } } } @@ -291,7 +294,8 @@ Graph *ReadArguments() Graph *graph = (Graph *)(malloc(sizeof(Graph))); int V, weight; - scanf("%d", &V); + // scanf("%d", &V); + V = 5000; graph->V = V; graph->edge = (Edge *)(malloc(((V * V - V) / 2) * sizeof(Edge))); @@ -301,7 +305,9 @@ Graph *ReadArguments() { for (int j = 0; j < V; j++) { - scanf("%d", &weight); + // scanf("%d", &weight); + int r = rand() % (2* V); + int weight = (r < 0.2 * V) ? -1: r; if ((weight != -1) && (i <= j)) { graph->edge[E].src = i; @@ -384,31 +390,31 @@ int compare_src(const void *a, const void *b) void PreQuickSort (Edge result[], int resultLength, int size, int rank) { - if (rank==0) { + // if (rank==0) { //Implementasi quicksort disini - QuickSort(result, 1, resultLength, rank, size, 0); + QuickSort(result, 1, resultLength, rank, size, 0); - } else { - MPI_Datatype mpi_edge_type; - makeStruct(&mpi_edge_type); + // } else { + // MPI_Datatype mpi_edge_type; + // makeStruct(&mpi_edge_type); - int idxcnt = 0; - int arrLength; - MPI_Status status; - while (rank >= pow2(idxcnt)) { - idxcnt = idxcnt+1; - } - - int source = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); - source = MPI_Get_count(&status, mpi_edge_type, &arrLength); - int src = status.MPI_SOURCE; - - Edge* temp = (Edge *) malloc (sizeof(Edge) *arrLength); - source = MPI_Recv(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - QuickSort(temp, 0, arrLength, rank, size, idxcnt); - source = MPI_Send(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD); - free(temp); - } + // int idxcnt = 0; + // int arrLength; + // MPI_Status status; + // while (rank >= pow2(idxcnt)) { + // idxcnt = idxcnt+1; + // } + + // int source = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + // source = MPI_Get_count(&status, mpi_edge_type, &arrLength); + // int src = status.MPI_SOURCE; + + // Edge* temp = (Edge *) malloc (sizeof(Edge) *arrLength); + // source = MPI_Recv(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + // QuickSort(temp, 0, arrLength, rank, size, idxcnt); + // source = MPI_Send(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD); + // free(temp); + // } } @@ -425,7 +431,7 @@ void KruskalMST(Graph *graph, Edge result[], int *e, int size, int rank) // QuickSortWeight(graph->edge, 0, graph->E, size, rank); printf("Process %d\n", rank); PreQuickSort(graph->edge, graph->E, size, rank); - + // PrintArray(graph->edge, 0, graph->E-1); // Step 2: Allocate memory for creating V subsets Subset *subsets = CreateSubset(V); @@ -443,6 +449,8 @@ void KruskalMST(Graph *graph, Edge result[], int *e, int size, int rank) } // QuickSortSrc(graph->edge, 0, graph->E, size, rank); + // QuickSortSrc(result, 1, (*e)); + qsort(result, *e, sizeof(result[0]), compare_src); return; } @@ -477,15 +485,39 @@ int main() int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); + if (rank == 0){ + Graph *graph= ReadArguments(); + Edge result[graph->V]; + int e = 0; + double t = MPI_Wtime(); + KruskalMST(graph, result, &e, size, rank); + t = MPI_Wtime() - t; + printResult(result, e); + printf("Waktu Eksekusi: %f ms \n", (t * 1000)); + } else { + printf("Process %d\n", rank); + MPI_Datatype mpi_edge_type; + makeStruct(&mpi_edge_type); + + int idxcnt = 0; + int arrLength; + MPI_Status status; + while (rank >= pow2(idxcnt)) { + idxcnt = idxcnt+1; + } + + int source = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + source = MPI_Get_count(&status, mpi_edge_type, &arrLength); + int src = status.MPI_SOURCE; + // arrLength = 100; + + Edge* temp = (Edge *) malloc (sizeof(Edge) *arrLength); + source = MPI_Recv(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + QuickSort(temp, 0, arrLength, rank, size, idxcnt); + source = MPI_Send(temp, arrLength, mpi_edge_type, src, 1, MPI_COMM_WORLD); + free(temp); + } - Graph *graph= ReadArguments(); - Edge result[graph->V]; - int e = 0; - int t = clock(); - KruskalMST(graph, result, &e, size, rank); - t = clock() - t; - 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/a.out b/src/a.out index ca0e3f3676b803bd3985abab5529eaf3eda0b2d9..c025e37d329922d797f1eb508f8b6361f8e36884 100755 Binary files a/src/a.out and b/src/a.out differ diff --git a/src/b.out b/src/b.out new file mode 100755 index 0000000000000000000000000000000000000000..242962d277a94b229fa5945ad3f7db3743b75f51 Binary files /dev/null and b/src/b.out differ diff --git a/src/c.out b/src/c.out new file mode 100755 index 0000000000000000000000000000000000000000..fe49bd9526854d08142ec62c9dc90cdb8d94b197 Binary files /dev/null and b/src/c.out differ diff --git a/src/log b/src/log new file mode 100644 index 0000000000000000000000000000000000000000..b20862adaba836b0e0e52e8f5b4aab971e28b9da Binary files /dev/null and b/src/log differ