Search in sources :

Example 1 with Backend

use of gov.sandia.n2a.plugins.extpoints.Backend in project n2a by frothga.

the class UIController method prepareAndSubmitRunEnsemble.

public boolean prepareAndSubmitRunEnsemble(Component parentComponent, MNode model) throws Exception {
    // Set up simulators.
    List<ExtensionPoint> simEP = PluginManager.getExtensionsForPoint(Backend.class);
    Backend[] simulators = new Backend[simEP.size()];
    int s = 0;
    for (ExtensionPoint ep : simEP) {
        simulators[s++] = (Backend) ep;
    }
    // Set up execution environments.
    // Hack to suppress errors in this outdated code.
    HostSystem[] envs = new HostSystem[0];
    // TODO: Fix this with appropriate interfaces.
    String name = (String) ReflectionUtil.invoke("getName", model);
    String owner = (String) ReflectionUtil.invoke("getOwner", model);
    CreateRunEnsembleDialog dlg = new CreateRunEnsembleDialog((JFrame) SwingUtilities.getRoot(parentComponent), // TODO change from -1
    -1, /*TEMP*/
    name, owner, 12342347483L, /*TEMP until appropriate interfaces*/
    model, simulators, simulators[0], envs, HostSystem.get("localhost"), false);
    dlg.setVisible(true);
    if (dlg.getResult() == CreateRunEnsembleDialog.CREATE) {
        String label = dlg.getLabel();
        HostSystem env = dlg.getEnvironment();
        Backend simulator = dlg.getSimulator();
        ParameterSpecGroupSet groups = dlg.getParameterSpecGroupSet();
        ParameterSpecGroupSet simHandledGroups;
        try {
            // next line modifies groups to remove any that the Simulator will handle
            simHandledGroups = divideEnsembleParams(model, groups, simulator);
        } catch (RuntimeException e) {
            Dialogs.showDetails(MainFrame.instance, "could not create run ensemble", e);
            return false;
        }
        List<String> outputExpressions = dlg.getSelectedOutputExpressions();
        /*            
            int runNum = 0;
            for(ParameterSet set : groups.generateAllSetsFromSpecs(false)) {
                ParameterSet modelParamSet = set.subset("Model");
                ParameterSet simParamSet = set.subset("Simulator");
                modelParamSet.sliceKeyPathKeys();
                simParamSet.sliceKeyPathKeys();

                Run run = model.addRun(modelParamSet, re);
                re.addRun(run);

                Simulation simulation = simulator.createSimulation();
                ParameterDomain domain = new ParameterDomain(simParamSet);
                simulation.setSelectedParameters(domain);
                try {
                    RunState runState;
                    logger.debug(System.currentTimeMillis() + " before execute for run " +
                            runNum++);
                    // quick and dirty attempt at batch script version of doing ensemble
                    runState = simulation.prepare(run, simHandledGroups, env);
//                    runState = simulation.execute(run, simHandledGroups, env);
                    run.setState(XStreamWrapper.writeToString(runState));
                    run.save();
                } catch(Exception e1) {
                    UMF.handleUnexpectedError(null, e1, "Could not create the run.  An error occurred.");
                    return false;
                }
            }
            env.submitBatch(re);
 */
        return true;
    }
    return false;
}
Also used : Backend(gov.sandia.n2a.plugins.extpoints.Backend) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) CreateRunEnsembleDialog(gov.sandia.umf.platform.ui.ensemble.run.CreateRunEnsembleDialog) HostSystem(gov.sandia.n2a.execenvs.HostSystem) ParameterSpecGroupSet(gov.sandia.umf.platform.ensemble.params.groupset.ParameterSpecGroupSet) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint)

Example 2 with Backend

use of gov.sandia.n2a.plugins.extpoints.Backend in project n2a by frothga.

the class NodeJob method monitorProgress.

public synchronized void monitorProgress() {
    if (deleted)
        return;
    if (complete >= 1 && complete != 3)
        return;
    // Limit monitoring to no more than once per second.
    long elapsed = System.currentTimeMillis() - lastMonitored;
    long wait = 1000 - elapsed;
    if (wait > 0) {
        try {
            Thread.sleep(wait);
        } catch (InterruptedException e) {
        }
    }
    lastMonitored = System.currentTimeMillis();
    float oldComplete = complete;
    MNode source = getSource();
    Host env = Host.get(source);
    Path localJobDir = Host.getJobDir(Host.getLocalResourceDir(), source);
    // If job is remote, attempt to grab its state files.
    // TODO: handle remote jobs waiting in queue. Plan is to update "started" file with queue status.
    Path finished = localJobDir.resolve("finished");
    if (!Files.exists(finished) && env instanceof Remote) {
        @SuppressWarnings("resource") Remote remote = (Remote) env;
        if (remote.isConnected() || remote.isEnabled()) {
            try {
                Path remoteJobDir = Host.getJobDir(env.getResourceDir(), source);
                Path remoteFinished = remoteJobDir.resolve("finished");
                // throws an exception if the remote file does not exist
                Files.copy(remoteFinished, finished);
            } catch (Exception e) {
            }
        }
    }
    if (complete == -1) {
        Path started = localJobDir.resolve("started");
        if (Files.exists(started)) {
            complete = 0;
            dateStarted = new Date(Host.lastModified(started));
            lastActive = dateStarted.getTime();
        }
    }
    Backend simulator = Backend.getBackend(source.get("backend"));
    if (complete >= 0 && complete < 1) {
        float percentDone = 0;
        if (expectedSimTime == 0)
            expectedSimTime = new UnitValue(source.get("duration")).get();
        if (expectedSimTime > 0)
            percentDone = (float) (simulator.currentSimTime(source) / expectedSimTime);
        if (Files.exists(finished)) {
            checkFinished(finished);
        } else {
            try {
                long currentTime = System.currentTimeMillis();
                if (simulator.isActive(source)) {
                    lastActive = currentTime;
                } else if (currentTime - lastActive > activeTimeout) {
                    if (percentDone < 1) {
                        // Give it up for dead.
                        Files.copy(new ByteArrayInputStream("dead".getBytes("UTF-8")), finished);
                        complete = 4;
                    } else // Job appears to be actually finished, even though "finished" hasn't been written yet.
                    {
                        // Fake the "finished" file. This might be overwritten later by the batch process.
                        // Most likely, it will also report that we succeeded, so presume that things are fine.
                        Files.copy(new ByteArrayInputStream("success".getBytes("UTF-8")), finished);
                        complete = 1;
                    }
                }
            } catch (Exception e) {
            }
        }
        if (complete >= 0 && complete < 1)
            complete = Math.min(0.99999f, percentDone);
    }
    if (complete == 3) {
        // Check if process is still lingering
        if (!simulator.isActive(source))
            complete = 4;
    }
    PanelRun panelRun = PanelRun.instance;
    PanelStudy panelStudy = PanelStudy.instance;
    // Probably running headless, so skip all UI updates.
    if (panelRun == null)
        return;
    if (complete != oldComplete) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                panelRun.model.nodeChanged(NodeJob.this);
                if (panelRun.displayNode == NodeJob.this) {
                    panelRun.buttonStop.setEnabled(complete < 1 || complete == 3);
                    panelRun.viewJob(true);
                } else if (panelRun.displayNode instanceof NodeFile && panelRun.displayNode.getParent() == NodeJob.this) {
                    panelRun.buttonStop.setEnabled(complete < 1 || complete == 3);
                    // Update the display every 5 seconds during the run.
                    // Some displays, such as a chart, could take longer than 5s to construct, so don't interrupt those.
                    // Always update the display when a run finishes.
                    long currentTime = System.currentTimeMillis();
                    if (complete >= 1 && complete != 3 || panelRun.displayThread == null && currentTime - lastDisplay > 5000) {
                        lastDisplay = currentTime;
                        panelRun.viewFile(true);
                    }
                }
                // panelStudy could be null for a brief moment during startup
                if (panelStudy != null)
                    panelStudy.tableSamples.updateJob(key);
            }
        });
    }
    if (!panelRun.tree.isCollapsed(new TreePath(getPath())))
        build(panelRun.tree);
}
Also used : Path(java.nio.file.Path) TreePath(javax.swing.tree.TreePath) Remote(gov.sandia.n2a.host.Remote) UnitValue(gov.sandia.n2a.language.UnitValue) Host(gov.sandia.n2a.host.Host) MNode(gov.sandia.n2a.db.MNode) IOException(java.io.IOException) Date(java.util.Date) PanelStudy(gov.sandia.n2a.ui.studies.PanelStudy) Backend(gov.sandia.n2a.plugins.extpoints.Backend) TreePath(javax.swing.tree.TreePath) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with Backend

use of gov.sandia.n2a.plugins.extpoints.Backend in project n2a by frothga.

the class PanelEquations method launchJob.

public void launchJob() {
    if (record == null)
        return;
    prepareForTabChange();
    String jobKey = new SimpleDateFormat("yyyy-MM-dd-HHmmss", Locale.ROOT).format(new Date());
    MDoc job = (MDoc) AppData.runs.childOrCreate(jobKey);
    NodeJob.collectJobParameters(root.source, record.key(), job);
    // Force directory (and job file) to exist, so Backends can work with the dir.
    job.save();
    NodeJob.saveSnapshot(record, job);
    MainTabbedPane mtp = (MainTabbedPane) MainFrame.instance.tabs;
    mtp.setPreferredFocus(PanelRun.instance, PanelRun.instance.tree);
    mtp.selectTab("Runs");
    NodeJob node = PanelRun.instance.addNewRun(job, true);
    // Hack to allow local jobs to bypass the wait-for-host queue.
    // It would be better for all jobs to check for resources before starting.
    // However, the time cost for the local check could be as long as the job itself
    // (for very simple models). There is some expectation that the user knows
    // the state of their own system when they choose to hit the play button.
    Backend backend = Backend.getBackend(job.get("backend"));
    String backendName = backend.getName().toLowerCase();
    Host h = Host.get(job);
    boolean internal = backend instanceof InternalBackend;
    boolean localhost = !(h instanceof Remote);
    boolean forbidden = h.config.get("backend", backendName).equals("0");
    if (// use of Internal overrides host selection
    internal || (localhost && !forbidden)) {
        // In case it was "internal" but not "localhost", set host to correct value.
        job.set("localhost", "host");
        backend.start(job);
        h.monitor(node);
        return;
    }
    Host.waitForHost(node);
}
Also used : Backend(gov.sandia.n2a.plugins.extpoints.Backend) InternalBackend(gov.sandia.n2a.backend.internal.InternalBackend) MainTabbedPane(gov.sandia.n2a.ui.MainTabbedPane) Remote(gov.sandia.n2a.host.Remote) Host(gov.sandia.n2a.host.Host) NodeJob(gov.sandia.n2a.ui.jobs.NodeJob) InternalBackend(gov.sandia.n2a.backend.internal.InternalBackend) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) MDoc(gov.sandia.n2a.db.MDoc)

Example 4 with Backend

use of gov.sandia.n2a.plugins.extpoints.Backend in project n2a by frothga.

the class Main method runHeadless.

/**
 *        Assumes this app was started solely for the purpose of running one specific job.
 *        This job operates outside the normal job management. The user is responsible
 *        for everything, including load balancing, directory and file management.
 *        Jobs can run remotely, but there is no support for retrieving results.
 */
public static void runHeadless(MNode record) {
    // See PanelEquations.launchJob()
    // Use current working directory, on assumption that's what the caller wants.
    Path jobDir = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
    // This allows a remote job to run in the regular jobs directory there.
    String jobKey = new SimpleDateFormat("yyyy-MM-dd-HHmmss", Locale.ROOT).format(new Date());
    // Make this appear as if it is from the jobs collection.
    MDoc job = new MDoc(jobDir.resolve("job"), jobKey);
    String key = record.key();
    MNode doc = AppData.models.childOrEmpty(key);
    record.mergeUnder(doc);
    // TODO: the only reason to collate here is to ensure that host and backend are correctly identified if they are inherited. Need a more efficient method, such as lazy collation in MPart.
    MPart collated = new MPart(record);
    NodeJob.collectJobParameters(collated, key, job);
    NodeJob.saveSnapshot(record, job);
    // Handle remote host
    // If a remote host is used, it must be specified exactly, rather than a list of possibilities.
    Host host = Host.get(job);
    if (// Need to note the key so user can easily find the remote job directory.
    host instanceof Remote) {
        job.set(jobKey, "remoteKey");
        job.save();
    }
    // Start the job.
    Backend backend = Backend.getBackend(job.get("backend"));
    backend.start(job);
    // Wait for completion
    NodeJob node = new NodeJobHeadless(job);
    while (node.complete < 1) node.monitorProgress();
    // Convert to CSV, if requested.
    if (record.getFlag("$metadata", "csv")) {
        Table table = new Table(jobDir.resolve("out"), false);
        try {
            table.dumpCSV(jobDir.resolve("out.csv"));
        } catch (IOException e) {
        }
    }
    // Extract results requested in ASV
    MNode ASV = record.child("$metadata", "dakota", "ASV");
    // nothing more to do
    if (ASV == null)
        return;
    OutputParser output = new OutputParser();
    output.parse(jobDir.resolve("out"));
    try (BufferedWriter writer = Files.newBufferedWriter(jobDir.resolve("results"))) {
        for (MNode o : ASV) {
            String name = o.get();
            Column c = output.getColumn(name);
            float value = 0;
            if (c != null && !c.values.isEmpty())
                value = c.values.get(c.values.size() - 1);
            writer.write(value + " " + name);
        }
    } catch (IOException e) {
    }
}
Also used : Path(java.nio.file.Path) MPart(gov.sandia.n2a.eqset.MPart) Table(gov.sandia.n2a.ui.jobs.Table) Remote(gov.sandia.n2a.host.Remote) Host(gov.sandia.n2a.host.Host) IOException(java.io.IOException) MNode(gov.sandia.n2a.db.MNode) Date(java.util.Date) MDoc(gov.sandia.n2a.db.MDoc) BufferedWriter(java.io.BufferedWriter) Backend(gov.sandia.n2a.plugins.extpoints.Backend) Column(gov.sandia.n2a.ui.jobs.OutputParser.Column) NodeJob(gov.sandia.n2a.ui.jobs.NodeJob) OutputParser(gov.sandia.n2a.ui.jobs.OutputParser) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with Backend

