-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-completed-chunks.py
executable file
·53 lines (45 loc) · 1.36 KB
/
list-completed-chunks.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
#!/usr/bin/python3
# Lists the completed chunks in the binary output file produced by
# backpropagate-losses.
#
# The file starts with a header of 4096 bytes, of which the first 7429 bits mark
# the chunks that have been completed. The remaining bits are supposed to be
# zero (which the tool also checks).
#
# Completed chunk numbers are listed line-by-line. Each line contains either
# a single chunk or a range with the second endpoint exclusive. For example:
#
# 100 (1 chunk with index 100)
# 200-204 (4 chunk with indices 200, 201, 202, 203)
#
import sys
if len(sys.argv) != 2:
print('Usage: %s <filename>' % sys.argv[0])
sys.exit(0)
num_chunks = 7429
size_bytes = 4096
size_bits = size_bytes * 8
with open(sys.argv[1], mode='rb') as file:
data = file.read(size_bytes)
if len(data) != size_bytes:
print('Short read from input! Expected %d bytes, read %d.' % (size_bytes, len(data)))
sys.exit(1)
def GetBit(i):
return (data[i // 8] >> (i % 8)) & 1
i = 0
while i < size_bits:
while i < size_bits and GetBit(i) == 0:
i += 1
if i == size_bits:
break
if i >= num_chunks:
print('WARNING: detected unexpected 1 bit at index', i)
break
j = i + 1
while j < num_chunks and GetBit(j) == 1:
j += 1
if j - i == 1:
print('%d' % (i,)) # single chunk
if j - i > 1:
print('%d-%d' % (i, j)) # range from i to j (exclusive)
i = j