Skip to content
Snippets Groups Projects
Forked from IF3230-2016 / OpenMPI_2
2 commits ahead of the upstream repository.
send_recv.c 2.42 KiB
// 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();
}