From 59183097885269aa223c74aa8eef8a6e9d74ce33 Mon Sep 17 00:00:00 2001 From: Fabio Luporini Date: Tue, 19 Dec 2023 08:32:06 +0000 Subject: [PATCH 01/24] misc: Split Dockerfile.cpu into .cpu and .intel --- docker/Dockerfile.intel | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/docker/Dockerfile.intel b/docker/Dockerfile.intel index 48757d97763..c02c97d3d46 100644 --- a/docker/Dockerfile.intel +++ b/docker/Dockerfile.intel @@ -45,6 +45,7 @@ RUN apt-get update -y && \ apt-get install -y intel-oneapi-advisor # Drivers mandatory for intel gpu +<<<<<<< HEAD # https://dgpu-docs.intel.com/driver/installation.html#ubuntu-install-steps RUN wget -qO - https://repositories.intel.com/graphics/intel-graphics.key | gpg --dearmor > /usr/share/keyrings/intel-graphics.gpg RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/graphics/ubuntu jammy unified" > /etc/apt/sources.list.d/intel-gpu-jammy.list @@ -58,6 +59,16 @@ RUN apt-get update -y && apt-get dist-upgrade -y && \ mesa-vdpau-drivers mesa-vulkan-drivers va-driver-all vainfo hwinfo clinfo \ # Development packages libigc-dev intel-igc-cm libigdfcl-dev libigfxcmrt-dev level-zero-dev +======= +# https://dgpu-docs.intel.com/installation-guides/ubuntu/ubuntu-focal.html#ubuntu-20-04-focal +RUN wget -qO - https://repositories.intel.com/graphics/intel-graphics.key | gpg --dearmor > /usr/share/keyrings/intel-graphics.gpg +RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/graphics/ubuntu focal main" > /etc/apt/sources.list.d/intel.list + +RUN apt-get update -y && apt-get dist-upgrade -y && \ + apt-get install -y intel-opencl-icd intel-level-zero-gpu level-zero level-zero-dev \ + intel-media-va-driver-non-free libmfx1 libmfxgen1 libvpl2 \ + libigc-dev intel-igc-cm libigdfcl-dev libigfxcmrt-dev level-zero-dev +>>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) ############################################################## # ICC image @@ -78,7 +89,11 @@ ENV MPICC=mpiicc ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icc"' ############################################################## +<<<<<<< HEAD # ICX OpenMP image +======= +# ICX image +>>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) ############################################################## FROM oneapi as icx @@ -94,6 +109,35 @@ ENV MPICC=mpiicc ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icx"' ############################################################## +<<<<<<< HEAD +======= +# ICX hpc image +############################################################## +FROM oneapi as icx-hpc + +# Install both icc and icx to avoid missing dependencies +RUN apt-get update -y && \ + apt-get install -y intel-oneapi-compiler-dpcpp-cpp intel-oneapi-mpi-devel && \ + apt-get install -y intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic + +# Missig components +# https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit-download.html?operatingsystem=linux&distributions=aptpackagemanager +RUN curl -f "https://registrationcenter-download.intel.com/akdlm/IRC_NAS/ebf5d9aa-17a7-46a4-b5df-ace004227c0e/l_dpcpp-cpp-compiler_p_2023.2.1.8.sh" -O && \ + chmod +x l_dpcpp-cpp-compiler_p_2023.2.1.8.sh && ./l_dpcpp-cpp-compiler_p_2023.2.1.8.sh -a -s --eula accept && \ + rm l_dpcpp-cpp-compiler_p_2023.2.1.8.sh + +RUN apt-get clean && apt-get autoclean && apt-get autoremove -y && \ + rm -rf /var/lib/apt/lists/* + +# Devito config +ENV DEVITO_ARCH="icx" +ENV DEVITO_LANGUAGE="openmp" +# MPICC compiler for mpi4py +ENV MPICC=mpiicc +ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icx"' + +############################################################## +>>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) # ICX SYCL CPU image ############################################################## FROM icx as cpu-sycl @@ -108,6 +152,12 @@ ENV DEVITO_PLATFORM="intel64" ############################################################## FROM icx as gpu-sycl +<<<<<<< HEAD +======= +# NOTE: the name of this file ends with ".cpu" but this is a GPU image. +# It then feels a bit akward, so some restructuring might be needed + +>>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) # Devito config ENV DEVITO_ARCH="sycl" ENV DEVITO_LANGUAGE="sycl" From 237a2281a0a1760b6c8c1e3d859ca9732cac7cc1 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 27 Nov 2023 19:16:56 +0000 Subject: [PATCH 02/24] try petscfunc from abstractfunc --- devito/passes/iet/petsc.py | 136 ++++++++++++++++++++++++++++++++ devito/tools/dtypes_lowering.py | 20 ++++- 2 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 devito/passes/iet/petsc.py diff --git a/devito/passes/iet/petsc.py b/devito/passes/iet/petsc.py new file mode 100644 index 00000000000..a78159212dc --- /dev/null +++ b/devito/passes/iet/petsc.py @@ -0,0 +1,136 @@ +from devito.types.basic import AbstractSymbol, AbstractFunction +from devito.tools import petsc_type_to_ctype, dtype_to_ctype, dtype_to_cstr +import numpy as np +from sympy import Expr +from ctypes import POINTER + + + +class PETScObject(AbstractSymbol): + """ + PETScObjects. + """ + @property + def _C_ctype(self): + ctype = petsc_type_to_ctype(self._dtype) + return type(self._dtype, (ctype,), {}) + + + +# may need to also inherit from Expr +class PETScFunction(AbstractFunction): + """ + PETScFunctions. + """ + + @classmethod + def __dtype_setup__(cls, **kwargs): + grid = kwargs.get('grid') + dtype = kwargs.get('dtype') + if dtype is not None: + return dtype + elif grid is not None: + return grid.dtype + else: + return np.float32 + + @property + def dimensions(self): + """Tuple of Dimensions representing the object indices.""" + return self._dimensions + + @classmethod + def __indices_setup__(cls, **kwargs): + grid = kwargs.get('grid') + shape = kwargs.get('shape') + dimensions = kwargs.get('dimensions') + + if dimensions is None and shape is None and grid is None: + return (), () + + elif grid is None: + if dimensions is None: + raise TypeError("Need either `grid` or `dimensions`") + elif dimensions is None: + dimensions = grid.dimensions + + return dimensions, dimensions + + # @classmethod + # def __shape_setup__(cls, **kwargs): + # grid = kwargs.get('grid') + # dimensions = kwargs.get('dimensions') + # shape = kwargs.get('shape') + + # if dimensions is None and shape is None and grid is None: + # return None + + # elif grid is None: + # if shape is None: + # raise TypeError("Need either `grid` or `shape`") + # elif shape is None: + # if dimensions is not None and dimensions != grid.dimensions: + # raise TypeError("Need `shape` as not all `dimensions` are in `grid`") + # shape = grid.shape + # elif dimensions is None: + # raise TypeError("`dimensions` required if both `grid` and " + # "`shape` are provided") + + # return shape + + # @classmethod + # def __indices_setup__(cls, *args, **kwargs): + # grid = kwargs.get('grid') + # dimensions = kwargs.get('dimensions') + # if grid is None: + # if dimensions is None: + # raise TypeError("Need either `grid` or `dimensions`") + # elif dimensions is None: + # dimensions = grid.dimensions + + # return tuple(dimensions), tuple(dimensions) + + @property + def _C_ctype(self): + # from IPython import embed; embed() + ctypename = 'Petsc%s' % dtype_to_cstr(self._dtype).capitalize() + ctype = dtype_to_ctype(self.dtype) + r = POINTER(type(ctypename, (ctype,), {})) + for n in range(len(self.dimensions)-1): + r = POINTER(r) + return r + + # @property + # def _C_ctype(self): + # # from IPython import embed; embed() + # ctypename = 'Petsc%s' % dtype_to_cstr(self._dtype).capitalize() + # ctype = dtype_to_ctype(self.dtype) + # r = type(ctypename, (ctype,), {}) + # # for n in range(len(self.dimensions)-1): + # # r = POINTER(r) + # from IPython import embed; embed() + # return r + + @property + def _C_name(self): + return self.name + + + + +from devito.ir import Definition +da = PETScObject('da', dtype='DM') +tmp = Definition(da) +# print(tmp) + +from devito import * +grid = Grid((2, 2)) +x, y = grid.dimensions +# pointer and const functionality +ptr1 = PETScFunction(name='ptr1', dtype=np.int32, dimensions=grid.dimensions, shape=grid.shape, is_const=True) +defn1 = Definition(ptr1) +from IPython import embed; embed() +print(str(defn1)) + + + diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index 70a378dae49..0bc76fd1228 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -13,8 +13,7 @@ 'double3', 'double4', 'dtypes_vector_mapper', 'dtype_to_mpidtype', 'dtype_to_cstr', 'dtype_to_ctype', 'dtype_to_mpitype', 'dtype_len', 'ctypes_to_cstr', 'c_restrict_void_p', 'ctypes_vector_mapper', - 'is_external_ctype', 'infer_dtype', 'CustomDtype'] - + 'is_external_ctype', 'infer_dtype', 'CustomDtype', 'petsc_type_to_ctype'] # *** Custom np.dtypes @@ -311,3 +310,20 @@ def infer_dtype(dtypes): else: # E.g., mixed integer arithmetic return max(dtypes, key=lambda i: np.dtype(i).itemsize, default=None) + + +def petsc_type_to_ctype(dtype): + """ + Map PETSc types to ctypes type. + """ + return { + # 'PetscInt': ctypes.c_int, + # 'PetscScalar': ctypes.c_float, + 'Mat': ctypes.c_void_p, + 'Vec': ctypes.c_void_p, + 'PetscErrorCode': ctypes.c_int, + 'KSP': ctypes.c_void_p, + 'PC': ctypes.c_void_p, + 'DM': ctypes.c_void_p, + 'PetscMPIInt': ctypes.c_int + }[dtype] From d54286532a3c242203d6d6b63327bce9ccef5ead Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 30 Nov 2023 23:58:44 +0000 Subject: [PATCH 03/24] latest. --- devito/passes/iet/petsc.py | 100 +++++++++++++------------------- devito/tools/dtypes_lowering.py | 24 +++----- 2 files changed, 48 insertions(+), 76 deletions(-) diff --git a/devito/passes/iet/petsc.py b/devito/passes/iet/petsc.py index a78159212dc..1a589d9d501 100644 --- a/devito/passes/iet/petsc.py +++ b/devito/passes/iet/petsc.py @@ -1,21 +1,30 @@ from devito.types.basic import AbstractSymbol, AbstractFunction -from devito.tools import petsc_type_to_ctype, dtype_to_ctype, dtype_to_cstr +from devito.tools import petsc_type_to_ctype, dtype_to_ctype, dtype_to_cstr, dtype_to_petsctype import numpy as np from sympy import Expr -from ctypes import POINTER +from devito.types import LocalObject +from ctypes import POINTER, c_void_p +from devito.ir import Definition -class PETScObject(AbstractSymbol): - """ - PETScObjects. - """ - @property - def _C_ctype(self): - ctype = petsc_type_to_ctype(self._dtype) - return type(self._dtype, (ctype,), {}) +# class PETScObject(AbstractSymbol): +# @property +# def _C_ctype(self): +# ctype = petsc_type_to_ctype(self._dtype) +# return type(self._dtype, (ctype,), {}) +class PETScDM(LocalObject): + dtype = type('DM', (c_void_p,), {}) + + +da = PETScDM('da') +defn1 = Definition(da) +print(defn1) + + + # may need to also inherit from Expr class PETScFunction(AbstractFunction): @@ -36,25 +45,7 @@ def __dtype_setup__(cls, **kwargs): @property def dimensions(self): - """Tuple of Dimensions representing the object indices.""" return self._dimensions - - @classmethod - def __indices_setup__(cls, **kwargs): - grid = kwargs.get('grid') - shape = kwargs.get('shape') - dimensions = kwargs.get('dimensions') - - if dimensions is None and shape is None and grid is None: - return (), () - - elif grid is None: - if dimensions is None: - raise TypeError("Need either `grid` or `dimensions`") - elif dimensions is None: - dimensions = grid.dimensions - - return dimensions, dimensions # @classmethod # def __shape_setup__(cls, **kwargs): @@ -78,38 +69,26 @@ def __indices_setup__(cls, **kwargs): # return shape - # @classmethod - # def __indices_setup__(cls, *args, **kwargs): - # grid = kwargs.get('grid') - # dimensions = kwargs.get('dimensions') - # if grid is None: - # if dimensions is None: - # raise TypeError("Need either `grid` or `dimensions`") - # elif dimensions is None: - # dimensions = grid.dimensions + @classmethod + def __indices_setup__(cls, *args, **kwargs): + grid = kwargs.get('grid') + dimensions = kwargs.get('dimensions') + if grid is None: + if dimensions is None: + raise TypeError("Need either `grid` or `dimensions`") + elif dimensions is None: + dimensions = grid.dimensions - # return tuple(dimensions), tuple(dimensions) + return tuple(dimensions), tuple(dimensions) @property def _C_ctype(self): - # from IPython import embed; embed() - ctypename = 'Petsc%s' % dtype_to_cstr(self._dtype).capitalize() + petsc_type = dtype_to_petsctype(self.dtype) ctype = dtype_to_ctype(self.dtype) - r = POINTER(type(ctypename, (ctype,), {})) - for n in range(len(self.dimensions)-1): + r = type(petsc_type, (ctype,), {}) + for n in range(len(self.dimensions)): r = POINTER(r) return r - - # @property - # def _C_ctype(self): - # # from IPython import embed; embed() - # ctypename = 'Petsc%s' % dtype_to_cstr(self._dtype).capitalize() - # ctype = dtype_to_ctype(self.dtype) - # r = type(ctypename, (ctype,), {}) - # # for n in range(len(self.dimensions)-1): - # # r = POINTER(r) - # from IPython import embed; embed() - # return r @property def _C_name(self): @@ -118,19 +97,18 @@ def _C_name(self): -from devito.ir import Definition -da = PETScObject('da', dtype='DM') -tmp = Definition(da) +# from devito.ir import Definition +# da = PETScObject('da', dtype='DM') +# tmp = Definition(da) # print(tmp) from devito import * grid = Grid((2, 2)) x, y = grid.dimensions -# pointer and const functionality -ptr1 = PETScFunction(name='ptr1', dtype=np.int32, dimensions=grid.dimensions, shape=grid.shape, is_const=True) -defn1 = Definition(ptr1) -from IPython import embed; embed() -print(str(defn1)) +ptr1 = PETScFunction(name='ptr1', dtype=np.float32, dimensions=grid.dimensions, shape=grid.shape) +defn2 = Definition(ptr1) +# from IPython import embed; embed() +print(str(defn2)) diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index 0bc76fd1228..8233f543424 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -13,7 +13,7 @@ 'double3', 'double4', 'dtypes_vector_mapper', 'dtype_to_mpidtype', 'dtype_to_cstr', 'dtype_to_ctype', 'dtype_to_mpitype', 'dtype_len', 'ctypes_to_cstr', 'c_restrict_void_p', 'ctypes_vector_mapper', - 'is_external_ctype', 'infer_dtype', 'CustomDtype', 'petsc_type_to_ctype'] + 'is_external_ctype', 'infer_dtype', 'CustomDtype', 'dtype_to_petsctype'] # *** Custom np.dtypes @@ -310,20 +310,14 @@ def infer_dtype(dtypes): else: # E.g., mixed integer arithmetic return max(dtypes, key=lambda i: np.dtype(i).itemsize, default=None) - -def petsc_type_to_ctype(dtype): - """ - Map PETSc types to ctypes type. - """ + +def dtype_to_petsctype(dtype): + """Map numpy types to PETSc datatypes.""" + return { - # 'PetscInt': ctypes.c_int, - # 'PetscScalar': ctypes.c_float, - 'Mat': ctypes.c_void_p, - 'Vec': ctypes.c_void_p, - 'PetscErrorCode': ctypes.c_int, - 'KSP': ctypes.c_void_p, - 'PC': ctypes.c_void_p, - 'DM': ctypes.c_void_p, - 'PetscMPIInt': ctypes.c_int + np.int32: 'PetscInt', + np.float32: 'PetscScalar', + # np.int64: + # np.float64: }[dtype] From 7309ef50a1ecf731fa1d17b4cd5acfddf4ac2a53 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 4 Dec 2023 14:44:33 +0000 Subject: [PATCH 04/24] remove data allignment --- devito/passes/iet/petsc.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/devito/passes/iet/petsc.py b/devito/passes/iet/petsc.py index 1a589d9d501..b2913e3ce78 100644 --- a/devito/passes/iet/petsc.py +++ b/devito/passes/iet/petsc.py @@ -31,6 +31,7 @@ class PETScFunction(AbstractFunction): """ PETScFunctions. """ + _data_alignment = False @classmethod def __dtype_setup__(cls, **kwargs): @@ -106,9 +107,11 @@ def _C_name(self): grid = Grid((2, 2)) x, y = grid.dimensions ptr1 = PETScFunction(name='ptr1', dtype=np.float32, dimensions=grid.dimensions, shape=grid.shape) -defn2 = Definition(ptr1) + +from IPython import embed; embed() +# defn2 = Definition(ptr1) # from IPython import embed; embed() -print(str(defn2)) +# print(/defn2) From e597d4de0f1bc3b7b77aa34995719b87a443d7dd Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 4 Dec 2023 17:47:08 +0000 Subject: [PATCH 05/24] new petsc types --- devito/passes/iet/petsc.py | 117 -------------------------------- devito/tools/dtypes_lowering.py | 2 - devito/types/__init__.py | 1 + devito/types/petsc.py | 77 +++++++++++++++++++++ tests/test_petsc.py | 54 +++++++++++++++ 5 files changed, 132 insertions(+), 119 deletions(-) create mode 100644 devito/types/petsc.py create mode 100644 tests/test_petsc.py diff --git a/devito/passes/iet/petsc.py b/devito/passes/iet/petsc.py index b2913e3ce78..e69de29bb2d 100644 --- a/devito/passes/iet/petsc.py +++ b/devito/passes/iet/petsc.py @@ -1,117 +0,0 @@ -from devito.types.basic import AbstractSymbol, AbstractFunction -from devito.tools import petsc_type_to_ctype, dtype_to_ctype, dtype_to_cstr, dtype_to_petsctype -import numpy as np -from sympy import Expr -from devito.types import LocalObject -from ctypes import POINTER, c_void_p -from devito.ir import Definition - - - -# class PETScObject(AbstractSymbol): -# @property -# def _C_ctype(self): -# ctype = petsc_type_to_ctype(self._dtype) -# return type(self._dtype, (ctype,), {}) - - -class PETScDM(LocalObject): - dtype = type('DM', (c_void_p,), {}) - - -da = PETScDM('da') -defn1 = Definition(da) -print(defn1) - - - - -# may need to also inherit from Expr -class PETScFunction(AbstractFunction): - """ - PETScFunctions. - """ - _data_alignment = False - - @classmethod - def __dtype_setup__(cls, **kwargs): - grid = kwargs.get('grid') - dtype = kwargs.get('dtype') - if dtype is not None: - return dtype - elif grid is not None: - return grid.dtype - else: - return np.float32 - - @property - def dimensions(self): - return self._dimensions - - # @classmethod - # def __shape_setup__(cls, **kwargs): - # grid = kwargs.get('grid') - # dimensions = kwargs.get('dimensions') - # shape = kwargs.get('shape') - - # if dimensions is None and shape is None and grid is None: - # return None - - # elif grid is None: - # if shape is None: - # raise TypeError("Need either `grid` or `shape`") - # elif shape is None: - # if dimensions is not None and dimensions != grid.dimensions: - # raise TypeError("Need `shape` as not all `dimensions` are in `grid`") - # shape = grid.shape - # elif dimensions is None: - # raise TypeError("`dimensions` required if both `grid` and " - # "`shape` are provided") - - # return shape - - @classmethod - def __indices_setup__(cls, *args, **kwargs): - grid = kwargs.get('grid') - dimensions = kwargs.get('dimensions') - if grid is None: - if dimensions is None: - raise TypeError("Need either `grid` or `dimensions`") - elif dimensions is None: - dimensions = grid.dimensions - - return tuple(dimensions), tuple(dimensions) - - @property - def _C_ctype(self): - petsc_type = dtype_to_petsctype(self.dtype) - ctype = dtype_to_ctype(self.dtype) - r = type(petsc_type, (ctype,), {}) - for n in range(len(self.dimensions)): - r = POINTER(r) - return r - - @property - def _C_name(self): - return self.name - - - - -# from devito.ir import Definition -# da = PETScObject('da', dtype='DM') -# tmp = Definition(da) -# print(tmp) - -from devito import * -grid = Grid((2, 2)) -x, y = grid.dimensions -ptr1 = PETScFunction(name='ptr1', dtype=np.float32, dimensions=grid.dimensions, shape=grid.shape) - -from IPython import embed; embed() -# defn2 = Definition(ptr1) -# from IPython import embed; embed() -# print(/defn2) - - - diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index 8233f543424..cc8dc3afd40 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -318,6 +318,4 @@ def dtype_to_petsctype(dtype): return { np.int32: 'PetscInt', np.float32: 'PetscScalar', - # np.int64: - # np.float64: }[dtype] diff --git a/devito/types/__init__.py b/devito/types/__init__.py index 6ec8bdfd164..ff024f24bea 100644 --- a/devito/types/__init__.py +++ b/devito/types/__init__.py @@ -6,6 +6,7 @@ from .object import * # noqa from .lazy import * # noqa from .misc import * # noqa +from .petsc import * # noqa # Needed both within and outside Devito from .dimension import * # noqa diff --git a/devito/types/petsc.py b/devito/types/petsc.py new file mode 100644 index 00000000000..f4e999e3345 --- /dev/null +++ b/devito/types/petsc.py @@ -0,0 +1,77 @@ +from devito.types.basic import AbstractFunction +from devito.tools import dtype_to_petsctype, CustomDtype +import numpy as np +from devito.types import LocalObject + + +class DM(LocalObject): + dtype = CustomDtype('DM') + + +class Mat(LocalObject): + dtype = CustomDtype('Mat') + + +class Vec(LocalObject): + dtype = CustomDtype('Vec') + + +class PetscMPIInt(LocalObject): + dtype = CustomDtype('PetscMPIInt') + + +class KSP(LocalObject): + dtype = CustomDtype('KSP') + + +class PC(LocalObject): + dtype = CustomDtype('PC') + + +class KSPConvergedReason(LocalObject): + dtype = CustomDtype('KSPConvergedReason') + + +class PETScFunction(AbstractFunction): + """ + PETScFunctions. + """ + _data_alignment = False + + @classmethod + def __dtype_setup__(cls, **kwargs): + grid = kwargs.get('grid') + dtype = kwargs.get('dtype') + if dtype is not None: + return dtype + elif grid is not None: + return grid.dtype + else: + return np.float32 + + @classmethod + def __indices_setup__(cls, *args, **kwargs): + grid = kwargs.get('grid') + dimensions = kwargs.get('dimensions') + if grid is None: + if dimensions is None: + raise TypeError("Need either `grid` or `dimensions`") + elif dimensions is None: + dimensions = grid.dimensions + + return tuple(dimensions), tuple(dimensions) + + @property + def dimensions(self): + return self._dimensions + + @property + def _C_ctype(self): + petsc_type = dtype_to_petsctype(self.dtype) + modifier = '*' * len(self.dimensions) + customtype = CustomDtype(petsc_type, modifier=modifier) + return customtype + + @property + def _C_name(self): + return self.name diff --git a/tests/test_petsc.py b/tests/test_petsc.py new file mode 100644 index 00000000000..2ea5cbf9ba0 --- /dev/null +++ b/tests/test_petsc.py @@ -0,0 +1,54 @@ +from devito import Grid +from devito.ir.iet import Call, ElementalFunction, Definition, DummyExpr +from devito.passes.iet.languages.C import CDataManager +from devito.types import (DM, Mat, Vec, PetscMPIInt, KSP, + PC, KSPConvergedReason, PETScFunction) +import numpy as np + + +def test_petsc_local_object(): + """ + Test C++ support for PETSc LocalObjects. + """ + lo0 = DM('da') + lo1 = Mat('A') + lo2 = Vec('x') + lo3 = PetscMPIInt('size') + lo4 = KSP('ksp') + lo5 = PC('pc') + lo6 = KSPConvergedReason('reason') + + iet = Call('foo', [lo0, lo1, lo2, lo3, lo4, lo5, lo6]) + iet = ElementalFunction('foo', iet, parameters=()) + + dm = CDataManager(sregistry=None) + iet = CDataManager.place_definitions.__wrapped__(dm, iet)[0] + + assert 'DM da;' in str(iet) + assert 'Mat A;' in str(iet) + assert 'Vec x;' in str(iet) + assert 'PetscMPIInt size;' in str(iet) + assert 'KSP ksp;' in str(iet) + assert 'PC pc;' in str(iet) + assert 'KSPConvergedReason reason;' in str(iet) + + +def test_petsc_functions(): + """ + Test C++ support for PETScFunctions. + """ + grid = Grid((2, 2)) + x, y = grid.dimensions + + ptr0 = PETScFunction(name='ptr0', dtype=np.float32, dimensions=grid.dimensions, + shape=grid.shape) + ptr1 = PETScFunction(name='ptr1', dtype=np.float32, grid=grid) + + defn0 = Definition(ptr0) + defn1 = Definition(ptr1) + + expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) + + assert str(defn0) == 'PetscScalar**restrict ptr0;' + assert str(defn1) == 'PetscScalar**restrict ptr1;' + assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From c99ea823d2dafe053a9ca21f89bfd2c4676857eb Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 7 Dec 2023 10:58:32 +0000 Subject: [PATCH 06/24] add const func in petscfunction --- devito/tools/dtypes_lowering.py | 2 ++ devito/types/petsc.py | 34 +++++++++++++++++++++++++++++++++ tests/test_petsc.py | 3 +++ 3 files changed, 39 insertions(+) diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index cc8dc3afd40..57d4f27c5cd 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -318,4 +318,6 @@ def dtype_to_petsctype(dtype): return { np.int32: 'PetscInt', np.float32: 'PetscScalar', + np.int64: 'PetscInt', + np.float64: 'PetscScalar' }[dtype] diff --git a/devito/types/petsc.py b/devito/types/petsc.py index f4e999e3345..f1e1ea6660a 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -5,30 +5,54 @@ class DM(LocalObject): + """ + PETSc Data Management object (DM). + """ dtype = CustomDtype('DM') class Mat(LocalObject): + """ + PETSc Matrix object (Mat). + """ dtype = CustomDtype('Mat') class Vec(LocalObject): + """ + PETSc Vector object (Vec). + """ dtype = CustomDtype('Vec') class PetscMPIInt(LocalObject): + """ + PETSc datatype used to represent ‘int’ parameters + to MPI functions. + """ dtype = CustomDtype('PetscMPIInt') class KSP(LocalObject): + """ + PETSc KSP : Linear Systems Solvers. + Manages Krylov Methods. + """ dtype = CustomDtype('KSP') class PC(LocalObject): + """ + PETSc object that manages all preconditioners (PC). + """ dtype = CustomDtype('PC') class KSPConvergedReason(LocalObject): + """ + PETSc object - reason a Krylov method was determined + to have converged or diverged. + """ dtype = CustomDtype('KSPConvergedReason') @@ -38,6 +62,12 @@ class PETScFunction(AbstractFunction): """ _data_alignment = False + def __init_finalize__(self, *args, **kwargs): + + super(PETScFunction, self).__init_finalize__(*args, **kwargs) + + self._is_const = kwargs.get('is_const', False) + @classmethod def __dtype_setup__(cls, **kwargs): grid = kwargs.get('grid') @@ -75,3 +105,7 @@ def _C_ctype(self): @property def _C_name(self): return self.name + + @property + def is_const(self): + return self._is_const diff --git a/tests/test_petsc.py b/tests/test_petsc.py index 2ea5cbf9ba0..10216627830 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -43,12 +43,15 @@ def test_petsc_functions(): ptr0 = PETScFunction(name='ptr0', dtype=np.float32, dimensions=grid.dimensions, shape=grid.shape) ptr1 = PETScFunction(name='ptr1', dtype=np.float32, grid=grid) + ptr2 = PETScFunction(name='ptr2', dtype=np.float32, grid=grid, is_const=True) defn0 = Definition(ptr0) defn1 = Definition(ptr1) + defn2 = Definition(ptr2) expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) assert str(defn0) == 'PetscScalar**restrict ptr0;' assert str(defn1) == 'PetscScalar**restrict ptr1;' + assert str(defn2) == 'const PetscScalar**restrict ptr2;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From 76a8e660c984f1e10d7b34910fe2c1e68450f124 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Tue, 19 Dec 2023 13:37:34 +0000 Subject: [PATCH 07/24] dsl: Simplify PETScFunction class and add diff dtypes inside test_petsc_functions --- devito/types/petsc.py | 11 +++-------- tests/test_petsc.py | 8 +++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index f1e1ea6660a..f07cb298ada 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -27,7 +27,7 @@ class Vec(LocalObject): class PetscMPIInt(LocalObject): """ - PETSc datatype used to represent ‘int’ parameters + PETSc datatype used to represent `int` parameters to MPI functions. """ dtype = CustomDtype('PetscMPIInt') @@ -64,7 +64,7 @@ class PETScFunction(AbstractFunction): def __init_finalize__(self, *args, **kwargs): - super(PETScFunction, self).__init_finalize__(*args, **kwargs) + super().__init_finalize__(*args, **kwargs) self._is_const = kwargs.get('is_const', False) @@ -91,16 +91,11 @@ def __indices_setup__(cls, *args, **kwargs): return tuple(dimensions), tuple(dimensions) - @property - def dimensions(self): - return self._dimensions - @property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) modifier = '*' * len(self.dimensions) - customtype = CustomDtype(petsc_type, modifier=modifier) - return customtype + return CustomDtype(petsc_type, modifier=modifier) @property def _C_name(self): diff --git a/tests/test_petsc.py b/tests/test_petsc.py index 10216627830..94dfe54fbde 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -43,15 +43,21 @@ def test_petsc_functions(): ptr0 = PETScFunction(name='ptr0', dtype=np.float32, dimensions=grid.dimensions, shape=grid.shape) ptr1 = PETScFunction(name='ptr1', dtype=np.float32, grid=grid) - ptr2 = PETScFunction(name='ptr2', dtype=np.float32, grid=grid, is_const=True) + ptr2 = PETScFunction(name='ptr2', dtype=np.float64, grid=grid, is_const=True) + ptr3 = PETScFunction(name='ptr3', dtype=np.int32, grid=grid, is_const=True) + ptr4 = PETScFunction(name='ptr4', dtype=np.int64, grid=grid, is_const=True) defn0 = Definition(ptr0) defn1 = Definition(ptr1) defn2 = Definition(ptr2) + defn3 = Definition(ptr3) + defn4 = Definition(ptr4) expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) assert str(defn0) == 'PetscScalar**restrict ptr0;' assert str(defn1) == 'PetscScalar**restrict ptr1;' assert str(defn2) == 'const PetscScalar**restrict ptr2;' + assert str(defn3) == 'const PetscInt**restrict ptr3;' + assert str(defn4) == 'const PetscInt**restrict ptr4;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From 1b33b0ac635c09ca0f18cc0732208c64889e2bcc Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Tue, 19 Dec 2023 15:02:49 +0000 Subject: [PATCH 08/24] dsl: Inherit from DiscreteFunction for PETScFunction instead?' --- devito/types/petsc.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index f07cb298ada..8d7b61935b6 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -2,6 +2,7 @@ from devito.tools import dtype_to_petsctype, CustomDtype import numpy as np from devito.types import LocalObject +# from devito.types.dense import DiscreteFunction class DM(LocalObject): @@ -104,3 +105,42 @@ def _C_name(self): @property def is_const(self): return self._is_const + + +# class PETScFunction(DiscreteFunction): +# """ +# PETScFunctions. +# """ +# _data_alignment = False + +# def __init_finalize__(self, *args, **kwargs): + +# super().__init_finalize__(*args, **kwargs) + +# self._is_const = kwargs.get('is_const', False) + +# @classmethod +# def __indices_setup__(cls, *args, **kwargs): +# grid = kwargs.get('grid') +# dimensions = kwargs.get('dimensions') +# if grid is None: +# if dimensions is None: +# raise TypeError("Need either `grid` or `dimensions`") +# elif dimensions is None: +# dimensions = grid.dimensions + +# return tuple(dimensions), tuple(dimensions) + +# @property +# def _C_ctype(self): +# petsc_type = dtype_to_petsctype(self.dtype) +# modifier = '*' * len(self.dimensions) +# return CustomDtype(petsc_type, modifier=modifier) + +# @property +# def _C_name(self): +# return self.name + +# @property +# def is_const(self): +# return self._is_const From 12c2b8d264d9cc5b619ba5ee6af39898a20945a0 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 20 Dec 2023 10:18:11 +0000 Subject: [PATCH 09/24] compiler: Switch PETScFunction to inherit from Array --- devito/types/petsc.py | 69 ++----------------------------------------- tests/test_petsc.py | 18 ++++++----- 2 files changed, 12 insertions(+), 75 deletions(-) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index 8d7b61935b6..b5f77c68e04 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -1,8 +1,5 @@ -from devito.types.basic import AbstractFunction from devito.tools import dtype_to_petsctype, CustomDtype -import numpy as np -from devito.types import LocalObject -# from devito.types.dense import DiscreteFunction +from devito.types import LocalObject, Array class DM(LocalObject): @@ -57,7 +54,7 @@ class KSPConvergedReason(LocalObject): dtype = CustomDtype('KSPConvergedReason') -class PETScFunction(AbstractFunction): +class PETScFunction(Array): """ PETScFunctions. """ @@ -69,29 +66,6 @@ def __init_finalize__(self, *args, **kwargs): self._is_const = kwargs.get('is_const', False) - @classmethod - def __dtype_setup__(cls, **kwargs): - grid = kwargs.get('grid') - dtype = kwargs.get('dtype') - if dtype is not None: - return dtype - elif grid is not None: - return grid.dtype - else: - return np.float32 - - @classmethod - def __indices_setup__(cls, *args, **kwargs): - grid = kwargs.get('grid') - dimensions = kwargs.get('dimensions') - if grid is None: - if dimensions is None: - raise TypeError("Need either `grid` or `dimensions`") - elif dimensions is None: - dimensions = grid.dimensions - - return tuple(dimensions), tuple(dimensions) - @property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) @@ -105,42 +79,3 @@ def _C_name(self): @property def is_const(self): return self._is_const - - -# class PETScFunction(DiscreteFunction): -# """ -# PETScFunctions. -# """ -# _data_alignment = False - -# def __init_finalize__(self, *args, **kwargs): - -# super().__init_finalize__(*args, **kwargs) - -# self._is_const = kwargs.get('is_const', False) - -# @classmethod -# def __indices_setup__(cls, *args, **kwargs): -# grid = kwargs.get('grid') -# dimensions = kwargs.get('dimensions') -# if grid is None: -# if dimensions is None: -# raise TypeError("Need either `grid` or `dimensions`") -# elif dimensions is None: -# dimensions = grid.dimensions - -# return tuple(dimensions), tuple(dimensions) - -# @property -# def _C_ctype(self): -# petsc_type = dtype_to_petsctype(self.dtype) -# modifier = '*' * len(self.dimensions) -# return CustomDtype(petsc_type, modifier=modifier) - -# @property -# def _C_name(self): -# return self.name - -# @property -# def is_const(self): -# return self._is_const diff --git a/tests/test_petsc.py b/tests/test_petsc.py index 94dfe54fbde..bfb729619fa 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -40,12 +40,14 @@ def test_petsc_functions(): grid = Grid((2, 2)) x, y = grid.dimensions - ptr0 = PETScFunction(name='ptr0', dtype=np.float32, dimensions=grid.dimensions, - shape=grid.shape) - ptr1 = PETScFunction(name='ptr1', dtype=np.float32, grid=grid) - ptr2 = PETScFunction(name='ptr2', dtype=np.float64, grid=grid, is_const=True) - ptr3 = PETScFunction(name='ptr3', dtype=np.int32, grid=grid, is_const=True) - ptr4 = PETScFunction(name='ptr4', dtype=np.int64, grid=grid, is_const=True) + ptr0 = PETScFunction(name='ptr0', dimensions=grid.dimensions, dtype=np.float32) + ptr1 = PETScFunction(name='ptr1', dimensions=grid.dimensions, dtype=np.float32, + is_const=True) + ptr2 = PETScFunction(name='ptr2', dimensions=grid.dimensions, dtype=np.float64, + is_const=True) + ptr3 = PETScFunction(name='ptr3', dimensions=grid.dimensions, dtype=np.int32) + ptr4 = PETScFunction(name='ptr4', dimensions=grid.dimensions, dtype=np.int64, + is_const=True) defn0 = Definition(ptr0) defn1 = Definition(ptr1) @@ -56,8 +58,8 @@ def test_petsc_functions(): expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) assert str(defn0) == 'PetscScalar**restrict ptr0;' - assert str(defn1) == 'PetscScalar**restrict ptr1;' + assert str(defn1) == 'const PetscScalar**restrict ptr1;' assert str(defn2) == 'const PetscScalar**restrict ptr2;' - assert str(defn3) == 'const PetscInt**restrict ptr3;' + assert str(defn3) == 'PetscInt**restrict ptr3;' assert str(defn4) == 'const PetscInt**restrict ptr4;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From cbb4c77a20c4a5129e72bae20e21049c925ef814 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 20 Dec 2023 21:03:02 +0000 Subject: [PATCH 10/24] compiler: Edit PETScFunction to inherit from ArrayBasic --- devito/passes/iet/definitions.py | 10 ++++++++++ devito/types/basic.py | 1 + devito/types/petsc.py | 27 +++++++++++++++++++-------- tests/test_petsc.py | 8 ++++---- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index 913432da8ea..f041f80ce4b 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -219,6 +219,14 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): storage.update(obj, site, allocs=decl) + def _alloc_petsc_array_on_low_lat_mem(self, site, obj, storage): + """ + Allocate a PETScArray in the low latency memory. + """ + decl = Definition(obj) + definition = (decl) + storage.update(obj, site, standalones=definition) + def _alloc_pointed_array_on_high_bw_mem(self, site, obj, storage): """ Allocate the following objects in the high bandwidth memory: @@ -356,6 +364,8 @@ def place_definitions(self, iet, globs=None, **kwargs): self._alloc_object_array_on_low_lat_mem(iet, i, storage) elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) + elif i.is_PETScArray: + self._alloc_petsc_array_on_low_lat_mem(iet, i, storage) # Handle postponed global objects includes = set() diff --git a/devito/types/basic.py b/devito/types/basic.py index 17835933e35..9ac33accf93 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -255,6 +255,7 @@ class Basic(CodeSymbol): is_Array = False is_PointerArray = False is_ObjectArray = False + is_PETScArray = False is_Bundle = False is_Object = False is_LocalObject = False diff --git a/devito/types/petsc.py b/devito/types/petsc.py index b5f77c68e04..30ca184e0f5 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -1,5 +1,9 @@ -from devito.tools import dtype_to_petsctype, CustomDtype -from devito.types import LocalObject, Array +from devito.tools import (dtype_to_petsctype, CustomDtype, + dtype_to_ctype) +from devito.types import LocalObject +from devito.types.array import ArrayBasic +from ctypes import POINTER +import numpy as np class DM(LocalObject): @@ -54,23 +58,30 @@ class KSPConvergedReason(LocalObject): dtype = CustomDtype('KSPConvergedReason') -class PETScFunction(Array): - """ - PETScFunctions. - """ +class PETScFunction(ArrayBasic): + _data_alignment = False + is_PETScArray = True + def __init_finalize__(self, *args, **kwargs): super().__init_finalize__(*args, **kwargs) self._is_const = kwargs.get('is_const', False) + @classmethod + def __dtype_setup__(cls, **kwargs): + return kwargs.get('dtype', np.float32) + @property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) - modifier = '*' * len(self.dimensions) - return CustomDtype(petsc_type, modifier=modifier) + ctype = dtype_to_ctype(self.dtype) + r = type(petsc_type, (ctype,), {}) + for n in range(len(self.dimensions)): + r = POINTER(r) + return r @property def _C_name(self): diff --git a/tests/test_petsc.py b/tests/test_petsc.py index bfb729619fa..8a7a6458515 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -57,9 +57,9 @@ def test_petsc_functions(): expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) - assert str(defn0) == 'PetscScalar**restrict ptr0;' - assert str(defn1) == 'const PetscScalar**restrict ptr1;' - assert str(defn2) == 'const PetscScalar**restrict ptr2;' + assert str(defn0) == 'PetscScalar **restrict ptr0;' + assert str(defn1) == 'const PetscScalar **restrict ptr1;' + assert str(defn2) == 'const PetscScalar **restrict ptr2;' assert str(defn3) == 'PetscInt**restrict ptr3;' - assert str(defn4) == 'const PetscInt**restrict ptr4;' + assert str(defn4) == 'const PetscInt **restrict ptr4;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From 9253747680a2d0ca5b25c8bb2ae2cd4bdf0f5b73 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 20 Dec 2023 21:08:40 +0000 Subject: [PATCH 11/24] misc: Fix pytest for test_petsc.py --- tests/test_petsc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_petsc.py b/tests/test_petsc.py index 8a7a6458515..5dc60e2aff4 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -60,6 +60,6 @@ def test_petsc_functions(): assert str(defn0) == 'PetscScalar **restrict ptr0;' assert str(defn1) == 'const PetscScalar **restrict ptr1;' assert str(defn2) == 'const PetscScalar **restrict ptr2;' - assert str(defn3) == 'PetscInt**restrict ptr3;' + assert str(defn3) == 'PetscInt **restrict ptr3;' assert str(defn4) == 'const PetscInt **restrict ptr4;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From af2e47928ef7c33d28ca2ddf54a08303dbdfcf39 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 20 Dec 2023 21:20:16 +0000 Subject: [PATCH 12/24] compiler: Edit _C_ctype property of PETScFunction to utilise CustomDtype --- devito/types/petsc.py | 12 ++++-------- tests/test_petsc.py | 10 +++++----- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index 30ca184e0f5..45daf4243f3 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -1,8 +1,6 @@ -from devito.tools import (dtype_to_petsctype, CustomDtype, - dtype_to_ctype) +from devito.tools import dtype_to_petsctype, CustomDtype from devito.types import LocalObject from devito.types.array import ArrayBasic -from ctypes import POINTER import numpy as np @@ -77,11 +75,9 @@ def __dtype_setup__(cls, **kwargs): @property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) - ctype = dtype_to_ctype(self.dtype) - r = type(petsc_type, (ctype,), {}) - for n in range(len(self.dimensions)): - r = POINTER(r) - return r + modifier = '*' * len(self.dimensions) + customtype = CustomDtype(petsc_type, modifier=modifier) + return customtype @property def _C_name(self): diff --git a/tests/test_petsc.py b/tests/test_petsc.py index 5dc60e2aff4..bfb729619fa 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -57,9 +57,9 @@ def test_petsc_functions(): expr = DummyExpr(ptr0.indexed[x, y], ptr1.indexed[x, y] + 1) - assert str(defn0) == 'PetscScalar **restrict ptr0;' - assert str(defn1) == 'const PetscScalar **restrict ptr1;' - assert str(defn2) == 'const PetscScalar **restrict ptr2;' - assert str(defn3) == 'PetscInt **restrict ptr3;' - assert str(defn4) == 'const PetscInt **restrict ptr4;' + assert str(defn0) == 'PetscScalar**restrict ptr0;' + assert str(defn1) == 'const PetscScalar**restrict ptr1;' + assert str(defn2) == 'const PetscScalar**restrict ptr2;' + assert str(defn3) == 'PetscInt**restrict ptr3;' + assert str(defn4) == 'const PetscInt**restrict ptr4;' assert str(expr) == 'ptr0[x][y] = ptr1[x][y] + 1;' From 82723872aa14228f2b8c4f9c7efe98681ed7efa8 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Wed, 20 Dec 2023 21:22:33 +0000 Subject: [PATCH 13/24] compiler: Edit _C_ctype property of PETScFunction --- devito/types/petsc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index 45daf4243f3..b8ffae94ddf 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -76,8 +76,7 @@ def __dtype_setup__(cls, **kwargs): def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) modifier = '*' * len(self.dimensions) - customtype = CustomDtype(petsc_type, modifier=modifier) - return customtype + return CustomDtype(petsc_type, modifier=modifier) @property def _C_name(self): From 83d8143e92477ebba1b20f9eab060409c85dde15 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Fri, 22 Dec 2023 14:46:10 +0000 Subject: [PATCH 14/24] misc: Update file due to incorrect rebase --- docker/Dockerfile.intel | 50 ----------------------------------------- 1 file changed, 50 deletions(-) diff --git a/docker/Dockerfile.intel b/docker/Dockerfile.intel index c02c97d3d46..48757d97763 100644 --- a/docker/Dockerfile.intel +++ b/docker/Dockerfile.intel @@ -45,7 +45,6 @@ RUN apt-get update -y && \ apt-get install -y intel-oneapi-advisor # Drivers mandatory for intel gpu -<<<<<<< HEAD # https://dgpu-docs.intel.com/driver/installation.html#ubuntu-install-steps RUN wget -qO - https://repositories.intel.com/graphics/intel-graphics.key | gpg --dearmor > /usr/share/keyrings/intel-graphics.gpg RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/graphics/ubuntu jammy unified" > /etc/apt/sources.list.d/intel-gpu-jammy.list @@ -59,16 +58,6 @@ RUN apt-get update -y && apt-get dist-upgrade -y && \ mesa-vdpau-drivers mesa-vulkan-drivers va-driver-all vainfo hwinfo clinfo \ # Development packages libigc-dev intel-igc-cm libigdfcl-dev libigfxcmrt-dev level-zero-dev -======= -# https://dgpu-docs.intel.com/installation-guides/ubuntu/ubuntu-focal.html#ubuntu-20-04-focal -RUN wget -qO - https://repositories.intel.com/graphics/intel-graphics.key | gpg --dearmor > /usr/share/keyrings/intel-graphics.gpg -RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/graphics/ubuntu focal main" > /etc/apt/sources.list.d/intel.list - -RUN apt-get update -y && apt-get dist-upgrade -y && \ - apt-get install -y intel-opencl-icd intel-level-zero-gpu level-zero level-zero-dev \ - intel-media-va-driver-non-free libmfx1 libmfxgen1 libvpl2 \ - libigc-dev intel-igc-cm libigdfcl-dev libigfxcmrt-dev level-zero-dev ->>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) ############################################################## # ICC image @@ -89,11 +78,7 @@ ENV MPICC=mpiicc ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icc"' ############################################################## -<<<<<<< HEAD # ICX OpenMP image -======= -# ICX image ->>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) ############################################################## FROM oneapi as icx @@ -109,35 +94,6 @@ ENV MPICC=mpiicc ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icx"' ############################################################## -<<<<<<< HEAD -======= -# ICX hpc image -############################################################## -FROM oneapi as icx-hpc - -# Install both icc and icx to avoid missing dependencies -RUN apt-get update -y && \ - apt-get install -y intel-oneapi-compiler-dpcpp-cpp intel-oneapi-mpi-devel && \ - apt-get install -y intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic - -# Missig components -# https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit-download.html?operatingsystem=linux&distributions=aptpackagemanager -RUN curl -f "https://registrationcenter-download.intel.com/akdlm/IRC_NAS/ebf5d9aa-17a7-46a4-b5df-ace004227c0e/l_dpcpp-cpp-compiler_p_2023.2.1.8.sh" -O && \ - chmod +x l_dpcpp-cpp-compiler_p_2023.2.1.8.sh && ./l_dpcpp-cpp-compiler_p_2023.2.1.8.sh -a -s --eula accept && \ - rm l_dpcpp-cpp-compiler_p_2023.2.1.8.sh - -RUN apt-get clean && apt-get autoclean && apt-get autoremove -y && \ - rm -rf /var/lib/apt/lists/* - -# Devito config -ENV DEVITO_ARCH="icx" -ENV DEVITO_LANGUAGE="openmp" -# MPICC compiler for mpi4py -ENV MPICC=mpiicc -ENV MPI4PY_FLAGS='. /opt/intel/oneapi/setvars.sh && CFLAGS="-cc=icx"' - -############################################################## ->>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) # ICX SYCL CPU image ############################################################## FROM icx as cpu-sycl @@ -152,12 +108,6 @@ ENV DEVITO_PLATFORM="intel64" ############################################################## FROM icx as gpu-sycl -<<<<<<< HEAD -======= -# NOTE: the name of this file ends with ".cpu" but this is a GPU image. -# It then feels a bit akward, so some restructuring might be needed - ->>>>>>> c4373f90e (misc: Split Dockerfile.cpu into .cpu and .intel) # Devito config ENV DEVITO_ARCH="sycl" ENV DEVITO_LANGUAGE="sycl" From 5f71310e916368eaced1f53081dad28b3494604e Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Tue, 2 Jan 2024 11:46:05 +0000 Subject: [PATCH 15/24] compiler: Change name to PETScArray from PETScFunction --- devito/passes/iet/definitions.py | 12 +----------- devito/types/petsc.py | 2 +- tests/test_petsc.py | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index f041f80ce4b..a385b36e61a 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -219,14 +219,6 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): storage.update(obj, site, allocs=decl) - def _alloc_petsc_array_on_low_lat_mem(self, site, obj, storage): - """ - Allocate a PETScArray in the low latency memory. - """ - decl = Definition(obj) - definition = (decl) - storage.update(obj, site, standalones=definition) - def _alloc_pointed_array_on_high_bw_mem(self, site, obj, storage): """ Allocate the following objects in the high bandwidth memory: @@ -360,12 +352,10 @@ def place_definitions(self, iet, globs=None, **kwargs): elif globs is not None: # Track, to be handled by the EntryFunction being a global obj! globs.add(i) - elif i.is_ObjectArray: + elif i.is_ObjectArray or i.is_PETScArray: self._alloc_object_array_on_low_lat_mem(iet, i, storage) elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) - elif i.is_PETScArray: - self._alloc_petsc_array_on_low_lat_mem(iet, i, storage) # Handle postponed global objects includes = set() diff --git a/devito/types/petsc.py b/devito/types/petsc.py index b8ffae94ddf..117f0d8bd7f 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -56,7 +56,7 @@ class KSPConvergedReason(LocalObject): dtype = CustomDtype('KSPConvergedReason') -class PETScFunction(ArrayBasic): +class PETScArray(ArrayBasic): _data_alignment = False diff --git a/tests/test_petsc.py b/tests/test_petsc.py index bfb729619fa..fa05a32a3d6 100644 --- a/tests/test_petsc.py +++ b/tests/test_petsc.py @@ -2,7 +2,7 @@ from devito.ir.iet import Call, ElementalFunction, Definition, DummyExpr from devito.passes.iet.languages.C import CDataManager from devito.types import (DM, Mat, Vec, PetscMPIInt, KSP, - PC, KSPConvergedReason, PETScFunction) + PC, KSPConvergedReason, PETScArray) import numpy as np @@ -35,19 +35,19 @@ def test_petsc_local_object(): def test_petsc_functions(): """ - Test C++ support for PETScFunctions. + Test C++ support for PETScArrays. """ grid = Grid((2, 2)) x, y = grid.dimensions - ptr0 = PETScFunction(name='ptr0', dimensions=grid.dimensions, dtype=np.float32) - ptr1 = PETScFunction(name='ptr1', dimensions=grid.dimensions, dtype=np.float32, - is_const=True) - ptr2 = PETScFunction(name='ptr2', dimensions=grid.dimensions, dtype=np.float64, - is_const=True) - ptr3 = PETScFunction(name='ptr3', dimensions=grid.dimensions, dtype=np.int32) - ptr4 = PETScFunction(name='ptr4', dimensions=grid.dimensions, dtype=np.int64, - is_const=True) + ptr0 = PETScArray(name='ptr0', dimensions=grid.dimensions, dtype=np.float32) + ptr1 = PETScArray(name='ptr1', dimensions=grid.dimensions, dtype=np.float32, + is_const=True) + ptr2 = PETScArray(name='ptr2', dimensions=grid.dimensions, dtype=np.float64, + is_const=True) + ptr3 = PETScArray(name='ptr3', dimensions=grid.dimensions, dtype=np.int32) + ptr4 = PETScArray(name='ptr4', dimensions=grid.dimensions, dtype=np.int64, + is_const=True) defn0 = Definition(ptr0) defn1 = Definition(ptr1) From d4f0a6ad947897b7074db6bb9b9a388ec297ce06 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 11:12:00 +0000 Subject: [PATCH 16/24] compiler: Add intermediate class between PETScArray and ArrayBasic --- devito/passes/iet/definitions.py | 2 +- devito/types/basic.py | 1 - devito/types/petsc.py | 28 ++++++++++++++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index a385b36e61a..3979c65868f 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -352,7 +352,7 @@ def place_definitions(self, iet, globs=None, **kwargs): elif globs is not None: # Track, to be handled by the EntryFunction being a global obj! globs.add(i) - elif i.is_ObjectArray or i.is_PETScArray: + elif i.is_ObjectArray or i.is_AbstractArray: self._alloc_object_array_on_low_lat_mem(iet, i, storage) elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) diff --git a/devito/types/basic.py b/devito/types/basic.py index 9ac33accf93..17835933e35 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -255,7 +255,6 @@ class Basic(CodeSymbol): is_Array = False is_PointerArray = False is_ObjectArray = False - is_PETScArray = False is_Bundle = False is_Object = False is_LocalObject = False diff --git a/devito/types/petsc.py b/devito/types/petsc.py index 117f0d8bd7f..66c0b12abce 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -56,11 +56,20 @@ class KSPConvergedReason(LocalObject): dtype = CustomDtype('KSPConvergedReason') -class PETScArray(ArrayBasic): +class AbstractArray(ArrayBasic): + + """ + A customised version of ArrayBasic that allows objects to + be generated without a cast. This is particularly useful for + PETSc objects. Also, AbstractArray objects can + be explicitly specifed as constant. + """ _data_alignment = False - is_PETScArray = True + is_AbstractArray = True + + __rkwargs__ = ArrayBasic.__rkwargs__ + ('is_const',) def __init_finalize__(self, *args, **kwargs): @@ -72,12 +81,6 @@ def __init_finalize__(self, *args, **kwargs): def __dtype_setup__(cls, **kwargs): return kwargs.get('dtype', np.float32) - @property - def _C_ctype(self): - petsc_type = dtype_to_petsctype(self.dtype) - modifier = '*' * len(self.dimensions) - return CustomDtype(petsc_type, modifier=modifier) - @property def _C_name(self): return self.name @@ -85,3 +88,12 @@ def _C_name(self): @property def is_const(self): return self._is_const + + +class PETScArray(AbstractArray): + + @property + def _C_ctype(self): + petsc_type = dtype_to_petsctype(self.dtype) + modifier = '*' * len(self.dimensions) + return CustomDtype(petsc_type, modifier=modifier) From 5e426306c710c5d6131ffa0260435a78011ff530 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 13:33:58 +0000 Subject: [PATCH 17/24] compiler: Remove PETSc specific checks in place_definitions --- devito/passes/iet/definitions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index 3979c65868f..1679dfa1100 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -352,10 +352,12 @@ def place_definitions(self, iet, globs=None, **kwargs): elif globs is not None: # Track, to be handled by the EntryFunction being a global obj! globs.add(i) - elif i.is_ObjectArray or i.is_AbstractArray: + elif i.is_ObjectArray: self._alloc_object_array_on_low_lat_mem(iet, i, storage) elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) + else: + self._alloc_object_array_on_low_lat_mem(iet, i, storage) # Handle postponed global objects includes = set() From 5478d2eaa10c48a746842324a82a92f9a9009188 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 14:07:29 +0000 Subject: [PATCH 18/24] compiler: Add const functionality to ArrayBasic --- devito/types/array.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/devito/types/array.py b/devito/types/array.py index e51edd27ed2..5ae90c16238 100644 --- a/devito/types/array.py +++ b/devito/types/array.py @@ -19,6 +19,14 @@ class ArrayBasic(AbstractFunction): is_ArrayBasic = True + __rkwargs__ = AbstractFunction.__rkwargs__ + ('is_const',) + + def __init_finalize__(self, *args, **kwargs): + + super().__init_finalize__(*args, **kwargs) + + self._is_const = kwargs.get('is_const', False) + @classmethod def __indices_setup__(cls, *args, **kwargs): dimensions = kwargs['dimensions'] @@ -48,6 +56,10 @@ def shape(self): def shape_allocated(self): return self.symbolic_shape + @property + def is_const(self): + return self._is_const + class Array(ArrayBasic): From 943f251f022131c86a2dbc07fce4ecf3b78b5369 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 14:08:14 +0000 Subject: [PATCH 19/24] compiler: Remove PETSc specific check from place_definitions and edit PETScArray --- devito/passes/iet/definitions.py | 13 +++++++++++- devito/types/petsc.py | 35 ++++++++------------------------ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index 1679dfa1100..2a48f504bb3 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -219,6 +219,17 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): storage.update(obj, site, allocs=decl) + def _alloc_special_type_on_low_lat_mem(self, site, obj, storage): + """ + Allocate special types in the low latency memory e.g PETScArrays. + These types are typically customised versions of ArrayBasic + that deviate from the standard types + generated by the compiler. + """ + decl = Definition(obj) + + storage.update(obj, site, allocs=decl) + def _alloc_pointed_array_on_high_bw_mem(self, site, obj, storage): """ Allocate the following objects in the high bandwidth memory: @@ -357,7 +368,7 @@ def place_definitions(self, iet, globs=None, **kwargs): elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) else: - self._alloc_object_array_on_low_lat_mem(iet, i, storage) + self._alloc_special_type_on_low_lat_mem(iet, i, storage) # Handle postponed global objects includes = set() diff --git a/devito/types/petsc.py b/devito/types/petsc.py index 66c0b12abce..bef6b486cb6 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -56,44 +56,27 @@ class KSPConvergedReason(LocalObject): dtype = CustomDtype('KSPConvergedReason') -class AbstractArray(ArrayBasic): - +class PETScArray(ArrayBasic): """ - A customised version of ArrayBasic that allows objects to - be generated without a cast. This is particularly useful for - PETSc objects. Also, AbstractArray objects can - be explicitly specifed as constant. + PETScArrays are generated by the compiler only and represent + a customised variant of ArrayBasic. They are designed to + avoid generating a cast in the low-level code. """ _data_alignment = False - is_AbstractArray = True - - __rkwargs__ = ArrayBasic.__rkwargs__ + ('is_const',) - - def __init_finalize__(self, *args, **kwargs): - - super().__init_finalize__(*args, **kwargs) - - self._is_const = kwargs.get('is_const', False) + is_PETScArray = True @classmethod def __dtype_setup__(cls, **kwargs): return kwargs.get('dtype', np.float32) - @property - def _C_name(self): - return self.name - - @property - def is_const(self): - return self._is_const - - -class PETScArray(AbstractArray): - @property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) modifier = '*' * len(self.dimensions) return CustomDtype(petsc_type, modifier=modifier) + + @property + def _C_name(self): + return self.name From d222274482411e1146f91ba4f07420f578cb340e Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 16:14:31 +0000 Subject: [PATCH 20/24] compiler: Remove is_PETScArray --- devito/types/petsc.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/devito/types/petsc.py b/devito/types/petsc.py index bef6b486cb6..e2c6cfcd083 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -65,8 +65,6 @@ class PETScArray(ArrayBasic): _data_alignment = False - is_PETScArray = True - @classmethod def __dtype_setup__(cls, **kwargs): return kwargs.get('dtype', np.float32) From 55154a8ee8d6885dbeba9125c7e22a51a36d9bff Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Thu, 4 Jan 2024 18:53:04 +0000 Subject: [PATCH 21/24] compiler: Remove elif is_ObjectArray and use alloc_object_.. inside the else statement --- devito/passes/iet/definitions.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index 2a48f504bb3..786543807a4 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -219,17 +219,6 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): storage.update(obj, site, allocs=decl) - def _alloc_special_type_on_low_lat_mem(self, site, obj, storage): - """ - Allocate special types in the low latency memory e.g PETScArrays. - These types are typically customised versions of ArrayBasic - that deviate from the standard types - generated by the compiler. - """ - decl = Definition(obj) - - storage.update(obj, site, allocs=decl) - def _alloc_pointed_array_on_high_bw_mem(self, site, obj, storage): """ Allocate the following objects in the high bandwidth memory: @@ -363,12 +352,10 @@ def place_definitions(self, iet, globs=None, **kwargs): elif globs is not None: # Track, to be handled by the EntryFunction being a global obj! globs.add(i) - elif i.is_ObjectArray: - self._alloc_object_array_on_low_lat_mem(iet, i, storage) elif i.is_PointerArray: self._alloc_pointed_array_on_high_bw_mem(iet, i, storage) else: - self._alloc_special_type_on_low_lat_mem(iet, i, storage) + self._alloc_object_array_on_low_lat_mem(iet, i, storage) # Handle postponed global objects includes = set() From 6654c1d1ae967f954ec57327ba4c4fcc321a6ae8 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 8 Jan 2024 10:04:22 +0000 Subject: [PATCH 22/24] misc: Delete empty file --- devito/passes/iet/petsc.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 devito/passes/iet/petsc.py diff --git a/devito/passes/iet/petsc.py b/devito/passes/iet/petsc.py deleted file mode 100644 index e69de29bb2d..00000000000 From 6f375f8a4daa2bb639ebcfc3ef7bf84f28a6ce58 Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 8 Jan 2024 10:12:19 +0000 Subject: [PATCH 23/24] misc: Move dtype_to_petsctype to petsc.py and clean up blank lines --- devito/tools/dtypes_lowering.py | 13 +------------ devito/types/array.py | 2 -- devito/types/petsc.py | 16 ++++++++++++++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index 57d4f27c5cd..e4d6f65380e 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -13,7 +13,7 @@ 'double3', 'double4', 'dtypes_vector_mapper', 'dtype_to_mpidtype', 'dtype_to_cstr', 'dtype_to_ctype', 'dtype_to_mpitype', 'dtype_len', 'ctypes_to_cstr', 'c_restrict_void_p', 'ctypes_vector_mapper', - 'is_external_ctype', 'infer_dtype', 'CustomDtype', 'dtype_to_petsctype'] + 'is_external_ctype', 'infer_dtype', 'CustomDtype'] # *** Custom np.dtypes @@ -310,14 +310,3 @@ def infer_dtype(dtypes): else: # E.g., mixed integer arithmetic return max(dtypes, key=lambda i: np.dtype(i).itemsize, default=None) - - -def dtype_to_petsctype(dtype): - """Map numpy types to PETSc datatypes.""" - - return { - np.int32: 'PetscInt', - np.float32: 'PetscScalar', - np.int64: 'PetscInt', - np.float64: 'PetscScalar' - }[dtype] diff --git a/devito/types/array.py b/devito/types/array.py index 5ae90c16238..69d7d5126e1 100644 --- a/devito/types/array.py +++ b/devito/types/array.py @@ -22,9 +22,7 @@ class ArrayBasic(AbstractFunction): __rkwargs__ = AbstractFunction.__rkwargs__ + ('is_const',) def __init_finalize__(self, *args, **kwargs): - super().__init_finalize__(*args, **kwargs) - self._is_const = kwargs.get('is_const', False) @classmethod diff --git a/devito/types/petsc.py b/devito/types/petsc.py index e2c6cfcd083..d6d07b6e2c4 100644 --- a/devito/types/petsc.py +++ b/devito/types/petsc.py @@ -1,7 +1,8 @@ -from devito.tools import dtype_to_petsctype, CustomDtype +from devito.tools import CustomDtype from devito.types import LocalObject from devito.types.array import ArrayBasic import numpy as np +from cached_property import cached_property class DM(LocalObject): @@ -69,7 +70,7 @@ class PETScArray(ArrayBasic): def __dtype_setup__(cls, **kwargs): return kwargs.get('dtype', np.float32) - @property + @cached_property def _C_ctype(self): petsc_type = dtype_to_petsctype(self.dtype) modifier = '*' * len(self.dimensions) @@ -78,3 +79,14 @@ def _C_ctype(self): @property def _C_name(self): return self.name + + +def dtype_to_petsctype(dtype): + """Map numpy types to PETSc datatypes.""" + + return { + np.int32: 'PetscInt', + np.float32: 'PetscScalar', + np.int64: 'PetscInt', + np.float64: 'PetscScalar' + }[dtype] From e2e5afe48bbc38e6235350e8ca0d159b81df6b5b Mon Sep 17 00:00:00 2001 From: ZoeLeibowitz Date: Mon, 8 Jan 2024 10:14:28 +0000 Subject: [PATCH 24/24] misc: Clean up blank line --- devito/tools/dtypes_lowering.py | 1 + 1 file changed, 1 insertion(+) diff --git a/devito/tools/dtypes_lowering.py b/devito/tools/dtypes_lowering.py index e4d6f65380e..70a378dae49 100644 --- a/devito/tools/dtypes_lowering.py +++ b/devito/tools/dtypes_lowering.py @@ -15,6 +15,7 @@ 'ctypes_to_cstr', 'c_restrict_void_p', 'ctypes_vector_mapper', 'is_external_ctype', 'infer_dtype', 'CustomDtype'] + # *** Custom np.dtypes # NOTE: the following is inspired by pyopencl.cltypes