File size: 2,812 Bytes
bb690a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04d5b42
bb690a1
 
 
 
 
 
 
 
 
c134b6a
 
bb690a1
 
 
 
 
 
 
 
 
 
 
 
 
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
from config import update_device_command

def parse_base_code(contents, debug = False):
    import re
    indexes = [m.start() for m in re.finditer('#####', contents)]
    
    assert len(indexes) % 2 == 0
    
    # split to span
    spans = []
    # spans.append(contents[:indexes[0]])
    for i in range(len(indexes)):
        if i != len(indexes) - 1:
            spans.append(contents[indexes[i]:indexes[i+1]])
    # spans.append(contents[indexes[-1]:])
        
    spans_with_type = [
        
    ]
    for span in spans:
        if "setup" in span:
            spans_with_type.append((span, "setup"))
        elif "loop" in span:
            spans_with_type.append((span, "loop"))
        elif "main" in span:
            spans_with_type.append((span, "command"))
        else:
            spans_with_type.append((span, "other"))
    
    spans_with_type_added_device_control = []

    for span, type_ in spans_with_type:
        if type_ == "setup":
            spans_with_type_added_device_control.append((
                """cd $(cd "$(dirname "$0")";pwd); source gpu_utility.sh\n\n"""
                , "device_control"))
            spans_with_type_added_device_control.append((span, type_))
            spans_with_type_added_device_control.append(("""date_stamp=`date '+%Y-%m-%d-%H'`\nlog_dir="logs/$exp_name/$date_stamp"\nmkdir -p $log_dir\n""", "logging"))
            # spans_with_type_added_device_control.append((gpu_env, "device_control"))
            # spans_with_type_added_device_control.append((update_device_func, "device_control"))
        elif type_ == "loop":
            spans_with_type_added_device_control.append((span, type_))
        elif type_ == "command":
            spans_with_type_added_device_control.append((update_device_command, "device_control"))
            span_remove_the_first_part = span[span.index("\n"):]
            if not debug:
                spans_with_type_added_device_control.append((f"\n\ncommand=\"\"\"{span_remove_the_first_part}\"\"\"\n", type_))
                run_command = "eval nohup $command"
                run_command += "  > $log_dir/$RANDOM$RANDOM.log 2>&1 &"
                run_command += "\n\n\n"
                spans_with_type_added_device_control.append((run_command, type_))
            else: 
                spans_with_type_added_device_control.append(
                    (f"{span_remove_the_first_part}\n", type_)
                )
            sleep_command = "sleep $sleeptime\n\n"
            spans_with_type_added_device_control.append((sleep_command, type_))
        else:
            spans_with_type_added_device_control.append((span, type_))
    spans_without_type = [span for span, type_ in spans_with_type_added_device_control]
    spans_without_type_str = "".join(spans_without_type)
    return spans_without_type_str