Пример #1
0
    def _start(self, name_of_the_worker = 'worker_code', **options):



        if name_of_the_worker is None:
            if self.implementation_factory is None:
                raise exceptions.CodeException("Must provide the name of a worker script or the implementation_factory class")
            name_of_the_worker = self.make_executable_script_for(self.implementation_factory)
            if not options.setdefault("dynamic_python_code",True):
                raise exceptions.CodeException("dynamic code set to false, but python code generated")
        
        if self.use_python_interpreter:
            CodeInterface._start(self, name_of_the_worker = name_of_the_worker, interpreter_executable = self.python_interpreter, **options)
        else:
            CodeInterface._start(self, name_of_the_worker = name_of_the_worker, **options)
Пример #2
0
    def converted_keyword_and_list_arguments(self, arguments_list,
                                             keyword_arguments):
        dtype_to_values = self.specification.new_dtype_to_values()

        input_parameters_seen = set(
            map(lambda x: x.name, self.specification.input_parameters))
        names_in_argument_list = set([])
        for index, argument in enumerate(arguments_list):
            parameter = self.specification.input_parameters[index]
            names_in_argument_list.add(parameter.name)

            values = dtype_to_values[parameter.datatype]
            values[parameter.input_index] = argument
            input_parameters_seen.remove(parameter.name)

        for index, parameter in enumerate(self.specification.input_parameters):
            if parameter.name in keyword_arguments:
                values = dtype_to_values[parameter.datatype]
                values[parameter.input_index] = keyword_arguments[
                    parameter.name]
                input_parameters_seen.remove(parameter.name)

        for parameter in self.specification.input_parameters:
            if (parameter.name in input_parameters_seen
                ) and parameter.has_default_value():
                values = dtype_to_values[parameter.datatype]
                values[parameter.input_index] = parameter.default
                input_parameters_seen.remove(parameter.name)

        if input_parameters_seen:
            raise exceptions.CodeException(
                "Not enough parameters in call, missing " +
                str(sorted(input_parameters_seen)))

        return dtype_to_values
Пример #3
0
 def handle_result(function):
     try:
         dtype_to_result = function()
     except Exception, ex:
         raise exceptions.CodeException(
             "Exception when calling legacy code '{0}', exception was '{1}'"
             .format(self.specification.name, ex))
Пример #4
0
    def __call__(self, *arguments_list, **keyword_arguments):
        dtype_to_values, units = self.converted_keyword_and_list_arguments(
            arguments_list, keyword_arguments)
        encoded_units = self.convert_input_units_to_floats(units)

        handle_as_array = self.must_handle_as_array(dtype_to_values)

        if not self.owner is None:
            CODE_LOG.info("start call '%s.%s'", self.owner.__name__,
                          self.specification.name)

        call_id = random.randint(0, 1000)
        try:
            self.interface.channel.send_message(
                call_id,
                self.specification.id,
                dtype_to_arguments=dtype_to_values,
                encoded_units=encoded_units)

            dtype_to_result, output_encoded_units = self.interface.channel.recv_message(
                call_id,
                self.specification.id,
                handle_as_array,
                has_units=True)
        except Exception, ex:
            CODE_LOG.info(
                "Exception when calling function '{0}', of code '{1}', exception was '{2}'"
                .format(self.specification.name,
                        type(self.interface).__name__, ex))
            raise exceptions.CodeException(
                "Exception when calling function '{0}', of code '{1}', exception was '{2}'"
                .format(self.specification.name,
                        type(self.interface).__name__, ex))
Пример #5
0
    def converted_keyword_and_list_arguments(self, arguments_list,
                                             keyword_arguments):
        from amuse.units import quantities
        dtype_to_values = self.specification.new_dtype_to_values()
        units = [None] * len(self.specification.input_parameters)

        input_parameters_seen = set(
            map(lambda x: x.name, self.specification.input_parameters))
        names_in_argument_list = set([])
        for index, argument in enumerate(arguments_list):
            parameter = self.specification.input_parameters[index]
            names_in_argument_list.add(parameter.name)

            if quantities.is_quantity(argument):
                units[parameter.index_in_input] = argument.unit
                argument = argument.number

            values = dtype_to_values[parameter.datatype]
            values[parameter.input_index] = argument
            input_parameters_seen.remove(parameter.name)

        for index, parameter in enumerate(self.specification.input_parameters):
            if parameter.name in keyword_arguments:
                argument = keyword_arguments[parameter.name]
                if quantities.is_quantity(argument):
                    units[parameter.index_in_input] = argument.unit
                    argument = argument.number

                values = dtype_to_values[parameter.datatype]
                values[parameter.input_index] = argument
                input_parameters_seen.remove(parameter.name)

        for parameter in self.specification.input_parameters:
            if (parameter.name in input_parameters_seen
                ) and parameter.has_default_value():
                argument = parameter.default
                if quantities.is_quantity(argument):
                    units[parameter.index_in_input] = argument.unit
                    argument = argument.number

                values = dtype_to_values[parameter.datatype]
                values[parameter.input_index] = argument
                input_parameters_seen.remove(parameter.name)

        if input_parameters_seen:
            raise exceptions.CodeException(
                "Not enough parameters in call, missing " +
                str(sorted(input_parameters_seen)))

        return dtype_to_values, units
Пример #6
0
class CodeFunction(object):
    
    __doc__ = CodeDocStringProperty()
   
    def __init__(self, interface, owner, specification):
        """
        Implementation of the runtime call to the remote process.

        Performs the encoding of python arguments into lists
        of values, sends a message over an MPI channel and
        waits for a result message, decodes this message and
        returns.
        """
        self.interface = interface
        self.owner = owner
        self.specification = specification
    
    def __call__(self, *arguments_list, **keyword_arguments):
        if self.interface.async_request:
            try:
                self.interface.async_request.wait()
            except Exception,ex:
                warnings.warn("Ignored exception: " + str(ex))
            
        dtype_to_values = self.converted_keyword_and_list_arguments( arguments_list, keyword_arguments)
        
        handle_as_array = self.must_handle_as_array(dtype_to_values)
        
        if not self.owner is None:
            CODE_LOG.info("start call '%s.%s'",self.owner.__name__, self.specification.name)
        
        call_id = random.randint(0, 1000)
        
        try:
            self.interface.channel.send_message(call_id, self.specification.id, dtype_to_arguments = dtype_to_values)
            
            dtype_to_result = self.interface.channel.recv_message(call_id, self.specification.id, handle_as_array)
        except Exception, ex:
            CODE_LOG.info("Exception when calling function '{0}', of code '{1}', exception was '{2}'".format(self.specification.name, type(self.interface).__name__, ex))
            raise exceptions.CodeException("Exception when calling function '{0}', of code '{1}', exception was '{2}'".format(self.specification.name, type(self.interface).__name__, ex))