use of gov.sandia.n2a.plugins.extpoints.Backend in project n2a by frothga.

the class NodeJob method monitorProgress.

public void monitorProgress(final PanelRun panel) {
    if (complete >= 1)
        return;
    float oldComplete = complete;
    File path = new File(source.get()).getParentFile();
    if (complete == -1) {
        File started = new File(path, "started");
        if (started.exists()) {
            complete = 0;
            dateStarted = new Date(started.lastModified());
        }
    }
    if (complete < 1) {
        File finished = new File(path, "finished");
        if (finished.exists()) {
            dateFinished = new Date(finished.lastModified());
            String line = null;
            try {
                BufferedReader reader = new BufferedReader(new FileReader(finished));
                line = reader.readLine();
                reader.close();
            } catch (IOException e) {
            }
            if (line == null)
                line = "";
            if (line.length() >= 6 || Duration.between(dateFinished.toInstant(), Instant.now()).abs().getSeconds() > 10) {
                if (line.equals("success"))
                    complete = 1;
                else if (line.equals("killed"))
                    complete = 3;
                else
                    // includes "failure", "", and any other unknown string
                    complete = 2;
            }
        }
    }
    if (complete >= 0 && complete < 1) {
        if (expectedSimTime == 0)
            expectedSimTime = source.getOrDefaultDouble("$metadata", "duration", "0");
        if (expectedSimTime > 0) {
            Backend simulator = Backend.getBackend(source.get("$metadata", "backend"));
            double t = simulator.currentSimTime(source);
            if (t != 0)
                complete = Math.min(0.99999f, (float) (t / expectedSimTime));
        }
    }
    if (complete != oldComplete) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                if (panel.displayNode == NodeJob.this)
                    panel.viewJob();
                panel.model.nodeChanged(NodeJob.this);
                panel.tree.paintImmediately(panel.tree.getPathBounds(new TreePath(NodeJob.this.getPath())));
            }
        });
    }
    if (!panel.tree.isCollapsed(new TreePath(getPath())))
        build(panel.tree);
}
Also used : Backend(gov.sandia.n2a.plugins.extpoints.Backend) TreePath(javax.swing.tree.TreePath) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Aggregations

Backend (gov.sandia.n2a.plugins.extpoints.Backend)6 Date (java.util.Date)4 Host (gov.sandia.n2a.host.Host)3 Remote (gov.sandia.n2a.host.Remote)3 IOException (java.io.IOException)3 MDoc (gov.sandia.n2a.db.MDoc)2 MNode (gov.sandia.n2a.db.MNode)2 NodeJob (gov.sandia.n2a.ui.jobs.NodeJob)2 Path (java.nio.file.Path)2 SimpleDateFormat (java.text.SimpleDateFormat)2 TreePath (javax.swing.tree.TreePath)2 InternalBackend (gov.sandia.n2a.backend.internal.InternalBackend)1 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)1 EquationSet (gov.sandia.n2a.eqset.EquationSet)1 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)1 MPart (gov.sandia.n2a.eqset.MPart)1 Variable (gov.sandia.n2a.eqset.Variable)1 HostSystem (gov.sandia.n2a.execenvs.HostSystem)1 UnitValue (gov.sandia.n2a.language.UnitValue)1 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)1