Skip to content

Commit

Permalink
Make sure Context does not contain an active session when it shouldn't.
Browse files Browse the repository at this point in the history
Referemce kassonlab#214

* Add `gmx.core.has_feature` binding to `gmxapi::Version::hasFeature`
* Check whether workaround is necessary for bug kassonlab#214
* Reassign a new `gmx.core.Context` to `gmx.context.Context._api_object`
  if bugfix not reported present in libgmxapi
  • Loading branch information
eirrgang committed Dec 14, 2018
1 parent baa80e4 commit 7dafc56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/gmx/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from gmx import exceptions
from gmx import logging
from gmx import status
import gmx.core as gmxapi


# Module-level logger
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -71,7 +73,6 @@ def _md(context, element):
Returns:
A Director that the Context can use in launching the Session.
"""
import gmx.core
class Builder(object):
"""Translate md work element to a node in the session's DAG."""
def __init__(self, element):
Expand Down Expand Up @@ -123,17 +124,17 @@ def launch(rank=None):
# altered input.
_, temp_filename = tempfile.mkstemp(suffix='.tpr')
logger.debug('Updating input. Using temp file {}'.format(temp_filename))
gmx.core.copy_tprfile(source=infile[rank],
gmxapi.copy_tprfile(source=infile[rank],
destination=temp_filename,
end_time=self.runtime_params['end_time'])
tpr_file = temp_filename
else:
tpr_file = infile[rank]

logger.info('Loading TPR file: {}'.format(tpr_file))
system = gmx.core.from_tpr(tpr_file)
system = gmxapi.from_tpr(tpr_file)
dag.nodes[name]['system'] = system
mdargs = gmx.core.MDArgs()
mdargs = gmxapi.MDArgs()
mdargs.set(self.runtime_params)
# Workaround to give access to plugin potentials used in a context.
pycontext = element.workspec._context
Expand Down Expand Up @@ -664,7 +665,6 @@ def __init__(self, work=None, workdir_list=None, communicator=None):

# self.__context_array = list([Context(work_element) for work_element in work])
from gmx.workflow import WorkSpec
import gmx.core

# Until better Session abstraction exists at the Python level, a
# _session_communicator attribute will be added to and removed from the
Expand Down Expand Up @@ -723,7 +723,7 @@ def __init__(self, work=None, workdir_list=None, communicator=None):
# This setter must be called after the operations map has been populated.
self.work = work

self._api_object = gmx.core.Context()
self._api_object = gmxapi.Context()

@property
def work(self):
Expand Down Expand Up @@ -996,7 +996,9 @@ def __enter__(self):
if not runner is None:
runners.append(runner)
closers.append(graph.nodes[name]['close'])

# Get a session object to return. It must simply provide a `run()` function.
context = self # Part of workaround for bug gmxapi-214
class Session(object):
def __init__(self, runners, closers):
self.runners = list(runners)
Expand All @@ -1012,22 +1014,33 @@ def run(self):
to_be_deleted.insert(0, i)
for i in to_be_deleted:
del self.runners[i]
return True

def close(self):
for close in self.closers:
logger.debug("Closing node: {}".format(close))
close()
# Workaround for bug gmxapi-214
if not gmxapi.has_feature('0.0.7-bugfix-https://github.com/kassonlab/gmxapi/issues/214'):
context._api_object = gmxapi.Context()


self._session = Session(runners, closers)
else:
logger.info("Context rank {} has no work to do".format(self.rank))

context = self # Part of workaround for bug gmxapi-214
class NullSession(object):
def run(self):
logger.info("Running null session on rank {}.".format(self.rank))
return status.Status()
def close(self):
logger.info("Closing null session.")
# Workaround for bug gmxapi-214
if not gmxapi.has_feature('0.0.7-bugfix-https://github.com/kassonlab/gmxapi/issues/214'):
context._api_object = gmxapi.Context()
return

self._session = NullSession()
self._session.rank = self.rank

Expand Down
4 changes: 4 additions & 0 deletions src/gmx/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "tprfile.h"
#include "gmxapi/status.h"
#include "gmxapi/version.h"

#include "pybind11/pybind11.h"

Expand Down Expand Up @@ -69,6 +70,9 @@ PYBIND11_MODULE(core, m) {
m.doc() = docstring;

// Export core bindings

m.def("has_feature", &gmxapi::Version::hasFeature, "Check the gmxapi library for a named feature.");

py::class_< ::gmxapi::Status > gmx_status(m, "Status", "Holds status for API operations.");


Expand Down

0 comments on commit 7dafc56

Please sign in to comment.