diff --git a/.1.swn b/.1.swn new file mode 100644 index 0000000000000000000000000000000000000000..78f154a882f594e6b5312fdcc17c2a2280cb7047 Binary files /dev/null and b/.1.swn differ diff --git a/.1.swo b/.1.swo new file mode 100644 index 0000000000000000000000000000000000000000..0d260d303686db7e160298b1f583a535b02dda1c Binary files /dev/null and b/.1.swo differ diff --git a/.1.swp b/.1.swp new file mode 100644 index 0000000000000000000000000000000000000000..8b5e961fa3698199d23820d34adb387a851a0b18 Binary files /dev/null and b/.1.swp differ diff --git a/.10.swp b/.10.swp new file mode 100644 index 0000000000000000000000000000000000000000..3c618d39e045be95c3a7f4758e713de879029b62 Binary files /dev/null and b/.10.swp differ diff --git a/.100.swp b/.100.swp new file mode 100644 index 0000000000000000000000000000000000000000..cd25c788f433ee79b9f0c0281da828403c32c42a Binary files /dev/null and b/.100.swp differ diff --git a/.2.swo b/.2.swo new file mode 100644 index 0000000000000000000000000000000000000000..ba13b5fddaea103f9477e07ae9ab7eace8112faa Binary files /dev/null and b/.2.swo differ diff --git a/.2.swp b/.2.swp new file mode 100644 index 0000000000000000000000000000000000000000..4168df0d7afe19e19496412261bd88ad0f8dc78b Binary files /dev/null and b/.2.swp differ diff --git a/.6.swp b/.6.swp new file mode 100644 index 0000000000000000000000000000000000000000..d209c805f0f1b696712e22aaa579a58877798fc4 Binary files /dev/null and b/.6.swp differ diff --git a/bucket b/bucket new file mode 100755 index 0000000000000000000000000000000000000000..8db9d832eff22e82836d5eba8f83963d09a66152 Binary files /dev/null and b/bucket differ diff --git a/bucket.c b/bucket.c new file mode 100644 index 0000000000000000000000000000000000000000..3a6197cc3317b31d8ab96766b8ba4e3a0498bb8f --- /dev/null +++ b/bucket.c @@ -0,0 +1,112 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <mpi.h> +#include <assert.h> +#include <string.h> + +void insertion_sort(int* array, int n) +{ + int c, d, t; + for (c = 1 ; c <= n - 1 && array[c] != -1; c++) { + d = c; + + while ( d > 0 && array[d] < array[d-1]) { + t = array[d]; + array[d] = array[d-1]; + array[d-1] = t; + + d--; + } + } +} + +int *create_rand_nums(int num_elements) { + int *rand_nums = (int *)malloc(sizeof(int) * (num_elements+5)); + assert(rand_nums != NULL); + int i; + for (i = 0; i < num_elements; i++) { + rand_nums[i] = (abs((int)rand()) % num_elements); + } + return rand_nums; +} + + + +int main(int argc, char** argv) { + if (argc != 2) { + fprintf(stderr, "Usage: avg num_elements_per_proc\n"); + exit(1); + } + + int num_elements_per_proc = atoi(argv[1]); + srand(time(NULL)); + + MPI_Init(NULL, NULL); + + int world_rank; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + int world_size; + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + + + int *rand_nums = NULL; + if (world_rank == 0) { + rand_nums = create_rand_nums(num_elements_per_proc); + } + + time_t start_t; + if(world_rank == 0) + start_t = clock(); + + int * all_bucket; + int * offset; + int range = (num_elements_per_proc+world_size)/world_size; + if(world_rank == 0) { + all_bucket = (int *) malloc(sizeof(int) * num_elements_per_proc * (world_size+5)); + offset = (int *) malloc(sizeof(int) * (world_size+5)); + memset(all_bucket, -1, sizeof(int)*num_elements_per_proc*world_size); + int i; + for(i = 0; i < world_size; i++) { + offset[i] = i*num_elements_per_proc; + } + for(i = 0; i < num_elements_per_proc; i++) { + int no_group = rand_nums[i]/range; + all_bucket[offset[no_group]++] = rand_nums[i]; + } + } + + + + int *sub_rand_nums = (int *)malloc(sizeof(int) * +num_elements_per_proc); + assert(sub_rand_nums != NULL); + + MPI_Scatter(all_bucket, num_elements_per_proc, MPI_INT, sub_rand_nums, +num_elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD); + + insertion_sort(sub_rand_nums,num_elements_per_proc); + MPI_Gather(sub_rand_nums, num_elements_per_proc, MPI_INT, all_bucket, num_elements_per_proc, MPI_INT, 0, +MPI_COMM_WORLD); + + if (world_rank == 0) { + int i, j; + j = 0; + for(i = 0; i<num_elements_per_proc*world_size; i++) + if(all_bucket[i] != -1) + rand_nums[j++] = all_bucket[i]; + time_t finish_t = clock(); + for(i = 0; i<num_elements_per_proc; i++) + printf("%d\n", rand_nums[i]); + + printf("Running time %d data of %d process : %.5f ms\n", num_elements_per_proc, world_size, (double)(finish_t-start_t)*1000/CLOCKS_PER_SEC); + free(rand_nums); + free(all_bucket); + free(offset); + } + free(sub_rand_nums); + + MPI_Barrier(MPI_COMM_WORLD); + MPI_Finalize(); +} + diff --git a/insertion.c b/insertion.c new file mode 100644 index 0000000000000000000000000000000000000000..e9c893fb70db1eadaa931533bca7a8c6ba3fad67 --- /dev/null +++ b/insertion.c @@ -0,0 +1,35 @@ +#include <stdio.h> + +int main() +{ + int n, array[1000], c, d, t; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) { + scanf("%d", &array[c]); + } + + for (c = 1 ; c <= n - 1; c++) { + d = c; + + while ( d > 0 && array[d] < array[d-1]) { + t = array[d]; + array[d] = array[d-1]; + array[d-1] = t; + + d--; + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c <= n - 1; c++) { + printf("%d\n", array[c]); + } + + return 0; +} diff --git a/mpi_hostfile b/mpi_hostfile new file mode 100644 index 0000000000000000000000000000000000000000..d19b4b1c76d1317822a86f7a9b4afaef00760072 --- /dev/null +++ b/mpi_hostfile @@ -0,0 +1,7 @@ +localhost +167.205.35.25 +167.205.35.26 +167.205.35.28 +167.205.35.29 +167.205.35.30 +167.205.35.31