Konstantinos
Konstantinos

Reputation: 973

MPI C how to access struct/variables of other rank

So if i have a C code running with MPI and a struct like that:

typedef struct Node{
    int succ;
    int pred;
    int has_token;
    char state;
}node;

can a rank access another rank node?
For example i have:

//What i want:
if(rank==0){
    //so rank 0.state               lets say i want rank 2.state
    if(currentRankNode.state=='I' && someOtherRankNode.state=='S'){
        //do_smth
    }
}

And the question is, what should i replace someOtherRankNode with to get for example rank 2's node and afterwards the state?

Upvotes: 0

Views: 75

Answers (2)

gagiuntoli
gagiuntoli

Reputation: 530

No, at least you need one communication routine like MPI_Send, MPI_BCast, MPI_AllGather in order to send someOtherRankNode.state to rank == 0. For example:

#include <stdio.h>
#include <mpi.h>

int main(void)
{
    char rank_0_state;
    char rank_1_state;

    MPI_Init(NULL, NULL);
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    if (world_size != 2)
        return 1;

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0)
        rank_0_state = 'I';

    if (rank == 1)
        rank_1_state = 'S';


    if (rank == 0) {
        MPI_Status status;
        MPI_Recv(&rank_1_state, 1, MPI_BYTE, 1, 0, MPI_COMM_WORLD, &status);
    }
    if (rank == 1)
        MPI_Send(&rank_1_state, 1, MPI_BYTE, 0, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        if (rank_0_state == 'I' && rank_1_state == 'S') {
            printf("I am rank 0 receiving the right value from rank 1\n");
        }
    }

    MPI_Finalize();

    return 0;
}

Try to use MPI_Type_struct for sending that structure properly across processes.

Upvotes: 1

Lurendrejer
Lurendrejer

Reputation: 692

With what little information you avail here, I think what you're asking is how to peek into the memory of another MPI process (i.e. to look at the current state of the process with rank == 2).

By default the memory spaces of MPI processes are completely separated by the operating system, even if running on the same physical machine. You'll have to synchronize state knowledge by the means of sending and receiving messages. In newer versions of MPI you can use features like MPI Windows: https://www.mpich.org/static/docs/v3.2/www3/MPI_Win_allocate_shared.html, although this is somewhat more complicated.

Upvotes: 0

Related Questions