From 60eb62615c6905a0bdf4457771da6dc8ad8c71df Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Fri, 17 Feb 2023 11:46:01 -0600 Subject: [PATCH 1/7] Explicitly test exception handling --- include/nemlerror.h | 5 +++++ src/nemlerror.cxx | 14 ++++++++++++++ src/nemlerror_wrap.cxx | 7 +++++++ test/test_nemlerror.py | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 test/test_nemlerror.py diff --git a/include/nemlerror.h b/include/nemlerror.h index 8a754dd1..8a24c306 100644 --- a/include/nemlerror.h +++ b/include/nemlerror.h @@ -30,6 +30,11 @@ class NEML_EXPORT NonlinearSolverError: public NEMLError { NonlinearSolverError(std::string msg); }; +enum class ExceptionType { NEMLError, LinalgError, NonlinearSolverError }; + +/// Simple test function that throws an exception +void throw_exception(ExceptionType type); + } // namespace neml #endif // NEMLERROR diff --git a/src/nemlerror.cxx b/src/nemlerror.cxx index 3118fbf3..d711e7fc 100644 --- a/src/nemlerror.cxx +++ b/src/nemlerror.cxx @@ -27,5 +27,19 @@ NonlinearSolverError::NonlinearSolverError(std::string msg) : } +void throw_exception(ExceptionType type) +{ + switch (type) { + case ExceptionType::NEMLError: + throw NEMLError("This is an error"); + break; + case ExceptionType::LinalgError: + throw LinalgError("This is another error"); + break; + case ExceptionType::NonlinearSolverError: + throw NonlinearSolverError("This is still another error"); + break; + } +} } // namespace neml diff --git a/src/nemlerror_wrap.cxx b/src/nemlerror_wrap.cxx index d717ae02..5c1cbd1d 100644 --- a/src/nemlerror_wrap.cxx +++ b/src/nemlerror_wrap.cxx @@ -14,6 +14,13 @@ PYBIND11_MODULE(nemlerror, m) { py::register_exception(m, "NEMLError"); py::register_exception(m, "LinalgError"); py::register_exception(m, "NonlinearSolverError"); + + py::enum_(m, "ExceptionType") + .value("NEMLError", ExceptionType::NEMLError) + .value("LinalgError", ExceptionType::LinalgError) + .value("NonlinearSolverError", ExceptionType::NonlinearSolverError); + + m.def("throw_exception", &throw_exception); } } // namespace neml diff --git a/test/test_nemlerror.py b/test/test_nemlerror.py new file mode 100644 index 00000000..b7dd84e7 --- /dev/null +++ b/test/test_nemlerror.py @@ -0,0 +1,20 @@ +import sys +sys.path.append('..') + +from common import * + +from neml import nemlerror +import unittest + +class TestHandleException(unittest.TestCase): + def test_NEMLError(self): + self.assertRaises(nemlerror.NEMLError, + lambda: nemlerror.throw_exception(nemlerror.ExceptionType.NEMLError)) + + def test_LinalgError(self): + self.assertRaises(nemlerror.LinalgError, + lambda: nemlerror.throw_exception(nemlerror.ExceptionType.LinalgError)) + + def test_NonlinearSolverError(self): + self.assertRaises(nemlerror.NonlinearSolverError, + lambda: nemlerror.throw_exception(nemlerror.ExceptionType.NonlinearSolverError)) From c8c17ac484ba1611a07c5cebd9250035cfa9e61c Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Mon, 20 Feb 2023 08:31:12 -0600 Subject: [PATCH 2/7] Parallel exception handling in polycrystal classes --- src/cp/batch.cxx | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/cp/batch.cxx b/src/cp/batch.cxx index 09923470..57cc1075 100644 --- a/src/cp/batch.cxx +++ b/src/cp/batch.cxx @@ -1,5 +1,7 @@ #include "cp/batch.h" +#include + #ifdef USE_OMP #include #endif @@ -24,16 +26,26 @@ void evaluate_crystal_batch(SingleCrystalModel & model, size_t n, omp_set_num_threads(nthreads); #endif + size_t error_count = 0; #ifdef USE_OMP -#pragma omp parallel for +#pragma omp parallel for reduction(+: error_count) #endif for (size_t i=0; i 0) + throw NEMLError("Errors occurred in a parallel OpenMP region"); } void init_history_batch(SingleCrystalModel & model, size_t n, double * const hist) From 48b2792870c1394e33f274e18fbe6dccbb23f016 Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Thu, 17 Aug 2023 14:19:01 -0500 Subject: [PATCH 3/7] Disable error message for Janzen --- src/cp/batch.cxx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cp/batch.cxx b/src/cp/batch.cxx index 57cc1075..9ebfefe3 100644 --- a/src/cp/batch.cxx +++ b/src/cp/batch.cxx @@ -39,8 +39,6 @@ void evaluate_crystal_batch(SingleCrystalModel & model, size_t n, u_np1[i], u_n[i], p_np1[i], p_n[i]); } catch (NEMLError & e) { - std::cerr << "Exception in OpenMP block:" << std::endl; - std::cerr << e.message() << std::endl; ++error_count; } } From ef41b0be2cae30b14111a7d1f97cf82cff8f7567 Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Thu, 17 Aug 2023 14:42:55 -0500 Subject: [PATCH 4/7] Issues with the type of exception pybind is connect C++ exceptions to --- pybind11 | 2 +- test/test_nemlmath.py | 4 ++-- test/test_parse.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pybind11 b/pybind11 index 80dc998e..1a917f18 160000 --- a/pybind11 +++ b/pybind11 @@ -1 +1 @@ -Subproject commit 80dc998efced8ceb2be59756668a7e90e8bef917 +Subproject commit 1a917f1852eb7819b671fc3fa862840f4c491a07 diff --git a/test/test_nemlmath.py b/test/test_nemlmath.py index 093ca060..f2b3d38c 100644 --- a/test/test_nemlmath.py +++ b/test/test_nemlmath.py @@ -282,10 +282,10 @@ def test_invert(self): self.assertTrue(np.allclose(inv_ns, inv)) def test_nonsquare(self): - self.assertRaises(RuntimeError, invert_mat, self.nonsquare) + self.assertRaises(Exception, invert_mat, self.nonsquare) def test_nonmatrix(self): - self.assertRaises(RuntimeError, invert_mat, self.big) + self.assertRaises(Exception, invert_mat, self.big) class TestSolve(unittest.TestCase): def setUp(self): diff --git a/test/test_parse.py b/test/test_parse.py index 0c786e7f..a4dca475 100644 --- a/test/test_parse.py +++ b/test/test_parse.py @@ -7,7 +7,7 @@ class TestErrors(unittest.TestCase): def test_badobject(self): - with self.assertRaises(RuntimeError): + with self.assertRaises(Exception): test = parse.parse_xml(localize("examples.xml"), "test_badobject") def test_nomodel(self): From 27ee07ca22ac6967236a3ceb5e639d5b30e08f17 Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Thu, 17 Aug 2023 15:33:14 -0500 Subject: [PATCH 5/7] Hopefully fix the windows build issue --- .github/workflows/package.yml | 2 +- include/nemlerror.h | 3 --- src/nemlerror.cxx | 15 --------------- src/nemlerror_wrap.cxx | 2 -- test/test_nemlerror.py | 20 -------------------- 5 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 test/test_nemlerror.py diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index ea5c6779..5404f0e6 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -5,7 +5,7 @@ on: jobs: build_wheels_linux_mac: env: - CIBW_SKIP: cp311-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* + CIBW_SKIP: cp311-* cp313-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* CIBW_BEFORE_ALL_LINUX: yum -y install cmake cmake3 blas-devel lapack-devel && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 --slave /usr/local/bin/ctest ctest /usr/bin/ctest --slave /usr/local/bin/cpack cpack /usr/bin/cpack --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake --family cmake && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 --slave /usr/local/bin/ctest ctest /usr/bin/ctest3 --slave /usr/local/bin/cpack cpack /usr/bin/cpack3 --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 --family cmake CIBW_BEFORE_ALL_MACOS: brew install cmake openblas superlu gfortran CIBW_BEFORE_BUILD: "cmake -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=OFF . && cmake --build . && rm CMakeCache.txt && rm -rf CMakeFiles && pip install -r requirements.txt" diff --git a/include/nemlerror.h b/include/nemlerror.h index 8a24c306..fc9960e3 100644 --- a/include/nemlerror.h +++ b/include/nemlerror.h @@ -32,9 +32,6 @@ class NEML_EXPORT NonlinearSolverError: public NEMLError { enum class ExceptionType { NEMLError, LinalgError, NonlinearSolverError }; -/// Simple test function that throws an exception -void throw_exception(ExceptionType type); - } // namespace neml #endif // NEMLERROR diff --git a/src/nemlerror.cxx b/src/nemlerror.cxx index d711e7fc..368abcc8 100644 --- a/src/nemlerror.cxx +++ b/src/nemlerror.cxx @@ -27,19 +27,4 @@ NonlinearSolverError::NonlinearSolverError(std::string msg) : } -void throw_exception(ExceptionType type) -{ - switch (type) { - case ExceptionType::NEMLError: - throw NEMLError("This is an error"); - break; - case ExceptionType::LinalgError: - throw LinalgError("This is another error"); - break; - case ExceptionType::NonlinearSolverError: - throw NonlinearSolverError("This is still another error"); - break; - } -} - } // namespace neml diff --git a/src/nemlerror_wrap.cxx b/src/nemlerror_wrap.cxx index 5c1cbd1d..930e06d3 100644 --- a/src/nemlerror_wrap.cxx +++ b/src/nemlerror_wrap.cxx @@ -19,8 +19,6 @@ PYBIND11_MODULE(nemlerror, m) { .value("NEMLError", ExceptionType::NEMLError) .value("LinalgError", ExceptionType::LinalgError) .value("NonlinearSolverError", ExceptionType::NonlinearSolverError); - - m.def("throw_exception", &throw_exception); } } // namespace neml diff --git a/test/test_nemlerror.py b/test/test_nemlerror.py deleted file mode 100644 index b7dd84e7..00000000 --- a/test/test_nemlerror.py +++ /dev/null @@ -1,20 +0,0 @@ -import sys -sys.path.append('..') - -from common import * - -from neml import nemlerror -import unittest - -class TestHandleException(unittest.TestCase): - def test_NEMLError(self): - self.assertRaises(nemlerror.NEMLError, - lambda: nemlerror.throw_exception(nemlerror.ExceptionType.NEMLError)) - - def test_LinalgError(self): - self.assertRaises(nemlerror.LinalgError, - lambda: nemlerror.throw_exception(nemlerror.ExceptionType.LinalgError)) - - def test_NonlinearSolverError(self): - self.assertRaises(nemlerror.NonlinearSolverError, - lambda: nemlerror.throw_exception(nemlerror.ExceptionType.NonlinearSolverError)) From eba951120479b4bb59105488e89eff3f5c7dd00b Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Thu, 17 Aug 2023 15:59:34 -0500 Subject: [PATCH 6/7] I hate windows --- neml/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/neml/__init__.py b/neml/__init__.py index 32fe0805..bde048ee 100644 --- a/neml/__init__.py +++ b/neml/__init__.py @@ -3,4 +3,7 @@ ndir = os.path.dirname(__file__) os.environ['PATH'] += os.pathsep + ndir +if os.name == 'nt': + os.add_dll_directory(ndir) + import neml.history From 2e96c9cf91beaa5cfe30bbf79894af40ece064c3 Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Thu, 17 Aug 2023 17:29:55 -0500 Subject: [PATCH 7/7] Please fix the python build --- .github/workflows/package.yml | 2 +- .github/workflows/test_packages.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 5404f0e6..eea028de 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -5,7 +5,7 @@ on: jobs: build_wheels_linux_mac: env: - CIBW_SKIP: cp311-* cp313-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* + CIBW_SKIP: cp311-* cp312-* cp313-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* CIBW_BEFORE_ALL_LINUX: yum -y install cmake cmake3 blas-devel lapack-devel && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 --slave /usr/local/bin/ctest ctest /usr/bin/ctest --slave /usr/local/bin/cpack cpack /usr/bin/cpack --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake --family cmake && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 --slave /usr/local/bin/ctest ctest /usr/bin/ctest3 --slave /usr/local/bin/cpack cpack /usr/bin/cpack3 --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 --family cmake CIBW_BEFORE_ALL_MACOS: brew install cmake openblas superlu gfortran CIBW_BEFORE_BUILD: "cmake -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=OFF . && cmake --build . && rm CMakeCache.txt && rm -rf CMakeFiles && pip install -r requirements.txt" diff --git a/.github/workflows/test_packages.yml b/.github/workflows/test_packages.yml index e23a9e19..71b7ceab 100644 --- a/.github/workflows/test_packages.yml +++ b/.github/workflows/test_packages.yml @@ -3,7 +3,7 @@ on: pull_request jobs: test_wheels_linux_mac: env: - CIBW_SKIP: cp311-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* + CIBW_SKIP: cp311-* cp312-* cp313-* *-manylinux_i686 pp* *_ppc641e *_s390x *_aarch64 *musllinux* CIBW_BEFORE_ALL_LINUX: yum -y install cmake cmake3 blas-devel lapack-devel && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 --slave /usr/local/bin/ctest ctest /usr/bin/ctest --slave /usr/local/bin/cpack cpack /usr/bin/cpack --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake --family cmake && alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 --slave /usr/local/bin/ctest ctest /usr/bin/ctest3 --slave /usr/local/bin/cpack cpack /usr/bin/cpack3 --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 --family cmake CIBW_BEFORE_ALL_MACOS: brew install cmake openblas superlu gfortran CIBW_BEFORE_BUILD: "cmake -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=OFF . && cmake --build . && rm CMakeCache.txt && rm -rf CMakeFiles && pip install -r requirements.txt"