示例#1
0
    def _adjust_target(self, spec):
        """Assumes that the architecture and the compiler have been
        set already and checks if the current target microarchitecture
        is the default and can be optimized by the compiler.

        If not, downgrades the microarchitecture until a suitable one
        is found. If none can be found raise an error.

        Args:
            spec: spec to be concretized

        Returns:
            True if any modification happened, False otherwise
        """
        import archspec.cpu

        # Try to adjust the target only if it is the default
        # target for this platform
        current_target = spec.architecture.target
        current_platform = spack.architecture.get_platform(
            spec.architecture.platform
        )

        default_target = current_platform.target('default_target')
        if PackagePrefs.has_preferred_targets(spec.name):
            default_target = self.target_from_package_preferences(spec)

        if current_target != default_target or (
                self.abstract_spec and
                self.abstract_spec.architecture and
                self.abstract_spec.architecture.concrete):
            return False

        try:
            current_target.optimization_flags(spec.compiler)
        except archspec.cpu.UnsupportedMicroarchitecture:
            microarchitecture = current_target.microarchitecture
            for ancestor in microarchitecture.ancestors:
                candidate = None
                try:
                    candidate = spack.architecture.Target(ancestor)
                    candidate.optimization_flags(spec.compiler)
                except archspec.cpu.UnsupportedMicroarchitecture:
                    continue

                if candidate is not None:
                    msg = ('{0.name}@{0.version} cannot build optimized '
                           'binaries for "{1}". Using best target possible: '
                           '"{2}"')
                    msg = msg.format(spec.compiler, current_target, candidate)
                    tty.warn(msg)
                    spec.architecture.target = candidate
                    return True
            else:
                raise

        return False
示例#2
0
    def concretize_architecture(self, spec):
        """If the spec is empty provide the defaults of the platform. If the
        architecture is not a string type, then check if either the platform,
        target or operating system are concretized. If any of the fields are
        changed then return True. If everything is concretized (i.e the
        architecture attribute is a namedtuple of classes) then return False.
        If the target is a string type, then convert the string into a
        concretized architecture. If it has no architecture and the root of the
        DAG has an architecture, then use the root otherwise use the defaults
        on the platform.
        """
        # ensure type safety for the architecture
        if spec.architecture is None:
            spec.architecture = spack.spec.ArchSpec()

        if spec.architecture.platform and \
                (spec.architecture.os and spec.architecture.target):
            return False

        # Get platform of nearest spec with a platform, including spec
        # If spec has a platform, easy
        if spec.architecture.platform:
            new_plat = spack.architecture.get_platform(
                spec.architecture.platform)
        else:
            # Else if anyone else has a platform, take the closest one
            # Search up, then down, along build/link deps first
            # Then any nearest. Algorithm from compilerspec search
            platform_spec = find_spec(
                spec, lambda x: x.architecture and x.architecture.platform)
            if platform_spec:
                new_plat = spack.architecture.get_platform(
                    platform_spec.architecture.platform)
            else:
                # If no platform anywhere in this spec, grab the default
                new_plat = spack.architecture.platform()

        # Get nearest spec with relevant platform and an os
        # Generally, same algorithm as finding platform, except we only
        # consider specs that have a platform
        if spec.architecture.os:
            new_os = spec.architecture.os
        else:
            new_os_spec = find_spec(
                spec, lambda x: (x.architecture and x.architecture.platform ==
                                 str(new_plat) and x.architecture.os))
            if new_os_spec:
                new_os = new_os_spec.architecture.os
            else:
                new_os = new_plat.operating_system('default_os')

        # Get the nearest spec with relevant platform and a target
        # Generally, same algorithm as finding os
        if spec.architecture.target:
            new_target = spec.architecture.target
        else:
            new_target_spec = find_spec(
                spec, lambda x: (x.architecture and x.architecture.platform ==
                                 str(new_plat) and x.architecture.target))
            if new_target_spec:
                new_target = new_target_spec.architecture.target
            else:
                # To get default platform, consider package prefs
                if PackagePrefs.has_preferred_targets(spec.name):
                    target_prefs = PackagePrefs(spec.name, 'target')
                    target_specs = [
                        spack.spec.Spec('target=%s' % tname)
                        for tname in cpu.targets
                    ]

                    def tspec_filter(s):
                        # Filter target specs by whether the architecture
                        # family is the current machine type. This ensures
                        # we only consider x86_64 targets when on an
                        # x86_64 machine, etc. This may need to change to
                        # enable setting cross compiling as a default
                        target = cpu.targets[str(s.architecture.target)]
                        arch_family_name = target.family.name
                        return arch_family_name == platform.machine()

                    # Sort filtered targets by package prefs
                    target_specs = list(filter(tspec_filter, target_specs))
                    target_specs.sort(key=target_prefs)

                    new_target = target_specs[0].architecture.target
                else:
                    new_target = new_plat.target('default_target')

        # Construct new architecture, compute whether spec changed
        arch_spec = (str(new_plat), str(new_os), str(new_target))
        new_arch = spack.spec.ArchSpec(arch_spec)
        spec_changed = new_arch != spec.architecture
        spec.architecture = new_arch
        return spec_changed
