-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
69 lines (54 loc) · 2.12 KB
/
misc.py
File metadata and controls
69 lines (54 loc) · 2.12 KB
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
import os
import sys
import csv
#from mpi4py import MPI
#comm = MPI.COMM_WORLD
#n_ranks = comm.Get_size()
#rank = comm.Get_rank()
def print_flush_root(this_rank, val, output_file='', root=0, output_folder='./', save_stdout=True, print_terminal=False):
# print(or not) the argument, val, for all ranks.
# save(or not) the argument, val, if the current rank is the root rank
if print_terminal:
print("rank:", this_rank, val)
if save_stdout and this_rank == root:
if not os.path.exists(output_folder):
os.mkdir(output_folder)
if not output_file:
output_file = "stdo.csv"
file_path = os.path.join(output_folder, output_file)
with open(file_path, 'a') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow([this_rank ,val])
sys.stdout.flush()
return None
def print_flush_all(this_rank, val, output_file='', output_folder='./', save_stdout=True, print_terminal=False):
# print and/or save the argument, val, for all ranks.
if print_terminal:
print("rank:", this_rank, val)
if save_stdout:
if not os.path.exists(output_folder):
os.mkdir(output_folder)
if not output_file:
output_file = f"stdo_{this_rank}.csv"
file_path = os.path.join(output_folder, output_file)
with open(file_path, 'a') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow([this_rank ,val])
sys.stdout.flush()
return None
def create_summary(save_path, locals_dict, fname="summary.txt", verbose=False):
if not os.path.exists(save_path):
os.makedirs(save_path)
f = open(os.path.join(save_path, fname), 'w')
f.write('============== PARAMETERS ==============\n')
for var_name in locals_dict:
try:
line = '{:<40}{}\n'.format(var_name, str(locals_dict[var_name]))
if verbose:
print(line)
f.write(line)
except:
pass
f.write('========================================')
f.close()
return None