diff --git a/mpi_hostfile b/mpi_hostfile index 7753b6542c073b8001045de391eca106d7e270ac..1aab781c29869ebddbe342b46de587fe30d3c24d 100644 --- a/mpi_hostfile +++ b/mpi_hostfile @@ -1,6 +1,5 @@ #daftar host localhost -167.205.35.25 167.205.35.26 167.205.35.28 167.205.35.29 diff --git a/send b/send index 6f64c5ec9c89c4a16ad07fa456193fb021e41baa..e1de1070a06326219eb18e663b5403a02ae42ed2 100755 Binary files a/send and b/send differ diff --git a/send.c b/send.c index 9ca1060c25d62db168043e36ad3a250b3ee8dc7e..570fbcd6b2f5cc39de96a9a5adc75e473848ab57 100644 --- a/send.c +++ b/send.c @@ -1,5 +1,6 @@ #include "mpi.h" #include <stdio.h> +#include <stdlib.h> struct sub_array { int *data; @@ -10,10 +11,10 @@ struct sub_array { }; int *create_rand_nums(int num_elements) { - int *rand_nums = (float *)malloc(sizeof(float) * num_elements); + int *rand_nums = (int *)malloc(sizeof(int) * num_elements); int i; for (i = 0; i < num_elements; i++) { - rand_nums[i] = (rand() / (int)num_elements); + rand_nums[i] = (rand() % num_elements); } return rand_nums; } @@ -30,10 +31,13 @@ int *insertion_sort(int *arr, int size){ d--; } } + return arr; } int main(int argc, char **argv) { - int numtasks, rank, dest, source, rc, count, tag=1; + double total_time = 0.0; + total_time -= MPI_Wtime(); + int numtasks, rank, dest, source, rc, count, tag=1; MPI_Status Stat; int i,j; @@ -53,66 +57,77 @@ int main(int argc, char **argv) { int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); - if (rank == 0) { + if (world_rank == 0) { int *rand_array = create_rand_nums(N); + for (i = 0; i <=10; i++){ + printf("%d ", rand_array[i]); + } + printf("\n"); struct sub_array arr_of_sub[world_size]; /* Create sub arrays for each process */ for (i = 0; i < world_size; i++){ arr_of_sub[i].data = (int *) malloc (N * sizeof(int)); arr_of_sub[i].id = i+1; - arr_of_sub[i].lower_bound = N/world_size*i; - arr_of_sub[i].upper_bound = N/world_size*(i+1); + arr_of_sub[i].lower_bound = N/(world_size)*i; + arr_of_sub[i].upper_bound = N/(world_size)*(i+1); arr_of_sub[i].size = 0; + printf("up %d - down %d\n",arr_of_sub[i].upper_bound, arr_of_sub[i].lower_bound); } /* Assign each element to sub array */ for (i = 0; i < world_size; i++){ - for (j = 0; i < N; i++){ - if (rand_array[j] < arr_of_sub[i].upper_bound && rand_array[j] > arr_of_sub[i].lower_bound){ - arr_of_sub[i].size++; + for (j = 0; j < N; j++){ + if (rand_array[j] < arr_of_sub[i].upper_bound && rand_array[j] >= arr_of_sub[i].lower_bound){ arr_of_sub[i].data[arr_of_sub[i].size] = rand_array[j]; + arr_of_sub[i].size++; } } } /* Send each sub array to each process */ - for (i = 0; i < world_size; i++){ - printf("size %d = %d\n", i, arr_of_sub[i].size); - rc = MPI_Send(&arr_of_sub[i].size, 1, MPI_INT, (i+1), tag, MPI_COMM_WORLD); - rc = MPI_Send(&arr_of_sub[i].data, arr_of_sub[i].size, MPI_INT, (i+1), tag, MPI_COMM_WORLD); - } - + for (i = 1; i < world_size; i++){ + rc = MPI_Send(&arr_of_sub[i].size, 1, MPI_INT, i, tag, MPI_COMM_WORLD); + rc = MPI_Send(arr_of_sub[i].data, arr_of_sub[i].size, MPI_INT, i, tag, MPI_COMM_WORLD); + } + /* Receive sorted sub array from each process */ - for (i = 0; i < world_size; i++){ - rc = MPI_Recv(&arr_of_sub[i].data, arr_of_sub[i].size, MPI_INT, i+1, tag, MPI_COMM_WORLD, &Stat); + for (i = 1; i < world_size; i++){ + rc = MPI_Recv(arr_of_sub[i].data, arr_of_sub[i].size, MPI_INT, i, tag, MPI_COMM_WORLD, &Stat); } + printf("size %d\n", arr_of_sub[0].size); + arr_of_sub[0].data = insertion_sort(arr_of_sub[0].data, arr_of_sub[0].size); /* Gabungin arraynya */ int x = 0; for (i = 0; i < world_size; i++){ for(j = 0; j < arr_of_sub[i].size; j++){ rand_array[x] = arr_of_sub[i].data[j]; - x++; + x++; } } + total_time += MPI_Wtime(); - } else if (rank == 1) { - int *sub_arr; - int size; - + for (i = 0; i < N; i++){ + printf("%d ",rand_array[i]); + } + printf("\n"); + printf("total time = %lf\n", total_time); + } else { /* Receive unsorted array from root process */ - rc = MPI_Recv(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &Stat); - rc = MPI_Recv(&sub_arr, size, MPI_INT, 0, tag, MPI_COMM_WORLD, &Stat); + int size; + rc = MPI_Recv(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &Stat); + int *sub_arr = (int *) malloc (N* sizeof(int)); + rc = MPI_Recv(sub_arr, size, MPI_INT, 0, tag, MPI_COMM_WORLD, &Stat); printf("size %d\n", size); /* Sort the array using insertion sort */ sub_arr = insertion_sort(sub_arr,size); /* Send sorted array to root process */ - rc = MPI_Send(&sub_arr, size, MPI_INT, 0, tag, MPI_COMM_WORLD); + rc = MPI_Send(sub_arr, size, MPI_INT, 0, tag, MPI_COMM_WORLD); } - + MPI_Finalize(); }