From c6029f472405bea6956639e66cc84959063ac93b Mon Sep 17 00:00:00 2001 From: Rod Douglass Date: Tue, 2 Jan 2024 19:20:22 -0700 Subject: [PATCH] Modified setup.py and pyamgx/System.pyx. Changes needed to successfully install pyamgx on latest amgx install, Python3, and Ubuntu 22.04 --- examples/FiPy/diffusion.py | 3 +- examples/test.py | 69 ++++++++++++++++++++++++++++++++++++++ examples/testBICGSTAB.py | 69 ++++++++++++++++++++++++++++++++++++++ examples/testGMRES.py | 69 ++++++++++++++++++++++++++++++++++++++ pyamgx/System.pyx | 4 +-- setup.py | 15 +++++++-- 6 files changed, 224 insertions(+), 5 deletions(-) create mode 100644 examples/test.py create mode 100644 examples/testBICGSTAB.py create mode 100644 examples/testGMRES.py diff --git a/examples/FiPy/diffusion.py b/examples/FiPy/diffusion.py index 864f2c2..d4ef3e5 100644 --- a/examples/FiPy/diffusion.py +++ b/examples/FiPy/diffusion.py @@ -38,7 +38,8 @@ pyamgx.initialize() from pyamgx_solver import PyAMGXSolver -with open (os.environ['AMGX_DIR']+'/core/configs/AMG_CLASSICAL_PMIS.json') as f: +#with open (os.environ['AMGX_DIR']+'/core/configs/AMG_CLASSICAL_PMIS.json') as f: +with open (os.environ['AMGX_DIR']+'/src/configs/AMG_CLASSICAL_PMIS.json') as f: cfg = json.load(f) cfg['solver']['max_iters'] = 1000 diff --git a/examples/test.py b/examples/test.py new file mode 100644 index 0000000..f0b118f --- /dev/null +++ b/examples/test.py @@ -0,0 +1,69 @@ + +import numpy as np +import scipy.sparse as sparse +import scipy.sparse.linalg as splinalg + +import pyamgx + +pyamgx.initialize() + +# Initialize config and resources: +cfg = pyamgx.Config().create_from_dict({ + "config_version": 2, + "determinism_flag": 1, + "exception_handling" : 1, + "solver": { + "monitor_residual": 1, + "solver": "BICGSTAB", + "convergence": "RELATIVE_INI_CORE", + "preconditioner": { + "solver": "NOSOLVER" + } + } +}) + +rsc = pyamgx.Resources().create_simple(cfg) + +# Create matrices and vectors: +A = pyamgx.Matrix().create(rsc) +b = pyamgx.Vector().create(rsc) +x = pyamgx.Vector().create(rsc) + +# Create solver: +solver = pyamgx.Solver().create(rsc, cfg) + +# Upload system: + +N = 5 + +M = sparse.csr_matrix(np.random.rand(N, N)) +rhs = np.random.rand(N) +sol = np.zeros(N, dtype=np.float64) + +A.upload_CSR(M) +b.upload(rhs) +x.upload(sol) + +# Setup and solve system: +solver.setup(A) +solver.solve(b, x) + +# Download solution +x.download(sol) +print("pyamgx solution: ", sol) +print(" norm: ", np.linalg.norm(M*sol-rhs)) + +X = splinalg.spsolve(M, rhs) + +print("scipy solution: ", X) +print(" norm: ", np.linalg.norm(M*X-rhs)) + +# Clean up: +A.destroy() +x.destroy() +b.destroy() +solver.destroy() +rsc.destroy() +cfg.destroy() + +pyamgx.finalize() diff --git a/examples/testBICGSTAB.py b/examples/testBICGSTAB.py new file mode 100644 index 0000000..6ec2fff --- /dev/null +++ b/examples/testBICGSTAB.py @@ -0,0 +1,69 @@ + +import numpy as np +import scipy.sparse as sparse +import scipy.sparse.linalg as splinalg + +import pyamgx + +pyamgx.initialize() + +# Initialize config and resources: +cfg = pyamgx.Config().create_from_dict({ + "config_version": 2, + "determinism_flag": 1, + "exception_handling" : 1, + "solver": { + "monitor_residual": 1, + "solver": "BICGSTAB", + "convergence": "RELATIVE_INI_CORE", + "preconditioner": { + "solver": "SCHWARTZ" + } + } +}) + +rsc = pyamgx.Resources().create_simple(cfg) + +# Create matrices and vectors: +A = pyamgx.Matrix().create(rsc) +b = pyamgx.Vector().create(rsc) +x = pyamgx.Vector().create(rsc) + +# Create solver: +solver = pyamgx.Solver().create(rsc, cfg) + +# Upload system: + +N = 20 + +M = sparse.csr_matrix(np.random.rand(N, N)) +rhs = np.random.rand(N) +sol = np.zeros(N, dtype=np.float64) + +A.upload_CSR(M) +b.upload(rhs) +x.upload(sol) + +# Setup and solve system: +solver.setup(A) +solver.solve(b, x) + +# Download solution +x.download(sol) +print("pyamgx solution: ", sol) +print(" norm: ", np.linalg.norm(M*sol-rhs)) + +X = splinalg.spsolve(M, rhs) + +print("scipy solution: ", X) +print(" norm: ", np.linalg.norm(M*X-rhs)) + +# Clean up: +A.destroy() +x.destroy() +b.destroy() +solver.destroy() +rsc.destroy() +cfg.destroy() + +pyamgx.finalize() diff --git a/examples/testGMRES.py b/examples/testGMRES.py new file mode 100644 index 0000000..ce76b41 --- /dev/null +++ b/examples/testGMRES.py @@ -0,0 +1,69 @@ + +import numpy as np +import scipy.sparse as sparse +import scipy.sparse.linalg as splinalg + +import pyamgx + +pyamgx.initialize() + +# Initialize config and resources: +cfg = pyamgx.Config().create_from_dict({ + "config_version": 2, + "determinism_flag": 1, + "exception_handling" : 1, + "solver": { + "monitor_residual": 1, + "solver": "GMRES", + "convergence": "RELATIVE_INI_CORE", + "preconditioner": { + "solver": "NOSOLVER" + } + } +}) + +rsc = pyamgx.Resources().create_simple(cfg) + +# Create matrices and vectors: +A = pyamgx.Matrix().create(rsc) +b = pyamgx.Vector().create(rsc) +x = pyamgx.Vector().create(rsc) + +# Create solver: +solver = pyamgx.Solver().create(rsc, cfg) + +# Upload system: + +N = 200 + +M = sparse.csr_matrix(np.random.rand(N, N)) +rhs = np.random.rand(N) +sol = np.zeros(N, dtype=np.float64) + +A.upload_CSR(M) +b.upload(rhs) +x.upload(sol) + +# Setup and solve system: +solver.setup(A) +solver.solve(b, x) + +# Download solution +x.download(sol) +print("pyamgx solution: ", sol) +print(" norm: ", np.linalg.norm(M*sol-rhs)) + +X = splinalg.spsolve(M, rhs) + +print("scipy solution: ", X) +print(" norm: ", np.linalg.norm(M*X-rhs)) + +# Clean up: +A.destroy() +x.destroy() +b.destroy() +solver.destroy() +rsc.destroy() +cfg.destroy() + +pyamgx.finalize() diff --git a/pyamgx/System.pyx b/pyamgx/System.pyx index 495cc4f..9934b6c 100644 --- a/pyamgx/System.pyx +++ b/pyamgx/System.pyx @@ -46,10 +46,10 @@ def reset_signal_handler(): check_error(AMGX_reset_signal_handler()) -cdef void c_register_print_callback(AMGX_print_callback function): +cdef void c_register_print_callback(AMGX_print_callback function)noexcept: AMGX_register_print_callback(function) -cdef void c_print_callback(char *msg, int length): +cdef void c_print_callback(char *msg, int length)noexcept: global print_callback print_callback(msg.decode('utf-8')) diff --git a/setup.py b/setup.py index cc44e17..74e4e01 100644 --- a/setup.py +++ b/setup.py @@ -6,8 +6,12 @@ import sys import numpy -AMGX_DIR = os.environ.get('AMGX_DIR') -AMGX_BUILD_DIR = os.environ.get('AMGX_BUILD_DIR') +#AMGX_DIR = os.environ.get('AMGX_DIR') +#AMGX_BUILD_DIR = os.environ.get('AMGX_BUILD_DIR') +key = 'AMGX_DIR' +AMGX_DIR = os.getenv(key) +key2 = 'AMGX_BUILD_DIR' +AMGX_BUILD_DIR = os.getenv(key2) if sys.platform == "win32": @@ -73,8 +77,14 @@ library_dirs = [ numpy.get_include(), ] + AMGX_lib_dirs, +<<<<<<< HEAD runtime_library_dirs = runtime_lib_dirs )]) +======= + runtime_library_dirs = [ + numpy.get_include(), + ] + AMGX_lib_dirs,)], compiler_directives={"language_level": "3"}) +>>>>>>> c3d8943 (Modified setup.py and pyamgx/System.pyx.) setup(name='pyamgx', author='Ashwin Srinath', @@ -82,3 +92,4 @@ ext_modules = ext, data_files=data_files, zip_safe=False) +