dubfi.linalg.mpi_worker

MPI linear algebra worker script (private module).

This script is run in parallel by multiple MPI processes (workers). Each worker has the same set of variables stored on the respective processor. The workers await and follow instructions from the parent. Instructions include creating objects, assigning variables, and doing linear algebra operations on these variables.

This module shall be used jointly with mpi_parent.py and does not have a stable API by itself!

Added in version 0.1.0: (initial release)

Attributes

comm

rank

Rank (identifier) of this MPI process

Classes

MpiAbstractVector

AbstractVector that sends reduce operations to MPI parent.

MpiVectorWorker

Distributed vector: Part of a vector handled by this worker process.

MpiVectorWorkerZero

Distributed all-zero vector.

MpiAbstractOperator

AbstractOperator that sends reduce operations to MPI parent.

MpiDenseBdMatrixWorker

Dense matrix representing part of distributed, approximately diagonal, linear operator.

MpiLinParamVectorWorker

Part of distributed, linearly parametrize vector.

MpiDensePostRWorker

Segment of distributed, parametrized linear operator constructed from flux ensemble.

MpiGradOpWorker

Segment of distributed gradient operator, used for gradient of MpiDensePostRWorker.

MpiTraceProdOpWorker

Segment of distributed trace product operator.

Functions

set_log_file(log_file, level)

Set file to which log output shall be written, replacing other log handlers.

receive_array()

Receive numpy array of unknown shape from parent.

_trace_product_self(s, e, hens, x, loc, chainT)

Compute part of trace product for MpiGradOpWorker (special case).

_trace_product_generic(s, e, hens, x, loc, chain, ...)

Compute part of trace product for MpiGradOpWorker (generic).

Module Contents

dubfi.linalg.mpi_worker.comm: mpi4py.MPI.Intracomm | mpi4py.MPI.Intercomm

MPI communication object

dubfi.linalg.mpi_worker.rank: int

Rank (identifier) of this MPI process

dubfi.linalg.mpi_worker.set_log_file(log_file, level)

Set file to which log output shall be written, replacing other log handlers.

Parameters:
  • log_file (str)

  • level (str | None)

dubfi.linalg.mpi_worker.receive_array()

Receive numpy array of unknown shape from parent.

Return type:

numpy.ndarray

class dubfi.linalg.mpi_worker.MpiAbstractVector

Bases: dubfi.linalg.types.AbstractVector

AbstractVector that sends reduce operations to MPI parent.

apply(other)

Compute scalar product with other vector (along unbuffered segment) and send result to parent.

dot_array()

Compute scalar product with array received from parent along some non-vector dimension.

tonumpy()

Compute numpy array representation of unbuffered segment and send result to parent.

abstractmethod _apply(other)

Compute scalar product with other vector (along unbuffered segment).

Parameters:

other (dubfi.linalg.types.AbstractVector)

Return type:

numpy.ndarray | float

class dubfi.linalg.mpi_worker.MpiVectorWorker

Bases: MpiAbstractVector

Distributed vector: Part of a vector handled by this worker process.

Distributed AbstractVector.

property shape

Shape of this part of the vector.

classmethod fromparent()

Create new instance and receive data from parent to initialize it.

classmethod fromdata(data, other)

Create new instance and initialize it with given data.

Parameters:
  • data (np.ndarray) – array to construct vector, last dimension must be vector dimension

  • other ((object providing start_idx and end_idx attributes)) – some MPI worker object from which start_idx and end_idx can be copied

iscompatible(other)

Check for compatibility with other vector.

Parameters:

other (dubfi.linalg.types.AbstractVector)

Return type:

bool

property data

Numpy array representation of self (buffered segment).

_apply(other)

Compute scalar product with other vector (along unbuffered segment).

Parameters:

other (dubfi.linalg.types.AbstractVector)

Return type:

numpy.ndarray

__add__(other)

Add two vectors, applying broadcasting rules.

__iadd__(other)

Add vector in-place.

__sub__(other)

Subtract two vectors, applying broadcasting rules.

__isub__(other)

Subtract vector in-place.

__mul__(other)

Multiply by scalar or numpy array treated as scalar along vector space dimension.

mul_array()

Multiply by array received from parent.

dot(other)

Compute scalar product along some non-vector dimension.

iszero()

Check if all elements are zero and send result to parent.

class dubfi.linalg.mpi_worker.MpiVectorWorkerZero(shape, start_idx=-1, end_idx=-1)

Bases: MpiVectorWorker

Distributed all-zero vector.

Distributed all-zero AbstractVector.

Parameters:
  • start_idx (int)

  • end_idx (int)

property shape

Shape of this part of the vector.

property data

Shape of this part of the vector.

iszero()

Check if all elements are zero and send result to parent (always true).

tonumpy()

Compute numpy array representation of unbuffered segment and send result to parent.

class dubfi.linalg.mpi_worker.MpiAbstractOperator

Bases: dubfi.linalg.types.AbstractOperator

AbstractOperator that sends reduce operations to MPI parent.

trace()

Compute trace of self and send result to parent.

trace_product(other)

Compute trace of (self @ other) and send result to parent.

sandwich(vec)

Compute vec @ self @ vec and send result to parent.

logdet()

Compute log(det(self)) and send result to parent.

This assumes that self is a positive definite, real-symmetric matrix.

dot_array()

Compute scalar product with array received from parent along some non-vector dimension.

Return type:

dubfi.linalg.types.AbstractOperator

class dubfi.linalg.mpi_worker.MpiDenseBdMatrixWorker

Bases: MpiAbstractOperator

Dense matrix representing part of distributed, approximately diagonal, linear operator.

Dense matrix representation of segment of linear operator.

property shape

Shape of array (matrix) representation of self.

property symmetric

True if self equals its adjoint.

classmethod fromparent()

Construct from parent by receiving all data.

classmethod fromdata(data, other, symmetric=False)

Construct from given data.

Parameters:
  • data (numpy.ndarray)

  • symmetric (bool)

property data

Numpy array representation of self.

iscompatible(other)

Check whether other is an MpiDenseBdMatrixWorker of compatible shape.

Parameters:

other (dubfi.linalg.types.AbstractOperator)

Return type:

bool

apply(vec)

Apply operator on vector: self @ vec.

rapply(vec)

Apply transpose operator on vector: self.T @ vec.

diagonal()

Return diagonal of self as MPI vector worker.

__mul__(other)

Multiply element-wise.

mul_array()

Multiply element-wise by array received from parent.

inv()

Compute (multiplicative) inverse operator.

solve(vec)

Solve linear equation self @ x = vec for x.

_trace()

Compute trace of self.

Return type:

numpy.ndarray | float

_trace_product(other)

Compute trace of (self @ other).

Parameters:

other (dubfi.linalg.types.AbstractOperator)

Return type:

numpy.ndarray

chain(other)

Combine operators: self @ other.

_logdet()

Compute log(det(self)) and send result to parent.

This assumes that self is a positive definite, real-symmetric matrix.

dot(other)

Inner product along non-vector dimensions.

Parameters:

other (numpy.ndarray)

tonumpy()

Compute numpy array (matrix) representation of unbuffered segment and send result to parent.

__add__(other)

Add other operator.

__iadd__(other)

Add other operator in-place.

__sub__(other)

Subtract other operator.

__isub__(other)

Subtract other operator in-place.

_sandwich(vec)

Compute vec @ self @ vec and send result to parent.

class dubfi.linalg.mpi_worker.MpiLinParamVectorWorker

Bases: dubfi.linalg.generic.ParametrizedVector

Part of distributed, linearly parametrize vector.

Worker of distributed linear parametrized vector.

property shape

Shape of array representation of self.

classmethod fromparent()

Create new instance and receive data from parent to initialize it.

__call__(s)

Compute vector at given parameters.

call_mpi()

Compute vector at parameters received from parent.

grad(s)

Compute gradient of vector at given parameters.

grad_mpi()

Compute gradient of vector at parameters received from parent.

hess(s)

Compute Hesse matrix of vector at given parameters.

hess_mpi()

Compute Hesse matrix of vector at parameters received from parent.

class dubfi.linalg.mpi_worker.MpiDensePostRWorker

Bases: dubfi.linalg.generic.ParametrizedOperator

Segment of distributed, parametrized linear operator constructed from flux ensemble.

Dense matrix representation of distributed R matrix.

classmethod fromparent()

Create new instance and receive data from parent to initialize it.

__call__(s)

Compute operator at given parameters.

call_mpi()

Compute operator at parameters received from parent.

grad(s)

Compute gradient of operator at given parameters.

grad_dense(s)

More flexible than grad(), but requires more memory.

Parameters:

s (numpy.ndarray)

Return type:

MpiDenseBdMatrixWorker

grad_mpi()

Compute gradient of operator at parameters received from parent.

hess(s)

Compute Hesse matrix of operator at given parameters.

hess_dense(s)

More flexible than hess(), but requires more memory.

Parameters:

s (numpy.ndarray)

Return type:

MpiDenseBdMatrixWorker

hess_mpi()

Compute Hesse matrix of operator at parameters received from parent.

class dubfi.linalg.mpi_worker.MpiGradOpWorker(other, x, chain=None)

Bases: MpiAbstractOperator

Segment of distributed gradient operator, used for gradient of MpiDensePostRWorker.

Construct auxiliary operator for computing gradient of R matrix.

Define operator G of shape (state, obs, obs) by:

G[s, i, j] = sum  H[m, s, i] X[m, j] L[i, j] + (i <-> j)
              m

if chain is None or:

G[s, i, j] = sum ( H[m, s, i] X[m, k] L[i, k] + (i <-> k) ) C[k, j]
             m,k

where C=chain, X=x, H=other.h_ens, L=other.loc and it is assumed that L == L.T

Parameters:

chain (None | MpiDenseBdMatrixWorker)

property shape

Shape of array (matrix) representation of self.

apply(vec)

Apply operator on vector: self @ vec.

chain(other)

Combine operators: self @ other.

abstractmethod tonumpy()

Numpy array (matrix) representation of self.

abstractmethod solve(vec)

Solve linear equation self @ x = vec for x.

abstractmethod __mul__(other)

Multiply element-wise.

_trace()

Compute trace of self and send result to parent.

_trace_product(other)

Compute trace(self @ other) and send result to parent.

Note: This is usually by far the slowest part of the inversion.

dot(other)

Inner product along non-vector dimensions.

dubfi.linalg.mpi_worker._trace_product_self(s, e, hens, x, loc, chainT)

Compute part of trace product for MpiGradOpWorker (special case).

Helper for MpiGradOpWorker._trace_product() in commonly used case.

Parameters:
  • s (int)

  • e (int)

  • hens (numpy.ndarray)

  • x (numpy.ndarray)

  • loc (numpy.ndarray)

  • chainT (numpy.ndarray)

Return type:

numpy.ndarray

dubfi.linalg.mpi_worker._trace_product_generic(s, e, hens, x, loc, chain, ohens, ox, oloc, ochain)

Compute part of trace product for MpiGradOpWorker (generic).

Helper for MpiGradOpWorker._trace_product() in general case.

Parameters:
  • s (int)

  • e (int)

  • hens (numpy.ndarray)

  • x (numpy.ndarray)

  • loc (numpy.ndarray)

  • chain (numpy.ndarray)

  • ohens (numpy.ndarray)

  • ox (numpy.ndarray)

  • oloc (numpy.ndarray)

  • ochain (numpy.ndarray)

Return type:

numpy.ndarray

class dubfi.linalg.mpi_worker.MpiTraceProdOpWorker(ensemble, loc, other)

Bases: MpiAbstractOperator

Segment of distributed trace product operator.

Linear operator of limited functionality to be used in trace products.

Parameters:
  • ensemble (numpy.ndarray)

  • loc (numpy.ndarray)

property shape

Shape of array (matrix) representation of self.

_trace_product(other)

Compute trace of (self @ other).

_trace()

Compute trace of self.

_sandwich(vec)

Compute vec @ self @ vec.

abstractmethod apply(other)

Apply operator on vector: self @ vec (not implemented).

abstractmethod rapply(other)

Apply transpose operator on vector: self.T @ vec (not implemented).

abstractmethod __mul__(other)

Multiply element-wise (not implemented).

abstractmethod solve(vec)

Solve linear equation self @ x = vec for x (not implemented).

abstractmethod tonumpy()

Numpy array (matrix) representation of self (not implemented).

dot(other)

Inner product along non-vector dimensions.