Skip to content

Feat: added MultiOperator and multiproc/multithread support to matmat/rmatmat - #780

Open
mrava87 wants to merge 8 commits into
PyLops:devfrom
mrava87:feat-multi_matmat
Open

Feat: added MultiOperator and multiproc/multithread support to matmat/rmatmat#780
mrava87 wants to merge 8 commits into
PyLops:devfrom
mrava87:feat-multi_matmat

Conversation

@mrava87

@mrava87 mrava87 commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

This PR aims to extend the multiproc/multithread capabilities in PyLops.

It implements:

  • unified base class MultiOperator for all operators with multiproc/multithread support (adopted by BlockDiag, HStack, VStack
  • multiproc/multithread support in matmat/rmatmat methods
  • multiproc/multithread support in Kronecker
  • tests for matmat/rmatmat method of a generic operator (eg MatrixMult) and Kronecker with multiproc/multithread

Some examples and timings for the new multiproc/multithread support in matmat/rmatmat and Kronecker are in https://github.com/PyLops/pylops_notebooks/blob/master/developement/Basic_multi.ipynb

@codacy-production

codacy-production Bot commented Jul 26, 2026

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 high

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
ErrorProne 1 high

View in Codacy

🟢 Metrics -26 complexity · -5 duplication

Metric Results
Complexity -26
Duplication -5

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@mrava87
mrava87 requested a review from cako August 1, 2026 16:18
@mrava87
mrava87 marked this pull request as ready for review August 1, 2026 16:18
@mrava87

mrava87 commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author

@cako if you have time, appreciate any feedback on this 😄

The initial motivation was to support multiproc/threading in Kronecker for the UDCT operator (or any operator that does not internally handle broadcasting over dimensions), but when I started working on it I realized that we had a lot of code repetitions over the few operators already supporting multiproc/threading and so did a bit of code restructuring with a new base class...

@cako cako left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks pretty good, I just left a couple of suggestions!

Comment thread pylops/_multioperator.py
Comment on lines +16 to +18
ylocal = op(x).squeeze()
with lock:
y[:] += ylocal

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this logic already existed but I can see this becoming a bottleneck for large parallelization. I think a better strategy would be to let the multioperator return its local result and the main thread/process can finish summing. Because at that point its a single assessor writing, there's no need for a lock.

Comment thread pylops/linearoperator.py
Comment on lines +473 to +491
def _matmat_multithread(self, X: NDArray, pool: ThreadPoolExecutor) -> NDArray:
"""Matrix-matrix multiplication (multithreaded version)"""
ys = list(
pool.map(
lambda args: _matvec_rmatvec_map(*args),
[(self._matvec, col.reshape(-1)) for col in X.T],
)
)
y = np.vstack(ys).T
return y

def _matmat_multiproc(self, X: NDArray, pool: Pool) -> NDArray:
"""Matrix-matrix multiplication (multiprocess version)"""
ys = pool.starmap(
_matvec_rmatvec_map,
[(self._matvec, col.reshape(-1)) for col in X.T],
)
y = np.vstack(ys).T
return y

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part missing sp.sparse.issparse(X) special treatment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants