ENH: reduce: specialisation for reduce of vector2D (used in gamg)

This commit is contained in:
mattijs
2012-02-14 18:23:12 +00:00
parent 5e7d5dbc03
commit 9420dd7bfe
3 changed files with 92 additions and 83 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -28,6 +28,7 @@ License
#include "Pstream.H" #include "Pstream.H"
#include "ops.H" #include "ops.H"
#include "vector2D.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -95,7 +96,7 @@ T returnReduce
} }
// Insist there is a specialisation for the reduction of a scalar // Insist there is a specialisation for the sum reduction of scalar(s)
void reduce void reduce
( (
scalar& Value, scalar& Value,
@ -103,6 +104,13 @@ void reduce
const int tag = Pstream::msgType() const int tag = Pstream::msgType()
); );
void reduce
(
vector2D& Value,
const sumOp<vector2D>& bop,
const int tag = Pstream::msgType()
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -59,6 +59,9 @@ void Foam::reduce(scalar&, const sumOp<scalar>&, const int)
{} {}
void Foam::reduce(vector2D&, const sumOp<vector2D>&, const int)
{}
Foam::label Foam::UPstream::nRequests() Foam::label Foam::UPstream::nRequests()
{ {

View File

@ -305,39 +305,48 @@ void Foam::reduce(scalar& Value, const sumOp<scalar>& bop, const int tag)
scalar sum; scalar sum;
MPI_Allreduce(&Value, &sum, 1, MPI_SCALAR, MPI_SUM, MPI_COMM_WORLD); MPI_Allreduce(&Value, &sum, 1, MPI_SCALAR, MPI_SUM, MPI_COMM_WORLD);
Value = sum; Value = sum;
}
/* if (Pstream::debug)
int myProcNo = UPstream::myProcNo(); {
int nProcs = UPstream::nProcs(); Pout<< "Foam::reduce : reduced value:" << Value << endl;
}
}
//
// receive from children
//
int level = 1;
int thisLevelOffset = 2;
int childLevelOffset = thisLevelOffset/2;
int childProcId = 0;
while void Foam::reduce(vector2D& Value, const sumOp<vector2D>& bop, const int tag)
( {
(childLevelOffset < nProcs) if (Pstream::debug)
&& (myProcNo % thisLevelOffset) == 0 {
) Pout<< "Foam::reduce : value:" << Value << endl;
}
if (!UPstream::parRun())
{
return;
}
if (UPstream::nProcs() <= UPstream::nProcsSimpleSum)
{
if (UPstream::master())
{ {
childProcId = myProcNo + childLevelOffset; for
(
scalar value; int slave=UPstream::firstSlave();
slave<=UPstream::lastSlave();
if (childProcId < nProcs) slave++
)
{ {
vector2D value;
if if
( (
MPI_Recv MPI_Recv
( (
&value, &value,
1, 2,
MPI_SCALAR, MPI_SCALAR,
UPstream::procID(childProcId), UPstream::procID(slave),
tag, tag,
MPI_COMM_WORLD, MPI_COMM_WORLD,
MPI_STATUS_IGNORE MPI_STATUS_IGNORE
@ -346,34 +355,24 @@ void Foam::reduce(scalar& Value, const sumOp<scalar>& bop, const int tag)
{ {
FatalErrorIn FatalErrorIn
( (
"reduce(scalar& Value, const sumOp<scalar>& sumOp)" "reduce(vector2D& Value, const sumOp<vector2D>& sumOp)"
) << "MPI_Recv failed" ) << "MPI_Recv failed"
<< Foam::abort(FatalError); << Foam::abort(FatalError);
} }
Value = bop(Value, value); Value = bop(Value, value);
} }
level++;
thisLevelOffset <<= 1;
childLevelOffset = thisLevelOffset/2;
} }
else
//
// send and receive from parent
//
if (!UPstream::master())
{ {
int parentId = myProcNo - (myProcNo % thisLevelOffset);
if if
( (
MPI_Send MPI_Send
( (
&Value, &Value,
1, 2,
MPI_SCALAR, MPI_SCALAR,
UPstream::procID(parentId), UPstream::procID(UPstream::masterNo()),
tag, tag,
MPI_COMM_WORLD MPI_COMM_WORLD
) )
@ -381,19 +380,53 @@ void Foam::reduce(scalar& Value, const sumOp<scalar>& bop, const int tag)
{ {
FatalErrorIn FatalErrorIn
( (
"reduce(scalar& Value, const sumOp<scalar>& sumOp)" "reduce(vector2D& Value, const sumOp<vector2D>& sumOp)"
) << "MPI_Send failed" ) << "MPI_Send failed"
<< Foam::abort(FatalError); << Foam::abort(FatalError);
} }
}
if (UPstream::master())
{
for
(
int slave=UPstream::firstSlave();
slave<=UPstream::lastSlave();
slave++
)
{
if
(
MPI_Send
(
&Value,
2,
MPI_SCALAR,
UPstream::procID(slave),
tag,
MPI_COMM_WORLD
)
)
{
FatalErrorIn
(
"reduce(vector2D& Value, const sumOp<vector2D>& sumOp)"
) << "MPI_Send failed"
<< Foam::abort(FatalError);
}
}
}
else
{
if if
( (
MPI_Recv MPI_Recv
( (
&Value, &Value,
1, 2,
MPI_SCALAR, MPI_SCALAR,
UPstream::procID(parentId), UPstream::procID(UPstream::masterNo()),
tag, tag,
MPI_COMM_WORLD, MPI_COMM_WORLD,
MPI_STATUS_IGNORE MPI_STATUS_IGNORE
@ -402,52 +435,17 @@ void Foam::reduce(scalar& Value, const sumOp<scalar>& bop, const int tag)
{ {
FatalErrorIn FatalErrorIn
( (
"reduce(scalar& Value, const sumOp<scalar>& sumOp)" "reduce(vector2D& Value, const sumOp<vector2D>& sumOp)"
) << "MPI_Recv failed" ) << "MPI_Recv failed"
<< Foam::abort(FatalError); << Foam::abort(FatalError);
} }
} }
}
else
// {
// distribute to my children vector2D sum;
// MPI_Allreduce(&Value, &sum, 2, MPI_SCALAR, MPI_SUM, MPI_COMM_WORLD);
level--; Value = sum;
thisLevelOffset >>= 1;
childLevelOffset = thisLevelOffset/2;
while (level > 0)
{
childProcId = myProcNo + childLevelOffset;
if (childProcId < nProcs)
{
if
(
MPI_Send
(
&Value,
1,
MPI_SCALAR,
UPstream::procID(childProcId),
tag,
MPI_COMM_WORLD
)
)
{
FatalErrorIn
(
"reduce(scalar& Value, const sumOp<scalar>& sumOp)"
) << "MPI_Send failed"
<< Foam::abort(FatalError);
}
}
level--;
thisLevelOffset >>= 1;
childLevelOffset = thisLevelOffset/2;
}
*/
} }
if (Pstream::debug) if (Pstream::debug)