Search in sources :

Example 11 with Host

use of gov.sandia.n2a.host.Host in project n2a by frothga.

the class SettingsHost method getPanel.

@Override
public Component getPanel() {
    if (scrollPane != null)
        return scrollPane;
    JPanel view = new JPanel();
    scrollPane = new JScrollPane(view);
    // About one line of text. Typically, one "click" of the wheel does 3 steps, so about 45px or 3 lines of text.
    scrollPane.getVerticalScrollBar().setUnitIncrement(15);
    for (Host h : Host.getHosts()) if (h instanceof Remote)
        model.addElement(h);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setToolTipText("Press Insert to create host. Press Delete to remove host.");
    InputMap inputMap = list.getInputMap();
    inputMap.put(KeyStroke.getKeyStroke("INSERT"), "add");
    inputMap.put(KeyStroke.getKeyStroke("ctrl shift EQUALS"), "add");
    inputMap.put(KeyStroke.getKeyStroke("DELETE"), "delete");
    inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"), "delete");
    ActionMap actionMap = list.getActionMap();
    actionMap.put("add", new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // Create new record
            String hostname = Host.uniqueName();
            // Get default class. The user can change it later.
            Host h = Host.create(hostname, "");
            // Focus record in UI
            int index = list.getSelectedIndex();
            if (index < 0)
                index = model.getSize();
            model.add(index, h);
            // Assumption: this triggers a selection change event, which will in turn call displayRecord().
            list.setSelectedIndex(index);
            fieldName.requestFocusInWindow();
        }
    });
    actionMap.put("delete", new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // Remove current record
            int index = list.getSelectedIndex();
            Host h = list.getSelectedValue();
            if (h == null)
                return;
            Host.remove(h, true);
            // Focus another record, or clear UI
            model.remove(index);
            index = Math.min(index, model.size() - 1);
            // triggers selection change event, resulting in call to displayRecord()
            if (index >= 0)
                list.setSelectedIndex(index);
        }
    });
    list.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            displayRecord();
        }
    });
    fieldName = new MTextField(null, "", "", 20, true);
    fieldName.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            Host h = list.getSelectedValue();
            String newName = fieldName.getText();
            Host.rename(h, newName);
            list.repaint();
            if (editor instanceof NameChangeListener)
                ((NameChangeListener) editor).nameChanged(h.name);
        }
    });
    for (ExtensionPoint ext : PluginManager.getExtensionsForPoint(Factory.class)) {
        Factory f = (Factory) ext;
        if (f instanceof FactoryRemote)
            comboClass.addItem(f.className());
    }
    comboClass.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String newClass = comboClass.getSelectedItem().toString();
            // Change the class. This requires destroying the current instance and constructing
            // a new one that is bound to the same record.
            int index = list.getSelectedIndex();
            Host h = list.getSelectedValue();
            if (h == null)
                return;
            String originalClass = h.getClassName();
            if (newClass.equals(originalClass))
                return;
            Host.remove(h, false);
            Host h2 = Host.create(h.name, newClass);
            h.transferJobsTo(h2);
            model.set(index, h2);
            displayRecord();
        }
    });
    editorHolder.setLayout(new BorderLayout());
    JPanel panelList = Lay.BL("C", list, "pref=[100,200]");
    panelList.setBorder(LineBorder.createBlackLineBorder());
    panelList = (JPanel) Lay.eb(Lay.BL("C", panelList), "5");
    Lay.BLtg(view, "N", Lay.BL("W", Lay.BxL("H", Lay.BL("N", panelList), Box.createHorizontalStrut(5), Lay.BL("N", Lay.BxL(Lay.FL(Lay.lb("Name"), fieldName), Lay.FL(Lay.lb("Class"), comboClass), editorHolder)))));
    scrollPane.setFocusCycleRoot(true);
    if (list.getModel().getSize() > 0)
        list.setSelectedIndex(0);
    return scrollPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) ActionMap(javax.swing.ActionMap) ActionEvent(java.awt.event.ActionEvent) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Remote(gov.sandia.n2a.host.Remote) FactoryRemote(gov.sandia.n2a.host.Host.FactoryRemote) Factory(gov.sandia.n2a.host.Host.Factory) Host(gov.sandia.n2a.host.Host) MTextField(gov.sandia.n2a.ui.MTextField) ListSelectionListener(javax.swing.event.ListSelectionListener) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) InputMap(javax.swing.InputMap) ChangeListener(javax.swing.event.ChangeListener) AbstractAction(javax.swing.AbstractAction) FactoryRemote(gov.sandia.n2a.host.Host.FactoryRemote)

Example 12 with Host

use of gov.sandia.n2a.host.Host in project n2a by frothga.

the class Main method studyHeadless.

/**
 *        Run a study from the command line.
 *        Unlike runHeadless(), this function uses all the usual job management machinery.
 */
