Esempio n. 1
0
 def setupParallelContext(self, parallel_split=None):
     self.overall_parallel_context = parallel.getContext()
     with parallel.split(parallel_split) as parallel_context:
         parallel_context = parallel_context.getCommunicator()
         self.remaining_parallel_context = parallel.getContext()
         if 'parallel_context' in self.defn_for:
             self.assignAll(
                 'parallel_context', value=parallel_context, const=True)
Esempio n. 2
0
 def change(self, changes):
     """Returns the output value after applying 'changes', a list of
     (optimisable_parameter_ordinal, new_value) tuples."""
     
     t0 = time.time()
     self.evaluations += 1
     assert parallel.getContext() is self.overall_parallel_context, (
         parallel.getContext(), self.overall_parallel_context)
     
     # If ALL of the changes made in the last step are reversed in this step
     # then it is safe to undo them first, taking advantage of the 1-deep
     # cache.
     if self.with_undo and self.last_undo:
         for (i, v) in self.last_undo:
             if (i,v) not in changes:
                 break
         else:
             changes = [ch for ch in changes if ch not in self.last_undo]
             self._switch = not self._switch
             for (i, v) in self.last_undo:
                 self.last_values[i] = v
     
     self.last_undo = []
     program = self.cellsChangedBy(changes)
     
     if self.with_undo:
         self._switch = not self._switch
         data = self.cell_values[self._switch]
         base = self.cell_values[not self._switch]
         
         # recycle and undo interact in bad ways
         for rank in self.recycled_cells:
             if data[rank] is not base[rank]:
                 self.spare[rank] = data[rank]
         data[:] = base[:]
         for cell in program:
             if cell.recycled:
                 if data[cell.rank] is base[cell.rank]:
                     data[cell.rank]=self.spare[cell.rank]
                     assert data[cell.rank] is not base[cell.rank]
     else:
         data = self.cell_values[self._switch]
     
     # Set new OptPar values
     changed_optpars = []
     for (i, v)  in changes:
         if i < len(self.opt_pars):
             assert isinstance(v*1.0, float), v
             changed_optpars.append((i, self.last_values[i]))
             self.last_values[i] = v
             data[i] = self.opt_pars[i].transformFromOptimiser(v)
         else:
             data[i] = v
     
     with parallel.parallel_context(self.remaining_parallel_context):
         try:
             if self.trace:
                 self.tracingUpdate(changes, program, data)
             else:
                 self.plainUpdate(program, data)
             
             # if non-optimiser parameter was set then undo is invalid
             if (self.last_undo and
                     max(self.last_undo)[0] >= len(self.opt_pars)):
                 self.last_undo = []
             else:
                 self.last_undo = changed_optpars
         
         except CalculationInterupted, detail:
             if self.with_undo:
                 self._switch = not self._switch
             for (i,v) in changed_optpars:
                 self.last_values[i] = v
             self.last_undo = []
             (cell, exception) = detail.args
             raise exception
         
         finally:
    def change(self, changes):
        """Returns the output value after applying 'changes', a list of
        (optimisable_parameter_ordinal, new_value) tuples."""

        t0 = time.time()
        self.evaluations += 1
        assert parallel.getContext() is self.overall_parallel_context, (
            parallel.getContext(), self.overall_parallel_context)

        # If ALL of the changes made in the last step are reversed in this step
        # then it is safe to undo them first, taking advantage of the 1-deep
        # cache.
        if self.with_undo and self.last_undo:
            for (i, v) in self.last_undo:
                if (i, v) not in changes:
                    break
            else:
                changes = [ch for ch in changes if ch not in self.last_undo]
                self._switch = not self._switch
                for (i, v) in self.last_undo:
                    self.last_values[i] = v

        self.last_undo = []
        program = self.cellsChangedBy(changes)

        if self.with_undo:
            self._switch = not self._switch
            data = self.cell_values[self._switch]
            base = self.cell_values[not self._switch]

            # recycle and undo interact in bad ways
            for rank in self.recycled_cells:
                if data[rank] is not base[rank]:
                    self.spare[rank] = data[rank]
            data[:] = base[:]
            for cell in program:
                if cell.recycled:
                    if data[cell.rank] is base[cell.rank]:
                        data[cell.rank] = self.spare[cell.rank]
                        assert data[cell.rank] is not base[cell.rank]
        else:
            data = self.cell_values[self._switch]

        # Set new OptPar values
        changed_optpars = []
        for (i, v) in changes:
            if i < len(self.opt_pars):
                assert isinstance(v * 1.0, float), v
                changed_optpars.append((i, self.last_values[i]))
                self.last_values[i] = v
                data[i] = self.opt_pars[i].transformFromOptimiser(v)
            else:
                data[i] = v

        with parallel.parallel_context(self.remaining_parallel_context):
            try:
                if self.trace:
                    self.tracingUpdate(changes, program, data)
                else:
                    self.plainUpdate(program, data)

                # if non-optimiser parameter was set then undo is invalid
                if (self.last_undo
                        and max(self.last_undo)[0] >= len(self.opt_pars)):
                    self.last_undo = []
                else:
                    self.last_undo = changed_optpars

            except CalculationInterupted, detail:
                if self.with_undo:
                    self._switch = not self._switch
                for (i, v) in changed_optpars:
                    self.last_values[i] = v
                self.last_undo = []
                (cell, exception) = detail.args
                raise exception

            finally: