Skip to content

Instantly share code, notes, and snippets.

@wangyu-
Forked from varun-nagaraja/mpi_sample.c
Created April 5, 2019 13:59
Show Gist options
  • Select an option

  • Save wangyu-/fc9f4215c31399235305b0e3b993aac8 to your computer and use it in GitHub Desktop.

Select an option

Save wangyu-/fc9f4215c31399235305b0e3b993aac8 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous revised this gist Sep 19, 2011. No changes.
  2. @invalid-email-address Anonymous created this gist Sep 19, 2011.
    46 changes: 46 additions & 0 deletions mpi_sample.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #include <stdio.h>
    #include <string.h>
    #include "mpi.h"

    int main(int argc, char* argv[]){
    int my_rank; /* rank of process */
    int p; /* number of processes */
    int source; /* rank of sender */
    int dest; /* rank of receiver */
    int tag=0; /* tag for messages */
    char message[100]; /* storage for message */
    MPI_Status status ; /* return status for receive */

    /* start up MPI */

    MPI_Init(&argc, &argv);

    /* find out process rank */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &p);


    if (my_rank !=0){
    /* create message */
    sprintf(message, "Hello MPI World from process %d!", my_rank);
    dest = 0;
    /* use strlen+1 so that '\0' get transmitted */
    MPI_Send(message, strlen(message)+1, MPI_CHAR,
    dest, tag, MPI_COMM_WORLD);
    }
    else{
    printf("Hello MPI World From process 0: Num processes: %d\n",p);
    for (source = 1; source < p; source++) {
    MPI_Recv(message, 100, MPI_CHAR, source, tag,
    MPI_COMM_WORLD, &status);
    printf("%s\n",message);
    }
    }
    /* shut down MPI */
    MPI_Finalize();


    return 0;
    }