Tutorial
Native MPI example codes

nativempi.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mpi.h"

int main(int argc, char **argv)
{
      int nodenum, nprocs, secret, i;
      int originator = 0;
      static char host_buf[256];
      MPI_Status status;

      MPI_Init(&argc, &argv);
      MPI_Comm_rank(MPI_COMM_WORLD, &nodenum);
      MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

      gethostname(host_buf, 256);
      printf("Hi! Node %d of %d, hostname %s\n", nodenum, nprocs, host_buf);

      if (nodenum == originator)
      {
              secret = 5674351;
              printf("Node %d has a secret: %d\n", nodenum, secret);
              MPI_Send(&secret, 1, MPI_INT, (nodenum+1)%nprocs, 0, MPI_COMM_WORLD);
              MPI_Recv(&secret, 1, MPI_INT, (nodenum-1+nprocs)%nprocs, 0, MPI_COMM_WORLD, &status);
      }
      else
      {
              MPI_Recv(&secret, 1, MPI_INT, (nodenum-1+nprocs)%nprocs, 0, MPI_COMM_WORLD, &status);
              MPI_Send(&secret, 1, MPI_INT, (nodenum+1)%nprocs, 0, MPI_COMM_WORLD);
      }

      printf("Node %d thinks the secret is %d\n", nodenum, secret);
      MPI_Barrier(MPI_COMM_WORLD);
      printf("Node %d reaches barrier and exits.\n", nodenum);
      MPI_Finalize();

      exit(0);
}

back

mixedmpi.c

The red text marks the lines that must be included in order to make Legion calls.

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mpi.h"
#include "legion/Legion.h"
#include "legion/Legion_libBasicFiles.h"
#include "legion/LegionNativeMPIUtils.h"
        
int main(int argc, char **argv)
{   
      int nodenum, nprocs, secret, i;
      int originator = 0;
      static char host_buf[256], buf[1000];
      MPI_Status status;
        
      /* This file must be present in your current context space */
      char file[] = "secret";
      
      MPI_Init(&argc, &argv);
      MPI_Comm_rank(MPI_COMM_WORLD, &nodenum);
      MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
      LegionNativeMPI_init(&argc, &argv);

      gethostname(host_buf, 256);
      printf("Hi! Node %d of %d, hostname %s\n", nodenum, nprocs, host_buf);

      BasicFiles_init();
      BasicFiles_FileDescriptor fd;
      if (!BasicFiles_exists(file))
      {
              fprintf(stderr, "%s: Can't find context file %s!\n", host_buf, file);
              exit(1);
      }
      if ((fd = BasicFiles_open(file, 0)) == BasicFiles_BadFd)
      {
              fprintf(stderr, "%s: Can't open context file %s!\n", host_buf, file);
              exit(1); 
      }
      if (BasicFiles_read(fd, buf, 100) < 0)
      {
              fprintf(stderr, "%s: Can't read context file %s!\n", host_buf, file);
              exit(1);
      }
      if (BasicFiles_close(fd) < 0)
      {
              fprintf(stderr, "%s: Can't close context file %s!\n", host_buf, file);
              exit(1);
      }

      if (nodenum == originator)
      {
              secret = atoi(buf);
              printf("Node %d has a secret: %d\n", nodenum, secret);
              MPI_Send(&secret, 1, MPI_INT, (nodenum+1)%nprocs, 0, MPI_COMM_WORLD);
              MPI_Recv(&secret, 1, MPI_INT, (nodenum-1+nprocs)%nprocs, 0, MPI_COMM_WORLD, &status);
      }
      else
      {
              MPI_Recv(&secret, 1, MPI_INT, (nodenum-1+nprocs)%nprocs, 0, MPI_COMM_WORLD, &status);
              MPI_Send(&secret, 1, MPI_INT, (nodenum+1)%nprocs, 0, MPI_COMM_WORLD);
      }

      printf("Node %d thinks the secret is %d\n", nodenum, secret);
      if (secret == atoi(buf))
              printf("Node %d verifies that the secret is %d\n", nodenum, secret);
      MPI_Barrier(MPI_COMM_WORLD);
      printf("Node %d reaches barrier and exits.\n", nodenum);
      MPI_Finalize();

      BasicFiles_done();

      exit(0);

back

Last modified: Wed Feb 2 16:24:44 2000

 

[Home] [General] [Documentation] [Software]
[Testbeds] [Et Cetera] [Map/Search]

legion@Virginia.edu
http://legion.virginia.edu/