summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/cpu/schema.py
blob: 2cb456730ee3d8691e93dc790afb568302662d00 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import json
import os.path

try:
    from collections.abc import MutableMapping  # novm
except ImportError:
    from collections import MutableMapping

compilers_schema = {
    'type': 'object',
    'properties': {
        'versions': {'type': 'string'},
        'name': {'type': 'string'},
        'flags': {'type': 'string'}
    },
    'required': ['versions', 'flags']
}

properties = {
    'microarchitectures': {
        'type': 'object',
        'patternProperties': {
            r'([\w]*)': {
                'type': 'object',
                'properties': {
                    'from': {
                        'anyOf': [
                            # More than one parent
                            {'type': 'array', 'items': {'type': 'string'}},
                            # Exactly one parent
                            {'type': 'string'},
                            # No parent
                            {'type': 'null'}
                        ]
                    },
                    'vendor': {
                        'type': 'string'
                    },
                    'features': {
                        'type': 'array',
                        'items': {'type': 'string'}
                    },
                    'compilers': {
                        'type': 'object',
                        'patternProperties': {
                            r'([\w]*)': {
                                'anyOf': [
                                    compilers_schema,
                                    {
                                        'type': 'array',
                                        'items': compilers_schema
                                    }
                                ]
                            }
                        }
                    }
                },
                'required': ['from', 'vendor', 'features']
            }
        }
    },
    'feature_aliases': {
        'type': 'object',
        'patternProperties': {
            r'([\w]*)': {
                'type': 'object',
                'properties': {},
                'additionalProperties': False
            }
        },
    },
    'conversions': {
        'type': 'object',
        'properties': {
            'description': {
                'type': 'string'
            },
            'arm_vendors': {
                'type': 'object',
            },
            'darwin_flags': {
                'type': 'object'
            }
        },
        'additionalProperties': False
    }
}

schema = {
    '$schema': 'http://json-schema.org/schema#',
    'title': 'Schema for microarchitecture definitions and feature aliases',
    'type': 'object',
    'additionalProperties': False,
    'properties': properties,
}


class LazyDictionary(MutableMapping):
    """Lazy dictionary that gets constructed on first access to any object key

    Args:
        factory (callable): factory function to construct the dictionary
    """

    def __init__(self, factory, *args, **kwargs):
        self.factory = factory
        self.args = args
        self.kwargs = kwargs
        self._data = None

    @property
    def data(self):
        if self._data is None:
            self._data = self.factory(*self.args, **self.kwargs)
        return self._data

    def __getitem__(self, key):
        return self.data[key]

    def __setitem__(self, key, value):
        self.data[key] = value

    def __delitem__(self, key):
        del self.data[key]

    def __iter__(self):
        return iter(self.data)

    def __len__(self):
        return len(self.data)


def _load_targets_json():
    """Loads ``microarchitectures.json`` in memory."""
    directory_name = os.path.dirname(os.path.abspath(__file__))
    filename = os.path.join(directory_name, 'microarchitectures.json')
    with open(filename, 'r') as f:
        return json.load(f)


#: In memory representation of the data in microarchitectures.json,
#: loaded on first access
targets_json = LazyDictionary(_load_targets_json)