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

Discover backends from entry points. #986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion nengo_gui/components/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def add_nengo_objects(self, page):
if Process.__module__ == "nengo_gui.components.slider":
self.node.output = self.override_output.make_step(
shape_in=None, shape_out=self.node.size_out, dt=None, rng=None)
elif page.settings.backend == 'nengo_spinnaker':
elif page.settings.backend == 'spinnaker':
# TODO: this should happen for any backend that does not support
# Processes
self.node.output = self.override_output.make_step(
Expand Down
31 changes: 21 additions & 10 deletions nengo_gui/exec_env.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import contextlib
import importlib
import os
from pkg_resources import iter_entry_points
import threading
import traceback
import sys
from nengo.utils.compat import StringIO


# list of Simulators to check for
known_modules = ['nengo', 'nengo_ocl', 'nengo_distilled',
'nengo_dl', 'nengo_mpi',
'nengo_brainstorm', 'nengo_spinnaker']
known_modules = ['reference', 'ocl', 'distilled', 'dl', 'mpi', 'brainstorm',
'spinnaker']


def discover_backends():
found_modules = {}
found_modules = {
ep.name: ep.load for ep in iter_entry_points(group='nengo.backends')}

for name in known_modules:
if name == 'reference':
prefixed_name = 'nengo'
else:
prefixed_name = 'nengo_' + name
if prefixed_name in found_modules:
continue
try:
mod = importlib.import_module(name)
except Exception as e:
# TODO only ignore ImportErrors "No module named ...", display
# other errors to the user as they might help debugging broken
# backend installations
continue
found_modules[name] = mod
found_modules[name] = lambda: mod.Simulator
return found_modules


Expand Down Expand Up @@ -107,13 +115,16 @@ def __enter__(self):
sys.stdout = self.stdout

if not self.allow_sim:
for mod in discover_backends().values():
self.simulators[mod] = mod.Simulator
mod.Simulator = make_dummy(mod.Simulator)
for load in discover_backends().values():
Simulator = load()
mod = importlib.import_module(Simulator.__module__)
name = Simulator.__name__
self.simulators[(mod, name)] = Simulator
setattr(mod, name, make_dummy(Simulator))

def __exit__(self, exc_type, exc_value, traceback):
for mod, cls in self.simulators.items():
mod.Simulator = cls
for (mod, name), cls in self.simulators.items():
setattr(mod, name, cls)
flag.executing = False

sys.stdout = sys.__stdout__
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def main():
'--debug', action='store_true', help='turn on debug logging')
parser.add_argument(
'-b', '--backend', metavar='BACKEND',
default='nengo', type=str, help='default backend to use')
default='reference', type=str, help='default backend to use')
parser.add_argument('--browser', dest='browser', type=str,
metavar='BROWSER', default=True,
help=browser_help)
Expand Down
11 changes: 6 additions & 5 deletions nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Page(object):
"""

# Some Simulators can only have one instance running at a time
singleton_sims = dict(nengo_spinnaker=None)
singleton_sims = dict(spinnaker=None)

def __init__(self, gui, filename, settings, reset_cfg=False):
self.gui = gui
Expand Down Expand Up @@ -458,7 +458,8 @@ def build(self):
c.add_nengo_objects(self)

# determine the backend to use
backend = importlib.import_module(self.settings.backend)
Simulator = nengo_gui.exec_env.discover_backends()[
self.settings.backend]()
# if only one Simulator is allowed at a time, finish the old one
old_sim = Page.singleton_sims.get(self.settings.backend, None)
if old_sim is not None and old_sim is not self:
Expand All @@ -468,15 +469,15 @@ def build(self):
exec_env = nengo_gui.exec_env.ExecutionEnvironment(self.filename,
allow_sim=True)
handles_progress = ('progress_bar' in
inspect.getargspec(backend.Simulator.__init__).args)
inspect.getargspec(Simulator.__init__).args)
# build the simulation
try:
with exec_env:
if handles_progress:
self.sim = backend.Simulator(
self.sim = Simulator(
self.model, progress_bar=self.locals['_viz_progress'])
else:
self.sim = backend.Simulator(self.model)
self.sim = Simulator(self.model)

except:
line = nengo_gui.exec_env.determine_line_number()
Expand Down