Adding Additional Parameters
To make full use of the performance advantages of multi-core CPUs, you can add the --threads parameter to the test command to manually set the number of threads.
Modify the test command by referring to the modification process of the batch_query call chain. Ensure that the main () method in /data/ann-benchmarks-main/ann_benchmarks/main.py can correctly transfer parameters.
- Open the module.py file and view the source code.
1vim /data/ann-benchmarks-main/ann_benchmarks/algorithms/base/module.py
In the batch_query method of the source code, the number of started threads is the same as the number of CPUs by default. You can manually set the number of threads by adding external input parameters.
- Open the main.py file and modify the code.
1vim /data/ann-benchmarks-main/ann_benchmarks/main.py- Add the --threads parameter to line 125.
1parser.add_argument("--threads", type=int, help="Number of threads. If not set, the default value will be used, and the number of threads enabled is equal to the number of CPU.", default=-1)

- Modify the run method in line 71 to invoke input parameters.
1run(definition, args.dataset, args.count, args.runs, args.batch, args.threads)

- Add the --threads parameter to line 125.
- Open the runner.py file and modify the code.
1vim /data/ann-benchmarks-main/ann_benchmarks/runner.py- Modify the parameter list of the run method in line 197.
1def run(definition: Definition, dataset_name: str, count: int, run_count: int, batch: bool, threads: int) -> None:

- Modify the run_individual_query method in line 230 to invoke input parameters.
1descriptor, results = run_individual_query(algo, X_train, X_test, distance, count, run_count, batch, threads)

- Modify the parameter list of the run_individual_query method in line 23.
1 2
def run_individual_query(algo: BaseANN, X_train: numpy.array, X_test: numpy.array, distance: str, count: int, run_count: int, batch: bool, threads: int) -> Tuple[dict, list]:

- Modify the batch_query method in line 86 to invoke input parameters.
1def batch_query(X: numpy.array, threads: int) -> List[Tuple[float, List[Tuple[int, float]]]]:

- Modify the batch_query method in line 105 to invoke input parameters.
1algo.batch_query(X, count, threads)

- Modify the batch_query method in line 124 to invoke input parameters.
1results = batch_query(X_test, threads)

- Modify the parameter list of the run method in line 197.
- Open the module.py file and modify the code.
1vim /data/ann-benchmarks-main/ann_benchmarks/algorithms/base/module.py- Modify the parameter list of the batch_query method.
1def batch_query(self, X: numpy.array, n: int, threads: int) -> None:
- Modify the internal implementation of the batch_query method.
1 2 3 4
if threads <= 0: pool = ThreadPool() else: pool = ThreadPool(threads)

- Modify the parameter list of the batch_query method.
Parent topic: Running Test