Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does numpy use multiple cores?

Tags:

numpy

scipy

As I understand it, python uses only one core of the CPU (?)

Does numpy use more cores to speed up calculations?

Reason for my question: I plan to buy a computer, and would like to know what CPU is best for fast numpy ( and scipy) calculations.

like image 485
Peter Stahlecker Avatar asked May 23 '26 13:05

Peter Stahlecker


1 Answers

To expand upon Michael Szczesny's comment, Numpy can utilize multiple CPU cores for performing basic vector and matrix operations by leveraging the BLAS (Basic Linear Algebra Subprograms) library(1), which gets installed with Numpy. However, it's important to note that not all operating systems and hardware configurations are supported by the BLAS library(2), which is why I tested that both macOS and Windows support BLAS as highlighted below.

Regarding the best processor, generally speaking, the more cores it has, the better — provided that it supports BLAS.

Note: (1) and (2) are citations - See references below.

Testing

Curious to see NumPy's multi-core usage in action, I ran the following Python script while monitoring CPU utilization through Activity Monitor on macOS (and similarly on Windows 10 using Task Manager):

import numpy as np

# Creating very large arrays
arr1 = np.random.rand(10000, 10000)
arr2 = np.random.rand(10000, 10000)

# Performing lots of math that should utilize multiple cores
result = np.dot(np.linalg.inv(arr1), arr2)

print("Finished")

Results

Activity Monitor revealed a noticeable spike in utilization across all ten cores when the script was run (See Screenshot). Comparable results were observed on a family member’s Windows machine equipped with an Intel CPU.

enter image description here

Tested Environment

Here are the specific CPUs and operating systems I tested:

  • macOS - Sonoma
    • Processors Tested: Apple Silicon
    • Python Versions Tested: 3.9, 3.11
    • Numpy Versions Tested: 1.23.4, 1.26.1
    • Installers Tested: pip, miniconda
  • Windows 10
    • Processors Tested: Intel i7-10510U, Intel i7-8700 (AMD processors were not available for testing)
    • Python Versions Tested: 3.9, 3.10
    • Numpy Versions Tested: 1.22.4, 1.23.1
    • Installers Tested: pip, miniconda

References

  1. BLAS (2022-11-14). Retrieved November 5, 2023, from https://www.netlib.org/blas/
  2. Parallel Programming with SciPy and NumPy (2015, revision 5e2833af). Retrieved November 5, 2023, from https://scipy-cookbook.readthedocs.io/items/ParallelProgramming.html. The pertinent quote states: "Many architectures now have a BLAS that takes advantage of a multicore machine. If your NumPy/SciPy is compiled using one of these, then dot() will be computed in parallel (if this is faster) without any additional effort on your part."
like image 130
David M. Helmuth Avatar answered May 26 '26 17:05

David M. Helmuth