a ts icvael eP a r a l l e l
CCoommppuutti inngg i n C
T
he message-passing interface defines scripting languages help developers deal with the
a set of API functions that let us- error-prone or high-level portions of parallel ap-
ers write high-performance parallel plications. Once the testing and debugging phases
programs to exchange data between are completed, they can recode the parallel scripts
processes and complete tasks in parallel.1 The in a compiled language—such as C or Fortran—
MPI standard was designed to support portability for optimized performance.
and platform independence. Therefore, an appli- Researchers have created MPI bindings for
cation source code written with MPI is portable several scripting languages, including MPI for
among different platforms. Traditionally, develop- Python,2,3 MatlabMPI,4 MPI Toolbox for Mat-
ers achieve high-performance parallel computing lab, MPI Toolbox for Octave, MPI Ruby,5 and
using a compiled language such as C or Fortran. Parallel::MPI for Perl. A language-MPI binding
However, a parallel scientific and engineering ap- is an API that lets users develop MPI programs
plication typically spends most of its time on a in that language. Therefore, these bindings offer
small amount of time-critical code. The rest of the all the benefits of a high-level scripting language
code typically deals with memory management, within a parallel runtime environment. However,
error handling, I/O, user interaction, and parallel some of them have incomplete MPI functionality.
process startup. Given this and the need for rapid Because scripting language syntax differs from C,
algorithm prototyping, the scientific community C++, or Fortran, the potential conversion from
is increasingly using scripting languages. Develop- scripting MPI programs to compiled MPI pro-
ers can use such languages to rapidly implement, grams is complicated. Some scripting languages
test, and debug parallel algorithms without need- also require developers to purchase a license for
ing to recompile and link every time they make a each node to install the necessary software. Fur-
modification. By avoiding the compile/link step, ther, despite scripting languages’ many benefits,
researchers working on a computationally inten-
1521-9615/10/$26.00 © 2010 ieee sive parallel application are most likely writing in
CopubliShed by the ieee CS and the aip
C, C++, or Fortran. For people interested in learn-
Yu-Cheng Chou ing parallel programming, C is also a good choice
Chung Yuan Christian University, Taiwan due to its broad use and international standard.
Stephen S. Nestinger The new numerical features in C99, the latest C
standard, greatly improve the performance of C
Worcester Polytechnic Institute
programs. Given these factors, it would be benefi-
Harry H. Cheng cial to have a C-based scripting environment with
University of California, Davis MPI capabilities.
marCh/april 2010 55
Figure 2. The chf function MPI_Init() is implemented as a separate function file, MPI_Init.chf, in
a Ch MPI package. Such function files constitute the Ch function library of a Ch MPI package. The chf
function MPI_Init() calls the chdl C function MPI_Init_chdl().
Figure 1 illustrates the underlying Ch MPI header file. The header file mpi.h is included at
concept: an MPICH2 C library function, the beginning of every C MPI program. This
MPI_Init(), is called by a Ch script—that is, a header file is the same as the one in the MPICH2
C program with MPI function calls—in the Ch C library, with the addition of several statements.
space. To invoke the C function MPI_Init(), Ch The macro LOAD_CHDL(mpi) invokes a function
searches the Ch function library for the corre- named dlopen() that loads the Ch dynami-
sponding Ch function MPI_Init() implemented cally loaded library libmpi.dl into the Ch in-
in the file MPI_Init.chf and passes parameters terpretive environment. The function dlopen()
to the function. The Ch function MPI_Init() returns a handle _Chmpi_handle, which is
then searches through the Ch dynamically loaded used in subsequent calls to functions dlsym()
library for the corresponding chdl C function, and dlclose(). The same macro invokes the
MPI_Init_chdl(), and passes parameters to it. function dlclose() to release the dynamically
The MPI_Init_chdl() function then invokes loaded library libmpi.dl from the Ch interpre-
the function MPI_Init() in the MPICH2 C tive environment when a C MPI program execu-
library. Any return values are passed back to the tion completes.
initial function call in the Ch space.
We can readily run a C MPI program in Ch The chf ch function. As Figure 2 shows, the chf
by typing in the script’s name at a Ch shell’s function MPI_Init() is implemented in the
command prompt. This lets us quickly modify file MPI_Init.chf. The function call fptr =
program code and generate results without re- dlsym(_Chmpi_handle, “MPI_Init_chdl”)
compiling and linking the program. When a C returns the address of the symbol MPI_Init_
MPI program is still under development, the er- chdl inside the dynamically loaded library libmpi.
ror messages given by interpretively executing dl pointed to by the handle _Chmpi_handle.
the program code in Ch are generally more in- The function call dlrunfun(fptr, &retval,
formative than the messages given by compiling MPI_Init, argc, argv) runs the chdl C func-
the program using conventional C compilers. For tion MPI_Init_chdl() found in the dynamically
example, this occurs in situations where an MPI loaded library libmpi.dl through the address
program contains syntax errors or accesses ele- pointed to by the pointer fptr. The second pa-
ments beyond an array’s extent. rameter is the address of a variable retval con-
The key components of a Ch MPI pack- taining the return value of the chdl C function
age include the header file mpi.h, chf func- MPI_Init_chdl(). Because, in this case, the
tions, and chdl C functions. To illustrate them, third parameter, MPI_Init, is the name of the
we’ll use the header file mpi.h, the chf func- chf function itself, the number and types of
tion MPI_Init(), and the chdl C function the remaining parameters are first examined ac-
MPI_Init_chdl(). cording to the function prototype of MPI_Init().
marCh/april 2010 57
2 P0
P1 and MatlabMPI using programs for parallel and
P0 serial calculation of π, and for C MPI and Ch MPI
P1
1 using a program for 2D matrix multiplication.
3
1
Execution Execution Execution
message Super multipurpose message message
daemon (SMPD) Remote Windows machine
1
4 Super multipurpose
daemon (SMPD)
4
Ch Ch
cpi.c MPICH2 C MPICH2 C
library cpi.c library
Figure 6. Executing cpi.c across Linux and Windows platforms in Ch using the MPICH2-based Ch MPI package. Each
SMPD launches a Ch interpreter to run cpi.c to calculate a partial π. Through Ch and Ch MPI, we can perform parallel
computation across heterogeneous platforms with identical Ch scripts.
Figure 7. The master process output when executing cpi.c on Linux (phoenix) and Windows (mouse1)
platforms. The master process is on the local phoenix machine, while the worker process is on the remote
mouse1 machine.
machines. Each machine has an Intel Pentium 4 in terms of bandwidth and latency. Hardware
processor running at 3.2 GHz and 512 Mbytes of setups for the benchmark were as follows (the
RAM. All of the machines are connected through letters correspond to the results in Figures 8
a switch with a 100 Megabit/sec transmission rate. and 9):
We also use two homogeneous network clus-
ters: one consists of only Linux machines, and the • two nodes from the 64-bit Linux cluster, using
other of only Windows machines. Each machine one processor from each node (a);
in both clusters has one Intel Pentium 4 processor • one node from the 64-bit Linux cluster, using
running at 3.2 GHz and 512 Mbytes of RAM. All two of the node’s processors (b);
of the machines are connected through a switch • one Linux machine and one Windows machine
with a 100 Megabit/sec transmission rate. from the heterogeneous network cluster (c);
• two Linux machines from the homogeneous
Bandwidth and latency Benchmark Linux network cluster (d); and
We benchmarked and compared Ch MPI to • two Windows machines from the homogeneous
a native C MPI implementation, MPICH2, Windows network cluster (e).
marCh/april 2010 59
Bandwidth (MB/s)
Bandwidth (MB/s)
80
600
60 500
400
40
300
200
20
100
0 0
10
00
00
0
1
10
00
00
10
00
00
00
10
00
00
00
,0
,0
,0
,0
1,
0,
0,
1,
0,
0,
10
00
10
00
,0
,0
,0
,0
1,
1,
10
00
10
00
1,
1,
12 12
C MPI C MPI
10 Ch MPI 10 Ch MPI
Bandwidth (MB/s)
8 Bandwidth (MB/s) 8
6 6
4 4
2 2
0 0
1
10
00
00
0
1
10
00
00
10
00
00
00
10
00
00
00
,0
,0
,0
,0
1,
0,
0,
1,
0,
0,
10
00
10
00
,0
,0
,0
,0
1,
1,
10
00
10
00
1,
1,
12
C MPI
10 Ch MPI
Bandwidth (MB/s)
0
1
10
0
10
00
0
,0
,0
,0
,0
1,
10
00
0
,0
,0
1,
10
00
1,
Figure 8. Bandwidth test results for C MPI and Ch MPI in semi-log plots. The hardware setups were (a) a 64-bit Linux cluster
with two processors and two nodes, (b) a 64-bit Linux cluster with two processors and one node, (c) a heterogeneous
network cluster, (d) a Linux network cluster, and (e) a Windows network cluster.
We obtained the benchmark programs running interface based on the MPICH implementation;
in both Ch MPI and native C MPI from Ohio see http://mvapich.cse.ohio-state.edu); they’re also
State University’s Mvapich project (that is, an available on our own Web site (http://iel.ucdavis.
MPI implementation over the InfiniBand VAPI edu/projects/chmpi). Both tests included the use
marCh/april 2010 61
Latency (microseconds)
Latency (microseconds)
1,000
1,000
100
100
10
10
1 1
10
0
1
10
10
00
0
10
00
,0
,0
,0
,0
,0
,0
,0
,0
1,
1,
10
00
0
10
00
,0
,0
,0
,0
1,
1,
10
00
10
00
1,
1,
(a) Message size (bytes) (b) Message size (bytes)
10,00,000 10,00,000
C MPI C MPI
1,00,000 Ch MPI 1,00,000 Ch MPI
Latency (microseconds)
Latency (microseconds)
10,000 10,000
1,000 1,000
100 100
10 10
1 1
1
10
00
00
10
00
00
0
10
00
00
00
10
00
00
00
,0
,0
,0
,0
1,
0,
0,
1,
0,
0,
10
00
10
00
,0
,0
,0
,0
1,
1,
10
00
10
00
1,
1,
(c) Message size (bytes) (d) Message size (bytes)
10,00,000
C MPI
1,00,000 Ch MPI
Latency (microseconds)
10,000
1,000
100
10
1
1
10
0
10
00
00
00
00
00
1,
0,
0,
10
00
,0
,0
1,
10
00
1,
Figure 9. C MPI and Ch MPI latency test results in log-log plots. As in previous tests, the hardware setups were (a) a 64-bit
Linux cluster with two processors and two nodes, (b) a 64-bit Linux cluster with two processors and one node,
(c) a heterogeneous network cluster, (d) a Linux network cluster, and (e) a Windows network cluster.
50
mm_parallel.c and its compiled executable
mm_parallel to perform the second parallel ex- 40
ecution test for matrix multiplication on the 64-bit 30
Linux cluster, where each of the cluster’s processors
runs one process. We created a library, libmxm.a, 20
together with a header file, mxm.h, to provide a
10
function, matrixmultiply(), which performs
2D matrix multiplication. We generated the com- 0
0 10 20 30 40 50 60 70
piled executable mm_parallel with libmxm.a.
Number of processors
Using Ch SDK, we created a Ch dynamically
loaded library, libmxm.dl, and a Ch function Figure 11. Execution speedups of mm_parallel
file, matrixmultiply.chf, to make the function and mm_parallel.c. Ch MPI scales linearly, with
matrixmultiply() callable from within the Ch almost the same rate as C MPI.
script mm_parallel.c.
We set the sizes of matrices A, B, and C in mm_ function call. The MPI function, MPI_Wtime(),
parallel.c to 2,880 × 2,000, 2,000 × 450, and was called to obtain the time in seconds.
2,880 × 450, respectively. In this test, the mas- Figure 10 shows the execution times and
ter process didn’t contribute to the computation, Figure 11 shows the execution speedups from
and we accounted only for the time spent by each running these two programs with different num-
worker process to complete its own computation. bers of processors. The number of processors in
We determined the execution time by finding the Figures 7 and 8 refers to the number of worker
maximum time among all the times spent by work- processes that contributed to the computa-
er processes to finish the matrix multiplication tion. As Figure 10 shows, the execution times of
marCh/april 2010 63
int main() x =
{
array double A[3][3] = {8,1,6, 0.05
3,5,7, 0.30
4,9,2}; 0.05
array double b[3][1] = {1,2,3};
array double x[3][1];
With Ch computational arrays, we can specify
x = inverse(A)*b; vector and matrix operations in a C source code as
printf("x =\n%7.2f", x);
scalar operations, as Figure 12 shows. In addition,
return 0; declaring a matrix as a Ch computational array is
}
straightforward regardless of the matrix’s size.
Execution speedup
50
cases, for example, the master process can specify
the number of tasks sent to a particular worker pro- 40
cess each time or limit the total amount of tasks 30
performed by certain worker processes. Ch MPI can
effectively facilitate the design of such a task farm 20
program because a user can change the program and 10
readily test it without the compilation process.
0
As an example, a simple MPI task farm program 0 10 20 30 40 50 60 70
task_farm.c can run on different numbers of Number of processors
processors to handle a total of 64 identical tasks
on our Linux cluster. The tasks are saved in an Figure 13. Execution speedups for different
input file, tasks, for the task farm program. numbers of processors running task_farm.c
to finish 64 identical tasks. A linear speedup is
Each task consists of consecutive commands cd
obtained as the number of processors increases.
output; ../nbody 1000 > out_1. Thus, each
task stands for changing to a subdirectory, output,
HTTP server
running an executable, nbody, from the parent di-
rectory with a command line argument, 1000, and HTML
redirecting the executable’s output to a file, out_1. document MPI program/
All the commands for one task should be separated zip file
1
by semicolons and on the same line. Because each
2 MPICH2
task is identical, a linear execution speedup is ex- Ch CGI
script host file
pected as the number of processors increases.
The nbody executable performs a simplified
Ch script 1
N-body simulation10 associated with a 1D line
of N particles. The number of particles is speci-
Ch script 2
fied as a command line argument. Each particle
has an initial position on the x-axis. Evaluating
a particle’s position depends on all the forces
exerted on this particle from all other particles.
A force exerted on a particle from another particle Ch
3
is proportional to the inverse of the square of the
Ch script 1
distance between these two particles. Forces on
a given particle from any particles on its left side
have a negative value because they’re pulling the Ch
particle in the negative x direction; forces from
Ch script 2
any particles on its right side have a positive value.
The program eventually outputs the average dis-
tance between two adjacent particles.
The execution time in task_farm.c is the time
required to finish all 64 tasks. Figure 13 shows ex-
ecution speedups for different numbers of proces- Figure 14. A Ch CGI script performs multiple tasks
on the Web server in the Web-based parallel
sors running task_farm.c; a linear speedup is computing application. It collects information
obtained as the number of processors increases. from an HTML document, retrieves an uploaded
With Ch MPI, users can readily run a task MPI program or archived file, creates an MPICH2
farm program such as task_farm.c on multiple host file and other Ch scripts, and invokes Ch
processors without compilation to decrease task- interpreters to run those scripts.
completion time.
can execute a CGI program to run external
Web-Based application of ch mpi applications and output dynamic results. Al-
for parallel computing though developers can write a CGI program
The CGI is a standard for interfacing external in any language, using scripting languages
applications with Web servers. A Web server makes it easier to debug, modify, and maintain.
marCh/april 2010 65
A
Ch is suitable for Web-based applications due to Ch MPI package lets developers
its interpretive nature. Like Java Server Page and run identical MPI C scripts across
Active Server Page, the Ch CGI Toolkit provides heterogeneous platforms. Inter-
four easy-to-use classes for CGI programming in pretive computing in Ch MPI is
Ch: CResponse, CRequest, CServer, and CCookie. efficient in comparison with other scripting
We’ve developed many Web-based applications— solutions, running more than twice as fast as
such as a Web-based system for control system both Matlab (for sequential π calculation) and
design and analysis—using the Ch CGI Toolkit. MatlabMPI (for parallel π calculation). Also,
marCh/april 2010 67