예제 #1
0
 def start_socket(self, port, host):
     client_socket = socket.create_connection((host, port))
     
     self.must_run = True
     while self.must_run:
         
         message = SocketMessage()
         message.receive(client_socket)
             
         result_message = SocketMessage(message.call_id, message.function_id, message.call_count)
         
         if message.function_id == 0:
             self.must_run = False
         else:
             if message.function_id in self.mapping_from_tag_to_legacy_function:
                 try:
                     self.handle_message(message, result_message)
                 except  BaseException as ex:
                     traceback.print_exc()
                     result_message.set_error(ex.__str__())
                     for type, attribute in self.dtype_to_message_attribute.iteritems():
                         array = getattr(result_message, attribute)
                         packed = pack_array(array, result_message.call_count, type)
                         setattr(result_message, attribute, packed)
 
             else:
                 result_message.set_error("unknown function id " + message.function_id)
         
         result_message.send(client_socket)
     
     client_socket.close()
예제 #2
0
파일: python_code.py 프로젝트: Ingwar/amuse
 def start_socket(self, port, host):
     client_socket = socket.create_connection((host, port))
     
     self.must_run = True
     while self.must_run:
         
         message = SocketMessage()
         message.receive(client_socket)
             
         result_message = SocketMessage(message.call_id, message.function_id, message.call_count)
         
         if message.function_id == 0:
             self.must_run = False
         else:
             if message.function_id in self.mapping_from_tag_to_legacy_function:
                 try:
                     self.handle_message(message, result_message)
                 except  BaseException as ex:
                     traceback.print_exc()
                     result_message.set_error(ex.__str__())
                     for type, attribute in self.dtype_to_message_attribute.iteritems():
                         array = getattr(result_message, attribute)
                         packed = pack_array(array, result_message.call_count, type)
                         setattr(result_message, attribute, packed)
 
             else:
                 result_message.set_error("unknown function id " + message.function_id)
         
         result_message.send(client_socket)
     
     client_socket.close()
    def start(self, mpi_port=None):
        if mpi_port is None:
            parent = self.intercomm
            self.communicators.append(parent)
        else:
            parent = MPI.COMM_WORLD.Connect(mpi_port, MPI.INFO_NULL, 0)
            self.communicators.append(parent)
        self.activeid = 0
        self.lastid += 1

        rank = parent.Get_rank()

        self.must_run = True
        while self.must_run:
            if self.id_to_activate >= 0 and self.id_to_activate != self.activeid:
                warnings.warn("activating: " + str(self.id_to_activate))
                self.activeid = self.id_to_activate
                self.id_to_activate = -1
                parent = self.communicators[self.activeid]
                rank = parent.Get_rank()
            message = ClientSideMPIMessage(
                polling_interval=self.polling_interval)
            message.receive(parent)

            result_message = ClientSideMPIMessage(message.call_id,
                                                  message.function_id,
                                                  message.call_count)

            if message.function_id == 0:
                self.must_run = False
            else:
                if message.function_id in self.mapping_from_tag_to_legacy_function:
                    try:
                        self.handle_message(message, result_message)
                    except Exception as ex:
                        warnings.warn(str(ex))
                        traceback.print_exc()
                        result_message.set_error(str(ex))
                        #for type, attribute in self.dtype_to_message_attribute.iteritems():
                        #    setattr(result_message, attribute, [])

                        for type, attribute in self.dtype_to_message_attribute.iteritems(
                        ):
                            array = getattr(result_message, attribute)
                            packed = pack_array(array,
                                                result_message.call_count,
                                                type)
                            setattr(result_message, attribute, packed)

                else:
                    result_message.set_error("unknown function id " +
                                             str(message.function_id))

            if rank == 0:
                result_message.send(parent)

        if self.must_disconnect:
            for x in self.communicators:
                x.Disconnect()
예제 #4
0
파일: python_code.py 프로젝트: Ingwar/amuse
    def handle_message(self, input_message, output_message):
        legacy_function = self.mapping_from_tag_to_legacy_function[input_message.function_id]
        specification = legacy_function.specification
        
        dtype_to_count = self.get_dtype_to_count(specification)
        
        
        if specification.name.startswith('internal__'):
            method = getattr(self, specification.name)
        else:
            method = getattr(self.implementation, specification.name)
        
        if specification.has_units:
            input_units = self.convert_floats_to_units(input_message.encoded_units)
        else:
            input_units = ()
        
        for type, attribute in self.dtype_to_message_attribute.iteritems():
            count = dtype_to_count.get(type,0)
            for x in range(count):
                if type == 'string':
                    getattr(output_message, attribute).append([""] * output_message.call_count)
                else:
                    getattr(output_message, attribute).append(numpy.zeros(output_message.call_count, dtype=type))

        for type, attribute in self.dtype_to_message_attribute.iteritems():
            array = getattr(input_message, attribute)
            unpacked = unpack_array(array, input_message.call_count, type)
            setattr(input_message,attribute, unpacked)
        
        units = [False] * len(specification.output_parameters)
        if specification.must_handle_array:
            keyword_arguments = self.new_keyword_arguments_from_message(input_message, None,  specification, input_units)
            result = method(**keyword_arguments)
            self.fill_output_message(output_message, None, result, keyword_arguments, specification, units)
        else:
            for index in range(input_message.call_count):
                keyword_arguments = self.new_keyword_arguments_from_message(input_message, index,  specification, input_units)
                try:
                    result = method(**keyword_arguments)
                    if result < 0:
                        print result, keyword_arguments
                except TypeError:
                    result = method(*list(keyword_arguments))
                    if result < 0:
                        print "list", result, keyword_arguments, list(keyword_arguments)
                self.fill_output_message(output_message, index, result, keyword_arguments, specification, units)
        
            
        for type, attribute in self.dtype_to_message_attribute.iteritems():
            array = getattr(output_message, attribute)
            packed = pack_array(array, input_message.call_count, type)
            setattr(output_message, attribute, packed)
        
        if specification.has_units:
            output_message.encoded_units = self.convert_output_units_to_floats(units)
예제 #5
0
    def handle_message(self, input_message, output_message):
        legacy_function = self.mapping_from_tag_to_legacy_function[input_message.function_id]
        specification = legacy_function.specification
        dtype_to_count = self.get_dtype_to_count(specification)
        
        
        if specification.name.startswith('internal__'):
            method = getattr(self, specification.name)
        else:
            method = getattr(self.implementation, specification.name)
        
        if specification.has_units:
            input_units = self.convert_floats_to_units(input_message.encoded_units)
        else:
            input_units = ()
        
        for type, attribute in self.dtype_to_message_attribute.iteritems():
            count = dtype_to_count.get(type,0)
            for x in range(count):
                if type == 'string':
                    getattr(output_message, attribute).append([""] * output_message.call_count)
                else:
                    getattr(output_message, attribute).append(numpy.zeros(output_message.call_count, dtype=type))

        for type, attribute in self.dtype_to_message_attribute.iteritems():
            array = getattr(input_message, attribute)
            unpacked = unpack_array(array, input_message.call_count, type)
            setattr(input_message,attribute, unpacked)
        
        units = [False] * len(specification.output_parameters)
        if specification.must_handle_array:
            keyword_arguments = self.new_keyword_arguments_from_message(input_message, None,  specification, input_units)
            result = method(**keyword_arguments)
            self.fill_output_message(output_message, None, result, keyword_arguments, specification, units)
        else:
            for index in range(input_message.call_count):
                keyword_arguments = self.new_keyword_arguments_from_message(input_message, index,  specification, input_units)
                try:
                    result = method(**keyword_arguments)
                    if result < 0:
                        print result, keyword_arguments
                except TypeError:
                    result = method(*list(keyword_arguments))
                    if result < 0:
                        print "list", result, keyword_arguments, list(keyword_arguments)
                self.fill_output_message(output_message, index, result, keyword_arguments, specification, units)
        
            
        for type, attribute in self.dtype_to_message_attribute.iteritems():
            array = getattr(output_message, attribute)
            packed = pack_array(array, input_message.call_count, type)
            setattr(output_message, attribute, packed)
        
        if specification.has_units:
            output_message.encoded_units = self.convert_output_units_to_floats(units)
예제 #6
0
    def start(self, mpi_port = None):
        if mpi_port is None:
            parent = self.intercomm
            self.communicators.append(parent)
        else:
            parent = MPI.COMM_WORLD.Connect(mpi_port, MPI.INFO_NULL, 0)
            self.communicators.append(parent)
        self.activeid = 0
        self.lastid += 1
        
        rank = parent.Get_rank()
        
        self.must_run = True
        while self.must_run:
            if self.id_to_activate >= 0 and self.id_to_activate != self.activeid:
                print "activating:", self.id_to_activate
                self.activeid = self.id_to_activate
                self.id_to_activate = -1
                parent = self.communicators[self.activeid]
                rank = parent.Get_rank()
            message = ClientSideMPIMessage(polling_interval = self.polling_interval)
            message.receive(parent)

            result_message = ClientSideMPIMessage(message.call_id, message.function_id, message.call_count)
            
            if message.function_id == 0:
                self.must_run = False
            else:
                if message.function_id in self.mapping_from_tag_to_legacy_function:
                    try:
                        self.handle_message(message, result_message)
                    except Exception as ex:
                        print "EX:", ex
                        traceback.print_exc()
                        result_message.set_error(str(ex))
                        #for type, attribute in self.dtype_to_message_attribute.iteritems():
                        #    setattr(result_message, attribute, [])
                            
                        for type, attribute in self.dtype_to_message_attribute.iteritems():
                            array = getattr(result_message, attribute)
                            packed = pack_array(array, result_message.call_count, type)
                            setattr(result_message, attribute, packed)
                        
                else:
                    result_message.set_error("unknown function id " + str(message.function_id))
            
            if rank == 0:
                result_message.send(parent)

        if self.must_disconnect:
            for x in self.communicators:
                x.Disconnect()
                                      str((result, keyword_arguments)))
                except TypeError, ex:
                    warnings.warn(
                        "mismatch in python function specification(?): " +
                        str(ex))
                    result = method(*list(keyword_arguments))
                    if result < 0:
                        warnings.warn("result <0 detected: list " +
                                      str((result, keyword_arguments)))
                self.fill_output_message(output_message, index, result,
                                         keyword_arguments, specification,
                                         units)

        for type, attribute in self.dtype_to_message_attribute.iteritems():
            array = getattr(output_message, attribute)
            packed = pack_array(array, input_message.call_count, type)
            setattr(output_message, attribute, packed)

        if specification.has_units:
            output_message.encoded_units = self.convert_output_units_to_floats(
                units)

    def new_keyword_arguments_from_message(self,
                                           input_message,
                                           index,
                                           specification,
                                           units=[]):
        keyword_arguments = OrderedDictionary()
        for parameter in specification.parameters:
            attribute = self.dtype_to_message_attribute[parameter.datatype]
            argument_value = None