Пример #1
0
def get_default_direction(var_name, decorator_arguments, param_args):
    # type: (str, typing.Dict[str, typing.Any], typing.List[typing.Any]) -> Parameter
    """ Returns the default direction for a given parameter.

    :param var_name: Variable name.
    :param decorator_arguments: Decorator arguments.
    :param param_args: Parameter args.
    :return: An identifier of the direction.
    """
    # We are the "self" or "cls" in an instance or class method that
    # modifies the given class, so we are an INOUT, CONCURRENT or
    # COMMUTATIVE
    self_dirs = [
        parameter.DIRECTION.INOUT, parameter.DIRECTION.CONCURRENT,
        parameter.DIRECTION.COMMUTATIVE
    ]
    if "targetDirection" in decorator_arguments:
        target_label = "targetDirection"
    else:
        target_label = "target_direction"
    if decorator_arguments[target_label].direction in self_dirs and \
            var_name in ["self", "cls"] and \
            param_args and \
            param_args[0] == var_name:
        return decorator_arguments[target_label]
    return get_new_parameter("IN")
Пример #2
0
    def get_default_direction(self, var_name):
        # type: (str) -> ...  # Parameter
        """ Returns the default direction for a given parameter.

        :param var_name: Variable name.
        :return: An identifier of the direction.
        """
        # We are the 'self' or 'cls' in an instance or classmethod that
        # modifies the given class, so we are an INOUT, CONCURRENT or
        # COMMUTATIVE
        self_dirs = [
            parameter.DIRECTION.INOUT, parameter.DIRECTION.CONCURRENT,
            parameter.DIRECTION.COMMUTATIVE
        ]
        if 'targetDirection' in self.decorator_arguments:
            target_label = 'targetDirection'
        else:
            target_label = 'target_direction'
        if self.decorator_arguments[target_label].direction in self_dirs and \
                var_name in ['self', 'cls'] and \
                self.param_args and \
                self.param_args[0] == var_name:
            return self.decorator_arguments[target_label]
        return get_new_parameter('IN')
Пример #3
0
    def __init__(self, *args, **kwargs):  # noqa
        """ Task constructor.

        This part is called in the decoration process, not as an
        explicit function call.

        We do two things here:
            a) Assign default values to unspecified fields
               (see _get_default_decorator_values).
            b) Transform the parameters from user friendly types
               (i.e Parameter.IN, etc) to a more convenient internal
               representation.

        :param kwargs: Decorator parameters. A task decorator has no positional
                       arguments.
        :param user_function: User function to execute.
        :param core_element: Core element for the task (only used in master,
                             but needed here to keep it between task
                             invocations).
        :param registered: If the core element has already been registered.
        :param signature: The user function signature.
        """
        self.task_type = "METHOD"
        decorator_name = "".join(('@', Task.__name__.lower()))
        super(Task, self).__init__(decorator_name, *args, **kwargs)

        self.decorator_arguments = kwargs
        # Set missing values to their default ones (step a)
        for (key, value) in self._get_default_decorator_values().items():
            if key not in self.decorator_arguments:
                self.decorator_arguments[key] = value
        # Give all parameters a unique instance for them (step b)
        # Note that when a user defines a task like
        # @task(a = IN, b = IN, c = INOUT)
        # both a and b point to the same IN object (the one from parameter.py)
        # Giving them a unique instance makes life easier in further steps
        for (key, value) in self.decorator_arguments.items():
            # Not all decorator arguments are necessarily parameters
            # (see self._get_default_decorator_values)
            if is_param(value):
                self.decorator_arguments[key] = get_new_parameter(value.key)
            # Specific case when value is a dictionary
            # Use case example:
            # @binary(binary="ls")
            # @task(hide={Type: FILE_IN, Prefix: "--hide="},
            #       sort={Type: IN, Prefix: "--sort="})
            # def myLs(flag, hide, sort):
            #   pass
            # Transform this dictionary to a Parameter object
            if isinstance(value, dict):
                if key not in [
                        "numba", "numba_flags", "numba_signature",
                        "numba_declaration"
                ]:
                    # Perform user -> instance substitution
                    # param = self.decorator_arguments[key][parameter.Type]
                    # Replace the whole dict by a single parameter object
                    self.decorator_arguments[key] = \
                        get_parameter_from_dictionary(
                            self.decorator_arguments[key]
                        )
                else:
                    # It is a reserved word that we need to keep the user
                    # defined value (not a Parameter object)
                    self.decorator_arguments[key] = value

        # Function to execute as task
        self.user_function = None
        # Global variables common for all tasks of this kind
        self.registered = None
        self.signature = None
        # Saved from the initial task
        self.interactive = None
        self.module = None
        self.function_arguments = None
        self.function_name = None
        self.module_name = None
        self.function_type = None
        self.class_name = None
        self.hints = None
        self.on_failure = None
        self.defaults = None
Пример #4
0
    def __init__(self, *args, **kwargs):  # noqa
        # type: (*typing.Any, **typing.Any) -> None
        """ Task constructor.

        This part is called in the decoration process, not as an
        explicit function call.

        We do two things here:
            a) Assign default values to unspecified fields.
            b) Transform the parameters from user friendly types
               (i.e Parameter.IN, etc) to a more convenient internal
               representation.

        :param args: Decorator positional parameters (ignored).
        :param kwargs: Decorator parameters. A task decorator has no positional
                       arguments.
        """
        self.task_type = IMPL_METHOD
        self.decorator_name = "".join(("@", Task.__name__.lower()))
        self.args = args
        self.kwargs = kwargs
        self.scope = context.in_pycompss()
        self.core_element = CE()
        self.core_element_configured = False

        # Set missing values to their default ones (step a)
        self.decorator_arguments = kwargs
        default_decorator_values = {
            "target_direction": parameter.INOUT,
            "returns": False,
            "cache_returns": True,
            "priority": False,
            "defaults": {},
            "time_out": 0,
            "is_replicated": False,
            "is_distributed": False,
            "computing_nodes": 1,
            "is_reduce": False,
            "chunk_size": 0,
            "tracing_hook": False,
            "numba": False,  # numba mode (jit, vectorize, guvectorize)
            "numba_flags": {},  # user defined extra numba flags
            "numba_signature": None,  # vectorize and guvectorize signature
            "numba_declaration": None,  # guvectorize declaration
            "varargs_type": parameter.IN,  # Here for legacy purposes
        }  # type: typing.Dict[str, typing.Any]
        for (key, value) in default_decorator_values.items():
            if key not in self.decorator_arguments:
                self.decorator_arguments[key] = value
        # Give all parameters a unique instance for them (step b)
        # Note that when a user defines a task like
        # @task(a = IN, b = IN, c = INOUT)
        # both a and b point to the same IN object (the one from parameter.py)
        # Giving them a unique instance makes life easier in further steps
        for (key, value) in self.decorator_arguments.items():
            # Not all decorator arguments are necessarily parameters
            # (see default_decorator_values)
            if is_param(value):
                self.decorator_arguments[key] = get_new_parameter(value.key)
            # Specific case when value is a dictionary
            # Use case example:
            # @binary(binary="ls")
            # @task(hide={Type: FILE_IN, Prefix: "--hide="},
            #       sort={Type: IN, Prefix: "--sort="})
            # def myLs(flag, hide, sort):
            #   pass
            # Transform this dictionary to a Parameter object
            if isinstance(value, dict):
                if key not in [
                        "numba", "numba_flags", "numba_signature",
                        "numba_declaration"
                ]:
                    # Perform user -> instance substitution
                    # param = self.decorator_arguments[key][parameter.Type]
                    # Replace the whole dict by a single parameter object
                    self.decorator_arguments[key] = \
                        get_parameter_from_dictionary(
                            self.decorator_arguments[key]
                        )
                else:
                    # It is a reserved word that we need to keep the user
                    # defined value (not a Parameter object)
                    self.decorator_arguments[key] = value
        self.user_function = dummy_function  # type: typing.Callable
        # Global variables common for all tasks of this kind
        self.registered = False
        self.signature = ""
        # Saved from the initial task
        self.interactive = False
        self.module = None  # type: typing.Any
        self.function_arguments = tuple()  # type: tuple
        self.function_name = ""
        self.module_name = ""
        self.function_type = -1
        self.class_name = ""
        self.hints = tuple()  # type: tuple
        self.on_failure = ""
        self.defaults = dict()  # type: dict