Lazarus

Programming => Packages and Libraries => Lazarus Extra Components => Topic started by: Leledumbo on December 06, 2010, 07:17:44 pm

Title: Using MPICH2
Post by: Leledumbo on December 06, 2010, 07:17:44 pm
I'm currently playing with MPICH2 due to my parallel and concurrent programming class. Is there anybody experienced on this? I'd like to try doing some parallel operation over data allocated in an array, the operation can be done in parallel, but the printing of the result is not, and it has to be done when all processes have done their job. How can I do this?

Here's what I've done so far:
Code: [Select]
program MPISelectionSort;

{$mode objfpc}{$H+}
{$ifdef unix}
{$linklib c}
{$endif}

uses
  MPI;

const
  Max = 100;

type
  TData = array [0 .. Max - 1] of Integer;

var
  NProcs,ID,Count,i,j,Temp,Start,End_,Min: Integer;
  Data: TData;
begin
  MPI_Init(@argc,@argv);
  MPI_Comm_size(MPI_COMM_WORLD,@NProcs);
  MPI_Comm_rank(MPI_COMM_WORLD,@ID);
  for i := 0 to Max - 1 do
    Data[i] := Random(512) - 256;
  MPI_Bcast(@Data[0],Length(Data),MPI_INT,0,MPI_COMM_WORLD);
  Count := Max div NProcs;
  Start := ID * Count;
  End_  := Start + Count - 1;
  WriteLn('Count=',Count,',Start=',Start,',End=',End_);
  for i := Start to End_ - 1 do begin
    Min := i;
    for j := i + 1 to End_ do
      if Data[j] < Data[Min] then
        Min := j;
    Temp := Data[i];
    Data[i] := Data[Min];
    Data[Min] := Temp;
  end;
  MPI_Finalize;
  if ID = 0 then
    for i := 0 to Max - 1 do begin
      Write(Data[i]:5);
      if (i + 1) mod 10 = 0 then WriteLn;
    end;
end.
No, it's not yet correct. The master process could have written the data before all other processes finish their job. And if I've managed this, the sorting is still correct "locally", meaning that it's correct only for a partition. For example, if I have 5 processes, process with ID=0 is responsible to sort data from index 0 to 19 and it should be correct for that index range (i.e. partition) only, ignoring results from other partitions. Same things apply to other partitions.