Home > manopt > autodiff > basic_examples_AD > using_gpu_AD.m

using_gpu_AD

PURPOSE ^

Manopt example on how to use GPU to compute the egrad and the ehess via AD.

SYNOPSIS ^

function using_gpu_AD()

DESCRIPTION ^

 Manopt example on how to use GPU to compute the egrad and the ehess via AD.

 This file is basically the same as using_gpu.m.

 See also: using_gpu

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function using_gpu_AD()
0002 % Manopt example on how to use GPU to compute the egrad and the ehess via AD.
0003 %
0004 % This file is basically the same as using_gpu.m.
0005 %
0006 % See also: using_gpu
0007 
0008 % This file is part of Manopt: www.manopt.org.
0009 % Original author: Nicolas Boumal, Xiaowen Jiang, Aug. 21, 2021.
0010 % Contributors:
0011 % Change log:
0012 
0013     % Verify that the deep learning tool box was installed
0014     assert(exist('dlarray', 'file') == 2, ['Deep learning tool box is '... 
0015     'needed for automatic differentiation.\n Please install the'...
0016     'latest version of the deep learning tool box and \nupgrade to Matlab'...
0017     ' R2021b if possible.'])
0018 
0019     if exist('OCTAVE_VERSION', 'builtin')
0020         warning('manopt:usinggpu', 'Octave does not handle GPUs at this time.');
0021         return;
0022     end
0023 
0024     if gpuDeviceCount() <= 0
0025         warning('manopt:usinggpu', 'No GPU available: cannot run example.');
0026         return;
0027     end
0028 
0029     % Construct a large problem to illustrate the use of GPU.
0030     % Below, we will compute p left-most eigenvectors of A (symmetric).
0031     % On a particular test computer.
0032     p = 3;
0033     n = 10000;
0034     A = randn(n);
0035     A = A+A';
0036     
0037     inner = @(U, V) U(:)'*V(:);
0038     
0039     % First, setup and run the optimization problem on the CPU.
0040     problem_cpu.M = grassmannfactory(n, p, 1); % 1 copy of Grassmann(n, p)
0041     problem_cpu.cost = @(X) .5*inner(X, A*X);  % Rayleigh quotient to be minimized
0042     problem_cpu = manoptAD(problem_cpu);       % Obtain the egrad and ehess via AD
0043     X0 = problem_cpu.M.rand();                 % Random initial guess
0044     tic_cpu = tic();
0045     X_cpu = trustregions(problem_cpu, X0);     % run any solver
0046     time_cpu = toc(tic_cpu);
0047     
0048     % Then, move the data to the GPU, redefine the problem
0049     % activate the GPU flag in the factory, and run it again.
0050     A = gpuArray(A);
0051     problem_gpu.M = grassmannfactory(n, p, 1, true); % true is the GPU flag;
0052     problem_gpu.cost = @(X) .5*inner(X, A*X);        % Code for cost
0053     problem_gpu = manoptAD(problem_gpu);         % Work on gpu now
0054     X0 = gpuArray(X0);
0055     tic_gpu = tic();
0056     X_gpu = trustregions(problem_gpu, X0);
0057     time_gpu = toc(tic_gpu);
0058     
0059     fprintf('Total time CPU: %g\nTotal time GPU: %g\nSolution difference: %g\n', ...
0060             time_cpu, time_gpu, norm(X_cpu - X_gpu, 'fro'));
0061     
0062 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005