This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
[WIP] Libnethack shared #140
Draft
tscmoo
wants to merge
14
commits into
main
Choose a base branch
from
libnethack-shared
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c289976
wip shared
tscmoo 7eaffe5
stash
tscmoo 80f59d5
clean up
tscmoo 908f61f
fix non-shared path
tscmoo 7fa21bb
load DT_NEEDED libraries
tscmoo bd5cebe
Disable rename & unlink
tscmoo a9e1897
cmake cleanup
tscmoo 37c45c5
Some abstractin' and refactorin'
tscmoo 2c281fc
Some more
tscmoo 60a62c9
Throw error library could not be opened
tscmoo 2de2491
clang-format
tscmoo 8a0c39e
clang-format
tscmoo cecde27
black
tscmoo a77cb20
test_nethack: update so it works with shared libnethack
tscmoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,13 +75,13 @@ def _set_env_vars(options, hackdir, wizkit=None): | |
if wizkit is not None: | ||
os.environ["WIZKIT"] = wizkit | ||
|
||
_nhinstances = 0 | ||
|
||
# TODO: Not thread-safe for many reasons. | ||
# TODO: On Linux, we could use dlmopen to use different linker namespaces, | ||
# which should allow several instances of this. On MacOS, that seems | ||
# a tough call. | ||
class Nethack: | ||
_instances = 0 | ||
|
||
def __init__( | ||
self, | ||
|
@@ -93,34 +93,56 @@ def __init__( | |
wizard=False, | ||
hackdir=HACKDIR, | ||
): | ||
global _nhinstances | ||
self._copy = copy | ||
|
||
_nhinstances = _nhinstances + 1 | ||
|
||
if not os.path.exists(hackdir) or not os.path.exists( | ||
os.path.join(hackdir, "sysconf") | ||
): | ||
raise FileNotFoundError( | ||
"Couldn't find NetHack installation at '%s'." % hackdir | ||
) | ||
|
||
# Create a HACKDIR for us. | ||
self._tempdir = tempfile.TemporaryDirectory(prefix="nle") | ||
self._vardir = self._tempdir.name | ||
|
||
# Save cwd and restore later. Currently libnethack changes | ||
# directory on loading. | ||
self._oldcwd = os.getcwd() | ||
self.shared = False; | ||
|
||
# Symlink a few files. | ||
for fn in ["nhdat", "sysconf"]: | ||
os.symlink(os.path.join(hackdir, fn), os.path.join(self._vardir, fn)) | ||
# Touch a few files. | ||
for fn in ["perm", "logfile", "xlogfile"]: | ||
os.close(os.open(os.path.join(self._vardir, fn), os.O_CREAT)) | ||
os.mkdir(os.path.join(self._vardir, "save")) | ||
if _pynethack.supports_shared(): | ||
# "shared" mode does some hacky things to enable using a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mix of 2-letter and 4-letter indentation is weeeiiiirdd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. I apologize. I come from a world of 2 indentation. Formatting will be cleaned up. |
||
# shared libnethack.so, prevents writing to any files, and does | ||
# not chdir. | ||
self.shared = True | ||
dlpath = DLPATH | ||
self._hackdir = hackdir | ||
else: | ||
|
||
# Hacky AF: Copy our so into this directory to load several copies ... | ||
dlpath = os.path.join(self._vardir, "libnethack.so") | ||
shutil.copyfile(DLPATH, dlpath) | ||
# Create a HACKDIR for us. | ||
if os.getenv("SLURM_JOBID"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This slurm stuff probably doesn't belong here and I'll most likely remove it from this PR |
||
self._tempdir = None | ||
self._vardir = "/scratch/slurm_tmpdir/%s/nle-%d-%d" % (os.getenv("SLURM_JOBID"), os.getpid(), _nhinstances) | ||
os.mkdir(self._vardir) | ||
else: | ||
self._tempdir = tempfile.TemporaryDirectory(prefix="nle") | ||
self._vardir = self._tempdir.name | ||
print("_vardir is ", self._vardir) | ||
|
||
self._hackdir = self._vardir | ||
|
||
# Save cwd and restore later. Currently libnethack changes | ||
# directory on loading. | ||
self._oldcwd = os.getcwd() | ||
|
||
# Symlink a few files. | ||
for fn in ["nhdat", "sysconf"]: | ||
os.symlink(os.path.join(hackdir, fn), os.path.join(self._vardir, fn)) | ||
# Touch a few files. | ||
for fn in ["perm", "logfile", "xlogfile"]: | ||
os.close(os.open(os.path.join(self._vardir, fn), os.O_CREAT)) | ||
os.mkdir(os.path.join(self._vardir, "save")) | ||
|
||
# Hacky AF: Copy our so into this directory to load several copies ... | ||
dlpath = os.path.join(self._vardir, "libnethack.so") | ||
shutil.copyfile(DLPATH, dlpath) | ||
|
||
if options is None: | ||
options = NETHACKOPTIONS | ||
|
@@ -129,10 +151,10 @@ def __init__( | |
self._options.append("playmode:debug") | ||
self._wizard = wizard | ||
|
||
_set_env_vars(self._options, self._vardir) | ||
_set_env_vars(self._options, self._hackdir) | ||
self._ttyrec = ttyrec | ||
|
||
self._pynethack = _pynethack.Nethack(dlpath, ttyrec) | ||
self._pynethack = _pynethack.Nethack(dlpath, ttyrec, self.shared) | ||
|
||
self._obs_buffers = {} | ||
|
||
|
@@ -154,6 +176,8 @@ def step(self, action): | |
return self._step_return(), self._pynethack.done() | ||
|
||
def _write_wizkit_file(self, wizkit_items): | ||
if self._vardir is None: | ||
raise RuntimeError("FIXME: shared wizkit: can't write to HACKDIR as it is a shared directory") | ||
heiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO ideally we need to check the validity of the requested items | ||
with open(os.path.join(self._vardir, WIZKIT_FNAME), "w") as f: | ||
for item in wizkit_items: | ||
|
@@ -164,9 +188,9 @@ def reset(self, new_ttyrec=None, wizkit_items=None): | |
if not self._wizard: | ||
raise ValueError("Set wizard=True to use the wizkit option.") | ||
self._write_wizkit_file(wizkit_items) | ||
_set_env_vars(self._options, self._vardir, wizkit=WIZKIT_FNAME) | ||
_set_env_vars(self._options, self._hackdir, wizkit=WIZKIT_FNAME) | ||
else: | ||
_set_env_vars(self._options, self._vardir) | ||
_set_env_vars(self._options, self._hackdir) | ||
if new_ttyrec is None: | ||
self._pynethack.reset() | ||
else: | ||
|
@@ -178,11 +202,13 @@ def reset(self, new_ttyrec=None, wizkit_items=None): | |
|
||
def close(self): | ||
self._pynethack.close() | ||
try: | ||
os.chdir(self._oldcwd) | ||
except IOError: | ||
os.chdir(os.path.dirname(os.path.realpath(__file__))) | ||
self._tempdir.cleanup() | ||
if not self.shared: | ||
try: | ||
os.chdir(self._oldcwd) | ||
except IOError: | ||
os.chdir(os.path.dirname(os.path.realpath(__file__))) | ||
if self._tempdir is not None: | ||
self._tempdir.cleanup() | ||
|
||
def set_initial_seeds(self, core, disp, reseed=False): | ||
self._pynethack.set_initial_seeds(core, disp, reseed) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably update some of these comments when we merge this.