Jamie Jackson
Jamie Jackson

Reputation: 96

Looping through an MPI ring in C

I'm having trouble figuring out how to do multiple iterations in my MPI code. Just from testing multiple for loops in different spots tells me I am doing something wrong. Part of our instructions are to pass the token around the ring multiple times specified in the command line arguments, but i cant seem to get it to work correctly by even hard coding the iterations. I left the top for loop in(Which doesn't work) but commented out the others.

This is probably a more complicated task than I am anticipating. Any help is appreciated

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

int main(int argc, char* argv[])
{
    double starttime, endtime;
    MPI_Init(&argc, &argv);
    starttime = MPI_Wtime();
    int world_rank;
    int world_size;

    for (int i = 0; i < 3; i++)

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

//pass the token

        int token =1000;

//for (int i = 0; i < 3; i++)
//{
    if (world_rank == 0)
    {
        token = 0;
        MPI_Send(&token, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

        MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD, 
        MPI_STATUS_IGNORE);

        printf("FINAL TOKEN %d\n", token);
    }
    else if(world_rank < world_size - 1 )
    {
        MPI_Recv(&token, 1, MPI_INT, world_rank-1, 0, MPI_COMM_WORLD, 
        MPI_STATUS_IGNORE);

        printf("%d has token %d sending to %d\n", world_rank, token, 
        world_rank+1);
        token = token + 2;

        MPI_Send(&token, 1, MPI_INT, world_rank+1, 0, MPI_COMM_WORLD);
        }
    else
        {
        MPI_Recv(&token, 1, MPI_INT, world_rank-1, 0, MPI_COMM_WORLD, 
        MPI_STATUS_IGNORE);
        printf("%d has token %d sending to %d\n", world_rank, token, 
        world_rank+1);
        token = token + 2;
        MPI_Send(&token, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
        }
    endtime = MPI_Wtime();
    printf("That took %f seconds\n",endtime-starttime);
    }
    MPI_Finalize();

    return 0;
}

Here's the compile and run output:

$ mpicc -std=c99 test.c
$ mpirun -np 3 ./a.out
1 has token 0 sending to 2
That took 0.000187 seconds
2 has token 2 sending to 3
That took 0.000145 seconds
FINAL TOKEN 4
That took 0.000180 seconds
FINAL TOKEN 4
That took 0.000212 seconds
2 has token 2 sending to 3
That took 0.000202 seconds
2 has token 2 sending to 3
That took 0.000233 seconds
1 has token 0 sending to 2
That took 0.000291 seconds
1 has token 0 sending to 2
That took 0.000322 seconds
FINAL TOKEN 4
That took 0.000244 seconds

Upvotes: 0

Views: 2422

Answers (1)

Steve
Steve

Reputation: 1587

As noted in this comment, the for loop is missing braces {} and the token would be reset each iteration of the loop.

Also, you can remove one of your cases and include world_rank==world_size with the modulo operator %.

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

int main(int argc, char* argv[])
{
    double starttime, endtime;
    MPI_Init(&argc, &argv);
    starttime = MPI_Wtime();
    int world_rank;
    int world_size;

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

    //pass the token

    int token =1000;

    for (int i = 0; i < 3; i++)
    {
        if (world_rank == 0)
        {
            // token = 0; removed
            MPI_Send(&token, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

            MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD, 
            MPI_STATUS_IGNORE);

            printf("FINAL TOKEN %d\n", token);
        }
        else
        {
            MPI_Recv(&token, 1, MPI_INT, world_rank-1, 0, MPI_COMM_WORLD, 
            MPI_STATUS_IGNORE);

            printf("%d has token %d sending to %d\n", world_rank, token, 
            (world_rank+1)%world_size); // modified
            token = token + 2;

            MPI_Send(&token, 1, MPI_INT, (world_rank+1)%world_size, 0, MPI_COMM_WORLD); // modified
        }

        endtime = MPI_Wtime();
        printf("That took %f seconds\n",endtime-starttime);
    }
    MPI_Finalize();

    return 0;
}

Upvotes: 3

Related Questions