forked from ibackus/ICgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ICglobal_settings.py
209 lines (140 loc) · 6.09 KB
/
ICglobal_settings.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
"""
This is the global settings module for ICgen. To access the global settings,
simply import them:
from ICglobal_settings import global_settings
Changing/accessing settings is the same as for a dict, ie:
global_settings[key] = val
Settings can be saved via global_settings.save()
Defaults can be restored by global_settings.restore_defaults()
Created on Mon Aug 11 14:38:17 2014
@author: ibackus
"""
import cPickle as pickle
import os
import socket
from textwrap import TextWrapper
_dir = os.path.dirname(os.path.abspath(__file__))
_filename = os.path.join(_dir, 'global_settings.p')
# --------------------------------------------------------------
# DEFAULT SETTINGS
# --------------------------------------------------------------
defaults = {}
# ***** Misc *****
misc = {}
# Maximum number of particles used in calculating velocities
misc['max_particles'] = int(1e7)
defaults['misc'] = misc
# ***** Cluster presets *****
node_info = {}
node_info['scheduler'] = 'PBS'
defaults['node_info'] = node_info
# ***** ChaNGa presets *****
# Format : [runner, runner args, ChaNGa, ChaNGa args]
changa_presets = {}
changa_presets['local'] = ['charmrun_sinks', '++local', 'ChaNGa_sinks', '-D 3 +consph']
changa_presets['mpi'] = ['mpirun', '--mca mtl mx --mca pml cm', 'ChaNGa_uw_mpi', '-D 3 +consph']
changa_presets['default'] = 'local'
defaults['changa_presets'] = changa_presets
# --------------------------------------------------------------
# Settings class
# --------------------------------------------------------------
class settings(dict):
"""
Global settings object. Call without arguments to echo settings.
Changing/accessing settings is the same as for a dict, ie:
global_settings[key] = val
Settings can be saved via global_settings.save()
Defaults can be restored by global_settings.restore_defaults()
Dynamically generated settings (such as node information) can be
re-generated by global_settings.dynamic_settings().
"""
@classmethod
def loader(cls, filename):
return pickle.load(open(filename, 'rb'))
def __init__(self):
dict.__init__(self)
self.filename = _filename
# Load settings and compare to defaults. If the settings don't contain
# an entry in defaults, add it to the settings
if os.path.isfile(self.filename):
# Load up previous settings
current_settings = settings.loader(self.filename)
# Compare to defaults
for key, value in defaults.iteritems():
if key not in current_settings:
current_settings[key] = value
else:
current_settings = defaults
for key, val in current_settings.iteritems():
self[key] = val
self.dynamic_settings()
def __call__(self):
def print_dict(a, n_tabs = 0):
tab = n_tabs * 2 * ' '
wrapper = TextWrapper(80, tab, tab)
for key, val in a.iteritems():
if isinstance(val, dict):
print ''
print wrapper.fill('{0}'.format(key))
#print n_tabs*' ', key, ':'
print_dict(val, n_tabs+1)
print ''
else:
#print n_tabs*' ',key,val
print wrapper.fill('{0} : {1}'.format(key,val))
print '**** GLOBAL SETTINGS ****'
print_dict(self)
def restore_defaults(self):
"""
Restore to default settings
"""
keys = self.keys()
for key in keys:
self.pop(key, None)
for key, val in defaults.iteritems():
self[key] = val
self.dynamic_settings()
def dynamic_settings(self):
"""
Generates the dynamically created settings
"""
# --------------------------------------------------------
# Generate node information
# --------------------------------------------------------
node_info = self['node_info']
node_info['hostname'] = socket.gethostname()
scheduler = node_info['scheduler']
if scheduler == 'PBS':
if 'PBS_NODEFILE' not in os.environ:
nodelist = [node_info['hostname']]
else:
nodefile = os.environ['PBS_NODEFILE']
nodelist = []
for line in open(nodefile,'r'):
nodelist.append(line.strip())
nodelist = list(set(nodelist))
else:
# Assume there's no scheduler
return [node_info['hostname']]
node_info['nodelist'] = nodelist
secondary_nodes = set(node_info['nodelist'])
secondary_nodes.discard(node_info['hostname'])
node_info['secondary_nodes'] = list(secondary_nodes)
self['node_info'] = node_info
# Dynamically generated changa presets
if 'mpi' in self['changa_presets']:
mpi_largefiles = list(self['changa_presets']['mpi'])
hosts = ','.join(node_info['secondary_nodes'])
host_flag = ' --host ' + hosts
mpi_largefiles[1] += host_flag
self['changa_presets']['mpi_largefiles'] = mpi_largefiles
def save(self):
"""
Save settings
"""
pickle.dump(self, open(self.filename, 'wb'), 2)
# --------------------------------------------------------------
# Create settings
# --------------------------------------------------------------
global_settings = settings()