summaryrefslogtreecommitdiff
path: root/lib/spack/spack/platforms/cray.py
blob: dc959c7eafea2f7d03c29dc67279fbab2d9a86a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import re
import spack.config
from spack.util.executable import which
from spack.architecture import Platform, Target, NoPlatformError
from spack.operating_systems.linux_distro import LinuxDistro
from spack.operating_systems.cnl import Cnl


# Craype- module prefixes that are not valid CPU targets.
NON_TARGETS = ('hugepages', 'network', 'target', 'accel', 'xtpe')


def _target_from_clean_env(name):
    '''Return the default back_end target as loaded in a clean login session.

    A bash subshell is launched with a wiped environment and the list of loaded
    modules is parsed for the first acceptable CrayPE target.
    '''
    # Based on the incantation:
    # echo "$(env - USER=$USER /bin/bash -l -c 'module list -lt')"
    default_modules = []
    targets = []
    if name != 'front_end':
        env = which('env')
        env.add_default_arg('-')
        # CAUTION - $USER is generally needed to initialize the environment.
        # There may be other variables needed for general success.
        output = env('USER=%s' % os.environ['USER'],
                    '/bin/bash', '-l', '-c', 'module list -lt',
                    output=str, error=str)
        pattern = 'craype-(?!{0})(\S*)'.format('|'.join(NON_TARGETS))
        for line in output.splitlines():
            if 'craype-' in line:
                targets.extend(re.findall(pattern, line))
            if len(line.split()) == 1:
                default_modules.append(line)
        # if default_modules:
        #     print 'Found default modules:'
        #     for defmod in default_modules:
        #         print '    ', defmod
    return targets[0] if targets else None


class Cray(Platform):
    priority = 10

    def __init__(self):
        ''' Create a Cray system platform.

        Target names should use craype target names but not include the
        'craype-' prefix. Uses first viable target from:
          self
          envars [SPACK_FRONT_END, SPACK_BACK_END]
          configuration file "targets.yaml" with keys 'front_end', 'back_end'
          scanning /etc/bash/bashrc.local for back_end only
        '''
        super(Cray, self).__init__('cray')

        # Get targets from config or make best guess from environment:
        conf = spack.config.get_config('targets')
        for name in ('front_end', 'back_end'):
            _target = getattr(self, name, None)
            if _target is None:
                _target = os.environ.get('SPACK_' + name.upper())
            if _target is None:
                _target = conf.get(name)
            if _target is None:
                _target = _target_from_clean_env(name)
            setattr(self, name, _target)

            if _target is not None:
                self.add_target(name, Target(_target, 'craype-' + _target))
                self.add_target(_target, Target(_target, 'craype-' + _target))

        if self.back_end is not None:
            self.default = self.back_end
            self.add_target(
                'default', Target(self.default, 'craype-' + self.default))
        else:
            raise NoPlatformError()

        front_distro = LinuxDistro()
        back_distro = Cnl()

        self.default_os = str(back_distro)
        self.back_os = self.default_os
        self.front_os = str(front_distro)

        self.add_operating_system(self.back_os, back_distro)
        self.add_operating_system(self.front_os, front_distro)

    @classmethod
    def setup_platform_environment(self, pkg, env):
        """ Change the linker to default dynamic to be more
            similar to linux/standard linker behavior
        """
        env.set('CRAYPE_LINK_TYPE', 'dynamic')
        cray_wrapper_names = join_path(spack.build_env_path, 'cray')
        if os.path.isdir(cray_wrapper_names):
            env.prepend_path('PATH', cray_wrapper_names)
            env.prepend_path('SPACK_ENV_PATHS', cray_wrapper_names)

    @classmethod
    def detect(self):
        return os.environ.get('CRAYPE_VERSION') is not None