Commit c0c92f98 authored by David Moxey's avatar David Moxey

Add communicator wrappers for Python, ability for Python tests to run in parallel

parent 33591be7
from NekPy.LibUtilities import SessionReader
from NekPy.LibUtilities import SessionReader, ReduceOperator
from NekPy.StdRegions import ConstFactorMap, ConstFactorType, VarCoeffMap, VarCoeffType
from NekPy.SpatialDomains import MeshGraph
from NekPy.MultiRegions import ContField2D
......@@ -14,6 +14,9 @@ if len(sys.argv) < 2:
session = SessionReader.CreateInstance(sys.argv)
graph = MeshGraph.Read(session)
# See if we're actually running in parallel!
comm = session.GetComm()
# Override polynomial order and create ContField2D.
graph.SetExpansionsToPolyOrder(10)
exp = ContField2D(session, graph, session.GetVariable(0))
......@@ -35,7 +38,22 @@ fx = -(lamb + 2*np.pi*np.pi) * sol
# Solve Helmholtz equation.
helm_sol = exp.BwdTrans(exp.HelmSolve(fx, factors, coeffs))
print("L infinity error: %.6e" % np.max(np.abs(helm_sol - sol)))
L2_error = exp.L2(helm_sol, sol)
Linf_error = exp.Linf(helm_sol, sol)
Linf_error_comm = comm.AllReduce(
np.max(np.abs(helm_sol - sol)), ReduceOperator.ReduceMax)
# Test reduction of Array types.
reduce_test = np.zeros(comm.GetSize())
reduce_test[comm.GetRank()] = 1.0
comm.AllReduce(reduce_test, ReduceOperator.ReduceSum)
# Print out some stats for debugging.
if comm.GetRank() == 0:
print("L 2 error (variable nek) : %.6e" % L2_error)
print("L inf error (variable nek) : %.6e" % Linf_error)
print("L inf error (variable nekpy) : %.6e" % Linf_error_comm)
print("Reduction test : %d" % round(reduce_test.sum()))
# Clean up!
session.Finalise()
......@@ -3,12 +3,25 @@
<description>Helmholtz solver in 2D domain</description>
<executable python="true"> Helmholtz2D.py </executable>
<parameters>Helmholtz2D_P7.xml</parameters>
<processes>4</processes>
<files>
<file description="Session File">../../MultiRegions/Tests/Helmholtz2D_P7.xml</file>
</files>
<metrics>
<metric type="Linf" id="1">
<value tolerance="1e-7">6.120209e-05</value>
<value tolerance="1e-7" variable="nek">6.120209e-05</value>
<value tolerance="1e-7" variable="nekpy">6.120209e-05</value>
</metric>
<metric type="L2" id="2">
<value tolerance="1e-7" variable="nek">4.302677e-05</value>
</metric>
<metric type="regex" id="3">
<regex>^Reduction test.*: (\d+)</regex>
<matches>
<match>
<field id="0">4</field>
</match>
</matches>
</metric>
</metrics>
</test>
......@@ -669,7 +669,7 @@ namespace Nektar
/**
*
*/
CommSharedPtr& SessionReader::GetComm()
CommSharedPtr SessionReader::GetComm()
{
return m_comm;
}
......
......@@ -191,7 +191,7 @@ namespace Nektar
/// Returns the session name with process rank
LIB_UTILITIES_EXPORT const std::string GetSessionNameRank() const;
/// Returns the communication object.
LIB_UTILITIES_EXPORT CommSharedPtr &GetComm();
LIB_UTILITIES_EXPORT CommSharedPtr GetComm();
/// Returns if file system shared
LIB_UTILITIES_EXPORT bool GetSharedFilesystem();
/// Finalises the session.
......
......@@ -66,7 +66,15 @@ enum ReduceOperator
{
ReduceSum,
ReduceMax,
ReduceMin
ReduceMin,
SIZE_ReduceOperator
};
const char* const ReduceOperatorMap[] =
{
"ReduceSum",
"ReduceMax",
"ReduceMin"
};
/// Base communications class
......
......@@ -114,5 +114,7 @@ void export_SessionReader()
.def("GetVariable", &SessionReader::GetVariable,
py::return_value_policy<py::copy_const_reference>())
.def("GetComm", &SessionReader::GetComm)//, py::return_internal_reference<>())
;
}
......@@ -3,6 +3,7 @@ ADD_NEKPY_LIBRARY(LibUtilities SOURCES
BasicUtils/SessionReader.cpp
BasicUtils/SharedArray.cpp
BasicUtils/ShapeType.cpp
Communication/Comm.cpp
Foundations/Basis.cpp
Foundations/Points.cpp
LinearAlgebra/NekMatrix.cpp
......
////////////////////////////////////////////////////////////////////////////////
//
// File: ShapeType.cpp
//
// For more information, please see: http://www.nektar.info/
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Python wrapper for ShapeType.
//
////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <LibUtilities/Communication/Comm.h>
#include <LibUtilities/BasicUtils/SharedArray.hpp>
#include <LibUtilities/Python/NekPyConfig.hpp>
using namespace Nektar;
using namespace Nektar::LibUtilities;
template<typename T>
T AllReduce(CommSharedPtr &comm, T toReduce, ReduceOperator oper)
{
comm->AllReduce(toReduce, oper);
return toReduce;
}
/**
* @brief Export for Comm communicator.
*/
void export_Comm()
{
// Export ReduceOperator enum
NEKPY_WRAP_ENUM(ReduceOperator, ReduceOperatorMap);
/*
void (Comm::*AllReduce_Double)(double &, ReduceOperator) =
&Comm::AllReduce<double>;
void (Comm::*AllReduce_Int)(int &, ReduceOperator) =
&Comm::AllReduce<int>;
*/
py::class_<Comm, std::shared_ptr<Comm>,
boost::noncopyable>("Comm", py::no_init)
.def("GetSize", &Comm::GetSize)
.def("GetRank", &Comm::GetRank)
.def("GetType", &Comm::GetType,
py::return_value_policy<py::copy_const_reference>())
.def("AllReduce", &AllReduce<double>)
.def("AllReduce", &AllReduce<int>)
.def("AllReduce", &AllReduce<long>)
.def("AllReduce", &AllReduce<Array<OneD, NekDouble>>)
;
}
......@@ -35,6 +35,7 @@
#include <LibUtilities/Python/NekPyConfig.hpp>
void export_Basis();
void export_Comm();
void export_Points();
void export_SessionReader();
void export_ShapeType();
......@@ -50,6 +51,7 @@ BOOST_PYTHON_MODULE(_LibUtilities)
np::initialize();
export_Basis();
export_Comm();
export_Points();
export_SessionReader();
export_ShapeType();
......
......@@ -136,6 +136,21 @@ NekDouble ExpList_L2_Error(
return exp->L2(in, err);
}
NekDouble ExpList_Linf(
ExpListSharedPtr exp,
const Array<OneD, const NekDouble> &in)
{
return exp->Linf(in);
}
NekDouble ExpList_Linf_Error(
ExpListSharedPtr exp,
const Array<OneD, const NekDouble> &in,
const Array<OneD, const NekDouble> &err)
{
return exp->Linf(in, err);
}
py::tuple ExpList_GetCoords(ExpListSharedPtr exp)
{
int nPhys = exp->GetNpoints();
......@@ -231,6 +246,8 @@ void export_ExpList()
))
.def("L2", &ExpList_L2)
.def("L2", &ExpList_L2_Error)
.def("Linf", &ExpList_Linf)
.def("Linf", &ExpList_Linf_Error)
.def("SetPhysArray", &ExpList_SetPhysArray)
.def("SetPhys", &ExpList_SetPhys)
.def("GetPhys", &ExpList_GetPhys)
......
......@@ -248,6 +248,11 @@ int main(int argc, char *argv[])
// Output from stdout and stderr are directed to the files output.out
// and output.err, respectively.
if (file.IsPythonTest())
{
command += "PYTHONPATH=\"@CMAKE_BINARY_DIR@\" ";
}
#ifdef NEKTAR_TEST_FORCEMPIEXEC
#else
if (file.GetNProcesses() > 1)
......@@ -272,7 +277,6 @@ int main(int argc, char *argv[])
if (file.IsPythonTest())
{
command += "PYTHONPATH=\"@CMAKE_BINARY_DIR@\" ";
command += "@PYTHON_EXECUTABLE@ ";
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment