Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

279 make solve function for ode solvers thread safe #322

Merged
merged 35 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
813fc93
decoupling solver stats
K20shores Oct 19, 2023
ddeaaa3
put the jacobian on the state
K20shores Oct 20, 2023
f81d44d
moving L and U to state
K20shores Oct 20, 2023
07a0f54
tests pass
K20shores Oct 20, 2023
02e0718
added openmp test
K20shores Oct 20, 2023
a8370c5
stuff
K20shores Oct 20, 2023
eb3bfea
jit now supports the thread safe solver
K20shores Oct 23, 2023
bdf58e2
adding a test for the jit solver for thread safety
K20shores Oct 23, 2023
6d635dd
merging
K20shores Oct 23, 2023
aa5ea0c
removing data member
K20shores Oct 23, 2023
2dc189c
removing N
K20shores Oct 23, 2023
8a4307c
removing n
K20shores Oct 23, 2023
c203293
ordering state parameters
K20shores Oct 23, 2023
7afb076
more ordering...
K20shores Oct 23, 2023
d013616
ordering
K20shores Oct 23, 2023
aedb60e
ordering
K20shores Oct 23, 2023
732e7b9
fixing some things?
K20shores Oct 24, 2023
1c07420
thing
K20shores Oct 24, 2023
430864c
fixing cuda stuff?
K20shores Oct 24, 2023
2219aa5
correcting analytical tests
K20shores Oct 25, 2023
27093e6
removing duplicate linkage of LLVM with the tests
K20shores Oct 25, 2023
ec754d1
removing system from rosenbrock solver
K20shores Oct 25, 2023
887e927
moving solver parameters into its own file
K20shores Oct 25, 2023
ad6713e
Merge branch 'main' into 279-make-solve-function-for-ode-solvers-thre…
K20shores Oct 25, 2023
6aeea86
removing invalid state constructor
K20shores Oct 26, 2023
2aa76f1
removing state from jit process set
K20shores Oct 26, 2023
e4b5e70
removing assert
K20shores Oct 26, 2023
81686ce
setting variable names
K20shores Oct 26, 2023
f98d981
removing parallel build to see if clang builds
K20shores Oct 26, 2023
74e1a82
Merge branch '279-make-solve-function-for-ode-solvers-thread-safe' of…
K20shores Oct 26, 2023
3f80301
verbose build on clang
K20shores Oct 26, 2023
7e55464
attempting to force libc++ for clang to make test pass
K20shores Oct 26, 2023
9829a9e
only linking libc++ if on linux...
K20shores Oct 26, 2023
aa60756
matching linux properly
K20shores Oct 26, 2023
c50352e
comment
K20shores Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
run: cmake -S . -B build -D CMAKE_BUILD_TYPE=${{ matrix.build_type }}

- name: Build
run: cmake --build build --parallel 10
run: cmake --build build --verbose

- name: Run tests
run: |
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ endif()

add_compile_definitions(DEFAULT_VECTOR_SIZE=${DEFAULT_VECTOR_MATRIX_SIZE})

# on ubuntu with clang, an incorrect version of the c++ standard library was being linked
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# If the compiler is Clang, use libc++
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

################################################################################
# Dependencies

Expand Down
8 changes: 0 additions & 8 deletions cmake/test_util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ function(create_standard_test)
target_link_libraries(test_${TEST_NAME} PUBLIC ${library})
endforeach()

if(ENABLE_JSON)
target_link_libraries(test_${TEST_NAME} PRIVATE nlohmann_json::nlohmann_json)
endif()

if(ENABLE_LLVM)
target_link_libraries(test_${TEST_NAME} PRIVATE ${llvm_libs})
endif()

if(NOT DEFINED TEST_WORKING_DIRECTORY)
set(TEST_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN mkdir /build \
-D CMAKE_BUILD_TYPE=debug \
-D ENABLE_CLANG_TIDY:BOOL=FALSE \
../micm \
&& make install -j 8
&& make install -j

# now test if we can use the installed files
RUN cd /micm/test/integration/cmake/find_package \
Expand Down
2 changes: 2 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"json_url": "https://ncar.github.io/micm/_static/switcher.json",
"version_match": release,
},
"pygment_light_style": "tango",
"pygment_dark_style": "monokai"
}

html_css_files = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Finally, set and upate the rate constants as needed:
// so we need to track how much time the solver was able to integrate for and continue
// solving until we finish
double elapsed_solve_time = 0;
+ state.SetCustomRateParameter("PHOTO.my photolysis rate", photo_rate);
+state.SetCustomRateParameter("PHOTO.my photolysis rate", photo_rate);

while (elapsed_solve_time < time_step)
{
Expand All @@ -205,7 +205,7 @@ Finally, set and upate the rate constants as needed:
}

print_state(time_step * (i + 1), state);
+ photo_rate *= 1.5;
+photo_rate *= 1.5;
}

And this is final output. Notice that the concentration of G ends up much higher than in
Expand Down
10 changes: 10 additions & 0 deletions include/micm/process/arrhenius_rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ namespace micm
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions, std::vector<double>::const_iterator custom_parameters) const override;

/// @brief Calculate the rate constant
/// @param conditions The current environmental conditions of the chemical system
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions) const override;

double calculate(const double& temperature, const double& pressure) const;
};

Expand All @@ -72,6 +77,11 @@ namespace micm
return calculate(conditions.temperature_, conditions.pressure_);
}

inline double ArrheniusRateConstant::calculate(const Conditions& conditions) const
{
return calculate(conditions.temperature_, conditions.pressure_);
}

inline double ArrheniusRateConstant::calculate(const double& temperature, const double& pressure) const
{
return parameters_.A_ * std::exp(parameters_.C_ / temperature) * std::pow(temperature / parameters_.D_, parameters_.B_) *
Expand Down
10 changes: 10 additions & 0 deletions include/micm/process/branched_rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace micm
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions, std::vector<double>::const_iterator custom_parameters) const override;

/// @brief Calculate the rate constant
/// @param conditions The current environmental conditions of the chemical system
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions) const override;

/// @brief Calculate the rate constant
/// @param temperature Temperature in [K]
/// @param air_number_density Number density in [mol m-3]
Expand Down Expand Up @@ -92,6 +97,11 @@ namespace micm
return calculate(conditions.temperature_, conditions.air_density_);
}

inline double BranchedRateConstant::calculate(const Conditions& conditions) const
{
return calculate(conditions.temperature_, conditions.air_density_);
}

inline double BranchedRateConstant::calculate(const double& temperature, const double& air_number_density) const
{
double pre = parameters_.X_ * std::exp(-parameters_.Y_ / temperature);
Expand Down
18 changes: 6 additions & 12 deletions include/micm/process/cuda_process_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

#include <micm/process/process_set.hpp>
#include <micm/util/cuda_param.hpp>
#include <micm/process/cuda_process_set.cuh>

#ifdef USE_CUDA
# include <micm/process/cuda_process_set.cuh>
#endif

#ifdef USE_CUDA
namespace micm
{
/// @brief A GPU-based implementation of ProcessSet
Expand All @@ -20,8 +16,7 @@ namespace micm
/// @brief Create a process set calculator for a given set of processes
/// @param processes Processes to create calculator for
/// @param state Solver state
template<template<class> class MatrixPolicy>
CudaProcessSet(const std::vector<Process>& processes, const State<MatrixPolicy>& state);
CudaProcessSet(const std::vector<Process>& processes, const std::map<std::string, std::size_t>& variable_map);

template<template<class> typename MatrixPolicy>
requires VectorizableDense<MatrixPolicy<double>> std::chrono::nanoseconds AddForcingTerms(
Expand All @@ -39,9 +34,8 @@ namespace micm
const;
};

template<template<class> class MatrixPolicy>
inline CudaProcessSet::CudaProcessSet(const std::vector<Process>& processes, const State<MatrixPolicy>& state)
: ProcessSet(processes, state)
inline CudaProcessSet::CudaProcessSet(const std::vector<Process>& processes, const std::map<std::string, std::size_t>& variable_map)
: ProcessSet(processes, variable_map)
{
}

Expand Down Expand Up @@ -73,6 +67,7 @@ namespace micm
std::chrono::nanoseconds kernel_duration = micm::cuda::AddForcingTermsKernelDriver(matrix, processSet);
return kernel_duration; // time performance of kernel function
}

template<template<class> class MatrixPolicy, template<class> class SparseMatrixPolicy>
requires VectorizableDense<MatrixPolicy<double>> && VectorizableSparse<SparseMatrixPolicy<double>>
inline std::chrono::nanoseconds CudaProcessSet::AddJacobianTerms(
Expand Down Expand Up @@ -104,5 +99,4 @@ namespace micm
std::chrono::nanoseconds kernel_duration = micm::cuda::AddJacobianTermsKernelDriver(matrix, sparseMatrix, processSet);
return kernel_duration; // time performance of kernel function
}
} // namespace micm
#endif
} // namespace micm
12 changes: 3 additions & 9 deletions include/micm/process/jit_process_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ namespace micm
/// @param compiler JIT compiler
/// @param processes Processes to create calculator for
/// @param state Solver state
template<template<class> class MatrixPolicy>
JitProcessSet(
std::shared_ptr<JitCompiler> compiler,
const std::vector<Process> &processes,
const State<MatrixPolicy> &state);
const std::map<std::string, std::size_t>& variable_map);

