UMBC logo
UMBC High Performance Computing Facility
How to run C++ programs on tara

Introduction

Now we'll see how to run a C++ program on the cluster. Before proceeding, make sure you've read the How To Run tutorial first.

Serial Example

Let's try to compile this simple C++ program:
#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 0;
}


Download: ../code-2010/hello_serial-cpp/hello-serial.cpp
To compile this using GCC (for example) and create the executable hello-serial-gcc, type:
[araim1@tara-fe1 hello_serial-cpp]$ g++ hello-serial.cpp -o hello-serial-gcc
[araim1@tara-fe1 hello_serial-cpp]$
Running this program should produce the hello message:
[araim1@tara-fe1 hello_serial-cpp]$ ./hello-serial-gcc 
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   !!  
[araim1@tara-fe1 hello_serial-cpp]$

Parallel Example

Now we'll try to run a slightly more complicated MPI C++ program
#include <iostream>
#include <mpi.h>

using namespace std;

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

    int id = MPI::COMM_WORLD.Get_rank();
    int np = MPI::COMM_WORLD.Get_size();
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int len;

    MPI::Get_processor_name(processor_name, len);

    cout << "Hello world from process " << id
         << " out of " << np
         << ", processor name " << processor_name
         << endl;

    MPI::Finalize();
    return 0;
}


Download: ../code-2010/hello_parallel-cpp/hello_parallel.cpp
The program can be compiled with the MPI C++ compiliation command:
[araim1@tara-fe1 hello_parallel-cpp]$ mpicxx hello_parallel.cpp -o hello_parallel
[araim1@tara-fe1 hello_parallel-cpp]$
Running the hello_parallel program is now exactly the same as running a C MPI program. For more information, see How to run C programs.