| jd e ( @ 2008-02-23 11:10:00 |
| Current music: | Radiohead - Videotape |
MPI versus Erlang - A comparison of two message-passing implementations
Anyone that has written a concurrent application knows that when two threads of execution share resources, programs become difficult to develop and debug. Unreliable locks and mutexes must be used, and juggling threads becomes painful. This is why message passing is a superior technique. Similarly to purely functional programming, message passing strives to reduce the sharing of state. In functional programming, a function has inputs and outputs and does not modify any variables outside the scope of the function during its execution. In message passing, a thread of execution has similar inputs and outputs and does not modify any value outside of its reserved memory block.
Here is an example that illustrates the message-passing implementation in MPI, an extension for C. Two threads of execution are created at execution time, and one thread sense a message to the other, which is then responsible for printing that message.
1 #include <mpi.h>
2
3 int main(int argc, char *argv[]) {
4 int rank, size, source, dest, tag, message;
5
6 MPI_Init(&argc, &argv);
7 MPI_Status status;
8 MPI_Comm_size(MPI_COMM_WORLD, &size);
9 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
10
11 source, tag = 0;
12 dest = 1;
13
14 if (rank == 0) {
15 message = 1234;
16 MPI_Send(
17 &message, 1, MPI_INT,
18 dest, tag, MPI_COMM_WORLD
19 );
20 }
21 else {
22 MPI_Recv(
23 &message, 1, MPI_DOUBLE,
24 source, tag, MPI_COMM_WORLD, &status
25 );
26 printf("Received message: %d\n", message);
27 }
28
29 MPI_Finalize();
30 return 0;
31 }
1> make
/usr/bin/mpicc -o send_recv send_recv.c
2> srun -N 2 ./send_recv
Received message: 1234
Here is the same example in Erlang:
1 -module(send_recv).
2 -export([send/0, recv/0]).
3
4 send() ->
5 Pid = spawn(fun recv/0),
6 Message = 1234,
7 Pid ! Message.
8
9 recv() ->
10 receive
11 Message ->
12 io:format("Received message: ~p~n", [Message])
13 end.
1> c(send_recv).
{ok,send_recv}
2> send_recv:send().
Received message: 1234
1234
I know which I would rather code in ;]
To be fair, MPI is designed for massively scalable high-performance computing applications. If one needed to parallelize an application across a thousand-core supercompute cluster, Erlang would not be usable. Further, Erlang's multi-core support is limited to SMP machines, whereas MPI can operate on both SMP machines and clusters.