~JitProcessSet();

Expand Down Expand Up @@ -103,20 +102,15 @@ namespace micm
}

template<std::size_t L>
template<template<class> class MatrixPolicy>
inline JitProcessSet<L>::JitProcessSet(
std::shared_ptr<JitCompiler> compiler,
const std::vector<Process> &processes,
const State<MatrixPolicy> &state)
: ProcessSet(processes, state),
const std::map<std::string, std::size_t>& variable_map)
: ProcessSet(processes, variable_map),
compiler_(compiler)
{
forcing_function_ = NULL;
jacobian_function_ = NULL;
if (state.variables_.size() != L || state.variables_.GroupVectorSize() != L)
{
throw std::runtime_error("Invalid state for JitProcessSet. Check the the VectorMatrix template parameters.");
}
this->GenerateForcingFunction();
}

Expand Down
16 changes: 8 additions & 8 deletions include/micm/process/process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace micm
/// @brief Update the solver state rate constants
/// @param processes The set of processes being solved
/// @param state The solver state to update
template<template<class> class MatrixPolicy>
template<template<class> class MatrixPolicy, template<class> class SparseMatrixPolicy>
requires(!VectorizableDense<MatrixPolicy<double>>) static void UpdateState(
const std::vector<Process>& processes,
State<MatrixPolicy>& state);
template<template<class> class MatrixPolicy>
State<MatrixPolicy, SparseMatrixPolicy>& state);
template<template<class> class MatrixPolicy, template<class> class SparseMatrixPolicy>
requires(VectorizableDense<MatrixPolicy<double>>) static void UpdateState(
const std::vector<Process>& processes,
State<MatrixPolicy>& state);
State<MatrixPolicy, SparseMatrixPolicy>& state);

friend class ProcessBuilder;
static ProcessBuilder create();
Expand Down Expand Up @@ -105,10 +105,10 @@ namespace micm
ProcessBuilder& phase(const Phase& phase);
};

template<template<class> class MatrixPolicy>
template<template<class> class MatrixPolicy, template<class> class SparseMatrixPolicy>
requires(!VectorizableDense<MatrixPolicy<double>>) void Process::UpdateState(
const std::vector<Process>& processes,
State<MatrixPolicy>& state)
State<MatrixPolicy, SparseMatrixPolicy>& state)
{
for (std::size_t i{}; i < state.custom_rate_parameters_.size(); ++i)
{
Expand All @@ -128,10 +128,10 @@ namespace micm
}
}

template<template<class> class MatrixPolicy>
template<template<class> class MatrixPolicy, template<class> class SparseMatrixPolicy>
requires(VectorizableDense<MatrixPolicy<double>>) void Process::UpdateState(
const std::vector<Process>& processes,
State<MatrixPolicy>& state)
State<MatrixPolicy, SparseMatrixPolicy>& state)
{
const auto& v_custom_parameters = state.custom_rate_parameters_.AsVector();
auto& v_rate_constants = state.rate_constants_.AsVector();
Expand Down
12 changes: 5 additions & 7 deletions include/micm/process/process_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ namespace micm

/// @brief Create a process set calculator for a given set of processes
/// @param processes Processes to create calculator for
/// @param state Solver state
template<template<class> class MatrixPolicy>
ProcessSet(const std::vector<Process>& processes, const State<MatrixPolicy>& state);
/// @param StateParameters Solver state
ProcessSet(const std::vector<Process>& processes, const std::map<std::string, std::size_t>& variable_map);

/// @brief Return the full set of non-zero Jacobian elements for the set of processes
/// @return Jacobian elements as a set of index pairs
Expand Down Expand Up @@ -74,8 +73,7 @@ namespace micm
SparseMatrixPolicy<double>& jacobian) const;
};

