detkit.Profile.cpu_utilization#

Profile.cpu_utilization()#

Measure CPU utilization.

Returns:
cpu_utilfloat

CPu utilization in percent.

Notes

CPU utilization is defined as follows. Suppose there are \(m\) CPU threads. For each thread \(i = 1, \dots, m\), we define

  • \(u_i\): thread’s user time

  • \(s_i\): thread’s system time

  • \(d_i\): thread’s idle time

The CPU utilization \(\gamma\), in percent, is defined by:

\[\gamma = 100 \times \frac{1}{m} \sum_{i}^{m} \frac{u_i + s_i}{u_i + s_i + d_i}\]

Examples

Inquiry the CPU utilization for matrix-matrix multiplication \(\mathbf{C} \leftarrow \alpha \mathbf{A} \mathbf{B} + \beta \mathbf{C}\) on a machine with 8 CPU threads:

>>> from detkit import matmul, Profile

>>> # Create random matrices
>>> import numpy
>>> n = 10000
>>> A = numpy.random.randn(n, n)
>>> B = numpy.random.randn(n, n)
>>> C = numpy.random.randn(n, n)

>>> # Make sure arrays have column-ordering
>>> A = numpy.asfortranarray(A)
>>> B = numpy.asfortranarray(B)
>>> C = numpy.asfortranarray(C)

>>> # Initiate profiling
>>> prof = Profile()
>>> prof.set()

>>> # Perform matrix-matrix multiplication
>>> alpha, beta = 1.0, 2.0
>>> X = matmul(A, B, C, alpha=alpha, beta=beta, overwrite=True)

>>> # Check CPU utilization (in percent)
>>> print(prof.cpu_utilization())
56.28

The above result means 54.2 percent of all 8 CPU threads were utilized.