sample program

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

/* some dummy user code that just allocates memory */

void foo2(void **ptr, int me) {
  *ptr = malloc(1024 * 1024 * 128 * me);
}

void foo1(void **ptr, int me) {
  foo2(ptr, me);
}

/* main function */

int main(int argc, char **argv)
{
  int me, np, i, n = 1000000, nsleep = 0, buf = - 1;
  void *ptr;

  /* find out how long receiver should sleep */

  if ( argc > 1 ) {
    nsleep = atoi(argv[1]);
  }

  /* initialize MPI */

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &np);
  MPI_Comm_rank(MPI_COMM_WORLD, &me);

  /* have user program allocate some memory just for the fun of it */

  foo1(&ptr, me);

  /* have rank 0 send rank 1 lots of messages, but have rank 1 sleep first */

  if ( me == 0 ) {
    for ( i = 0; i < n; i++ )
      MPI_Send(&buf, 1, MPI_INT, 1, 343, MPI_COMM_WORLD                   );
  } else {
    sleep(nsleep);
    for ( i = 0; i < n; i++ )
      MPI_Recv(&buf, 1, MPI_INT, 0, 343, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  }

  free(ptr);
  MPI_Finalize();
  return 0;
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact