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

Optimize memory usage in empirical_cross_covariance calculation #214

Merged
merged 1 commit into from
Mar 7, 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/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
iterative_ensemble_smoother documentation build configuration file.
"""

import datetime
import os
import re
Expand Down
1 change: 1 addition & 0 deletions src/iterative_ensemble_smoother/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
steplength_exponential

"""

try:
from ._version import version as __version__
from ._version import version_tuple
Expand Down
10 changes: 9 additions & 1 deletion src/iterative_ensemble_smoother/esmda_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ def empirical_cross_covariance(
X = X - np.mean(X, axis=1, keepdims=True)

# Compute outer product and divide
cov = X @ Y.T / (X.shape[1] - 1)
# If X is a large matrix, it might be stored as a float32 array to save memory.
# However, if Y is of type float64,
# the resulting cross-covariance matrix will be float64,
# potentially doubling the memory usage even if X is float32.
# To prevent unnecessary memory consumption,
# we cast Y to the same data type as X before computing the dot product.
# This ensures that the output cross-covariance matrix uses memory efficiently
# while retaining the precision dictated by X's data type.
cov = X @ Y.astype(X.dtype).T / (X.shape[1] - 1)
assert cov.shape == (X.shape[0], Y.shape[0])
return cov

Expand Down
1 change: 1 addition & 0 deletions src/iterative_ensemble_smoother/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Contains (publicly available, but not officially supported) experimental
features of iterative_ensemble_smoother
"""

import numbers
import warnings
from typing import Callable, List, Optional, Sequence, Tuple, TypeVar, Union
Expand Down
Loading