示例#3
0
    def concretize_architecture(self, spec):
        """If the spec is empty provide the defaults of the platform. If the
        architecture is not a string type, then check if either the platform,
        target or operating system are concretized. If any of the fields are
        changed then return True. If everything is concretized (i.e the
        architecture attribute is a namedtuple of classes) then return False.
        If the target is a string type, then convert the string into a
        concretized architecture. If it has no architecture and the root of the
        DAG has an architecture, then use the root otherwise use the defaults
        on the platform.
        """
        # ensure type safety for the architecture
        if spec.architecture is None:
            spec.architecture = spack.spec.ArchSpec()

        if spec.architecture.concrete:
            return False

        # Get platform of nearest spec with a platform, including spec
        # If spec has a platform, easy
        if spec.architecture.platform:
            new_plat = spack.architecture.get_platform(
                spec.architecture.platform)
        else:
            # Else if anyone else has a platform, take the closest one
            # Search up, then down, along build/link deps first
            # Then any nearest. Algorithm from compilerspec search
            platform_spec = find_spec(
                spec, lambda x: x.architecture and x.architecture.platform
            )
            if platform_spec:
                new_plat = spack.architecture.get_platform(
                    platform_spec.architecture.platform)
            else:
                # If no platform anywhere in this spec, grab the default
                new_plat = spack.architecture.platform()

        # Get nearest spec with relevant platform and an os
        # Generally, same algorithm as finding platform, except we only
        # consider specs that have a platform
        if spec.architecture.os:
            new_os = spec.architecture.os
        else:
            new_os_spec = find_spec(
                spec, lambda x: (x.architecture and
                                 x.architecture.platform == str(new_plat) and
                                 x.architecture.os)
            )
            if new_os_spec:
                new_os = new_os_spec.architecture.os
            else:
                new_os = new_plat.operating_system('default_os')

        # Get the nearest spec with relevant platform and a target
        # Generally, same algorithm as finding os
        curr_target = None
        if spec.architecture.target:
            curr_target = spec.architecture.target
        if spec.architecture.target and spec.architecture.target_concrete:
            new_target = spec.architecture.target
        else:
            new_target_spec = find_spec(
                spec, lambda x: (x.architecture and
                                 x.architecture.platform == str(new_plat) and
                                 x.architecture.target and
                                 x.architecture.target != curr_target)
            )
            if new_target_spec:
                if curr_target:
                    # constrain one target by the other
                    new_target_arch = spack.spec.ArchSpec(
                        (None, None, new_target_spec.architecture.target))
                    curr_target_arch = spack.spec.ArchSpec(
                        (None, None, curr_target))
                    curr_target_arch.constrain(new_target_arch)
                    new_target = curr_target_arch.target
                else:
                    new_target = new_target_spec.architecture.target
            else:
                # To get default platform, consider package prefs
                if PackagePrefs.has_preferred_targets(spec.name):
                    new_target = self.target_from_package_preferences(spec)
                else:
                    new_target = new_plat.target('default_target')
                if curr_target:
                    # convert to ArchSpec to compare satisfaction
                    new_target_arch = spack.spec.ArchSpec(
                        (None, None, str(new_target)))
                    curr_target_arch = spack.spec.ArchSpec(
                        (None, None, str(curr_target)))

                    if not new_target_arch.satisfies(curr_target_arch):
                        # new_target is an incorrect guess based on preferences
                        # and/or default
                        valid_target_ranges = str(curr_target).split(',')
                        for target_range in valid_target_ranges:
                            t_min, t_sep, t_max = target_range.partition(':')
                            if not t_sep:
                                new_target = t_min
                                break
                            elif t_max:
                                new_target = t_max
                                break
                            elif t_min:
                                # TODO: something better than picking first
                                new_target = t_min
                                break

        # Construct new architecture, compute whether spec changed
        arch_spec = (str(new_plat), str(new_os), str(new_target))
        new_arch = spack.spec.ArchSpec(arch_spec)
        spec_changed = new_arch != spec.architecture
        spec.architecture = new_arch
        return spec_changed
