Beispiel #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.platforms.by_name(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.target.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
Beispiel #2
0
    def concretize_variants(self, spec):
        """If the spec already has variants filled in, return.  Otherwise, add
           the user preferences from packages.yaml or the default variants from
           the package specification.
        """
        changed = False
        preferred_variants = PackagePrefs.preferred_variants(spec.name)
        pkg_cls = spec.package_class
        for name, variant in pkg_cls.variants.items():
            if name not in spec.variants:
                changed = True
                if name in preferred_variants:
                    spec.variants[name] = preferred_variants.get(name)
                else:
                    spec.variants[name] = variant.make_default()

        return changed
Beispiel #3
0
    def concretize_variants(self, spec):
        """If the spec already has variants filled in, return.  Otherwise, add
           the user preferences from packages.yaml or the default variants from
           the package specification.
        """
        changed = False
        preferred_variants = PackagePrefs.preferred_variants(spec.name)
        pkg_cls = spec.package_class
        for name, variant in pkg_cls.variants.items():
            var = spec.variants.get(name, None)
            if var and '*' in var:
                # remove variant wildcard before concretizing
                # wildcard cannot be combined with other variables in a
                # multivalue variant, a concrete variant cannot have the value
                # wildcard, and a wildcard does not constrain a variant
                spec.variants.pop(name)
            if name not in spec.variants:
                changed = True
                if name in preferred_variants:
                    spec.variants[name] = preferred_variants.get(name)
                else:
                    spec.variants[name] = variant.make_default()

        return changed
Beispiel #4
0
    def concretize_compiler(self, spec):
        """If the spec already has a compiler, we're done.  If not, then take
           the compiler used for the nearest ancestor with a compiler
           spec and use that.  If the ancestor's compiler is not
           concrete, then used the preferred compiler as specified in
           spackconfig.

           Intuition: Use the spackconfig default if no package that depends on
           this one has a strict compiler requirement.  Otherwise, try to
           build with the compiler that will be used by libraries that
           link to this one, to maximize compatibility.
        """
        # Pass on concretizing the compiler if the target or operating system
        # is not yet determined
        if not spec.architecture.concrete:
            # We haven't changed, but other changes need to happen before we
            # continue. `return True` here to force concretization to keep
            # running.
            return True

        # Only use a matching compiler if it is of the proper style
        # Takes advantage of the proper logic already existing in
        # compiler_for_spec Should think whether this can be more
        # efficient
        def _proper_compiler_style(cspec, aspec):
            compilers = spack.compilers.compilers_for_spec(
                cspec, arch_spec=aspec
            )
            # If the spec passed as argument is concrete we want to check
            # the versions match exactly
            if (cspec.concrete and compilers and
                cspec.version not in [c.version for c in compilers]):
                return []

            return compilers

        if spec.compiler and spec.compiler.concrete:
            if (self.check_for_compiler_existence and not
                    _proper_compiler_style(spec.compiler, spec.architecture)):
                _compiler_concretization_failure(
                    spec.compiler, spec.architecture)
            return False

        # Find another spec that has a compiler, or the root if none do
        other_spec = spec if spec.compiler else find_spec(
            spec, lambda x: x.compiler, spec.root)
        other_compiler = other_spec.compiler
        assert other_spec

        # Check if the compiler is already fully specified
        if other_compiler and other_compiler.concrete:
            if (self.check_for_compiler_existence and not
                    _proper_compiler_style(other_compiler, spec.architecture)):
                _compiler_concretization_failure(
                    other_compiler, spec.architecture)
            spec.compiler = other_compiler
            return True

        if other_compiler:  # Another node has abstract compiler information
            compiler_list = spack.compilers.find_specs_by_arch(
                other_compiler, spec.architecture
            )
            if not compiler_list:
                # We don't have a matching compiler installed
                if not self.check_for_compiler_existence:
                    # Concretize compiler spec versions as a package to build
                    cpkg_spec = spack.compilers.pkg_spec_for_compiler(
                        other_compiler
                    )
                    self.concretize_version(cpkg_spec)
                    spec.compiler = spack.spec.CompilerSpec(
                        other_compiler.name, cpkg_spec.versions)
                    return True
                else:
                    # No compiler with a satisfactory spec was found
                    raise UnavailableCompilerVersionError(
                        other_compiler, spec.architecture
                    )
        else:
            # We have no hints to go by, grab any compiler
            compiler_list = spack.compilers.all_compiler_specs()
            if not compiler_list:
                # Spack has no compilers.
                raise spack.compilers.NoCompilersError()

        # By default, prefer later versions of compilers
        compiler_list = sorted(
            compiler_list, key=lambda x: (x.name, x.version), reverse=True)
        ppk = PackagePrefs(other_spec.name, 'compiler')
        matches = sorted(compiler_list, key=ppk)

        # copy concrete version into other_compiler
        try:
            spec.compiler = next(
                c for c in matches
                if _proper_compiler_style(c, spec.architecture)).copy()
        except StopIteration:
            # No compiler with a satisfactory spec has a suitable arch
            _compiler_concretization_failure(
                other_compiler, spec.architecture)

        assert spec.compiler.concrete
        return True  # things changed.
Beispiel #5
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
Beispiel #6
0
    def concretize_version(self, spec):
        """If the spec is already concrete, return.  Otherwise take
           the preferred version from spackconfig, and default to the package's
           version if there are no available versions.

           TODO: In many cases we probably want to look for installed
                 versions of each package and use an installed version
                 if we can link to it.  The policy implemented here will
                 tend to rebuild a lot of stuff becasue it will prefer
                 a compiler in the spec to any compiler already-
                 installed things were built with.  There is likely
                 some better policy that finds some middle ground
                 between these two extremes.
        """
        # return if already concrete.
        if spec.versions.concrete:
            return False

        # List of versions we could consider, in sorted order
        pkg_versions = spec.package_class.versions
        usable = [v for v in pkg_versions
                  if any(v.satisfies(sv) for sv in spec.versions)]

        yaml_prefs = PackagePrefs(spec.name, 'version')

        # The keys below show the order of precedence of factors used
        # to select a version when concretizing.  The item with
        # the "largest" key will be selected.
        #
        # NOTE: When COMPARING VERSIONS, the '@develop' version is always
        #       larger than other versions.  BUT when CONCRETIZING,
        #       the largest NON-develop version is selected by default.
        keyfn = lambda v: (
            # ------- Special direction from the user
            # Respect order listed in packages.yaml
            -yaml_prefs(v),

            # The preferred=True flag (packages or packages.yaml or both?)
            pkg_versions.get(Version(v)).get('preferred', False),

            # ------- Regular case: use latest non-develop version by default.
            # Avoid @develop version, which would otherwise be the "largest"
            # in straight version comparisons
            not v.isdevelop(),

            # Compare the version itself
            # This includes the logic:
            #    a) develop > everything (disabled by "not v.isdevelop() above)
            #    b) numeric > non-numeric
            #    c) Numeric or string comparison
            v)
        usable.sort(key=keyfn, reverse=True)

        if usable:
            spec.versions = ver([usable[0]])
        else:
            # We don't know of any SAFE versions that match the given
            # spec.  Grab the spec's versions and grab the highest
            # *non-open* part of the range of versions it specifies.
            # Someone else can raise an error if this happens,
            # e.g. when we go to fetch it and don't know how.  But it
            # *might* work.
            if not spec.versions or spec.versions == VersionList([':']):
                raise NoValidVersionError(spec)
            else:
                last = spec.versions[-1]
                if isinstance(last, VersionRange):
                    if last.end:
                        spec.versions = ver([last.end])
                    else:
                        spec.versions = ver([last.start])
                else:
                    spec.versions = ver([last])

        return True   # Things changed
Beispiel #7
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
Beispiel #8
0
    def concretize_compiler(self, spec):
        """If the spec already has a compiler, we're done.  If not, then take
           the compiler used for the nearest ancestor with a compiler
           spec and use that.  If the ancestor's compiler is not
           concrete, then used the preferred compiler as specified in
           spackconfig.

           Intuition: Use the spackconfig default if no package that depends on
           this one has a strict compiler requirement.  Otherwise, try to
           build with the compiler that will be used by libraries that
           link to this one, to maximize compatibility.
        """
        # Pass on concretizing the compiler if the target or operating system
        # is not yet determined
        if not (spec.architecture.platform_os and spec.architecture.target):
            # We haven't changed, but other changes need to happen before we
            # continue. `return True` here to force concretization to keep
            # running.
            return True

        # Only use a matching compiler if it is of the proper style
        # Takes advantage of the proper logic already existing in
        # compiler_for_spec Should think whether this can be more
        # efficient
        def _proper_compiler_style(cspec, aspec):
            return spack.compilers.compilers_for_spec(cspec, arch_spec=aspec)

        if spec.compiler and spec.compiler.concrete:
            if (self.check_for_compiler_existence and not
                    _proper_compiler_style(spec.compiler, spec.architecture)):
                _compiler_concretization_failure(
                    spec.compiler, spec.architecture)
            return False

        # Find another spec that has a compiler, or the root if none do
        other_spec = spec if spec.compiler else find_spec(
            spec, lambda x: x.compiler, spec.root)
        other_compiler = other_spec.compiler
        assert(other_spec)

        # Check if the compiler is already fully specified
        if (other_compiler and other_compiler.concrete and
                not self.check_for_compiler_existence):
            spec.compiler = other_compiler.copy()
            return True

        all_compiler_specs = spack.compilers.all_compiler_specs()
        if not all_compiler_specs:
            # If compiler existence checking is disabled, then we would have
            # exited by now if there were sufficient hints to form a full
            # compiler spec. Therefore even if compiler existence checking is
            # disabled, compilers must be available at this point because the
            # available compilers are used to choose a compiler. If compiler
            # existence checking is enabled then some compiler must exist in
            # order to complete the spec.
            raise spack.compilers.NoCompilersError()

        if other_compiler in all_compiler_specs:
            spec.compiler = other_compiler.copy()
            if not _proper_compiler_style(spec.compiler, spec.architecture):
                _compiler_concretization_failure(
                    spec.compiler, spec.architecture)
            return True

        # Filter the compilers into a sorted list based on the compiler_order
        # from spackconfig
        compiler_list = all_compiler_specs if not other_compiler else \
            spack.compilers.find(other_compiler)
        if not compiler_list:
            # No compiler with a satisfactory spec was found
            raise UnavailableCompilerVersionError(other_compiler)

        # By default, prefer later versions of compilers
        compiler_list = sorted(
            compiler_list, key=lambda x: (x.name, x.version), reverse=True)
        ppk = PackagePrefs(other_spec.name, 'compiler')
        matches = sorted(compiler_list, key=ppk)

        # copy concrete version into other_compiler
        try:
            spec.compiler = next(
                c for c in matches
                if _proper_compiler_style(c, spec.architecture)).copy()
        except StopIteration:
            # No compiler with a satisfactory spec has a suitable arch
            _compiler_concretization_failure(
                other_compiler, spec.architecture)

        assert(spec.compiler.concrete)
        return True  # things changed.
Beispiel #9
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