0001 function using_gpu_AD()
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
0030
0031
0032 p = 3;
0033 n = 10000;
0034 A = randn(n);
0035 A = A+A';
0036
0037 inner = @(U, V) U(:)'*V(:);
0038
0039
0040 problem_cpu.M = grassmannfactory(n, p, 1);
0041 problem_cpu.cost = @(X) .5*inner(X, A*X);
0042 problem_cpu = manoptAD(problem_cpu);
0043 X0 = problem_cpu.M.rand();
0044 tic_cpu = tic();
0045 X_cpu = trustregions(problem_cpu, X0);
0046 time_cpu = toc(tic_cpu);
0047
0048
0049
0050 A = gpuArray(A);
0051 problem_gpu.M = grassmannfactory(n, p, 1, true);
0052 problem_gpu.cost = @(X) .5*inner(X, A*X);
0053 problem_gpu = manoptAD(problem_gpu);
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