template<template<class> class MatrixPolicy>
inline ProcessSet::ProcessSet(const std::vector<Process>& processes, const State<MatrixPolicy>& state)
inline ProcessSet::ProcessSet(const std::vector<Process>& processes, const std::map<std::string, std::size_t>& variable_map)
: number_of_reactants_(),
reactant_ids_(),
number_of_products_(),
Expand All @@ -90,14 +88,14 @@ namespace micm
{
if (reactant.IsParameterized())
continue; // Skip reactants that are parameterizations
reactant_ids_.push_back(state.variable_map_.at(reactant.name_));
reactant_ids_.push_back(variable_map.at(reactant.name_));
++number_of_reactants;
}
for (auto& product : process.products_)
{
if (product.first.IsParameterized())
continue; // Skip products that are parameterizations
product_ids_.push_back(state.variable_map_.at(product.first.name_));
product_ids_.push_back(variable_map.at(product.first.name_));
yields_.push_back(product.second);
++number_of_products;
}
Expand Down
9 changes: 9 additions & 0 deletions include/micm/process/rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ namespace micm
return 0;
}

/// @brief Calculate the rate constant for a set of conditions
/// @param conditions The current environmental conditions of the chemical system
/// @param custom_parameters User defined rate constant parameters
/// @return The reaction rate constant
virtual double calculate(const Conditions& conditions) const
{
return 0;
}

/// @brief Calculate the rate constant for a set of conditions
/// @param conditions The current environmental conditions of the chemical system
/// @param custom_parameters User defined rate constant parameters
Expand Down
11 changes: 11 additions & 0 deletions include/micm/process/surface_rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ namespace micm
/// @param custom_parameters User-defined rate constant parameters
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions, std::vector<double>::const_iterator custom_parameters) const override;

/// @brief Calculate the rate constant
/// @param conditions The current environmental conditions of the chemical system
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions) const override;
};

inline SurfaceRateConstant::SurfaceRateConstant(const SurfaceRateConstantParameters& parameters)
Expand All @@ -70,6 +75,12 @@ namespace micm
return std::unique_ptr<RateConstant>{ new SurfaceRateConstant{ *this } };
}

inline double SurfaceRateConstant::calculate(
const Conditions& conditions) const
{
throw std::runtime_error("Surface rate constants must be supplied with a radius and number density using the alternative calculate function");
}

inline double SurfaceRateConstant::calculate(
const Conditions& conditions,
std::vector<double>::const_iterator custom_parameters) const
Expand Down
11 changes: 11 additions & 0 deletions include/micm/process/ternary_chemical_activation_rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ namespace micm
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions, std::vector<double>::const_iterator custom_parameters) const override;

/// @brief Calculate the rate constant
/// @param conditions The current environmental conditions of the chemical system
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions) const override;

/// @brief Calculate the rate constant
/// @param temperature Temperature in [K]
/// @param air_number_density Number density in [mol m-3]
Expand All @@ -75,6 +80,12 @@ namespace micm
return std::unique_ptr<RateConstant>{ new TernaryChemicalActivationRateConstant{ *this } };
}

inline double TernaryChemicalActivationRateConstant::calculate(const Conditions& conditions) const
{
double val = calculate(conditions.temperature_, conditions.air_density_);
return val;
}

inline double TernaryChemicalActivationRateConstant::calculate(
const Conditions& conditions,
std::vector<double>::const_iterator custom_parameters) const
Expand Down
10 changes: 10 additions & 0 deletions include/micm/process/troe_rate_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ namespace micm
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions, std::vector<double>::const_iterator custom_parameters) const override;

/// @brief Calculate the rate constant
/// @param conditions The current environmental conditions of the chemical system
/// @return A rate constant based off of the conditions in the system
double calculate(const Conditions& conditions) const override;

/// @brief Calculate the rate constant
/// @param temperature Temperature in [K]
/// @param air_number_density Number density in [mol m-3]
Expand All @@ -74,6 +79,11 @@ namespace micm
return std::unique_ptr<RateConstant>{ new TroeRateConstant{ *this } };
}

inline double TroeRateConstant::calculate(const Conditions& conditions) const
{
return calculate(conditions.temperature_, conditions.air_density_);
}

inline double TroeRateConstant::calculate(
const Conditions& conditions,
std::vector<double>::const_iterator custom_parameters) const
Expand Down
Loading