Skip to content

Commit 7a777d5

Browse files
committed
doc: small improvements in addingsolver page
1 parent c0a55e0 commit 7a777d5

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

docs/source/addingsolver.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,44 @@ As for any Python class, our solver will need an ``__init__`` method. In this ca
5353
of the base class. Two input parameters are passed to the ``__init__`` method and saved as members of our class,
5454
namely the operator :math:`\mathbf{Op}` associated with the system of equations we wish to solve,
5555
:math:`\mathbf{y}=\mathbf{Op}\,\mathbf{x}`, and optionally a :class:`pylops.optimization.callback.Callbacks` object. Moreover,
56-
an additional parameters is created that contains the current time (this is used later to report the execution time
57-
of the solver). Here is the ``__init__`` method of the base class:
56+
two additional parameters are created that contains the counter of the iterations (which will be incremented every time the
57+
``step`` method is called) and the current time (this is used later to report the execution time of the solver). Here is the
58+
``__init__`` method of the base class:
5859

5960
.. code-block:: python
6061
6162
def __init__(self, Op, callbacks=None):
6263
self.Op = Op
6364
self.callbacks = callbacks
6465
self._registercallbacks()
66+
self.iiter = 0
6567
self.tstart = time.time()
6668
69+
Next, we will write the *memory_usage* method. This method allows users to get a prediction of the memory usage of
70+
the solver ahead of time (before running any of the methods of the solver as described below). It is very useful, especially
71+
for large problems, to get a feeling whether the current hardware resources (of the CPU or GPU if the user plans to run the solver on
72+
CuPy arrays and with a CuPy-enabled operator) will be sufficient to succesfully carry out the optimization process.
73+
74+
.. code-block:: python
75+
76+
def memory_usage(self, show: False, unit = "B"):
77+
nbytes = np.dtype(self.Op.dtype).itemsize
78+
79+
# Setup
80+
memuse = (self.Op.shape[1] + 3 * self.Op.shape[0]) * nbytes
81+
82+
# Step (additional variables to those in setup)
83+
memuse += (self.Op.shape[1] + self.Op.shape[0]) * nbytes
84+
85+
if show:
86+
print(f"CG predicted memory usage: {memuse / _units[unit]:.2f} {unit}")
87+
88+
return memuse
89+
90+
Note that, although very useful, this method is not strictly needed to run the solver; so at the beginning you could just
91+
add a ``pass`` to the core of this method. However, since this method is marked as ``@abstractmethod`` in the base class,
92+
you can't simply skip it.
93+
6794
We can now move onto writing the *setup* of the solver in the method ``setup``. We will need to write
6895
a piece of code that prepares the solver prior to being able to apply a step. In general, this requires defining the
6996
data vector ``y`` and the initial guess of the solver ``x0`` (if not provided, this will be automatically set to be a zero

0 commit comments

Comments
 (0)