CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Running, Solving & CFD (
-   -   Reduce operation for an array (

xiao December 14, 2008 01:01

Hi all, I am writing a para
Hi all,

I am writing a parallel code and need to do a reduce operation on a vector array, i.e.

someScalar = some number in local cpu;
someArray = new vector[100];

For the scalar it should be:
reduce(someScalar, sumOp<scalar>());

What would be corresponding operation for the array? Would this work?

reduce(someArray, sumOp<vector[]>());

Doing this element-wise would be slow, I imagine, because there is too much communication involved.

Thanks very much!


xiao December 15, 2008 13:04

Anyone had experience to share
Anyone had experience to share? Or the experts are all on vacation now?

MPI has MPI_Allreduce, MPI_reduce, and many other function. Does openfoam have equivalent wrapper for them? Where should I find them? Pstream only has a few of them.


xiao December 22, 2008 16:48

After some (actually, a lot of
After some (actually, a lot of) research, I finally figured out.

The function I was looking for was:
listCombineGather() and listCombineScatter().

But this only works for list, not for arrays... but I guess is not hard to change arrays to lists. Just change the definition is enough. List has index operations.

For the reference of those who used to program in raw MPI styles, the scatter(), gather(), combineScatter(), and combineGather() are different from the MPI_scatter and MPI_gather, but corresponding to MPI_Bcast and MPI_reduce() respectively.

MPI_Scatter and MPI_gather corresponding to gatherList() and scatterList() in Foam.

ntrask December 23, 2008 11:57

To use the all of the gather/s
To use the all of the gather/scatter operations the object that you're gathering needs to have the <<>> operators overloaded and certain constructors defined. You should use a list instead of an array (they're pretty much the same anyway) because it has the interface set up.


xiao December 23, 2008 18:33

Do you have some example code
Do you have some example code doing gather/scatter/reduce operations on data structures other than scalar and list? The following is from function cloud::move(). This is an operation on lableListList, but it requires a customer-defined operation.

// List of the numbers of particles to be transfered across the
00179 // processor patches
00180 labelList nsTransPs(transferList.size());
00182 forAll(transferList, i)
00183 {
00184 nsTransPs[i] = transferList[i].size();
00185 }
00187 // List of the numbers of particles to be transfered across the
00188 // processor patches for all the processors
00189 labelListList allNTrans(Pstream::nProcs());
00190 allNTrans[Pstream::myProcNo()] = nsTransPs;
00191 combineReduce(allNTrans, combineNsTransPs());

************************************************** *******
class combineNsTransPs
00097 {
00099 public:
00101 void operator()(labelListList& x, const labelListList& y) const
00102 {
00103 forAll(y, i)
00104 {
00105 if (y[i].size())
00106 {
00107 x[i] = y[i];
00108 }
00109 }
00110 }
00111 };

Fransje July 20, 2011 08:08

A few years late, but it might help other people looking for the same information..

For as far as I know, tt is possible to reduce all the variables types defined in OpenFOAM.

So for a vector, one would do:


vector myVector(vector::zero);

//do something with myVector

reduce( myVector, sumOp<vector>() );

Of course, the same can also be done using other variable types. For example:


scalarField myScalarField( theSizeYouWant, 0.0);

//do something with myScalarField

reduce( myScalarField, sumOp<scalarField>() );

Of course, other reduceOps can be used, like minOp<>(), maxOp<>() etc.

Looping over a field is not a good idea, because it is prohibitively expensive in inter-processor communication, and can slow your simulation to a point where it would be faster to run it on one processor.

All times are GMT -4. The time now is 20:50.