PTL logo

From: Ben Martin (benjmart_at_[hidden])
Date: 2011-12-02 16:32:57


Яна Седова wrote:
> Hello. I try to find the max value for "myObjOnThisProc" value on all processes:
> myObj = Communicator.world.Allreduce<myClass>(myObjOnThisProc, Operation<myClass>.Max);
>
> But standard Operation.Max cannot work properly with an object of an user class? Would you give me some example?
>

Hi,

You should be able to. Here is an example that works for me (note that
you need to mark the class serializable and define operators < and >) :

using System;
using MPI;

[Serializable]
class Thing
{
  public int x;

  public static bool operator <(Thing thing1, Thing thing2) {
   return (thing1.x < thing2.x);
  }

  public static bool operator >(Thing thing1, Thing thing2) {
   return (thing1.x > thing2.x);
  }

};

class Allreduce
{
  static void Main(string[] args)
  {
    using (MPI.Environment env = new MPI.Environment(ref args))
    {
      Intracommunicator world = (Intracommunicator)Communicator.world;

      Thing thing = new Thing();
      thing.x = world.Rank;

      Thing max = world.Allreduce(thing, Operation<Thing>.Max);
      System.Console.WriteLine("Max rank = " + max.x);
    }
  }
};

(If this still doesn't work, if you can post the error message I might
be able to help you more.)

Ben Martin