Skip to content

Commit

Permalink
Avoid eclsum caching, causes segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Feb 8, 2021
1 parent 211c3f2 commit 07c068c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 115 deletions.
34 changes: 11 additions & 23 deletions src/fmu/ensemble/common.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
"""Common functions for fmu.ensemble"""

import os
import sys
import logging

import six

from .etc import Interaction

fmux = Interaction()
logger = fmux.basiclogger(__name__)
logger = logging.getLogger(__name__)

ENV_NAME = "FMU_CONCURRENCY"

Expand All @@ -18,27 +13,20 @@ def use_concurrent():
This is based on both an environment variable
and presence of concurrent.futures, and on Python version
(Py2 deliberately not attempted to support)
Returns:
bool: True if concurrency mode should be used
"""
if six.PY2:
# Py2-support not attempted
return False
if "concurrent.futures" in sys.modules:
if ENV_NAME not in os.environ:
return True
env_var = os.environ[ENV_NAME]
if (
str(env_var) == "0"
or str(env_var).lower() == "false"
or str(env_var).lower() == "no"
):
return False
if ENV_NAME not in os.environ:
return True
# If concurrent.futures is not available to import, we end here.
return False
env_var = os.environ[ENV_NAME]
if (
str(env_var) == "0"
or str(env_var).lower() == "false"
or str(env_var).lower() == "no"
):
return False
return True


def set_concurrent(concurrent):
Expand Down
20 changes: 10 additions & 10 deletions src/fmu/ensemble/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def load_smry(
time_index="raw",
column_keys=None,
stacked=True,
cache_eclsum=True,
cache_eclsum=False,
start_date=None,
end_date=None,
include_restart=True,
Expand Down Expand Up @@ -828,7 +828,7 @@ def load_smry(
"load_smry": {
"column_keys": column_keys,
"time_index": time_index,
"cache_eclsum": cache_eclsum,
"cache_eclsum": False,
"start_date": start_date,
"end_date": end_date,
"include_restart": include_restart,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ def get_smry_dates(
normalize=True,
start_date=None,
end_date=None,
cache_eclsum=True,
cache_eclsum=False,
include_restart=True,
):
"""Return list of datetimes for an ensemble according to frequency
Expand Down Expand Up @@ -1111,11 +1111,11 @@ def get_smry_dates(
eclsumsdates = []
for _, realization in self.realizations.items():
if realization.get_eclsum(
cache=cache_eclsum, include_restart=include_restart
cache=False, include_restart=include_restart
):
eclsumsdates.append(
realization.get_eclsum(
cache=cache_eclsum, include_restart=include_restart
cache=False, include_restart=include_restart
).dates
)
return unionize_smry_dates(eclsumsdates, freq, normalize, start_date, end_date)
Expand All @@ -1125,7 +1125,7 @@ def get_smry_stats(
column_keys=None,
time_index="monthly",
quantiles=None,
cache_eclsum=True,
cache_eclsum=False,
start_date=None,
end_date=None,
):
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def get_smry_stats(
dframe = self.get_smry(
time_index=time_index,
column_keys=column_keys,
cache_eclsum=cache_eclsum,
cache_eclsum=False,
start_date=start_date,
end_date=end_date,
)
Expand Down Expand Up @@ -1434,7 +1434,7 @@ def get_smry(
self,
time_index=None,
column_keys=None,
cache_eclsum=True,
cache_eclsum=False,
start_date=None,
end_date=None,
include_restart=True,
Expand Down Expand Up @@ -1497,7 +1497,7 @@ def get_smry(
realization.get_smry,
time_index=time_index,
column_keys=column_keys,
cache_eclsum=cache_eclsum,
cache_eclsum=False,
include_restart=include_restart,
)
for realization in self.realizations.values()
Expand All @@ -1515,7 +1515,7 @@ def get_smry(
realization.get_smry(
time_index=time_index,
column_keys=column_keys,
cache_eclsum=cache_eclsum,
cache_eclsum=False,
include_restart=include_restart,
)
.assign(REAL=realidx)
Expand Down
2 changes: 1 addition & 1 deletion src/fmu/ensemble/realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def get_smry(
self,
time_index=None,
column_keys=None,
cache_eclsum=True,
cache_eclsum=False,
start_date=None,
end_date=None,
include_restart=True,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def test_batch():
ens = ScratchEnsemble(
"reektest", testdir + "/data/testensemble-reek001/" + "realization-*/iter-0"
)

ens.process_batch(
batch=[
{"load_scalar": {"localpath": "npv.txt"}},
{"load_smry": {"column_keys": "FOPT", "time_index": "yearly"}},
{"load_smry": {"column_keys": "*", "time_index": "daily"}},
]
)

assert len(ens.get_df("npv.txt")) == 5
assert len(ens.get_df("unsmry--daily")["FOPR"]) == 5490
assert len(ens.get_df("unsmry--yearly")["FOPT"]) == 25
Expand Down
54 changes: 0 additions & 54 deletions tests/test_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .test_ensembleset import symlink_iter

from fmu.ensemble import ScratchEnsemble, ScratchRealization
from fmu.ensemble.common import use_concurrent


try:
Expand Down Expand Up @@ -842,59 +841,6 @@ def test_nonexisting():
assert not nopermission


def test_eclsumcaching():
"""Test caching of eclsum, but only if we don't use concurrency"""

if use_concurrent():
pytest.skip("Not testing caching when we use concurrency")

if "__file__" in globals():
# Easen up copying test code into interactive sessions
testdir = os.path.dirname(os.path.abspath(__file__))
else:
testdir = os.path.abspath(".")

dirs = testdir + "/data/testensemble-reek001/" + "realization-*/iter-0"
ens = ScratchEnsemble("reektest", dirs)

# The problem here is if you load in a lot of UNSMRY files
# and the Python process keeps them in memory. Not sure
# how to check in code that an object has been garbage collected
# but for garbage collection to work, at least the realization
# _eclsum variable must be None.

ens.load_smry()
# Default is to do caching, so these will not be None:
assert all([x._eclsum for (idx, x) in ens.realizations.items()])

# If we redo this operation, the same objects should all
# be None afterwards:
ens.load_smry(cache_eclsum=None)
assert not any([x._eclsum for (idx, x) in ens.realizations.items()])

ens.get_smry()
assert all([x._eclsum for (idx, x) in ens.realizations.items()])

ens.get_smry(cache_eclsum=False)
assert not any([x._eclsum for (idx, x) in ens.realizations.items()])

ens.get_smry_stats()
assert all([x._eclsum for (idx, x) in ens.realizations.items()])

ens.get_smry_stats(cache_eclsum=False)
assert not any([x._eclsum for (idx, x) in ens.realizations.items()])

ens.get_smry_dates()
assert all([x._eclsum for (idx, x) in ens.realizations.items()])

# Clear the cached objects because the statement above has cached it..
for _, realization in ens.realizations.items():
realization._eclsum = None

ens.get_smry_dates(cache_eclsum=False)
assert not any([x._eclsum for (idx, x) in ens.realizations.items()])


def test_filedescriptors():
"""Test how filedescriptors are used.
Expand Down
27 changes: 0 additions & 27 deletions tests/test_etc.py

This file was deleted.

0 comments on commit 07c068c

Please sign in to comment.