diff --git a/send_rec b/send_rec new file mode 100755 index 0000000000000000000000000000000000000000..084d3925b9a6abf51649c3a0c49a668bcf4e7ab0 Binary files /dev/null and b/send_rec differ diff --git a/send_recv b/send_recv new file mode 100755 index 0000000000000000000000000000000000000000..b7f088a0412287604562b5f1614225b1f158db49 Binary files /dev/null and b/send_recv differ diff --git a/send_recv.c b/send_recv.c new file mode 100644 index 0000000000000000000000000000000000000000..c9aa6da7fb669bcfd063119c148175530c4f1b17 --- /dev/null +++ b/send_recv.c @@ -0,0 +1,81 @@ +// Copyright www.computing.llnl.gov +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <mpi.h> +#include <assert.h> + +float *create_rand_nums(int num_elements) { + float *rand_nums = (float *)malloc(sizeof(float) * num_elements); + assert(rand_nums != NULL); + int i; + for (i = 0; i < num_elements; i++) { + rand_nums[i] = rand() % num_elements; + } + return rand_nums; +} + +int main(int argc, char *argv[]) { + int num_elements = atoi(argv[1]); + + int tag = 1; + MPI_Status Stat; + MPI_Init(NULL, NULL); + int world_size; + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + int world_rank; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + + int num_elements_per_proc = num_elements / world_size; + + float *rand_nums = NULL; + + rand_nums = create_rand_nums(num_elements); + int i; + for (i = 0; i < num_elements; i++) { + printf("elemen ke-%d: %f\n", i, rand_nums[i]); + } + + // TODO: + // 1. buat elemen randomm + // 2. bagi elemen tersebut ke dalam bucket (jumlah bucket = world_size) + // 3. kirim ukuran bucket serta isi bucket ke proses yang sessuai + // 4. urutkan masing masing bucket di tiap proses + // 5. kirim kembali bucket yang terurut ke proses 0 + // 6. gabungkan bucket tersebut menjadi satu array + + + + // Create random elements + // float *rand_nums = NULL; + // if (world_rank == 0) { + // int i, j; + // rand_nums = create_rand_nums(num_elements); + + // for (i = 0; i < num_elements; i++) { + // printf("%d\n", rand_nums[i]); + // } + + // // Send sub elements to every process + // int counter = 0; + + // for (i = 0; i < world_size; i++) { + // float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc); + // for (j = 0; j < num_elements_per_proc; j++) { + // sub_rand_nums[j] = 1; + // counter++; + // } + // MPI_Send(sub_rand_nums, num_elements_per_proc, MPI_FLOAT, i, tag, MPI_COMM_WORLD); + // } + // } + + // float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc); + // MPI_Recv(sub_rand_nums, num_elements_per_proc, MPI_FLOAT, 0, tag, MPI_COMM_WORLD, &Stat); + + // printf("From procces %d\n", world_rank); + // int i; + // for (i = 0; i < num_elements_per_proc; i++) { + // printf("elemen ke-%d: %d\n", i, sub_rand_nums[i]); + // } + MPI_Finalize(); +}