Parallel Scientific Computing in C++ and MPI : A Seamless Approach to Parallel Algorithms and their Implementation - Compilation Guide
Article Index
Parallel Scientific Computing in C++ and MPI : A Seamless Approach to Parallel Algorithms and their Implementation
Sample Code Main
Sample Code Ch. 2
Sample Code Ch. 3
Sample Code Ch. 4
Sample Code Ch. 5
Sample Code Ch. 6
Sample Code Ch. 7
Sample Code Ch. 8
Sample Code Ch. 9
Sample Code Ch. 10
Compilation Guide
All Pages

Compilation Guide

This directory (/CODE) contains the following:

  • Source files (containing function/class definitions) and header files (containing function/class declarations) for SCmathlib, a scientific computing mathematics library which we have devised for use throughout the book. SCmathlib.h/SCmathlib.cpp contain general functions/classes which we may use in all the chapters of the book. SCchapter*.h/SCchapter*.cpp (where * = 2,3,...,10) are header and source files respectively which correspond to functions/classes that were introduced in the corresponding chapter.

    NOTE: The reason for creating this library is to allow the programmer to use the functions/classes declared and defined therein in any of the programs written throughout the book.

  • A Makefile for compiling the SCmathlib library

  • Directories Chapter2 - Chapter10, each of which contains programs which were written in the corresponding chapter.

A Note Concerning Compilers, Compilation and Navigation

In the writing of this software, every attempt has been made to conform to the ANSI C++ standard. Software development and testing were performed on an Intel processor based PC running Redhat Linux version 8.0. Compilation was accomplished using the GNU gcc 3.2 compiler. Although some compilation tests were accomplished using native compilers under AIX, IRIX and SunOS operating systems, proper compilation and execution are not guaranteed. No compilation tests were performed under the Microsoft Windows or Macintosh operating systems.

Navigation of this CD has been tested under both the Linux and the Microsoft Windows operating systems.

A Note to Macintosh Users: Mac OS X is required for properly navigating this CD. Prior versions of the Mac operating system appear to interpret the filenames on this CD using the 8.3 character convention, and hence filenames with the first eight characters being identical are not distinguishable.


Compilation of SCmathlib for Serial Programs

(1)Enter the directory CODE. If you do a directory listing, you should see a file called Makefile.

(2)type: gmake

After hitting return, you should see something similar (depending on what system you are running on) to the following:

g++ -O3 -c SCmathlib.cpp
g++ -O3 -c SCchapter2.cpp
g++ -O3 -c SCchapter3.cpp
g++ -O3 -c SCchapter4.cpp
g++ -O3 -c SCchapter5.cpp
g++ -O3 -c SCchapter6.cpp
g++ -O3 -c SCchapter7.cpp
g++ -O3 -c SCchapter8.cpp
g++ -O3 -c SCchapter9.cpp
ar rv libSCmathlib.a SCmathlib.o SCchapter2.o SCchapter3.o
SCchapter4.o SCchapter5.o SCchapter6.o SCchapter7.o
SCchapter8.o SCchapter9.o
a - SCmathlib.o
a - SCchapter2.o
a - SCchapter3.o
a - SCchapter4.o
a - SCchapter5.o
a - SCchapter6.o
a - SCchapter7.o
a - SCchapter8.o
a - SCchapter9.o

(3) If you now do a directory listing, you should see that the file libSCmathlib.a has been created.


Compilation of SCmathlib for Parallel Programs

(1) Enter the directory CODE. If you do a directory listing, you should see a file called Makefile.

(2) type: gmake MPISRC=1

After hitting return, you should see something similar (depending on what system you are running on) to the following:

g++ -O3 -DMPISRC -c SCmathlib.cpp
g++ -O3 -DMPISRC -c SCchapter2.cpp
g++ -O3 -DMPISRC -c SCchapter3.cpp
g++ -O3 -DMPISRC -c SCchapter4.cpp
g++ -O3 -DMPISRC -c SCchapter5.cpp
g++ -O3 -DMPISRC -c SCchapter6.cpp
g++ -O3 -DMPISRC -c SCchapter7.cpp
g++ -O3 -DMPISRC -c SCchapter8.cpp
g++ -O3 -DMPISRC -c SCchapter9.cpp
ar rv libSCmathlibP.a SCmathlib.o SCchapter2.o SCchapter3.o
SCchapter4.o SCchapter5.o SCchapter6.o SCchapter7.o
SCchapter8.o SCchapter9.o
a - SCmathlib.o
a - SCchapter2.o
a - SCchapter3.o
a - SCchapter4.o
a - SCchapter5.o
a - SCchapter6.o
a - SCchapter7.o
a - SCchapter8.o
a - SCchapter9.o