示例#4
0
    def concretize_architecture(self, spec):
        """If the spec is empty provide the defaults of the platform. If the
        architecture is not a string type, then check if either the platform,
        target or operating system are concretized. If any of the fields are
        changed then return True. If everything is concretized (i.e the
        architecture attribute is a namedtuple of classes) then return False.
        If the target is a string type, then convert the string into a
        concretized architecture. If it has no architecture and the root of the
        DAG has an architecture, then use the root otherwise use the defaults
        on the platform.
        """
        # ensure type safety for the architecture
        if spec.architecture is None:
            spec.architecture = spack.spec.ArchSpec()

        if spec.architecture.platform and \
                (spec.architecture.os and spec.architecture.target):
            return False

        # Get platform of nearest spec with a platform, including spec
        # If spec has a platform, easy
        if spec.architecture.platform:
            new_plat = spack.architecture.get_platform(
                spec.architecture.platform)
        else:
            # Else if anyone else has a platform, take the closest one
            # Search up, then down, along build/link deps first
            # Then any nearest. Algorithm from compilerspec search
            platform_spec = find_spec(
                spec, lambda x: x.architecture and x.architecture.platform)
            if platform_spec:
                new_plat = spack.architecture.get_platform(
                    platform_spec.architecture.platform)
            else:
                # If no platform anywhere in this spec, grab the default
                new_plat = spack.architecture.platform()

        # Get nearest spec with relevant platform and an os
        # Generally, same algorithm as finding platform, except we only
        # consider specs that have a platform
        if spec.architecture.os:
            new_os = spec.architecture.os
        else:
            new_os_spec = find_spec(
                spec, lambda x: (x.architecture and x.architecture.platform ==
                                 str(new_plat) and x.architecture.os))
            if new_os_spec:
                new_os = new_os_spec.architecture.os
            else:
                new_os = new_plat.operating_system('default_os')

        # Get the nearest spec with relevant platform and a target
        # Generally, same algorithm as finding os
        if spec.architecture.target:
            new_target = spec.architecture.target
        else:
            new_target_spec = find_spec(
                spec, lambda x: (x.architecture and x.architecture.platform ==
                                 str(new_plat) and x.architecture.target))
            if new_target_spec:
                new_target = new_target_spec.architecture.target
            else:
                # To get default platform, consider package prefs
                if PackagePrefs.has_preferred_targets(spec.name):
                    new_target = self.target_from_package_preferences(spec)
                else:
                    new_target = new_plat.target('default_target')

        # Construct new architecture, compute whether spec changed
        arch_spec = (str(new_plat), str(new_os), str(new_target))
        new_arch = spack.spec.ArchSpec(arch_spec)
        spec_changed = new_arch != spec.architecture
        spec.architecture = new_arch
        return spec_changed