Commit f87ca38d authored by Markus Holzer's avatar Markus Holzer Committed by Dominik Thoennes
Browse files

Update python coupling

parent 359cab34
......@@ -74,3 +74,4 @@ CMakeDefs.h
/walberla-config.cmake
/cmake-build-debug/
/cmake-build-release/
/cmake-build-debug-remote/
......@@ -578,18 +578,19 @@ endif ( )
##
#############################################################################################################################
if ( WALBERLA_BUILD_WITH_CODEGEN )
find_package( PythonInterp 3 QUIET REQUIRED)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import lbmpy"
set( Python_FIND_FRAMEWORK LAST )
find_package (Python COMPONENTS Interpreter Development)
execute_process(COMMAND ${Python_EXECUTABLE} -c "import lbmpy"
RESULT_VARIABLE LBMPY_FOUND )
if(NOT LBMPY_FOUND EQUAL 0)
message(FATAL_ERROR "WALBERLA_BUILD_WITH_CODEGEN activated but pystencils or lbmpy package not found.
Please install lbmpy e.g.: 'pip3 install lbmpy'")
endif()
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from pystencils.include import get_pystencils_include_path; print(get_pystencils_include_path())"
execute_process(COMMAND ${Python_EXECUTABLE} -c "from pystencils.include import get_pystencils_include_path; print(get_pystencils_include_path())"
OUTPUT_VARIABLE PYSTENCILS_INCLUDE_PATH)
include_directories( ${PYSTENCILS_INCLUDE_PATH} )
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import jinja2"
execute_process(COMMAND ${Python_EXECUTABLE} -c "import jinja2"
RESULT_VARIABLE JINJA2_FOUND )
if(NOT JINJA2_FOUND EQUAL 0)
message(FATAL_ERROR "WALBERLA_BUILD_WITH_CODEGEN activated and jinja2 package not found.
......@@ -607,13 +608,8 @@ endif()
##
#############################################################################################################################
if ( WALBERLA_BUILD_WITH_PYTHON )
find_package( PythonInterp 3.6 QUIET REQUIRED)
find_package( PythonLibs QUIET REQUIRED)
if( NOT (PYTHON_LIBRARY AND PYTHON_INCLUDE_DIR ) )
message( FATAL_ERROR "Couldn't find any python library" )
endif()
set( Python_FIND_FRAMEWORK LAST )
find_package( Python COMPONENTS Interpreter Development REQUIRED)
if(WALBERLA_CXX_COMPILER_IS_INTEL)
# Intel C++17 support introduced in 2.6.2 (https://github.com/pybind/pybind11/pull/2729)
......@@ -622,11 +618,11 @@ if ( WALBERLA_BUILD_WITH_PYTHON )
set(PYBIND11_MINIMUM_VERSION "2.6.0")
endif()
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pybind11; print(pybind11._version.__version__)"
execute_process(COMMAND ${Python_EXECUTABLE} -c "import pybind11; print(pybind11._version.__version__)"
OUTPUT_VARIABLE pybind11_VERSION ERROR_QUIET RESULT_VARIABLE pybind11_VERSION_RESULT)
string(STRIP "${pybind11_VERSION}" pybind11_VERSION)
if(pybind11_VERSION_RESULT EQUAL "0" AND pybind11_VERSION VERSION_GREATER_EQUAL "${PYBIND11_MINIMUM_VERSION}")
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pybind11 --cmakedir
execute_process(COMMAND ${Python_EXECUTABLE} -m pybind11 --cmakedir
OUTPUT_VARIABLE PYBIND11_CMAKE_PATH)
string(STRIP "${PYBIND11_CMAKE_PATH}" PYBIND11_CMAKE_PATH)
find_package(pybind11 PATHS "${PYBIND11_CMAKE_PATH}" NO_DEFAULT_PATH REQUIRED)
......@@ -656,10 +652,10 @@ if ( WALBERLA_BUILD_WITH_PYTHON )
add_subdirectory(extern/pybind11)
if(pybind11_VERSION VERSION_LESS "2.6.2")
# if pybind11 was installed into ${PYTHON_INCLUDE_DIR} (e.g. by pip), that will have a higher priority than the Git submodule unless we reorder the search path
# if pybind11 was installed into ${Python_INCLUDE_DIRS} (e.g. by pip), that will have a higher priority than the Git submodule unless we reorder the search path
# introduced in 2.6.0 (https://github.com/pybind/pybind11/issues/2709), fixed in 2.6.2 (https://github.com/pybind/pybind11/pull/2716)
set_property( TARGET pybind11::pybind11
PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/extern/pybind11/include" "${PYTHON_INCLUDE_DIR}")
PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/extern/pybind11/include" "${Python_INCLUDE_DIRS}")
endif()
endif()
......
......@@ -13,7 +13,6 @@ if (WALBERLA_BUILD_WITH_CUDA)
FlowAroundSphereCodeGen_InfoHeader.h)
waLBerla_add_executable( NAME FlowAroundSphereCodeGen FILE FlowAroundSphereCodeGen.cpp
DEPENDS blockforest boundary core domain_decomposition field geometry timeloop vtk FlowAroundSphereGenerated)
set_target_properties( FlowAroundSphereCodeGen PROPERTIES CXX_VISIBILITY_PRESET hidden)
else ()
waLBerla_generate_target_from_python(NAME FlowAroundSphereGenerated
FILE FlowAroundSphereCodeGen.py
......@@ -27,5 +26,4 @@ else ()
FlowAroundSphereCodeGen_InfoHeader.h)
waLBerla_add_executable( NAME FlowAroundSphereCodeGen FILE FlowAroundSphereCodeGen.cpp
DEPENDS blockforest boundary core domain_decomposition field geometry timeloop vtk FlowAroundSphereGenerated)
set_target_properties( FlowAroundSphereCodeGen PROPERTIES CXX_VISIBILITY_PRESET hidden)
endif()
\ No newline at end of file
......@@ -16,7 +16,6 @@ if (WALBERLA_BUILD_WITH_CUDA)
waLBerla_add_executable(NAME benchmark_multiphase
FILES benchmark_multiphase.cpp InitializerFunctions.cpp multiphase_codegen.py
DEPENDS blockforest core cuda field postprocessing lbm geometry timeloop gui BenchmarkPhaseFieldCodeGenGPU)
set_target_properties(benchmark_multiphase PROPERTIES CXX_VISIBILITY_PRESET hidden)
else ()
waLBerla_generate_target_from_python(NAME BenchmarkPhaseFieldCodeGenCPU
FILE multiphase_codegen.py
......@@ -32,6 +31,5 @@ else ()
waLBerla_add_executable(NAME benchmark_multiphase
FILES benchmark_multiphase.cpp InitializerFunctions.cpp multiphase_codegen.py
DEPENDS blockforest core field postprocessing lbm geometry timeloop gui BenchmarkPhaseFieldCodeGenCPU)
set_target_properties(benchmark_multiphase PROPERTIES CXX_VISIBILITY_PRESET hidden)
endif (WALBERLA_BUILD_WITH_CUDA)
......@@ -25,7 +25,6 @@ foreach(streaming_pattern pull push aa esotwist)
waLBerla_add_executable(NAME UniformGridGPU_${config}
FILES UniformGridGPU.cpp
DEPENDS blockforest boundary core cuda domain_decomposition field geometry timeloop vtk UniformGridGPUGenerated_${config})
set_target_properties( UniformGridGPU_${config} PROPERTIES CXX_VISIBILITY_PRESET hidden)
# all configs are excluded from all except for pull d3q27.
if (${streaming_pattern} STREQUAL "pull" AND ${stencil} STREQUAL "d3q27")
......
......@@ -16,6 +16,5 @@ foreach(config srt trt smagorinsky mrt entropic_kbc_n4 cumulant )
FILES UniformGridGenerated.cpp
DEPENDS blockforest boundary core domain_decomposition field geometry timeloop vtk gui
UniformGridGenerated_${config} python_coupling)
set_target_properties(UniformGridBenchmarkGenerated_${config} PROPERTIES CXX_VISIBILITY_PRESET hidden)
endforeach()
......@@ -25,10 +25,9 @@ if ( WALBERLA_BUILD_WITH_PYTHON )
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
set_target_properties( walberla_cpp PROPERTIES MACOSX_RPATH TRUE )
set_target_properties( walberla_cpp PROPERTIES CXX_VISIBILITY_PRESET hidden)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/setup.py ${CMAKE_CURRENT_BINARY_DIR}/setup.py )
add_custom_target( pythonModule ALL ${PYTHON_EXECUTABLE} setup.py build DEPENDS walberla_cpp )
add_custom_target( pythonModuleInstall ${PYTHON_EXECUTABLE} setup.py install --user DEPENDS walberla_cpp )
add_custom_target( pythonModule ALL ${Python_EXECUTABLE} setup.py build DEPENDS walberla_cpp )
add_custom_target( pythonModuleInstall ${Python_EXECUTABLE} setup.py install --user DEPENDS walberla_cpp )
endif()
......@@ -19,5 +19,3 @@ waLBerla_generate_target_from_python(NAME PhaseFieldCodeGenCPU
waLBerla_add_executable(NAME multiphaseCPU
FILES multiphase.cpp PythonExports.cpp InitializerFunctions.cpp multiphase_codegen.py
DEPENDS blockforest core field postprocessing lbm geometry timeloop gui PhaseFieldCodeGenCPU)
set_target_properties(multiphaseCPU PROPERTIES CXX_VISIBILITY_PRESET hidden)
......@@ -19,5 +19,3 @@ waLBerla_generate_target_from_python(NAME PhaseFieldCodeGenGPU
waLBerla_add_executable(NAME multiphaseGPU
FILES multiphase.cpp PythonExports.cpp InitializerFunctions.cpp util.cpp multiphase_codegen.py
DEPENDS blockforest core cuda field postprocessing lbm geometry timeloop gui PhaseFieldCodeGenGPU)
set_target_properties(multiphaseGPU PROPERTIES CXX_VISIBILITY_PRESET hidden)
......@@ -79,7 +79,7 @@ function( waLBerla_generate_target_from_python )
add_custom_command(OUTPUT ${generatedWithAbsolutePath}
DEPENDS ${sourceFile}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${WALBERLA_PYTHON_DIR}:$ENV{PYTHONPATH} ${PYTHON_EXECUTABLE} ${sourceFile} ${pythonParameters}
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${WALBERLA_PYTHON_DIR}:$ENV{PYTHONPATH} ${Python_EXECUTABLE} ${sourceFile} ${pythonParameters}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${codegenCfg}")
add_library(${PYGEN_NAME} ${generatedWithAbsolutePath})
......
Subproject commit 8de7772cc72daca8e947b79b83fea46214931604
Subproject commit f7b499615e14d70ab098a20deb0cdb3889998a1a
if (WALBERLA_BUILD_WITH_PYTHON)
waLBerla_add_module(DEPENDS core communication domain_decomposition stencil field blockforest vtk cuda)
target_link_libraries(python_coupling PUBLIC pybind11::embed core domain_decomposition)
set_target_properties(python_coupling PROPERTIES CXX_VISIBILITY_PRESET hidden)
endif()
\ No newline at end of file
......@@ -60,7 +60,6 @@ namespace python_coupling {
#ifdef WALBERLA_BUILD_WITH_PYTHON
public:
pybind11::dict & dict() { return d_; }
const pybind11::dict & dict() const { return d_; }
protected:
......
......@@ -97,6 +97,7 @@ void Manager::triggerInitialization()
py::module::import("__main__");
py::module::import("walberla_cpp");
// Setup python path
addPath( std::string(WALBERLA_SOURCE_DIR) + "/python" );
......
......@@ -22,7 +22,7 @@
#include "PythonCallback.h"
#include "DictWrapper.h"
#include "core/logging/all.h"
#ifdef WALBERLA_BUILD_WITH_PYTHON
#include "Manager.h"
......@@ -69,7 +69,6 @@ namespace python_coupling {
}
catch ( py::error_already_set &e) {
throw py::value_error(e.what());
return py::none();
}
}
......@@ -122,7 +121,7 @@ namespace python_coupling {
{
if ( ! isCallable() )
WALBERLA_ABORT_NO_DEBUG_INFO( "Could not call python function '" << functionName_ << "'. " <<
"Did you forget to set the callback function?" );
"Did you forget to set the callback function?" )
namespace py = pybind11;
......
......@@ -82,8 +82,8 @@ namespace python_coupling {
PythonCallback();
PythonCallback( const std::string & functionName );
PythonCallback( const std::string & moduleOrFile,
const std::string & functionName,
const std::vector<std::string> & argv = std::vector<std::string>() );
const std::string & functionName,
const std::vector<std::string> & argv = std::vector<std::string>() );
DictWrapper & data() { return *exposedVars_; }
const DictWrapper & data() const { return *exposedVars_; }
......@@ -97,9 +97,5 @@ namespace python_coupling {
shared_ptr<DictWrapper> callbackDict_;
};
} // namespace python_coupling
} // namespace walberla
......@@ -28,6 +28,14 @@
#ifdef WALBERLA_BUILD_WITH_PYTHON // macro defined in waLBerlaDefinitions.h
#define PYBIND11_NAMESPACE walberla::pybind11
namespace PYBIND11_NAMESPACE {}
namespace pybind11 {
// pybind11 uses ::pybind11 in a few places internally, so we need this alias
using namespace PYBIND11_NAMESPACE;
}
#include "pybind11/pybind11.h"
#endif
......@@ -373,63 +373,6 @@ void timingTreeStopWrapper(WcTimingTree & tt, const std::string& name)
tt.stop(name);
}
void exportTiming(py::module_ &m)
{
py::class_<WcTimer> (m, "Timer")
.def( py::init<>() )
.def( "start", &WcTimer::start )
.def( "stop", &WcTimer::end )
.def( "reset", &WcTimer::reset )
.def( "merge", &WcTimer::merge )
.def_property_readonly( "counter", &WcTimer::getCounter )
.def_property_readonly( "total", &WcTimer::total )
.def_property_readonly( "sumOfSquares", &WcTimer::sumOfSquares )
.def_property_readonly( "average", &WcTimer::average )
.def_property_readonly( "variance", &WcTimer::variance )
.def_property_readonly( "min", &WcTimer::min )
.def_property_readonly( "max", &WcTimer::max )
.def_property_readonly( "last", &WcTimer::last )
;
WcTimer & ( WcTimingPool::*pGetItem ) ( const std::string & ) = &WcTimingPool::operator[];
{
py::scope classScope =
py::class_<WcTimingPool, shared_ptr<WcTimingPool> > (m, "TimingPool")
.def( py::init<>() )
.def_property_readonly( "__getitem__", pGetItem)
.def( "__contains__", &WcTimingPool::timerExists )
.def( "getReduced", &WcTimingPool::getReduced)
.def( "merge", &WcTimingPool::merge)
.def( "clear", &WcTimingPool::clear )
.def( "unifyRegisteredTimersAcrossProcesses", &WcTimingPool::unifyRegisteredTimersAcrossProcesses )
.def( "logResultOnRoot", &WcTimingPool::logResultOnRoot)
;
WALBERLA_UNUSED( classScope );
py::enum_<timing::ReduceType>(m, "ReduceType")
.value("min" , timing::REDUCE_MIN)
.value("avg" , timing::REDUCE_AVG)
.value("max" , timing::REDUCE_MAX)
.value("total", timing::REDUCE_TOTAL)
.export_values()
;
}
const WcTimer & ( WcTimingTree::*pTimingTreeGet ) ( const std::string & ) const = &WcTimingTree::operator[];
py::class_<WcTimingTree, shared_ptr<WcTimingTree> > (m, "TimingTree")
.def( py::init<>() )
.def_property_readonly( "__getitem__", pTimingTreeGet )
.def( "start", &WcTimingTree::start )
.def( "stop", &timingTreeStopWrapper )
.def( "getReduced", &WcTimingTree::getReduced )
.def( "toDict", &buildDictFromTimingTree )
;
}
//======================================================================================================================
//
......@@ -700,8 +643,6 @@ void exportBasicWalberlaDatastructures(py::module_ &m)
exportCellInterval(m);
exportAABB(m);
exportTiming(m);
exportIBlock(m);
exportCommunication(m);
......
......@@ -14,12 +14,10 @@ if (WALBERLA_BUILD_WITH_PYTHON)
waLBerla_compile_test( FILES CallbackTest.cpp DEPENDS blockforest field )
waLBerla_execute_test( NAME CallbackTest
COMMAND $<TARGET_FILE:CallbackTest> ${CMAKE_CURRENT_SOURCE_DIR}/CallbackTest.py )
set_target_properties(CallbackTest PROPERTIES CXX_VISIBILITY_PRESET hidden)
waLBerla_compile_test( FILES FieldExportTest.cpp DEPENDS blockforest field )
waLBerla_execute_test( NAME FieldExportTest
COMMAND $<TARGET_FILE:FieldExportTest> ${CMAKE_CURRENT_SOURCE_DIR}/FieldExportTest.py )
set_target_properties(FieldExportTest PROPERTIES CXX_VISIBILITY_PRESET hidden)
add_test( NAME PythonWalberlaTest
COMMAND python3 -m unittest discover waLBerla_tests/ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/python/)
......
......@@ -32,14 +32,18 @@
using namespace walberla;
using ScalarField = GhostLayerField<int, 1>;
int main( int argc, char ** argv )
{
auto pythonManager = python_coupling::Manager::instance();
pythonManager->addExporterFunction( field::exportModuleToPython<Field<int,1>> );
pythonManager->addExporterFunction( field::exportModuleToPython<Field<int, 1>> );
pythonManager->triggerInitialization();
if ( argc != 2 ) {
WALBERLA_ABORT_NO_DEBUG_INFO("Wrong parameter count: \nUsage: \n ./CallbackTest CallbackTest.py");
if ( argc != 2 )
{
WALBERLA_ABORT_NO_DEBUG_INFO("Wrong parameter count: \nUsage: \n ./CallbackTest CallbackTest.py")
}
std::string pythonFile ( argv[1] );
......@@ -58,25 +62,27 @@ int main( int argc, char ** argv )
// This callback should sum up the two given integers
python_coupling::PythonCallback cb1 ( pythonFile, "cb1" );
WALBERLA_ASSERT( cb1.isCallable() );
WALBERLA_ASSERT( cb1.isCallable() )
cb1.data().exposeValue("input1", 5);
cb1.data().exposeValue("input2", 10);
cb1();
int result = cb1.data().get<int>( "returnValue" );
WALBERLA_CHECK_EQUAL( result, 15 );
typedef GhostLayerField<int,1> ScalarField;
WALBERLA_CHECK_EQUAL( result, 15 )
ScalarField f ( 3,2,2, 1, 25 );
python_coupling::PythonCallback cb2 ( pythonFile, "cb2" );
WALBERLA_ASSERT( cb2.isCallable() );
WALBERLA_ASSERT( cb2.isCallable() )
cb2.data().exposePtr("field", &f );
cb2();
WALBERLA_CHECK_EQUAL( f(0,0,0), 42 );
WALBERLA_CHECK_EQUAL( f(-1,-1,-1), 5 );
//python_coupling::Shell shell("MyGreatInputShell");
//shell();
WALBERLA_CHECK_EQUAL( f(0,0,0), 42 )
WALBERLA_CHECK_EQUAL( f(-1,-1,-1), 5 )
return 0;
return EXIT_SUCCESS;
}
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