2. Run imate Functions on GPU#
All functions in imate that accept the SLQ method (using method=slq
argument) can perform computations on GPU devices. To do so, include gpu=True
argument to the function syntax. The following examples show using multi-GPU devices to compute the log-determinant of a large matrix.
2.1. A Simple Example#
First, create a sample Toeplitz matrix with ten million in size using imate.toeplitz()
function.
>>> # Import toeplitz matrix
>>> from imate import toeplitz
>>> # Generate a sample matrix (a toeplitz matrix)
>>> n = 10000000
>>> A = toeplitz(2, 1, size=n, gram=True)
Next, create an imate.Matrix
object from matrix A:
>>> # Import Matrix class
>>> from imate import Matrix
>>> # Create a matrix operator object from matrix A
>>> Aop = Matrix(A)
Compute the log-determinant of the above matrix on GPU by passing gpu=True
to imate.logdet()
function. Recall GPU can only be employed using SLQ method by passing method=slq
argument.
>>> # Import logdet function
>>> from imate import logdet
>>> # Compute log-determinant of Aop
>>> logdet(Aop, method='slq', gpu=True)
13862193.020813728
2.2. Get Process Information#
It is useful pass the argument return_info=True
to get information about the computation process.
>>> # Compute log-determinant of Aop
>>> ld, info = logdet(Aop, method='slq', gpu=True, return_info=True)
The information about GPU devices used during the computation can be found in info['device']
key:
>>> from pprint import pprint
>>> pprint(info['device'])
{
'num_cpu_threads': 8,
'num_gpu_devices': 4,
'num_gpu_multiprocessors': 28,
'num_gpu_threads_per_multiprocessor': 2048
}
The processing time can be obtained by info['time']
key:
>>> pprint(info['time'])
{
'alg_wall_time': 1.7192635536193848,
'cpu_proc_time': 3.275628339,
'tot_wall_time': 3.5191736351698637
}
2.3. Verbose Output#
Alternatively, to print verbose information, including the information about GPU devices, pass verbose=True
to the function argument:
>>> # Compute log-determinant of Aop
>>> logdet(Aop, method='slq', gpu=True, verbose=True)
The above script prints the following table. The last section of the table shows device information.
results
==============================================================================
inquiries error samples
-------------------- --------------------- ---------
i parameters trace absolute relative num out converged
==============================================================================
1 none +1.386e+07 1.715e+03 0.012% 10 0 True
config
==============================================================================
matrix stochastic estimator
------------------------------------- -------------------------------------
gram: False method: slq
exponent: 1 lanczos degree: 20
num matrix parameters: 0 lanczos tol: 2.220e-16
data type: 64-bit orthogonalization: none
convergence error
------------------------------------- -------------------------------------
min num samples: 10 abs error tol: 0.000e+00
max num samples: 50 rel error tol: 1.00%
outlier significance level: 0.00% confidence level: 95.00%
process
==============================================================================
time device
------------------------------------- -------------------------------------
tot wall time (sec): 1.744e+00 num cpu threads: 8
alg wall time (sec): 1.722e+00 num gpu devices, multiproc: 4, 28
cpu proc time (sec): 1.752e+00 num gpu threads per multiproc: 2048
2.4. Set Number of GPU Devices#
By default, imate employs the maximum number of available GPU devices. To employ a specific number of GPU devices, set num_gpu-devices
in the function arguments. For instance
>>> # Import logdet function
>>> from imate import logdet
>>> # Compute log-determinant of Aop
>>> ld, info = logdet(Aop, method='slq', gpu=True, return_info=True,
... num_gpu_devices=2)
>>> # Check how many GPU devices used
>>> pprint(info['device'])
{
'num_cpu_threads': 8,
'num_gpu_devices': 2,
'num_gpu_multiprocessors': 28,
'num_gpu_threads_per_multiprocessor': 2048
}