Jump to content

MPI: Scatter

From Haidhi Wiki
Revision as of 11:30, 21 March 2026 by SanzSkuy (talk | contribs) (Created page with "<syntaxhighlight lang=cpp> #include <mpi.h> #include <iostream> #include <vector> int main(int argc, char** argv) { MPI_Init(NULL, NULL); int world_rank, world_size; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); int data_per_process = 2; std::vector<int> send_buffer = {10, 20, 30, 40, 50, 60, 70, 80}; std::vector<int> recv_buffer(data_per_process); MPI_Scatter( send_buffer.data(),...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#include <mpi.h>
#include <iostream>
#include <vector>

int main(int argc, char** argv) {
    MPI_Init(NULL, NULL);

    int world_rank, world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int data_per_process = 2;
    std::vector<int> send_buffer = {10, 20, 30, 40, 50, 60, 70, 80};
    std::vector<int> recv_buffer(data_per_process);

    MPI_Scatter(
        send_buffer.data(), 
        data_per_process, 
        MPI_INT,
        recv_buffer.data(), 
        data_per_process, 
        MPI_INT,
        0, 
        MPI_COMM_WORLD
    );

    std::cout << "Rank " << world_rank << " dapet data: ";
    for (int val : recv_buffer) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    MPI_Finalize();
    return 0;
}