This page last changed on Jan 11, 2009 by straha1.
Tutorial

This page is part of a tutorial that explains how to

Table of Contents

Simple Hello World

Let's try to compile this simple C++ program which we'll put in a file helloworld.cc:

#include <iostream>

using namespace std;

int main(int argc,char **argv) {
  cout<<"H   H EEEEE L     L      OOO   !!  "<<endl
      <<"H   H E     L     L     O   O  !!  "<<endl
      <<"HHHHH EEEE  L     L     O   O  !!  "<<endl
      <<"H   H E     L     L     O   O      "<<endl
      <<"H   H EEEEE LLLLL LLLLL  OOO   !!  "<<endl;

  return 1;
}

To compile this using GCC and create the executable helloworld-c++-gcc, type

g++ helloworld.cc -o helloworld-c++-gcc

Alternatively, to compile using PGI's C++ compiler and create the executable helloworld-c++-pgi, type

pgcpp helloworld.cc -o helloworld-c++-pgi

Running this program by typing ./helloworld-g++-gcc for GCC or ./helloworld-c++-pgi for PGI should produce this message:

H   H EEEEE L     L      OOO   !!
H   H E     L     L     O   O  !!
HHHHH EEEE  L     L     O   O  !!
H   H E     L     L     O   O
H   H EEEEE LLLLL LLLLL  OOO   !!

Parallel Hello World

Before you try to compile this program, you must set up your MPI and compiler using switcher as described here in the C tutorial. Make sure you follow the directions in that section before continuing.

Once you've used switcher to set up your compiler+MPI combination, create this C++ program in hello_parallel.cc:

// Include the MPI version 2 C++ bindings:
#include <mpi.h>
#include <iostream>
#include <string.h>

using namespace std;

int
main(int argc, char* argv[])
{
  // Initialize the MPI library:
  MPI::Init(argc, argv);

  // Get the number of processors this job is using:
  int rank = MPI::COMM_WORLD.Get_rank();

  // Get the rank of the processor this thread is running on.  (Each
  // processor has a unique rank.)
  int size = MPI::COMM_WORLD.Get_size();

  // Get the name of this processor (usually the hostname).  We call                                                      
  // memset to ensure the string is null-terminated.  Not all MPI                                                        
  // implementations null-terminate the processor name since the MPI                                                     
  // standard specifies that the name is *not* supposed to be returned                                                   
  // null-terminated.                                                                                                    
  char name[MPI_MAX_PROCESSOR_NAME];
  int len;
  memset(name,0,MPI_MAX_PROCESSOR_NAME);
  MPI::Get_processor_name(name,len);
  memset(name+len,0,MPI_MAX_PROCESSOR_NAME-len);

  cout << "hello_parallel.cc: Number of tasks="<<size<<" My rank=" << rank << " My name="<<name<<"."<<endl;

  // Tell the MPI library to release all resources it is using:
  MPI::Finalize();
  return 0;
}

Now that you've written your hello_parallel program, it is time to compile it with:

mpicxx hello_parallel.cc -o hello_parallel

That same command will work with any of the six compiler+MPI combinations.

To learn how to run this program, continue on to this page.

If you intend to create more complex programs that use multiple source files, you will need to learn how to link programs. (For example, if you have one source file with your main program and three more files with subroutines needed by the main program.) To learn about linking MPI and non-MPI programs, go to this page.

Document generated by Confluence on Mar 31, 2011 15:37