(3) If you now do a directory listing, you should see that the file libSCmathlibP.a has been created.

Serial Program Compilation Guide (from Appendix A)

For the purposes of this compilation example, we will assume that we are using the GNU g++ compiler to compile a C++ program we have written contained within the file myprog.cpp. In the following examples, the argument following the '-o' flag designates the file name to be used for the output. If no '-o' option is specified, most compilers default to using the name ''a.out''. We now present several different programming scenarios:

  • No user-defined libraries or user-defined header files are needed, and no special system libraries (such as those associated with math.h are needed):
  • g++ -o myprog myprog.cpp
  • No user-defined libraries or user-defined header files are needed, but the special system library corresponding to math.h is needed:
  • g++ -o myprog myprog.cpp -lmath
  • User-defined libraries, user-defined header files, and the special system library corresponding to math.h are needed:
  • g++ -o myprog myprog.cpp -I/users/kirby/includes -L/users/kirby/libs
    -lSCmathlib -lmath

    The string following the `-I' flag designates the location of the user-defined header files to be included. The string following the `-L' flag designates the location of the user-defined libraries to be included. The string `-lSCmathlib' links the program with the user-defined library we created, and the string `-lmath' links the program with the system math library corresponding to math.h


Parallel Program Compilation Guide (from Appendix B)

For the purposes of this compilation example, we will assume that we are using the GNU g++ compiler to compile a C++ program we have written contained within the file myprog.cpp. We will also assume that the machine on which you are trying to compile is a parallel machine with some version of MPI installed. You will need to contact your system administrator to find out the exact version of MPI that is available and the paths on your local architecture. In the following examples, the argument following the `-o' flag designates the file name to be used for the output. If no `-o' option is specified, most compilers default to using the name ``a.out''. We now present several different programming scenarios:

  • No user-defined libraries or user-defined header files are needed, and no special system libraries (such as those associated with math.h are needed) other than the MPI libraries:
  • g++ -o myprog myprog.cpp -lmpi
  • No user-defined libraries or user-defined header files are needed, but the special system library corresponding to math.h is needed along with the MPI libraries:
  • g++ -o myprog myprog.cpp -lmath -lmpi
  • User-defined libraries, user-defined header files and the special system library corresponding to math.h are needed along with the MPI libraries:
  • g++ -o myprog myprog.cpp -I/users/kirby/includes -L/users/kirby/libs
    -lSCmathlib -lmath -lmpi

    The string following the `-I' flag designates the location of the user-defined header files to be included. The string following the `-L' flag designates the location of the user-defined libraries to be included. The string `-lSCmathlib' links the program with the user-defined library we created, and the string `-lmath' links the program with the system math library corresponding to math.h. The string `-lmpi' links the MPI libraries.

You will need to contact your system administrator to verify the exact command used on your computing architecture to run an MPI program. In general, most architectures execute MPI programs in a fashion similar to the following:

mpirun -np 4 myprog

where mpirun is a special program used for starting execution on the parallel machine, `-np 4' indicates the number of processes requested, and myprog denotes the program executable compiled as discussed above.


A Note Concerning Makefiles

For those new to the Unix programming environment, addition information concerning makefiles can be found both on the web and in many good books, one of which is Managing Projects with make by Andrew Oram and Steve Talbott. Another useful source of information for gmake (GNU make, which is used above) and g++ (the GNU C++ compiler) is the GNU website, www.gnu.org.



A Note Concerning File Permissions

Depending on how your system is configured, it may be necessary to change the directory and file permissions in order to compile within the CODE directory. If you do encounter permission problems, do the following: change directory to the parent directory of the CODE directory (from which, under Unix, you can see the directory CODE by doing an 'ls'). Type the expression within quotes: "chmod -R 755 CODE". This will set the directory CODE and all files and subdirectories to be owner-RWX (read/write/execute) and group and world RX (read/execute).