forked from IntelLabs/open-omics-alphafold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_multiprocess_pre.py
184 lines (154 loc) · 6.33 KB
/
run_multiprocess_pre.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
import subprocess
import os
import psutil
import time
import multiprocessing as mp
from absl import app
from absl import flags
from absl import logging
flags.DEFINE_string('root_condaenv', None, 'conda environment directory path')
flags.DEFINE_string('root_home', None, 'home directory')
flags.DEFINE_string('data_dir', None, 'Path to directory of supporting data.')
flags.DEFINE_string('input_dir', None, 'root directory holding all .fa files')
flags.DEFINE_string('output_dir', None, 'Path to a directory that will store the results.')
flags.DEFINE_string('model_name', None, 'Names of models to use')
FLAGS = flags.FLAGS
script = "python run_preprocess.py"
base_fold_cmd = "{} \
--n_cpu={} \
--fasta_paths={} \
--output_dir={} \
--model_names={} \
--bfd_database_path={}/bfd/bfd_metaclust_clu_complete_id30_c90_final_seq.sorted_opt \
--uniref30_database_path={}/uniref30/UniRef30_2021_03 \
--uniref90_database_path={}/uniref90/uniref90.fasta \
--mgnify_database_path={}/mgnify/mgy_clusters_2022_05.fa \
--pdb70_database_path={}/pdb70/pdb70 \
--template_mmcif_dir={}/pdb_mmcif/mmcif_files \
--data_dir={} \
--max_template_date=2022-01-01 \
--obsolete_pdbs_path={}/pdb_mmcif/obsolete.dat \
--hhblits_binary_path=$PWD/hh-suite/build/release/bin/hhblits \
--hhsearch_binary_path=$PWD/hh-suite/build/release/bin/hhsearch \
--jackhmmer_binary_path=$PWD/hmmer/release/bin/jackhmmer \
--kalign_binary_path=`which kalign` \
--run_in_parallel=true \
"
def start_bash_subprocess(file_path, mem, core_list):
"""Starts a new bash subprocess and puts it on the specified cores."""
data_dir = FLAGS.data_dir
out_dir = FLAGS.output_dir
log_dir = FLAGS.root_home + "/logs/"
model_name=FLAGS.model_name
n_cpu = str(len(core_list))
command = base_fold_cmd.format(script, n_cpu, file_path, out_dir, model_name, data_dir, data_dir, data_dir, data_dir, data_dir, data_dir, data_dir, data_dir)
numactl_args = ["numactl", "-m", mem, "-C", "-".join([str(core_list[0]), str(core_list[-1])]), command]
print(" ".join(numactl_args))
with open(log_dir + 'pre_log_' + os.path.basename(file_path) + '.txt', 'w') as f:
try:
process = subprocess.call(" ".join(numactl_args), shell=True, universal_newlines=True, stdout=f, stderr=f)
except Exception as e:
print('exception for', os.path.basename(file_path), e)
return (process, file_path, mem, core_list)
def check_available_memory():
"""Checks for available memory using psutil."""
mem = psutil.virtual_memory()
available_memory = mem.available
return available_memory / 1024 ** 2
def get_file_size(file_path):
"""Gets the size of the file in bytes."""
size = subprocess.check_output(["wc", "-c", file_path])
size = int(size.decode("utf-8").split()[0])
return size
def multiprocessing_run(files, max_processes):
size_dict = dict()
for file in files:
size_dict[file] = get_file_size(file)
sorted_size_dict = dict(sorted(size_dict.items(), key=lambda item: item[1], reverse=True))
total_cores = os.cpu_count()//2
core_list = range(os.cpu_count()//2)
cores_per_process = total_cores // max_processes
pool = mp.Pool(processes=max_processes)
queue = [i for i in range(max_processes)]
error_files = []
def update_queue(result):
print(result)
queue.append(result[3][0] // cores_per_process)
if (result[0] != 0):
error_files.append(result[1])
# Iterate over the files and start a new subprocess for each file.
print(len(sorted_size_dict))
results = [None] * len(sorted_size_dict)
#numa_nodes
lscpu = subprocess.Popen(["lscpu"], stdout=subprocess.PIPE)
grep = subprocess.Popen(["grep", "NUMA node(s):"], stdin=lscpu.stdout, stdout=subprocess.PIPE)
awk = subprocess.Popen(["awk", "{print $3}"], stdin=grep.stdout, stdout=subprocess.PIPE)
#Get the output
numa_nodes = int(awk.communicate()[0])
i = 0
for file, value in sorted_size_dict.items():
file_path = file
process_num = queue.pop(0)
if process_num < max_processes//2:
mem = '0'
else:
if numa_nodes > 1:
mem = '1'
else:
mem = '0'
if max_processes == 1:
if numa_nodes > 1:
mem = '0,1'
else:
mem = '0'
results[i] = pool.apply_async(start_bash_subprocess, args=(file_path, mem, core_list[process_num*cores_per_process: (process_num+1)*cores_per_process]), callback = update_queue)
i += 1
while len(queue) == 0 and i < len(sorted_size_dict):
time.sleep(0.05)
pool.close()
pool.join()
return error_files
def main(argv):
t1 = time.time()
input_dir = FLAGS.input_dir
"""The main function."""
directory = input_dir
total_cores = os.cpu_count()//2
print("Total cores: ", os.cpu_count() // 2)
print("Total memory: {} MB ".format(check_available_memory()))
if check_available_memory() > 1024*1024 and total_cores % 32 == 0:
max_processes_list = [32, 16, 8, 4, 2, 1]
elif check_available_memory() > 512*1024 and total_cores % 16 == 0:
max_processes_list = [16, 8, 4, 2, 1]
elif check_available_memory() > 256*1024 and total_cores % 8 == 0:
max_processes_list = [8, 4, 2, 1]
else:
max_processes_list = [4, 2, 1]
# Get the list of files in the directory.
files = os.listdir(directory)
for i, file in enumerate(files):
files[i] = os.path.join(directory, file)
for max_processes in max_processes_list:
os.environ["OMP_NUM_THREADS"] = str(total_cores//max_processes)
print("Number of OMP Threads = {}, for {} instances".format(os.environ.get('OMP_NUM_THREADS'), max_processes))
if len(files) >= max_processes:
returned_files = multiprocessing_run(files, max_processes)
print("Following protein files couldn't be processed with {} instances".format(max_processes))
print(returned_files)
else:
continue
files = returned_files
print("Following protein files couldn't be processed")
print(files)
t2 = time.time()
print('### total preprocessing time: %d sec' % (t2-t1))
if __name__ == "__main__":
flags.mark_flags_as_required([
'root_home',
'data_dir',
'input_dir',
'output_dir',
'model_name'
])
# main()
app.run(main)