Skip to content

Commit a09bcbf

Browse files
jozefCerJozef Cernansky
and
Jozef Cernansky
authored
Added thread control (#3)
Co-authored-by: Jozef Cernansky <[email protected]>
1 parent 22910c7 commit a09bcbf

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

task1.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from __future__ import annotations
22

3+
import os
4+
os.environ['MKL_NUM_THREADS'] = '27'
5+
os.environ['OMP_NUM_THREADS'] = '27'
6+
os.environ['OMP_DYNAMIC'] = 'FALSE'
7+
os.environ['MKL_DYNAMIC'] = 'FALSE'
8+
39
import argparse
410
import gc
511
import time
@@ -20,6 +26,8 @@
2026

2127
import utils
2228

29+
torch.set_num_threads(27)
30+
faiss.omp_set_num_threads(27)
2331
SEED = 42
2432
torch.manual_seed(SEED)
2533

@@ -121,7 +129,10 @@ def search(self, queries: Tensor, k: int, nprobe: int = 100) -> tuple[np.ndarray
121129
D = np.empty((n_queries, k), dtype=np.float16)
122130
I = np.empty((n_queries, k), dtype=np.int32)
123131

124-
with ThreadPoolExecutor() as executor:
132+
torch.set_num_threads(3)
133+
faiss.omp_set_num_threads(3)
134+
135+
with ThreadPoolExecutor(max_workers=9) as executor:
125136
results = executor.map(
126137
lambda i: self._visit_buckets(k, predicted_bucket_ids[i], queries[i : i + 1], i, nprobe),
127138
range(n_queries),

task2.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from __future__ import annotations
22

3+
import os
4+
os.environ['MKL_NUM_THREADS'] = '4'
5+
os.environ['OMP_NUM_THREADS'] = '4'
6+
os.environ['OMP_DYNAMIC'] = 'FALSE'
7+
os.environ['MKL_DYNAMIC'] = 'FALSE'
8+
39
import argparse
410
import gc
511
import time
@@ -21,6 +27,8 @@
2127

2228
import utils
2329

30+
torch.set_num_threads(4)
31+
faiss.omp_set_num_threads(4)
2432
SEED = 42
2533
torch.manual_seed(SEED)
2634

@@ -143,7 +151,10 @@ def search(
143151
D = np.empty((n_queries, k), dtype=np.float16)
144152
I = np.empty((n_queries, k), dtype=np.int32)
145153

146-
with ThreadPoolExecutor() as executor:
154+
torch.set_num_threads(2)
155+
faiss.omp_set_num_threads(2)
156+
157+
with ThreadPoolExecutor(max_workers=2) as executor:
147158
results = executor.map(
148159
lambda i: self._visit_buckets(
149160
k,
@@ -202,7 +213,7 @@ def _chunk_sort(self, dataset: Path, classes: Tensor, chunk_i: int, offsets: Off
202213

203214
del chunk
204215

205-
with ThreadPoolExecutor() as executor:
216+
with ThreadPoolExecutor(max_workers=12) as executor:
206217
executor.map(
207218
lambda x: self._sort_data(
208219
decomposed_chunk,
@@ -244,7 +255,7 @@ def _create_buckets(self, dataset: Path, n_data: int, chunk_size: int) -> float:
244255
offsets = self._create_offsets(classes, n_chunks, chunk_size)
245256

246257
logger.debug('Started bucket_init')
247-
with ThreadPoolExecutor() as executor:
258+
with ThreadPoolExecutor(max_workers=12) as executor:
248259
executor.map(lambda x: self._bucket_init(classes, x), range(self.n_buckets))
249260

250261
logger.debug('First part done')

task3.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from __future__ import annotations
22

3+
import os
4+
os.environ['MKL_NUM_THREADS'] = '27'
5+
os.environ['OMP_NUM_THREADS'] = '27'
6+
os.environ['OMP_DYNAMIC'] = 'FALSE'
7+
os.environ['MKL_DYNAMIC'] = 'FALSE'
8+
39
import argparse
410
import gc
511
import time
@@ -21,6 +27,8 @@
2127

2228
import utils
2329

30+
torch.set_num_threads(27)
31+
faiss.omp_set_num_threads(27)
2432
SEED = 42
2533
torch.manual_seed(SEED)
2634

@@ -130,7 +138,10 @@ def search(
130138
D = np.empty((n_queries, k), dtype=np.float16)
131139
I = np.empty((n_queries, k), dtype=np.int32)
132140

133-
with ThreadPoolExecutor() as executor:
141+
torch.set_num_threads(3)
142+
faiss.omp_set_num_threads(3)
143+
144+
with ThreadPoolExecutor(max_workers=9) as executor: # max_workers=9
134145
results = executor.map(
135146
lambda i: self._visit_buckets(k, predicted_bucket_ids[i], decomposed_queries[i : i + 1], i, nprobe),
136147
range(n_queries),
@@ -181,7 +192,7 @@ def _chunk_sort(self, dataset: Path, classes: Tensor, chunk_i: int, offsets: Off
181192

182193
del chunk
183194

184-
with ThreadPoolExecutor() as executor:
195+
with ThreadPoolExecutor(max_workers=32) as executor:
185196
executor.map(
186197
lambda x: self._sort_data(
187198
decomposed_chunk, # noqa: F821
@@ -222,7 +233,7 @@ def _create_buckets(self, dataset: Path, n_data: int, chunk_size: int) -> float:
222233

223234
offsets = self._create_offsets(classes, n_chunks, chunk_size)
224235

225-
with ThreadPoolExecutor() as executor:
236+
with ThreadPoolExecutor(max_workers=32) as executor:
226237
executor.map(lambda x: self._bucket_init(classes, x), range(self.n_buckets))
227238

228239
logger.debug('First part done')

0 commit comments

Comments
 (0)