From 12598866eeb7325606972d4d553cf126f103d5a5 Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Thu, 15 Aug 2024 20:28:42 -0700 Subject: [PATCH 1/7] fixing Array2D wrapping --- mfem/_par/array.i | 59 +++++++++++++++++++++--- mfem/_ser/array.i | 60 ++++++++++++++++++++++--- mfem/common/array_instantiation_macro.i | 2 + test/test_blockmatrix.py | 4 ++ 4 files changed, 115 insertions(+), 10 deletions(-) diff --git a/mfem/_par/array.i b/mfem/_par/array.i index 95382e40..aa466eec 100644 --- a/mfem/_par/array.i +++ b/mfem/_par/array.i @@ -167,13 +167,62 @@ INSTANTIATE_ARRAY_BOOL IGNORE_ARRAY_METHODS_PREMITIVE(unsigned int) INSTANTIATE_ARRAY_NUMPYARRAY(uint, unsigned int, NPY_UINT) // 32bit +/* Array< Array *> */ +IGNORE_ARRAY_METHODS(mfem::Array *) +INSTANTIATE_ARRAY2(Array *, Array, intArray, 1) + /* - for these Array2D, we instantiate it. But we dont extend it since, Array2D does not - expose the interanl pointer to array1d. + Array2D:: Assign and data access */ + +%extend mfem::Array2D{ + void Assign(const T &a){ + *self = a; + } + void Assign(const mfem::Array2D &a){ + *self = a; + } + + void __setitem__(PyObject* param, const T v) { + if (PyTuple_Check(param)) { + PyErr_Clear(); + int i = PyInt_AsLong(PyTuple_GetItem(param, 0)); + int j = PyInt_AsLong(PyTuple_GetItem(param, 1)); + + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ValueError, "Argument must be i, j"); + return; + } + T *arr = self -> GetRow(i); + arr[j] = v; + } + } + T __getitem__(PyObject* param) { + if (PyTuple_Check(param)) { + PyErr_Clear(); + int i = PyInt_AsLong(PyTuple_GetItem(param, 0)); + int j = PyInt_AsLong(PyTuple_GetItem(param, 1)); + + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ValueError, "Argument must be i, j"); + i = 0; + j = 0; + } + T *arr = self -> GetRow(i); + return arr[j]; + } + } +} %template(intArray2D) mfem::Array2D; %template(doubleArray2D) mfem::Array2D; -/* Array< Array *> */ -IGNORE_ARRAY_METHODS(mfem::Array *) -INSTANTIATE_ARRAY2(Array *, Array, intArray, 1) +/* Array2D<* DenseMatrix>, Array2D<* SparseMatrix>, Array2D<* HypreParMatrix> */ + +IGNORE_ARRAY_METHODS(mfem::DenseMatrix *) +IGNORE_ARRAY_METHODS(mfem::SparseMatrix *) +IGNORE_ARRAY_METHODS(mfem::HypreParMatrix *) + +%template(densematArray2D) mfem::Array2D; +%template(sparsematArray2D) mfem::Array2D; +%template(hypreparmatArray2D) mfem::Array2D; + diff --git a/mfem/_ser/array.i b/mfem/_ser/array.i index 5d9f8e2a..c5699820 100644 --- a/mfem/_ser/array.i +++ b/mfem/_ser/array.i @@ -179,14 +179,64 @@ INSTANTIATE_ARRAY_BOOL IGNORE_ARRAY_METHODS_PREMITIVE(unsigned int) INSTANTIATE_ARRAY_NUMPYARRAY(uint, unsigned int, NPY_UINT) // 32bit +/* Array< Array *> */ +IGNORE_ARRAY_METHODS(mfem::Array *) +INSTANTIATE_ARRAY2(Array *, Array, intArray, 1) + /* - for these Array2D, we instantiate it. But we dont extend it since, Array2D does not - expose the interanl pointer to array1d. + Array2D:: Assign and data access */ + +%extend mfem::Array2D{ + void Assign(const T &a){ + *self = a; + } + void Assign(const mfem::Array2D &a){ + *self = a; + } + + void __setitem__(PyObject* param, const T v) { + if (PyTuple_Check(param)) { + PyErr_Clear(); + int i = PyInt_AsLong(PyTuple_GetItem(param, 0)); + int j = PyInt_AsLong(PyTuple_GetItem(param, 1)); + + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ValueError, "Argument must be i, j"); + return; + } + T *arr = self -> GetRow(i); + arr[j] = v; + } + } + T __getitem__(PyObject* param) { + if (PyTuple_Check(param)) { + PyErr_Clear(); + int i = PyInt_AsLong(PyTuple_GetItem(param, 0)); + int j = PyInt_AsLong(PyTuple_GetItem(param, 1)); + + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ValueError, "Argument must be i, j"); + i = 0; + j = 0; + } + T *arr = self -> GetRow(i); + return arr[j]; + } + } +} + %template(intArray2D) mfem::Array2D; %template(doubleArray2D) mfem::Array2D; -/* Array< Array *> */ -IGNORE_ARRAY_METHODS(mfem::Array *) -INSTANTIATE_ARRAY2(Array *, Array, intArray, 1) +/* Array2D<* DenseMatrix>, Array2D<* SparseMatrix>, Array2D<* HypreParMatrix> */ + +IGNORE_ARRAY_METHODS(mfem::DenseMatrix *) +IGNORE_ARRAY_METHODS(mfem::SparseMatrix *) + +%template(densematArray2D) mfem::Array2D; +%template(sparsematArray2D) mfem::Array2D; +//%template(hypreparmatArray2D) mfem::Array2D; + + diff --git a/mfem/common/array_instantiation_macro.i b/mfem/common/array_instantiation_macro.i index aac07719..c684e8dd 100644 --- a/mfem/common/array_instantiation_macro.i +++ b/mfem/common/array_instantiation_macro.i @@ -325,6 +325,8 @@ INSTANTIATE_ARRAY2(XXX, YYY, YYY, USEPTR) %ignore mfem::Array2D::Print; %ignore mfem::Array2D::PrintGZ; %ignore mfem::Array2D::SaveGZ; +%ignore mfem::Array2D::Load; +%ignore mfem::Array2D::Save; %enddef diff --git a/test/test_blockmatrix.py b/test/test_blockmatrix.py index c78066ba..fe29711c 100644 --- a/test/test_blockmatrix.py +++ b/test/test_blockmatrix.py @@ -35,3 +35,7 @@ m.SetBlock(1, 0, mmat) print(m._offsets) print(m._linked_mat) + +from mfem.common.sparse_utils import sparsemat_to_scipycsr +print(m.CreateMonolithic()) +print(sparsemat_to_scipycsr(m.CreateMonolithic(), np.float64).todense()) From da23550ebf2652300644c2d8bfeb61448fd7f85c Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Wed, 21 Aug 2024 17:53:30 -0400 Subject: [PATCH 2/7] working on memory handling. When Array is an array of MFEM object pointer, __setitem__should keep MFEM object from GCed.... --- mfem/common/array_setitem_typemap.i | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 mfem/common/array_setitem_typemap.i diff --git a/mfem/common/array_setitem_typemap.i b/mfem/common/array_setitem_typemap.i new file mode 100644 index 00000000..5ae2bb98 --- /dev/null +++ b/mfem/common/array_setitem_typemap.i @@ -0,0 +1,36 @@ +// +// this typemap is used together with %extend, which +// adds a class member taking int *pymfem_size +// +%define ARRAY_SETITEM(T) + %typemap(in) (int i, const T v) { + // generated by ARRAY_LISTTUPLE_INPUT(XXX, YYY) + if (!PyList_Check($input)) { + if (!PyTuple_Check($input)) { + PyErr_SetString(PyExc_ValueError, "Expecting a list/tuple"); + return NULL; + } else { + is_tuple = true; + } + } + size = (is_tuple) ? PyTuple_Size($input) : PyList_Size($input); + $1 = (void *) & size; +} + +%typemap(argout) (void *List_or_Tuple, XXX *_unused ) { + for (int i = 0; i < size$argnum; i++) { + PyObject *s = (is_tuple$argnum) ? PyTuple_GetItem($input, i) : PyList_GetItem($input,i); + (* result)[i] = (XXX)YYY(s); + } +} + +%typemap(typecheck) (void *List_or_Tuple, XXX *_unused ) { + $1 = 0; + if (PyList_Check($input)){ + $1 = 1; + } + if (PyTuple_Check($input)){ + $1 = 1; + } +} +%enddef From 0f3863a2d9ea5f2348b79850bd5965889495f74d Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Wed, 2 Oct 2024 10:08:18 -0400 Subject: [PATCH 3/7] added typemap... need to remove temporary thisown in array.i (if possible) --- mfem/_par/coefficient.i | 11 +++++++++++ mfem/_ser/array.i | 2 ++ mfem/_ser/coefficient.i | 11 +++++++++++ test/test_coefficient.py | 41 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 test/test_coefficient.py diff --git a/mfem/_par/coefficient.i b/mfem/_par/coefficient.i index a93d8a08..2b2d647d 100644 --- a/mfem/_par/coefficient.i +++ b/mfem/_par/coefficient.i @@ -110,6 +110,17 @@ namespace mfem { %include "../common/typemap_macros.i" LIST_TO_MFEMOBJ_POINTERARRAY_IN(mfem::IntegrationRule const *irs[], mfem::IntegrationRule *, 0) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::Coefficient *) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::VectorCoefficient *) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::MatrixCoefficient *) + +%import "../common/array_instantiation_macro.i" +IGNORE_ARRAY_METHODS(mfem::Coefficient *) +INSTANTIATE_ARRAY0(Coefficient *, Coefficient, 1) +IGNORE_ARRAY_METHODS(mfem::VectorCoefficient *) +INSTANTIATE_ARRAY0(VectorCoefficient *, VectorCoefficient, 1) +IGNORE_ARRAY_METHODS(mfem::MatrixCoefficient *) +INSTANTIATE_ARRAY0(MatrixCoefficient *, MatrixCoefficient, 1) %include "fem/coefficient.hpp" %include "../common/numba_coefficient.i" diff --git a/mfem/_ser/array.i b/mfem/_ser/array.i index 5d9f8e2a..eaa1cba0 100644 --- a/mfem/_ser/array.i +++ b/mfem/_ser/array.i @@ -94,6 +94,8 @@ XXXPTR_SIZE_IN(bool *data_, int asize, bool) namespace mfem{ %pythonprepend Array::__setitem__ %{ i = int(i) + if hasattr(v, "thisown"): + v.thisown = False %} %pythonprepend Array::Append %{ if isinstance(args[0], list): diff --git a/mfem/_ser/coefficient.i b/mfem/_ser/coefficient.i index 7c37066d..127310ce 100644 --- a/mfem/_ser/coefficient.i +++ b/mfem/_ser/coefficient.i @@ -110,6 +110,17 @@ namespace mfem { */ %include "../common/typemap_macros.i" LIST_TO_MFEMOBJ_POINTERARRAY_IN(mfem::IntegrationRule const *irs[], mfem::IntegrationRule *, 0) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::Coefficient *) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::VectorCoefficient *) +LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::MatrixCoefficient *) + +%import "../common/array_instantiation_macro.i" +IGNORE_ARRAY_METHODS(mfem::Coefficient *) +INSTANTIATE_ARRAY0(Coefficient *, Coefficient, 1) +IGNORE_ARRAY_METHODS(mfem::VectorCoefficient *) +INSTANTIATE_ARRAY0(VectorCoefficient *, VectorCoefficient, 1) +IGNORE_ARRAY_METHODS(mfem::MatrixCoefficient *) +INSTANTIATE_ARRAY0(MatrixCoefficient *, MatrixCoefficient, 1) %include "fem/coefficient.hpp" %include "../common/numba_coefficient.i" diff --git a/test/test_coefficient.py b/test/test_coefficient.py new file mode 100644 index 00000000..be68def5 --- /dev/null +++ b/test/test_coefficient.py @@ -0,0 +1,41 @@ +import sys +import numpy as np +import gc +from scipy.sparse import csr_matrix + +if len(sys.argv) > 1 and sys.argv[1] == '-p': + import mfem.par as mfem + use_parallel = True + + from mfem.common.mpi_debug import nicePrint as print + + from mpi4py import MPI + myid = MPI.COMM_WORLD.rank + +else: + import mfem.ser as mfem + use_parallel = False + myid = 0 + +def test(): + dim = 3 + max_attr = 5 + sigma_attr_coefs = mfem.MatrixCoefficientPtrArray(max_attr) + sigma_attr = mfem.intArray(max_attr) + tensors = [mfem.DenseMatrix(np.ones((3,3))*i) for i in range(max_attr)] + tensor_coefs = [mfem.MatrixConstantCoefficient(mat) for mat in tensors] + + for ti, tensor in enumerate(tensors): + # add matrix coefficient to list + print(ti) + xx = mfem.MatrixConstantCoefficient(tensor) + sigma_attr_coefs[ti] = xx + sigma_attr[ti] = ti+1 + + # Create PW Matrix Coefficient + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, sigma_attr_coefs, False) + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) + + +if __name__ == '__main__': + test() From 4056b070ea8dac19fa94709745461404f9bbd18f Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Thu, 3 Oct 2024 12:13:36 -0400 Subject: [PATCH 4/7] working on memory handling, passing on-the-fly list works --- mfem/_par/array.i | 2 ++ mfem/_par/coefficient.i | 6 ++++++ mfem/_ser/array.i | 21 +++++++++---------- mfem/_ser/coefficient.i | 6 ++++++ mfem/common/array_listtuple_typemap.i | 1 + test/test_coefficient.py | 29 +++++++++++++++++++++++++-- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/mfem/_par/array.i b/mfem/_par/array.i index 95382e40..6f2a3585 100644 --- a/mfem/_par/array.i +++ b/mfem/_par/array.i @@ -86,6 +86,8 @@ XXXPTR_SIZE_IN(bool *data_, int asize, bool) namespace mfem{ %pythonprepend Array::__setitem__ %{ i = int(i) + if hasattr(v, "thisown"): + v.thisown = False %} %pythonprepend Array::Append %{ if isinstance(args[0], list): diff --git a/mfem/_par/coefficient.i b/mfem/_par/coefficient.i index 2b2d647d..9edcdc2f 100644 --- a/mfem/_par/coefficient.i +++ b/mfem/_par/coefficient.i @@ -114,6 +114,12 @@ LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::C LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::VectorCoefficient *) LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::MatrixCoefficient *) +/* define CoefficientPtrArray, VectorCoefficientPtrArray, MatrixCoefficientPtrArray */ +%import "../common/array_listtuple_typemap.i" +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::Coefficient *, 1) +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::VectorCoefficient *, 1) +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::MatrixCoefficient *, 1) + %import "../common/array_instantiation_macro.i" IGNORE_ARRAY_METHODS(mfem::Coefficient *) INSTANTIATE_ARRAY0(Coefficient *, Coefficient, 1) diff --git a/mfem/_ser/array.i b/mfem/_ser/array.i index eaa1cba0..d58f6c8b 100644 --- a/mfem/_ser/array.i +++ b/mfem/_ser/array.i @@ -40,17 +40,6 @@ XXXPTR_SIZE_IN(int *data_, int asize, int) XXXPTR_SIZE_IN(double *data_, int asize, double) XXXPTR_SIZE_IN(bool *data_, int asize, bool) -%pythonappend mfem::Array::Array %{ - if len(args) == 1 and isinstance(args[0], list): - if (len(args[0]) == 2 and hasattr(args[0][0], 'disown') and - not hasattr(args[0][1], 'disown')): - ## first element is SwigObject, like - ## We do not own data in this case. - pass - else: - self.MakeDataOwner() -%} - %ignore mfem::Array::operator[]; %ignore mfem::Array2D::operator[]; %ignore mfem::BlockArray::operator[]; @@ -92,6 +81,16 @@ XXXPTR_SIZE_IN(bool *data_, int asize, bool) }; namespace mfem{ +%pythonappend Array::Array %{ + if len(args) == 1 and isinstance(args[0], list): + if (len(args[0]) == 2 and hasattr(args[0][0], 'disown') and + not hasattr(args[0][1], 'disown')): + ## first element is SwigObject, like + ## We do not own data in this case. + pass + else: + self.MakeDataOwner() +%} %pythonprepend Array::__setitem__ %{ i = int(i) if hasattr(v, "thisown"): diff --git a/mfem/_ser/coefficient.i b/mfem/_ser/coefficient.i index 127310ce..6809e266 100644 --- a/mfem/_ser/coefficient.i +++ b/mfem/_ser/coefficient.i @@ -114,6 +114,12 @@ LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::C LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::VectorCoefficient *) LIST_TO_MFEMOBJ_ARRAY_IN(const mfem::Array & coefs, mfem::MatrixCoefficient *) +/* define CoefficientPtrArray, VectorCoefficientPtrArray, MatrixCoefficientPtrArray */ +%import "../common/array_listtuple_typemap.i" +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::Coefficient *, 1) +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::VectorCoefficient *, 1) +ARRAY_LISTTUPLE_INPUT_SWIGOBJ(mfem::MatrixCoefficient *, 1) + %import "../common/array_instantiation_macro.i" IGNORE_ARRAY_METHODS(mfem::Coefficient *) INSTANTIATE_ARRAY0(Coefficient *, Coefficient, 1) diff --git a/mfem/common/array_listtuple_typemap.i b/mfem/common/array_listtuple_typemap.i index 9b263fca..4bbda049 100644 --- a/mfem/common/array_listtuple_typemap.i +++ b/mfem/common/array_listtuple_typemap.i @@ -71,6 +71,7 @@ if (!SWIG_IsOK(SWIG_ConvertPtr(s, (void **) &temp_ptr$argnum, ty, 0|0))) { PyErr_SetString(PyExc_ValueError, "List items must be XXX"); } else { + PyObject_SetAttrString(s, "thisown", Py_False); #if USEPTR==1 (* result)[i] = temp_ptr$argnum; #else diff --git a/test/test_coefficient.py b/test/test_coefficient.py index be68def5..5dae8105 100644 --- a/test/test_coefficient.py +++ b/test/test_coefficient.py @@ -22,6 +22,7 @@ def test(): max_attr = 5 sigma_attr_coefs = mfem.MatrixCoefficientPtrArray(max_attr) sigma_attr = mfem.intArray(max_attr) + tensors = [mfem.DenseMatrix(np.ones((3,3))*i) for i in range(max_attr)] tensor_coefs = [mfem.MatrixConstantCoefficient(mat) for mat in tensors] @@ -31,11 +32,35 @@ def test(): xx = mfem.MatrixConstantCoefficient(tensor) sigma_attr_coefs[ti] = xx sigma_attr[ti] = ti+1 - + + # Create PW Matrix Coefficient sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, sigma_attr_coefs, False) sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) + tensor_coefs = mfem.MatrixCoefficientPtrArray([mfem.MatrixConstantCoefficient(mat) for mat in tensors]) + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) + + data = tensor_coefs.GetData() + tensor_coefs2 = mfem.MatrixCoefficientPtrArray(data, 5, False) + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs2, False) + + print("exiting") + + + + if __name__ == '__main__': - test() + import tracemalloc + + tracemalloc.start() + + for i in range(10): + test() + print(tracemalloc.get_traced_memory()) + snapshot = tracemalloc.take_snapshot() + top_stats = snapshot.statistics('lineno') + for stat in top_stats[:10]: + print(stat) + From dc74992163e23316f5eae96d76a447e3a7080d40 Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Fri, 4 Oct 2024 15:57:40 -0400 Subject: [PATCH 5/7] added a macro to handle new kernel dispatch marcors --- mfem/_par/bilininteg.i | 1 + mfem/_par/quadinterpolator.i | 1 + mfem/_ser/bilininteg.i | 1 + mfem/_ser/lininteg.i | 4 +++- mfem/_ser/quadinterpolator.i | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mfem/_par/bilininteg.i b/mfem/_par/bilininteg.i index 2dbae46d..2766e5a0 100644 --- a/mfem/_par/bilininteg.i +++ b/mfem/_par/bilininteg.i @@ -40,6 +40,7 @@ import_array(); %ignore mfem::MassIntegrator::SetupPA; +%include "../common/kernel_dispatch.i" %include "fem/bilininteg.hpp" %feature("director") mfem::PyBilinearFormIntegrator; diff --git a/mfem/_par/quadinterpolator.i b/mfem/_par/quadinterpolator.i index 383d03f3..650bd395 100644 --- a/mfem/_par/quadinterpolator.i +++ b/mfem/_par/quadinterpolator.i @@ -25,6 +25,7 @@ import_array(); %import "../common/numpy_int_typemap.i" +%include "../common/kernel_dispatch.i" %include "fem/quadinterpolator.hpp" #endif diff --git a/mfem/_ser/bilininteg.i b/mfem/_ser/bilininteg.i index 1acb4445..8d549a2a 100644 --- a/mfem/_ser/bilininteg.i +++ b/mfem/_ser/bilininteg.i @@ -38,6 +38,7 @@ import_array(); %ignore mfem::MassIntegrator::SetupPA; +%include "../common/kernel_dispatch.i" %include "fem/bilininteg.hpp" %feature("director") mfem::PyBilinearFormIntegrator; diff --git a/mfem/_ser/lininteg.i b/mfem/_ser/lininteg.i index bba39695..428d0e6c 100644 --- a/mfem/_ser/lininteg.i +++ b/mfem/_ser/lininteg.i @@ -19,10 +19,12 @@ import_array(); %} + + %include "exception.i" %import "globals.i" -//%include "fem/coefficient.hpp" + %import "fe.i" %import "vector.i" %import "eltrans.i" diff --git a/mfem/_ser/quadinterpolator.i b/mfem/_ser/quadinterpolator.i index 9813ea3b..48f79612 100644 --- a/mfem/_ser/quadinterpolator.i +++ b/mfem/_ser/quadinterpolator.i @@ -25,6 +25,7 @@ import_array(); %import "../common/numpy_int_typemap.i" +%include "../common/kernel_dispatch.i" %include "fem/quadinterpolator.hpp" #endif From ed7d848ed6462e31d93559dfd36b78a620df81b2 Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Fri, 4 Oct 2024 16:36:04 -0400 Subject: [PATCH 6/7] adding kernel_dispatch.i to define empty macro --- mfem/common/kernel_dispatch.i | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 mfem/common/kernel_dispatch.i diff --git a/mfem/common/kernel_dispatch.i b/mfem/common/kernel_dispatch.i new file mode 100644 index 00000000..20475486 --- /dev/null +++ b/mfem/common/kernel_dispatch.i @@ -0,0 +1,6 @@ +/* + kernel_dispatch.i + + We set a macro simpily ingnore the MFEM_REGISTER_KERNELS macro + */ +#define MFEM_REGISTER_KERNELS(...) // From 5e81cd51d7754f25a01584e51bb653befa295621 Mon Sep 17 00:00:00 2001 From: Syun'ichi Shiraiwa Date: Sat, 5 Oct 2024 11:17:23 -0400 Subject: [PATCH 7/7] Fixed the name of wrapped Array2D object names --- mfem/_par/array.i | 6 ++--- mfem/_ser/array.i | 8 ++---- mfem/common/array_instantiation_macro.i | 3 ++- mfem/common/array_setitem_typemap.i | 36 ------------------------- test/test_coefficient.py | 29 ++++++++++---------- test/test_intrules.py | 6 ++--- test/test_vector.py | 2 +- 7 files changed, 26 insertions(+), 64 deletions(-) delete mode 100644 mfem/common/array_setitem_typemap.i diff --git a/mfem/_par/array.i b/mfem/_par/array.i index 19330454..633aea48 100644 --- a/mfem/_par/array.i +++ b/mfem/_par/array.i @@ -224,7 +224,7 @@ IGNORE_ARRAY_METHODS(mfem::DenseMatrix *) IGNORE_ARRAY_METHODS(mfem::SparseMatrix *) IGNORE_ARRAY_METHODS(mfem::HypreParMatrix *) -%template(densematArray2D) mfem::Array2D; -%template(sparsematArray2D) mfem::Array2D; -%template(hypreparmatArray2D) mfem::Array2D; +%template(DenseMatrixArray2D) mfem::Array2D; +%template(SparseMatrixArray2D) mfem::Array2D; +%template(HypreParMatrixArray2D) mfem::Array2D; diff --git a/mfem/_ser/array.i b/mfem/_ser/array.i index 51d42845..64bf6f4c 100644 --- a/mfem/_ser/array.i +++ b/mfem/_ser/array.i @@ -235,9 +235,5 @@ INSTANTIATE_ARRAY2(Array *, Array, intArray, 1) IGNORE_ARRAY_METHODS(mfem::DenseMatrix *) IGNORE_ARRAY_METHODS(mfem::SparseMatrix *) -%template(densematArray2D) mfem::Array2D; -%template(sparsematArray2D) mfem::Array2D; -//%template(hypreparmatArray2D) mfem::Array2D; - - - +%template(DenseMatrixArray2D) mfem::Array2D; +%template(SparseMatrixArray2D) mfem::Array2D; diff --git a/mfem/common/array_instantiation_macro.i b/mfem/common/array_instantiation_macro.i index c684e8dd..7d4d4627 100644 --- a/mfem/common/array_instantiation_macro.i +++ b/mfem/common/array_instantiation_macro.i @@ -1,6 +1,7 @@ %define INSTANTIATE_ARRAY2(XXX, YYY, ZZZ, USEPTR) #if USEPTR == 1 -%template(##ZZZ##Ptr##Array) mfem::Array; + //%template(##ZZZ##Ptr##Array) mfem::Array; +%template(##ZZZ##Array) mfem::Array; #else %template(##ZZZ##Array) mfem::Array; #endif diff --git a/mfem/common/array_setitem_typemap.i b/mfem/common/array_setitem_typemap.i deleted file mode 100644 index 5ae2bb98..00000000 --- a/mfem/common/array_setitem_typemap.i +++ /dev/null @@ -1,36 +0,0 @@ -// -// this typemap is used together with %extend, which -// adds a class member taking int *pymfem_size -// -%define ARRAY_SETITEM(T) - %typemap(in) (int i, const T v) { - // generated by ARRAY_LISTTUPLE_INPUT(XXX, YYY) - if (!PyList_Check($input)) { - if (!PyTuple_Check($input)) { - PyErr_SetString(PyExc_ValueError, "Expecting a list/tuple"); - return NULL; - } else { - is_tuple = true; - } - } - size = (is_tuple) ? PyTuple_Size($input) : PyList_Size($input); - $1 = (void *) & size; -} - -%typemap(argout) (void *List_or_Tuple, XXX *_unused ) { - for (int i = 0; i < size$argnum; i++) { - PyObject *s = (is_tuple$argnum) ? PyTuple_GetItem($input, i) : PyList_GetItem($input,i); - (* result)[i] = (XXX)YYY(s); - } -} - -%typemap(typecheck) (void *List_or_Tuple, XXX *_unused ) { - $1 = 0; - if (PyList_Check($input)){ - $1 = 1; - } - if (PyTuple_Check($input)){ - $1 = 1; - } -} -%enddef diff --git a/test/test_coefficient.py b/test/test_coefficient.py index 5dae8105..65204227 100644 --- a/test/test_coefficient.py +++ b/test/test_coefficient.py @@ -20,47 +20,48 @@ def test(): dim = 3 max_attr = 5 - sigma_attr_coefs = mfem.MatrixCoefficientPtrArray(max_attr) + sigma_attr_coefs = mfem.MatrixCoefficientArray(max_attr) sigma_attr = mfem.intArray(max_attr) + tensors = [mfem.DenseMatrix(np.ones((3,3))*i) for i in range(max_attr)] tensor_coefs = [mfem.MatrixConstantCoefficient(mat) for mat in tensors] - + sigma_array = mfem.MatrixCoefficientArray(tensor_coefs) + sigma_attr = mfem.intArray([1,2,3,4,5]) + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, sigma_array, False) + for ti, tensor in enumerate(tensors): # add matrix coefficient to list - print(ti) xx = mfem.MatrixConstantCoefficient(tensor) sigma_attr_coefs[ti] = xx sigma_attr[ti] = ti+1 - # Create PW Matrix Coefficient sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, sigma_attr_coefs, False) - sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) - tensor_coefs = mfem.MatrixCoefficientPtrArray([mfem.MatrixConstantCoefficient(mat) for mat in tensors]) + sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) + tensor_coefs = mfem.MatrixCoefficientArray([mfem.MatrixConstantCoefficient(mat) for mat in tensors]) sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs, False) data = tensor_coefs.GetData() - tensor_coefs2 = mfem.MatrixCoefficientPtrArray(data, 5, False) + tensor_coefs2 = mfem.MatrixCoefficientArray(data, 5, False) sigmaCoef = mfem.PWMatrixCoefficient(dim, sigma_attr, tensor_coefs2, False) print("exiting") - - - if __name__ == '__main__': import tracemalloc - + tracemalloc.start() - for i in range(10): + print(tracemalloc.get_traced_memory()) + + for i in range(3000): test() - print(tracemalloc.get_traced_memory()) + print(tracemalloc.get_traced_memory()) snapshot = tracemalloc.take_snapshot() - top_stats = snapshot.statistics('lineno') + top_stats = snapshot.statistics('lineno') for stat in top_stats[:10]: print(stat) diff --git a/test/test_intrules.py b/test/test_intrules.py index 542d027e..a1692fc2 100644 --- a/test/test_intrules.py +++ b/test/test_intrules.py @@ -52,13 +52,13 @@ def run_test(): irs = [mfem.IntRules.Get(i, 2) for i in range(mfem.Geometry.NumGeom)] - rulearray = mfem.IntegrationRulePtrArray(irs) + rulearray = mfem.IntegrationRuleArray(irs) ir2 = rulearray[2] points3 = [(ir2[i].x, ir2[i].y) for i in range(ir2.GetNPoints())] assert (points3 == points), "IntegrationPointArray coords does not agree (check 2)." - # check slice of IntegrationRulePtrArray + # check slice of IntegrationRuleArray rulearray2 = rulearray[:5] assert not rulearray2.OwnsData(), "subarray should not own data" ir2 = rulearray2[2] @@ -67,7 +67,7 @@ def run_test(): # create it from a pointer array data = rulearray2.GetData() # this returns - rulearray3 = mfem.IntegrationRulePtrArray((data, 5)) + rulearray3 = mfem.IntegrationRuleArray((data, 5)) ir2 = rulearray3[2] points3 = [(ir2[i].x, ir2[i].y) for i in range(ir2.GetNPoints())] assert (points3 == points), "IntegrationPointArray coords does not agree (check 3)." diff --git a/test/test_vector.py b/test/test_vector.py index 3fc69f98..99ab1c3f 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -29,7 +29,7 @@ def run_test(): a.Print_HYPRE("vector_hypre.dat") - x = mfem.VectorPtrArray([a]*3) + x = mfem.VectorArray([a]*3) x[2].Print() if __name__=='__main__':