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

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 << "Hello World!" << endl;
    return 0;
}


Download: ../code/hello_serial-cpp/hello-serial.cpp
We can compile with GCC's C++ compiler.
[araim1@maya-usr1 hello_serial-cpp]$ g++ hello-serial.cpp -o hello-serial
[araim1@maya-usr1 hello_serial-cpp]$
[araim1@maya-usr1 hello_serial-cpp]$ ./hello-serial
Hello World!
[araim1@maya-usr1 hello_serial-cpp]$
Or we can use the Intel C++ compiler.
[jongraf1@maya-usr1 hello_serial-cpp]$ icpc hello-serial.cpp -o hello-serial
[jongraf1@maya-usr1 hello_serial-cpp]$ ./hello-serial
Hello World!
[araim1@maya-usr1 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/hello_parallel-cpp/hello_parallel.cpp
The program can be compiled with the MPI C++ compiliation command:
[araim1@maya-usr1 hello_parallel-cpp]$ mpicxx hello_parallel.cpp -o hello_parallel
[araim1@maya-usr1 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.