5,316,870 members and growing! (18,069 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » General     Intermediate License: The Code Project Open License (CPOL)

Implementing parallel programming using MPI and C#

By Ali Hamdar

An example of implementing a parallel program using PURE MPI .NET library, C# and .NET remoting
C#, Windows, WCF, Dev

Posted: 15 Apr 2008
Updated: 15 Apr 2008
Views: 4,438
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.34 Rating: 3.78 out of 5
0 votes, 0.0%
1
2 votes, 14.3%
2
2 votes, 14.3%
3
1 vote, 7.1%
4
9 votes, 64.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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 .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 lauch all your parallel programs as threads in one single process for debugging and testing issues.

Background

.NET implemenation of 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 1 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. Add 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 MandelBrot set using more than one process.

The attachments constitute of 3 different projects:

1- The Interface that draws 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)
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(Points), "MandelBrot", WellKnownObjectMode.SingleCall);
        Cache.Attach(observer);
          

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)
                        list.AddRange(partList);
                }

                Points points;

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

                points = (Points)Activator.GetObject(typeof(Points), "tcp://localhost:8090/MandelBrot");

                points.SetMessage(list);

                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;

                            list.Add(set);

                            c.Imaginary += scaleY;

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

                        c.Real += scaleX;
                    }

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

                    Console.WriteLine("Done"); 
The configuration file should be set as necessary to determine the number of processes and where the processes will launch.

License

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

About the Author

Ali Hamdar


Been programming since 2001 while pretending to be a mind reader, psychologist, defense intelligence researcher, financial analyst, portfolio manager and at last a developer. He is an MCSD, MCDBA, MCAD, MCSD (again) and MCT.
http://www.ids.com.lb
Occupation: Web Developer
Company: Integrated Digital Systems - IDS
Location: Lebanon Lebanon

Other popular Windows Communication Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralCrashes for memembersam.hill16:21 15 Apr '08  
GeneralRe: Crashes for mememberlhfiedler0:06 22 Apr '08  
GeneralRe: Crashes for mememberMember 17701853:40 13 Jun '08  
GeneralRe: Crashes for mememberlhfiedler9:35 7 Jul '08  
GeneralRe: Crashes for mememberDmitri Nesteruk5:10 6 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Apr 2008
Editor:
Copyright 2008 by Ali Hamdar
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project