From 88490b009bc7aee766ca2a99d44629779b520575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Tue, 5 Nov 2024 15:49:16 +0100 Subject: [PATCH 1/6] removing unused files --- src/libmain/libmain.cc | 111 ------------------------ src/libmain/module_template/__init__.py | 15 ---- src/libmain/orbit/__init__.py | 13 --- src/libmain/orbit/_module_loader.py | 18 ---- src/libmain/orbit/pyORBIT.py | 32 ------- src/meson.build | 2 - 6 files changed, 191 deletions(-) delete mode 100644 src/libmain/libmain.cc delete mode 100644 src/libmain/module_template/__init__.py delete mode 100644 src/libmain/orbit/__init__.py delete mode 100644 src/libmain/orbit/_module_loader.py delete mode 100644 src/libmain/orbit/pyORBIT.py diff --git a/src/libmain/libmain.cc b/src/libmain/libmain.cc deleted file mode 100644 index 1844a4e6..00000000 --- a/src/libmain/libmain.cc +++ /dev/null @@ -1,111 +0,0 @@ -#include - -#include "wrap_orbit_mpi.hh" -#include "wrap_bunch.hh" -#include "wrap_spacecharge.hh" -#include "wrap_linacmodule.hh" -#include "wrap_teapotbase.hh" -#include "wrap_utils.hh" -#include "wrap_aperture.hh" -#include "wrap_foil.hh" -#include "wrap_collimator.hh" -#include "wrap_errorbase.hh" -#include "wrap_trackerrk4.hh" -#include "wrap_field_sources_module.hh" -#include "wrap_rfcavities.hh" -#include "wrap_impedances.hh" -#include "wrap_fieldtracker.hh" - -// Define the module methods for the orbit module -static PyMethodDef OrbitMethods[] = { - // Add methods here - {nullptr, nullptr, 0, nullptr} -}; - - -// Define the orbit module definition structure -static PyModuleDef OrbitModule = { - PyModuleDef_HEAD_INIT, - "orbit", - "pORBIT modules implemented in C++ .", - -1, - OrbitMethods, - nullptr, - nullptr, - nullptr, - nullptr -}; - -// Define the module initialization function -// It is a top level placeholder, has no methods or anything -PyMODINIT_FUNC PyInit__orbit(void) { - PyObject* orbit_module = PyModule_Create(&OrbitModule); - if (!orbit_module) { - return nullptr; - } - return orbit_module; -} - - -// Create wrappers with magic names -// that will be picked up by importlib - -PyMODINIT_FUNC PyInit__orbit_mpi(void) { - return wrap_orbit_mpi::initorbit_mpi(); -} - -PyMODINIT_FUNC PyInit__bunch(void) { - return wrap_orbit_bunch::initbunch(); -} - -PyMODINIT_FUNC PyInit__spacecharge(void) { - return initspacecharge(); -} - -PyMODINIT_FUNC PyInit__trackerrk4(void) { - return inittrackerrk4(); -} - -PyMODINIT_FUNC PyInit__teapot_base(void) { - return wrap_teapotbase::initteapotbase(); -} - -PyMODINIT_FUNC PyInit__linac(void) { - return wrap_linac::initlinac(); -} - -PyMODINIT_FUNC PyInit__orbit_utils(void) { - return wrap_orbit_utils::initutils(); -} - -PyMODINIT_FUNC PyInit__aperture(void) { - return wrap_aperture::initaperture(); -} - -PyMODINIT_FUNC PyInit__error_base(void) { - return wrap_errorbase::initerrorbase(); -} - -PyMODINIT_FUNC PyInit__collimator(void) { - return wrap_collimator::initcollimator(); -} - -PyMODINIT_FUNC PyInit__foil(void) { - return wrap_foil::initfoil(); -} - -PyMODINIT_FUNC PyInit__field_sources(void) { - return wrap_field_sources_module::initFieldSourcesModule(); -} - -PyMODINIT_FUNC PyInit__rfcavities(void) { - return wrap_rfcavities::initrfcavities(); -} - -PyMODINIT_FUNC PyInit__impedances(void) { - return wrap_impedances::initimpedances(); -} - -PyMODINIT_FUNC PyInit__fieldtracker(void) { - return wrap_fieldtracker::initfieldtracker(); -} diff --git a/src/libmain/module_template/__init__.py b/src/libmain/module_template/__init__.py deleted file mode 100644 index c1347c98..00000000 --- a/src/libmain/module_template/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -This is not a module, but a template used by setup.py to generate boilerplate code for each one of the pyORBIT C++ extension modules. - -All C++ extension modules MUST be listed in the setup.py file to be able to use them. - -This init file load a single extension module and makes it available for the parent package, i.e., orbit.core. -""" - -from orbit.core._module_loader import load_module - -mod_name = __name__.split(".")[-1] - -locals().update(load_module(mod_name).__dict__) - -del mod_name diff --git a/src/libmain/orbit/__init__.py b/src/libmain/orbit/__init__.py deleted file mode 100644 index 2d116de7..00000000 --- a/src/libmain/orbit/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -## Dynamically import all submodules -import os -import importlib - -submodules = [ - f.name for f in os.scandir(os.path.dirname(__file__)) if f.is_dir() and not f.name.startswith(".") and not f.name.startswith("_") -] - -for module in submodules: - _mod = importlib.import_module(f"orbit.core.{module}", package=None) - locals().update({module: _mod}) - -del _mod diff --git a/src/libmain/orbit/_module_loader.py b/src/libmain/orbit/_module_loader.py deleted file mode 100644 index b78c9a24..00000000 --- a/src/libmain/orbit/_module_loader.py +++ /dev/null @@ -1,18 +0,0 @@ -import importlib.machinery -import importlib.util - -from orbit.core import _orbit - - -def load_module(mod_name: str): - # The name of the module in the C++ shared library starts with an underscore. - mod_name = "_" + mod_name - - pkg_path = _orbit.__file__ - - loader = importlib.machinery.ExtensionFileLoader(mod_name, pkg_path) - spec = importlib.util.spec_from_file_location(mod_name, pkg_path) - module = importlib.util.module_from_spec(spec) - loader.exec_module(module) - - return module diff --git a/src/libmain/orbit/pyORBIT.py b/src/libmain/orbit/pyORBIT.py deleted file mode 100644 index 8299a4fd..00000000 --- a/src/libmain/orbit/pyORBIT.py +++ /dev/null @@ -1,32 +0,0 @@ -import os -import sys -import importlib.util - -""" -This script reproduces the behabior of the legacy pyORBIT executable. -It loads the pyORBIT extension modules and makes them loadable using their short names. - -When called with a script as an argument, it will execute the script. - -If called with python -i flag, it will open an interactive Python interpreter -where pyORBIT modules can be directly loaded. -""" - - -def load_extension_modules(): - # Find submodules - submodules = [ - f.name for f in os.scandir(os.path.dirname(__file__)) if f.is_dir() and not f.name.startswith(".") and not f.name.startswith("_") - ] - # Add a link to the modules without the leading underscore to be able to load them in legacy code - for module in submodules: - sys.modules[module] = sys.modules[f"_{module}"] - - -if __name__ == "__main__": - load_extension_modules() - if len(sys.argv) > 1: - # Running the script as a module - spec = importlib.util.spec_from_file_location("script", sys.argv[1]) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) diff --git a/src/meson.build b/src/meson.build index 341b389c..c41e31f6 100644 --- a/src/meson.build +++ b/src/meson.build @@ -111,7 +111,6 @@ sources = files([ 'trackerrk4/PyExternalEffects.cc', 'trackerrk4/RungeKuttaTracker.cc', 'trackerrk4/wrap_ext_effects_container.cc', - 'libmain/libmain.cc', 'mpi/wrap_orbit_mpi.cc', 'mpi/wrap_mpi_comm.cc', 'mpi/orbit_mpi.cc', @@ -237,7 +236,6 @@ inc = include_directories([ 'orbit/BunchDiagnostics', 'orbit', 'utils/integration', - 'libmain', 'orbit/Apertures' ]) From 0f9aed9c41e7b31a95ac8aee5199c03852d2b3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Tue, 5 Nov 2024 15:49:56 +0100 Subject: [PATCH 2/6] enabling MPI --- src/meson.build | 5 ++--- src/mpi/wrap_orbit_mpi.cc | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/meson.build b/src/meson.build index c41e31f6..73960077 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,6 +13,7 @@ base = meson.current_source_dir() + '/core' dependencies = [] dependencies += dependency('fftw3', version: '>= 3.0.0', required: true) +dependencies += dependency('mpich', version: '>= 4.0.0', required: true) sources = files([ 'linac/wrap_linacmodule.cc', @@ -244,7 +245,7 @@ inc = include_directories([ core_lib = library('core', sources: sources, include_directories: inc, - cpp_args: ['-fPIC', '-std=c++11'], + cpp_args: ['-fPIC', '-std=c++11', '-DUSE_MPI=1'], override_options: ['b_lundef=false'], dependencies: dependencies, install: true, @@ -254,8 +255,6 @@ core_lib = library('core', core_dep = declare_dependency(link_with : core_lib) - - python.extension_module('orbit_mpi', sources: [base + '/mpi_init.cc'], include_directories: inc, diff --git a/src/mpi/wrap_orbit_mpi.cc b/src/mpi/wrap_orbit_mpi.cc index 30a630cd..68e67a3f 100644 --- a/src/mpi/wrap_orbit_mpi.cc +++ b/src/mpi/wrap_orbit_mpi.cc @@ -178,7 +178,7 @@ namespace wrap_orbit_mpi{ }; PyMODINIT_FUNC initorbit_mpi(void) { - // Initialize MPI + // Initialize MPI ORBIT_MPI_Init(); PyObject *m, *d; From 1fc1bc0a50a6a8d9ce36f3e84f6859a26459f384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Tue, 5 Nov 2024 15:58:24 +0100 Subject: [PATCH 3/6] creating a Python module orbit.core that loads all C++ extension modules so that one can use all the possibilities one would expect from Python this also fixes issue #31 because all imports will initialize MPI --- py/orbit/core/__init__.py | 15 +++++++++++++++ py/orbit/core/meson.build | 10 ++++++++++ py/orbit/meson.build | 1 + 3 files changed, 26 insertions(+) create mode 100644 py/orbit/core/__init__.py create mode 100644 py/orbit/core/meson.build diff --git a/py/orbit/core/__init__.py b/py/orbit/core/__init__.py new file mode 100644 index 00000000..714df11f --- /dev/null +++ b/py/orbit/core/__init__.py @@ -0,0 +1,15 @@ +from . import aperture +from . import bunch +from . import collimator +from . import error_base +from . import field_sources +from . import fieldtracker +from . import foil +from . import impedances +from . import linac +from . import orbit_mpi +from . import rfcavities +from . import spacecharge +from . import teapot_base +from . import trackerrk4 +from . import orbit_utils diff --git a/py/orbit/core/meson.build b/py/orbit/core/meson.build new file mode 100644 index 00000000..71c6c920 --- /dev/null +++ b/py/orbit/core/meson.build @@ -0,0 +1,10 @@ + +py_sources = files([ + '__init__.py', +]) + +python.install_sources( + py_sources, + subdir: 'orbit/core', + # pure: true, +) diff --git a/py/orbit/meson.build b/py/orbit/meson.build index 6971bdc5..2ba16e73 100644 --- a/py/orbit/meson.build +++ b/py/orbit/meson.build @@ -1,5 +1,6 @@ subdir('bumps') +subdir('core') subdir('matching') subdir('time_dep') subdir('bunch_utils') From 7aeb01ef80c58af5e760c367dd72da29721c809b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Thu, 7 Nov 2024 14:38:49 +0100 Subject: [PATCH 4/6] adding automatic enabling MPI support if library is present --- src/meson.build | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/meson.build b/src/meson.build index 73960077..417736d2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,7 +13,16 @@ base = meson.current_source_dir() + '/core' dependencies = [] dependencies += dependency('fftw3', version: '>= 3.0.0', required: true) -dependencies += dependency('mpich', version: '>= 4.0.0', required: true) + +# Detecting if MPICH is installed and enabling support if present +mpi_dependency = dependency('mpich', version: '>= 4.0.0', required: false) + +if mpi_dependency.found() + cpp_args = ['-fPIC', '-std=c++11', '-DUSE_MPI=1'] + dependencies += mpi_dependency +else + cpp_args = ['-fPIC', '-std=c++11'] +endif sources = files([ 'linac/wrap_linacmodule.cc', @@ -245,20 +254,18 @@ inc = include_directories([ core_lib = library('core', sources: sources, include_directories: inc, - cpp_args: ['-fPIC', '-std=c++11', '-DUSE_MPI=1'], + cpp_args: cpp_args, override_options: ['b_lundef=false'], dependencies: dependencies, install: true, - ) core_dep = declare_dependency(link_with : core_lib) - python.extension_module('orbit_mpi', sources: [base + '/mpi_init.cc'], include_directories: inc, - cpp_args: ['-fPIC', '-std=c++11'], + cpp_args: cpp_args, dependencies: [core_dep], install: true, subdir: 'orbit/core', From d767c20216ead75b182e481bacd6fd81f73a1ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Thu, 7 Nov 2024 16:08:07 +0100 Subject: [PATCH 5/6] automatic import of orbit_mpi module only --- py/orbit/core/__init__.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/py/orbit/core/__init__.py b/py/orbit/core/__init__.py index 714df11f..bc3665d9 100644 --- a/py/orbit/core/__init__.py +++ b/py/orbit/core/__init__.py @@ -1,15 +1 @@ -from . import aperture -from . import bunch -from . import collimator -from . import error_base -from . import field_sources -from . import fieldtracker -from . import foil -from . import impedances -from . import linac from . import orbit_mpi -from . import rfcavities -from . import spacecharge -from . import teapot_base -from . import trackerrk4 -from . import orbit_utils From 03f448f050754e7cc7aad7e01fca0a4bd5e52fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20F=2E=20Esteban=20M=C3=BCller?= Date: Thu, 7 Nov 2024 21:54:54 +0100 Subject: [PATCH 6/6] adding also support for openmpi --- src/meson.build | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/meson.build b/src/meson.build index 417736d2..ecd0aeb1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -14,12 +14,16 @@ dependencies = [] dependencies += dependency('fftw3', version: '>= 3.0.0', required: true) -# Detecting if MPICH is installed and enabling support if present -mpi_dependency = dependency('mpich', version: '>= 4.0.0', required: false) +# Detecting if MPICH or OPENMPI are installed and enabling support if present +mpich_dependency = dependency('mpich', version: '>= 4.0.0', required: false) +openmpi_dependency = dependency('ompi', version: '>= 5.0.0', required: false) -if mpi_dependency.found() +if mpich_dependency.found() cpp_args = ['-fPIC', '-std=c++11', '-DUSE_MPI=1'] - dependencies += mpi_dependency + dependencies += mpich_dependency +elif openmpi_dependency.found() + cpp_args = ['-fPIC', '-std=c++11', '-DUSE_MPI=1'] + dependencies += openmpi_dependency else cpp_args = ['-fPIC', '-std=c++11'] endif @@ -217,7 +221,7 @@ sources = files([ 'teapot/wrap_matrix_generator.cc', 'teapot/teapotbase.cc', 'teapot/MatrixGenerator.cc' -]) +]) inc = include_directories([ python.get_variable('INCLUDEPY', ''), 'main', @@ -247,7 +251,7 @@ inc = include_directories([ 'orbit', 'utils/integration', 'orbit/Apertures' - + ]) @@ -397,9 +401,3 @@ python.extension_module('error_base', install: true, subdir: 'orbit/core', ) - - - - - -