Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Kirchhoff cuda integrated and tested successfully #617

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.9'
versionSpec: '3.11'
architecture: 'x64'

- script: |
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.9'
versionSpec: '3.11'
architecture: 'x64'

- script: |
Expand Down
3 changes: 2 additions & 1 deletion docs/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Basic operators
Real
Imag
Conj

ToCupy

Smoothing and derivatives
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions pylops/basicoperators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
Gradient Gradient.
FirstDirectionalDerivative First Directional derivative.
SecondDirectionalDerivative Second Directional derivative.
ToCupy Convert to CuPy.

"""

from .functionoperator import *
Expand Down Expand Up @@ -72,6 +74,8 @@
from .laplacian import *
from .gradient import *
from .directionalderivative import *
from .tocupy import *


__all__ = [
"FunctionOperator",
Expand Down Expand Up @@ -107,4 +111,5 @@
"Gradient",
"FirstDirectionalDerivative",
"SecondDirectionalDerivative",
"ToCupy",
]
60 changes: 60 additions & 0 deletions pylops/basicoperators/tocupy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
__all__ = ["ToCupy"]

from typing import Union

import numpy as np

from pylops import LinearOperator
from pylops.utils._internal import _value_or_sized_to_tuple
from pylops.utils.backend import to_cupy, to_numpy
from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray


class ToCupy(LinearOperator):
r"""Convert to CuPy.

Convert an input array to CuPy in forward mode,
and convert back to NumPy in adjoint mode.

Parameters
----------
dims : :obj:`list` or :obj:`int`
Number of samples for each dimension
dtype : :obj:`str`, optional
Type of elements in input array.
name : :obj:`str`, optional
Name of operator (to be used by :func:`pylops.utils.describe.describe`)

Attributes
----------
shape : :obj:`tuple`
Operator shape
explicit : :obj:`bool`
Operator contains a matrix that can be solved explicitly
(``True``) or not (``False``)

Notes
-----
The ToCupy operator is a special operator that does not perform
any transformation on the input arrays other than converting
them from NumPy to CuPy. This operator can be used when one
is interested to create a chain of operators where only one
(or some of them) act on CuPy arrays, whilst other operate
on NumPy arrays.

"""

def __init__(
self,
dims: Union[int, InputDimsLike],
dtype: DTypeLike = "float64",
name: str = "C",
) -> None:
dims = _value_or_sized_to_tuple(dims)
super().__init__(dtype=np.dtype(dtype), dims=dims, dimsd=dims, name=name)

def _matvec(self, x: NDArray) -> NDArray:
return to_cupy(x)

def _rmatvec(self, x: NDArray) -> NDArray:
return to_numpy(x)
44 changes: 44 additions & 0 deletions pylops/utils/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"get_sp_fft",
"get_complex_dtype",
"get_real_dtype",
"to_cupy",
"to_numpy",
"to_cupy_conditional",
"to_numpy_conditional",
"inplace_set",
"inplace_add",
"inplace_multiply",
Expand Down Expand Up @@ -484,6 +486,26 @@ def get_real_dtype(dtype: DTypeLike) -> DTypeLike:
return np.real(np.ones(1, dtype)).dtype


def to_cupy(x: NDArray) -> NDArray:
"""Convert x to cupy array

Parameters
----------
x : :obj:`numpy.ndarray` or :obj:`cupy.ndarray`
Array to evaluate

Returns
-------
x : :obj:`numpy.ndarray`
Converted array

"""
if deps.cupy_enabled:
if cp.get_array_module(x) == np:
x = cp.asarray(x)
return x


def to_numpy(x: NDArray) -> NDArray:
"""Convert x to numpy array

Expand Down Expand Up @@ -527,6 +549,28 @@ def to_cupy_conditional(x: npt.ArrayLike, y: npt.ArrayLike) -> NDArray:
return y


def to_numpy_conditional(x: npt.ArrayLike, y: npt.ArrayLike) -> NDArray:
"""Convert y to numpy array conditional to x being a numpy array

Parameters
----------
x : :obj:`numpy.ndarray` or :obj:`cupy.ndarray`
Array to evaluate
y : :obj:`numpy.ndarray`
Array to convert

Returns
-------
y : :obj:`cupy.ndarray`
Converted array

"""
if deps.cupy_enabled:
if cp.get_array_module(x) == np and cp.get_array_module(y) == cp:
y = cp.asnumpy(y)
return y


def inplace_set(x: npt.ArrayLike, y: npt.ArrayLike, idx: list) -> NDArray:
"""Perform inplace set based on input

Expand Down
Loading