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

Refactor Codebase by Removing Obsolete Functionality due to Introducing Efficient Compute Functions #2229

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release_notes/next/fix-2229-remove-shared-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#2229: Refactor Codebase by Removing Obsolete Functionality due to Introducing Efficient Compute Functions
67 changes: 0 additions & 67 deletions mantidimaging/core/parallel/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from functools import partial
from typing import Any, TYPE_CHECKING
from collections.abc import Callable

Expand All @@ -11,72 +10,6 @@
if TYPE_CHECKING:
from numpy import ndarray


def inplace3(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
func(data[0].array[i], data[1].array[i], data[2].array, **kwargs)


def inplace2(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
func(data[0].array[i], data[1].array[i], **kwargs)


def inplace1(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
func(data[0].array[i], **kwargs)


def return_to_self(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
data[0].array[i] = func(data[0].array[i], **kwargs)


def inplace_second_2d(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
func(data[0].array[i], data[1].array, **kwargs)


def return_to_second_at_i(func, data: list[pu.SharedArray] | list[pu.SharedArrayProxy], i, **kwargs):
data[1].array[i] = func(data[0].array[i], **kwargs)


def create_partial(func, fwd_function, **kwargs):
"""
Create a partial using functools.partial, to forward the kwargs to the
parallel execution of imap.

If you seem to be getting nans, check if the correct fwd_function is set!

:param func: Function that will be executed
:param fwd_function: The function will be forwarded through function.
:param kwargs: kwargs to forward to the function func that will be executed
:return: The decorated forwarded function, ready for further execution
"""
return partial(fwd_function, func, **kwargs)


def execute(partial_func: partial,
arrays: list[pu.SharedArray],
num_operations: int,
progress=None,
msg: str = '') -> None:
"""
Executes a function a given number of times using the provided list of SharedArray objects.

If all the arrays in the list use shared memory then the execution is done in parallel, with each process
accessing the data in shared memory.
If any arrays in the list do not use shared memory then the execution will be performed synchronously.

:param partial_func: A function constructed using create_partial
:param arrays: The list of SharedArray objects that the operations should be performed on
:param num_operations: The expected number of operations - should match the number of images being processed
Also used to set the number of progress steps
:param progress: Progress instance to use for progress reporting (optional)
:param msg: Message to be shown on the progress bar
:return:
"""

all_data_in_shared_memory, data = _check_shared_mem_and_get_data(arrays)
partial_func = partial(partial_func, data)
pu.execute_impl(num_operations, partial_func, all_data_in_shared_memory, progress, msg)


ComputeFuncType = (Callable[[int, list['ndarray'], dict[str, Any]], None]
| Callable[[int, 'ndarray', dict[str, Any]], None])

Expand Down