public static void studyHeadless(MNode record) {
    String key = record.key();
    MNode doc = AppData.models.childOrEmpty(key);
    record.mergeUnder(doc);
    MPart collated = new MPart(record);
    if (!collated.containsKey("study"))
        return;
    // Start host monitor threads (see PanelRun constructor for non-headless procedure)
    Host.restartAssignmentThread();
    for (Host h : Host.getHosts()) h.restartMonitorThread();
    MNode studyNode = PanelEquations.createStudy(collated);
    // constructed in paused state
    Study study = new Study(studyNode);
    // start
    study.togglePause();
    study.waitForCompletion();
    // Output CSV files, if requested.
    if (record.getFlag("$metadata", "csv")) {
        Path studyDir = study.getDir();
        try (BufferedWriter parms = Files.newBufferedWriter(studyDir.resolve("study.csv"))) {
            SampleTableModel samples = new SampleTableModel();
            samples.update(study);
            int rows = samples.getRowCount();
            int cols = samples.getColumnCount();
            int lastCol = cols - 1;
            // Header for study.csv file
            for (// first column is job status, so skip it
            int c = 1; // first column is job status, so skip it
            c < cols; // first column is job status, so skip it
            c++) {
                parms.write(samples.getColumnName(c));
                if (c < lastCol)
                    parms.write(",");
            }
            parms.newLine();
            // Rows for study.csv file, along with converted output of each job.
            for (int r = 0; r < rows; r++) {
                for (int c = 1; c < cols; c++) {
                    parms.write(samples.getValueAt(r, c).toString());
                    if (c < lastCol)
                        parms.write(",");
                }
                parms.newLine();
                NodeJob jobNode = study.getJob(r);
                Path jobDir = Host.getJobDir(Host.getLocalResourceDir(), jobNode.getSource());
                try {
                    Table table = new Table(jobDir.resolve("out"), false);
                    table.dumpCSV(studyDir.resolve(r + ".csv"));
                } catch (IOException e) {
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // See MainFrame window close listener
    // Save any modified data, particularly the study record.
    AppData.quit();
    // Close down any ssh sessions.
    Host.quit();
}
Also used : Path(java.nio.file.Path) Study(gov.sandia.n2a.ui.studies.Study) MPart(gov.sandia.n2a.eqset.MPart) Table(gov.sandia.n2a.ui.jobs.Table) SampleTableModel(gov.sandia.n2a.ui.studies.PanelStudy.SampleTableModel) Host(gov.sandia.n2a.host.Host) NodeJob(gov.sandia.n2a.ui.jobs.NodeJob) IOException(java.io.IOException) MNode(gov.sandia.n2a.db.MNode) BufferedWriter(java.io.BufferedWriter)

Example 13 with Host

use of gov.sandia.n2a.host.Host in project n2a by frothga.

the class Backend method getSimTimeFromOutput.

public static double getSimTimeFromOutput(MNode job, String outFileName, int timeColumn) {
    Path out;
    try {
        Host env = Host.get(job);
        Path resourceDir = env.getResourceDir();
        Path jobDir = Host.getJobDir(resourceDir, job);
        out = jobDir.resolve(outFileName);
    } catch (Exception e) {
        return 0;
    }
    // The divide by 2 at the end of the following line allows us to adapt down as well as up.
    int lineLength = job.getOrDefault(32, "lineLength") / 2;
    try (SeekableByteChannel channel = Files.newByteChannel(out, StandardOpenOption.READ)) {
        int columnIndex = 0;
        while (// limit to 1MiB
        lineLength <= 1024 * 1024) {
            long available = channel.size();
            // Don't exceed file size
            if (available < lineLength)
                break;
            channel.position(available - lineLength);
            // TODO: check if direct is OK here
            ByteBuffer buffer = ByteBuffer.allocate(lineLength);
            int received = channel.read(buffer);
            buffer.position(0);
            String column = "";
            boolean gotNL = false;
            boolean inSpace = false;
            for (long i = 0; i < received; i++) {
                // Technically, the file is in UTF-8, but this will only matter in column headings. We are looking for a float string, which will be in all lower ASCII.
                char c = (char) buffer.get();
                if (c == '\n' || c == '\r') {
                    gotNL = true;
                    continue;
                }
                if (gotNL) {
                    // Tabs and spaces should never be mixed in the output file.
                    boolean nextColumn = false;
                    if (c == '\t') {
                        // Tab always indicates next column.
                        nextColumn = true;
                    } else if (c == ' ') {
                        if (inSpace)
                            continue;
                        nextColumn = true;
                        inSpace = true;
                    } else {
                        inSpace = false;
                    }
                    if (nextColumn) {
                        if (columnIndex == timeColumn) {
                            try {
                                // Remember most recent value, to more quickly track situation in the output stream.
                                job.set(lineLength, "lineLength");
                                return Double.parseDouble(column);
                            } catch (NumberFormatException e) {
                                // This is probably a column header rather than a value.
                                // Skip to end of line and try again.
                                gotNL = false;
                                columnIndex = 0;
                            }
                        } else {
                            columnIndex++;
                        }
                    } else {
                        column = column + c;
                    }
                }
            }
            lineLength *= 2;
        }
    } catch (Exception e) {
    }
    return 0;
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) Host(gov.sandia.n2a.host.Host) ByteBuffer(java.nio.ByteBuffer) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint)

Aggregations

Host (gov.sandia.n2a.host.Host)13 Path (java.nio.file.Path)8 MNode (gov.sandia.n2a.db.MNode)7 Remote (gov.sandia.n2a.host.Remote)5 IOException (java.io.IOException)5 TreePath (javax.swing.tree.TreePath)5 Date (java.util.Date)4 MDoc (gov.sandia.n2a.db.MDoc)3 Backend (gov.sandia.n2a.plugins.extpoints.Backend)3 NodeJob (gov.sandia.n2a.ui.jobs.NodeJob)3 BufferedWriter (java.io.BufferedWriter)3 MPart (gov.sandia.n2a.eqset.MPart)2 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)2 Table (gov.sandia.n2a.ui.jobs.Table)2 ActionEvent (java.awt.event.ActionEvent)2 SimpleDateFormat (java.text.SimpleDateFormat)2 AbstractAction (javax.swing.AbstractAction)2 ActionMap (javax.swing.ActionMap)2 InputMap (javax.swing.InputMap)2 JPanel (javax.swing.JPanel)2