Click here to Skip to main content
13,143,185 members (59,564 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 15 Apr 2008

Implementing parallel programming using MPI and C#

, 15 Apr 2008
Rate this:
Please Sign up or sign in to vote.
An example of implementing a parallel program using the pure MPI .NET library, C#, and .NET Remoting.


Pure .NET MPI is a completely managed implementation of MPI. The object-oriented API is powerful, yet easy to use for parallel programming. It has been developed based on the .NET Framework 3.0, and ported to 3.5 using much of Windows Communication Foundation (WCF).

With the help of WCF, you can use the configuration file to declare the number of process that you are using, which process is the master process, and the locations of the process on your network as IP addresses and ports.

What is most fantastic in Pure MPI .NET is that you can launch all your parallel programs as threads in one single process for debugging and testing issues.


The .NET implementation of the Message Passing Interface (standard 2.0):

  • Built on Windows Communication Foundation (WCF).
  • Single process F5 experience for debugging and development.
  • Multi-threaded, multi-process, or multi-machine execution, and any combination there in.
  • MPI processors can run inside one process, or any number of processes on any number of machines.
  • Type-safe API.
  • x86, x64, and Itanium support.
  • Extensible. Supports existing and custom WCF bindings and channels. Adds higher level and custom synchronization mechanisms to communication objects.
  • Supports communication timeouts for deadlock detection and better error handling.
  • Environment and WCF settings completely configurable.

Using the code

This example draws a MandelBrot set using more than one process.

The attachments constitute of three different projects:

  1. The interface that draws the MandelBrot set.
  2. The MPI program.
  3. The library containing the marshaled objects.

The interface can work in sequential mode and in parallel mode.

When working in parallel mode, the interface will launch the MPI-enabled processes to calculate the MandelBrot set, and will use .NET Remoting to collect the result from the master process, as follows:

TcpChannel channel = new TcpChannel(8090);
ChannelServices.RegisterChannel(channel, false);
                      "MandelBrot", WellKnownObjectMode.SingleCall);

The following is the implementation of the master process:

Console.WriteLine("Master Process: Working...");

int row = 0;
for (int i = 1; i < comm.Size; i++)
    comm.Send<int>(i, "ROW_NUMBER", row);
    row += processIncrement;

ArrayList list = new ArrayList();
int max = (int)Math.Floor((double)width * height / partition) + 1;
for (int i = 0; i <= max; i++)
    ArrayList partList = 
      comm.Receive<ArrayList>(Constants.AnySource, "RESULT");

    if (partList != null)

Points points;

TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan, false);

points = (Points)Activator.GetObject(typeof(Points), 


Console.WriteLine("Finished Calculating: " + list.Count + " points.");

The following is the implementation of the slave process:

Console.WriteLine("Slave Process: " + comm.Rank + ".Working...");

int row = comm.Receive<int>(0, "ROW_NUMBER");

decimal scaleX = (maxNumber.Real - minNumber.Real) / width;
decimal scaleY = (maxNumber.Imaginary - minNumber.Imaginary) / height;

ComplexNumber c = new ComplexNumber();

ArrayList list = new ArrayList();

c.Real = minNumber.Real;
for (int x = 0; x < width; x++)
    c.Imaginary = minNumber.Imaginary + row * scaleY;
    for (int y = row; y < (row + processIncrement); y++)
        int count = CalculatePixel(c);

        PointSet set = new PointSet();
        set.W = x;
        set.H = y;
        set.Pixel = count;


        c.Imaginary += scaleY;

        if (list.Count == partition)
            comm.Send<ArrayList>(0, "RESULT", list);

    c.Real += scaleX;

if (list.Count > 0)
    comm.Send<ArrayList>(0, "RESULT", list);


The configuration file should be set as necessary to determine the number of processes and where the processes will launch.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ali Hamdar
Software Developer (Senior) Integrated Digital Systems - IDS
Lebanon Lebanon
Been programming since 2001 interested in finance, security, workflows, SharePoint and algorithms. He is an MCSD, MCDBA, MCAD, MCSD, (again), MCTS, MCPD and MCT.
My Blog:

You may also be interested in...


Comments and Discussions

Questionhow to do use MPI command Pin
Member 115554542-May-15 19:18
memberMember 115554542-May-15 19:18 
QuestionHas anyone a solution for the problem? Pin
Lycopsis18-Jan-12 10:58
memberLycopsis18-Jan-12 10:58 
GeneralMy vote of 2 Pin
pasondag23-Nov-10 9:20
memberpasondag23-Nov-10 9:20 
GeneralCrashes for me Pin
sam.hill15-Apr-08 15:21
membersam.hill15-Apr-08 15:21 
GeneralRe: Crashes for me Pin
lhfiedler21-Apr-08 23:06
memberlhfiedler21-Apr-08 23:06 
GeneralRe: Crashes for me Pin
Member 177018513-Jun-08 2:40
memberMember 177018513-Jun-08 2:40 
GeneralRe: Crashes for me Pin
lhfiedler7-Jul-08 8:35
memberlhfiedler7-Jul-08 8:35 
GeneralRe: Crashes for me Pin
Dmitri Nesteruk6-Jul-08 4:10
memberDmitri Nesteruk6-Jul-08 4:10 
GeneralRe: Crashes for me [modified] Pin
titan6810-Dec-09 7:35
membertitan6810-Dec-09 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 15 Apr 2008
Article Copyright 2008 by Ali Hamdar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid