Skip to content

Commit

Permalink
Merge pull request cyclus#1709 from bennibbelink/openmp
Browse files Browse the repository at this point in the history
  • Loading branch information
gonuke authored Feb 2, 2025
2 parents 0e67ce6 + fd8073b commit 5cc83af
Show file tree
Hide file tree
Showing 37 changed files with 400 additions and 184 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
apt,
conda,
]
parallel_flag : [
"",
"--parallel",
]

services:
registry:
Expand Down Expand Up @@ -67,6 +71,7 @@ jobs:
build-args: |
pkg_mgr=${{ matrix.pkg_mgr }}
ubuntu_version=${{ matrix.ubuntu_versions }}
build_flags=${{ matrix.parallel_flag }}
- name: Checkout Cycamore
if: ${{ github.event_name == 'pull_request' && steps.build-cyclus.outcome == 'success' }}
Expand Down Expand Up @@ -125,13 +130,13 @@ jobs:
steps.build-cymetric.outcome == 'failure' && '**Failure** :x:' ||
'**Skipped due to upstream failure** :warning:'}}" >> "$GITHUB_ENV"
echo "ARTIFACT_NAME=${{ matrix.ubuntu_versions }}_${{ matrix.pkg_mgr }}" >> "$GITHUB_ENV"
echo "ARTIFACT_NAME=${{ matrix.ubuntu_versions }}_${{ matrix.pkg_mgr }}${{ matrix.parallel_flag != '' && '_parallel' || '' }}" >> "$GITHUB_ENV"
- name: Construct Artifact
if: github.event_name == 'pull_request'
run: |
echo "
##### Build \`FROM cyclus_${{ matrix.ubuntu_versions }}_${{ matrix.pkg_mgr }}/cyclus\`
##### Build \`FROM cyclus_${{ matrix.ubuntu_versions }}_${{ matrix.pkg_mgr }}/cyclus\` ${{ matrix.parallel_flag }}
- Cycamore: ${{ env.CYCAMORE_BUILD_STATUS }}
- Cymetric: ${{ env.CYMETRIC_BUILD_STATUS }}" > ${{ env.ARTIFACT_NAME }}.txt
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/macos_build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ jobs:
macos-13,
macos-14,
]
parallel_flag : [
"",
"--parallel",
]
coin_flag : [
"",
"--allow-milps",
]

runs-on: ["${{ matrix.macos_versions }}"]

Expand Down Expand Up @@ -86,7 +94,7 @@ jobs:
export CXX=$CONDA_PREFIX/bin/clang++
export CPP=$CONDA_PREFIX/bin/clang-cpp
mkdir -p $(python3 -m site --user-site)
python3 install.py -j 3 --allow-milps
python3 install.py -j 3 ${{ matrix.parallel_flag }} ${{ matrix.coin_flag }}
export PATH=$HOME/.local/bin:$PATH
cyclus_unit_tests
cd tests && python3 -m pytest
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Since last release
* More random distributions for the random number generator (#1821, #1827)
* Added example of header code injection for facility cost (#1829)
* Table for recording active/dormant cycle periods (#1830)
* Support for OpenMP parallelized simulations (#1709)

**Changed:**

Expand Down
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IF(CODE_COVERAGE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
ENDIF()


# enable assembly
enable_language(ASM)

Expand Down Expand Up @@ -317,6 +318,19 @@ IF(NOT CYCLUS_DOC_ONLY)
SET(LIBS ${LIBS} ${Tcmalloc_LIBRARIES})
endif(Cython_FOUND)


IF(PARALLEL)
FIND_PACKAGE(OpenMP REQUIRED)
IF (OpenMP_CXX_FOUND)
MESSAGE("-- OpenMP Version: ${OpenMP_CXX_VERSION}")
MESSAGE("-- OpenMP Flags: ${OpenMP_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF(OpenMP_CXX_FOUND)
IF(OpenMP_CXX_LIBRARIES)
set(LIBS ${LIBS} ${OpenMP_CXX_LIBRARIES})
ENDIF(OpenMP_CXX_LIBRARIES)
ENDIF(PARALLEL)

##############################################################################################
#################################### end find libraries ######################################
##############################################################################################
Expand All @@ -337,7 +351,8 @@ IF(NOT CYCLUS_DOC_ONLY)
"${SQLite3_INCLUDE_DIR}"
"${HDF5_INCLUDE_DIRS}"
"${Boost_INCLUDE_DIR}"
"${COIN_INCLUDE_DIRS}")
"${COIN_INCLUDE_DIRS}"
"${OpenMP_CXX_INCLUDE_DIRS}")
IF(NOT inc_dirs STREQUAL "")
INCLUDE_DIRECTORIES(${inc_dirs})
ENDIF()
Expand Down
5 changes: 4 additions & 1 deletion INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ flags:
- Will NOT try to compile from assembly, if possible. This is slower as it must compile from source.

* - ``-D VAR``
- set environment variable(s).
- set environment variable(s).

* - ``--parallel``
- enable support for OpenMP directives


For example, if you have installed coin-Cbc from source or otherwise have it
Expand Down
15 changes: 14 additions & 1 deletion cli/cyclus.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "platform.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
Expand All @@ -9,7 +10,9 @@
#include <boost/filesystem.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/string_generator.hpp>

#if CYCLUS_IS_PARALLEL
#include <omp.h>
#endif // CYCLUS_IS_PARALLEL
#include "cyclus.h"
#include "hdf5_back.h"
#include "pyhooks.h"
Expand Down Expand Up @@ -280,6 +283,7 @@ int ParseCliArgs(ArgInfo* ai, int argc, char* argv[]) {
("py-to-json", po::value<std::string>(), "*.py input file")
("py-to-xml", po::value<std::string>(), "*.py input file")
("xml-to-py", po::value<std::string>(), "*.xml input file")
("nthreads,j", po::value<int>(), "number of threads to use, available if built with --parallel")
;

po::variables_map vm;
Expand Down Expand Up @@ -529,4 +533,13 @@ void GetSimInfo(ArgInfo* ai) {
if (ai->vm.count("output-path")) {
ai->output_path = ai->vm["output-path"].as<std::string>();
}

// Thread param
#if CYCLUS_IS_PARALLEL
int nthreads = 1;
if (ai->vm.count("nthreads")) {
nthreads = ai->vm["nthreads"].as<int>();
}
omp_set_num_threads(nthreads);
#endif // CYCLUS_IS_PARALLEL
}
9 changes: 8 additions & 1 deletion cli/cyclus_unit_test_driver.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "platform.h"
#include <iostream>
#include <string>
#include <stdio.h>

#include <gtest/gtest.h>

#if CYCLUS_IS_PARALLEL
#include <omp.h>
#endif // CYCLUS_IS_PARALLEL
#include "env.h"
#include "logger.h"

Expand All @@ -22,6 +25,10 @@ int main(int argc, char* argv[]) {
}
putenv(const_cast<char *>(test_env.c_str()));

#if CYCLUS_IS_PARALLEL
omp_set_num_threads(1);
#endif // CYCLUS_IS_PARALLEL

for ( int i = 0; i < argc; i++ ) {
std::string arg = argv[i];
if ( arg == "--help" ) {
Expand Down
3 changes: 3 additions & 0 deletions cyclus/agents.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cdef cppclass CyclusRegionShim "CyclusRegionShim" (cpp_cyclus.Region): # C++CON
void Tick() except *
void Tock() except *
void Decision() except *
cpp_bool IsShim() except *
# Extra interface
PyObject* self # the Python object we are shimming

Expand Down Expand Up @@ -91,6 +92,7 @@ cdef cppclass CyclusInstitutionShim "CyclusInstitutionShim" (cpp_cyclus.Institut
void Tick() except *
void Tock() except *
void Decision() except *
cpp_bool IsShim() except *
# Extra interface
PyObject* self # the Python object we are shimming

Expand Down Expand Up @@ -120,6 +122,7 @@ cdef cppclass CyclusFacilityShim "CyclusFacilityShim" (cpp_cyclus.Facility): #
void Tick() except *
void Tock() except *
void Decision() except *
cpp_bool IsShim() except *
cpp_bool CheckDecommissionCondition() except *
std_set[shared_ptr[cpp_cyclus.RequestPortfolio[cpp_cyclus.Material]]] GetMatlRequests() except *
std_set[shared_ptr[cpp_cyclus.RequestPortfolio[cpp_cyclus.Product]]] GetProductRequests() except *
Expand Down
9 changes: 9 additions & 0 deletions cyclus/agents.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ cdef cppclass CyclusRegionShim "CyclusRegionShim" (cpp_cyclus.Region):
void Decision() except *:
(<object> this.self).decision()

cpp_bool IsShim() except *:
return True


cdef cppclass CyclusInstitutionShim "CyclusInstitutionShim" (cpp_cyclus.Institution):
# A C++ class that acts as a Institution. It implements the Institution virtual
Expand Down Expand Up @@ -444,6 +447,9 @@ cdef cppclass CyclusInstitutionShim "CyclusInstitutionShim" (cpp_cyclus.Institut
void Decision() except *:
(<object> this.self).decision()

cpp_bool IsShim() except *:
return True


cdef int _GET_MAT_BIDS_TIME = -9999999999
cdef cpp_cyclus.CommodMap[cpp_cyclus.Material].type* _GET_MAT_BIDS_PTR = NULL
Expand Down Expand Up @@ -585,6 +591,9 @@ cdef cppclass CyclusFacilityShim "CyclusFacilityShim" (cpp_cyclus.Facility):
void Decision() except *:
(<object> this.self).decision()

cpp_bool IsShim() except *:
return True

cpp_bool CheckDecommissionCondition() except *:
rtn = (<object> this.self).check_decommission_condition()
return bool_to_cpp(rtn)
Expand Down
1 change: 1 addition & 0 deletions cyclus/cpp_cyclus.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ cdef extern from "time_listener.h" namespace "cyclus":
void Tick()
void Tock()
void Decision()
cpp_bool IsShim()


cdef extern from "trade.h" namespace "cyclus":
Expand Down
9 changes: 4 additions & 5 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ ENV GCOV=/opt/conda/bin/x86_64-conda-linux-gnu-gcov

FROM ${pkg_mgr}-coverage-deps AS cyclus-coverage
ARG make_cores=2

ARG build_flags="--parallel"
COPY . /cyclus
WORKDIR /cyclus

RUN python3 install.py -j ${make_cores} --allow-milps --code-coverage

RUN python3 install.py -j ${make_cores} --allow-milps --code-coverage ${build_flags}
ENV PATH=/root/.local/bin:$PATH
ENV LCOV_ARGS="--gcov-tool=${GCOV} --no-external"
RUN lcov -c -d /cyclus -o initial_coverage.info --initial ${LCOV_ARGS} && \
Expand All @@ -118,7 +117,7 @@ COPY --from=cyclus-coverage /cyclus /

FROM ${pkg_mgr}-deps AS cyclus
ARG make_cores=2

ARG build_flags="--parallel"
COPY . /cyclus
WORKDIR /cyclus

Expand All @@ -128,7 +127,7 @@ WORKDIR /cyclus

# You may add the option "--cmake-debug" to the following command
# for further CMake debugging.
RUN python3 install.py -j ${make_cores} --allow-milps
RUN python3 install.py -j ${make_cores} --allow-milps ${build_flags}
ENV PATH=/root/.local/bin:$PATH

FROM cyclus AS deb-generation
Expand Down
21 changes: 10 additions & 11 deletions docker/Rocky.dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
ARG rocky_version=9
FROM rockylinux:${rocky_version} as common-base
FROM rockylinux:${rocky_version} AS common-base

ENV TZ=America/Chicago
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN dnf update -y && \
dnf install -y 'dnf-command(config-manager)' && \
dnf install -y python3.11 epel-release

FROM common-base as rocky-8-config
FROM common-base AS rocky-8-config
RUN alternatives --set python3 /usr/bin/python3.11 && \
dnf config-manager --set-enabled powertools

FROM common-base as rocky-9-config
FROM common-base AS rocky-9-config
RUN alternatives --install /usr/bin/python3 python3 /bin/python3.11 10 && \
dnf config-manager --set-enabled crb

FROM rocky-${rocky_version}-config as dnf-deps
FROM rocky-${rocky_version}-config AS dnf-deps
RUN dnf update -y && dnf install -y \
which \
git \
Expand All @@ -37,7 +37,7 @@ RUN dnf update -y && dnf install -y \

RUN mkdir -p $(python3 -m site --user-site) && python3 -m pip install pandas tables cython jinja2

FROM dnf-deps as libxmlpp
FROM dnf-deps AS libxmlpp
RUN dnf update -y && dnf install -y wget m4 doxygen perl-open perl-XML-Parser diffutils pcre-cpp pcre-devel && \
python3 -m pip install meson ninja packaging && \
wget https://github.com/libxmlplusplus/libxmlplusplus/releases/download/4.0.3/libxml++-4.0.3.tar.xz && \
Expand All @@ -58,7 +58,7 @@ RUN dnf update -y && dnf install -y wget m4 doxygen perl-open perl-XML-Parser di
ninja && \
ninja install

FROM libxmlpp as cyclus
FROM libxmlpp AS cyclus
ARG make_cores=2

COPY . /cyclus
Expand All @@ -70,15 +70,14 @@ WORKDIR /cyclus

# You may add the option "--cmake-debug" to the following command
# for further CMake debugging.
RUN python3 install.py -j ${make_cores} -D Python3_EXECUTABLE=/usr/bin/python3.11
ENV PATH /root/.local/bin:$PATH
ENV LD_LIBRARY_PATH /root/.local/lib:/root/.local/lib/cyclus
RUN python3 install.py -j ${make_cores} --parallel -DPython3_EXECUTABLE=/usr/bin/python3.11
ENV PATH=/root/.local/bin:$PATH

FROM cyclus as cyclus-test
FROM cyclus AS cyclus-test

RUN cyclus_unit_tests

FROM cyclus-test as cyclus-pytest
FROM cyclus-test AS cyclus-pytest

RUN cd tests && python3 -m pytest

6 changes: 6 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def install_cyclus(args):
if args.fast is not None:
fast = 'TRUE' if args.fast else 'FALSE'
cmake_cmd.append('-DCYCLUS_FAST_COMPILE=' + fast)
if args.parallel is not None:
cmake_cmd += ['-DPARALLEL=' + ('TRUE' if args.parallel else 'FALSE')]

check_windows_cmake(cmake_cmd)
rtn = subprocess.check_call(cmake_cmd, cwd=args.build_dir,
Expand Down Expand Up @@ -211,6 +213,10 @@ def main():
action='store_false', help="Will NOT try to compile "
"from assembly, if possible. This is slower as it "
"must compile from source.")

parser.add_argument('--parallel', dest='parallel',
action='store_true', help="Will compile with -fopenmp flag to "
"enable multithreaded simulation support.")

args = parser.parse_args()
# modify roots as needed
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ if(COIN_FOUND)
else()
set(cyclus_has_coin 0)
endif()
if(PARALLEL)
set(cyclus_is_parallel 1)
else()
set(cyclus_is_parallel 0)
endif(PARALLEL)
CONFIGURE_FILE(platform.h.in "${CMAKE_CURRENT_SOURCE_DIR}/platform.h" @ONLY)
CONFIGURE_FILE(version.cc.in "${CMAKE_CURRENT_SOURCE_DIR}/version.cc" @ONLY)
CONFIGURE_FILE(version.h.in "${CMAKE_CURRENT_SOURCE_DIR}/version.h" @ONLY)
Expand Down
Loading

0 comments on commit 5cc83af

Please sign in to comment.