'Create or update an SGE parallel environment
name - name of parallel environment
nodes - list of nodes to include in the parallel environment
(default: all)
queue - configure queue to use the new parallel environment'

def _create_sge_pe(self, name='orte', nodes=None, queue='all.q'):

    mssh = self._master.ssh
    pe_exists = (mssh.get_status(('qconf    -sp    %s' % name)) == 0)
    verb = ('Updating' if pe_exists else 'Creating')
    log.info(("%s    SGE    parallel    environment    '%s'" % (verb, name)))
    if (not nodes):
        nodes = (self._nodes if self.master_is_exec_host else self.nodes)
    if (self.slots_per_host is None):
        pe_slots = sum(self.pool.map((lambda n: n.num_processors), nodes, jobid_fn=(lambda n: n.alias)))
    else:
        pe_slots = (self.slots_per_host * len(nodes))
    if (not pe_exists):
        penv = mssh.remote_file('/tmp/pe.txt', 'w')
        penv.write((sge.sge_pe_template % (name, pe_slots)))
        penv.close()
        mssh.execute(('qconf    -Ap    %s' % penv.name))
    else:
        mssh.execute(('qconf    -mattr    pe    slots    %s    %s' % (pe_slots, name)))
    if queue:
        log.info(("Adding    parallel    environment    '%s'    to    queue    '%s'" % (name, queue)))
        mssh.execute(('qconf    -mattr    queue    pe_list    "%s"    %s' % (name, queue)))
