Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- close all; clearvars; clc; delete(gcp('nocreate'));
- %% Parameters: Parallel computation and gpu
- global comput
- comput.Paral = 1; % |1| use parallel computation (required Parallel Computing Toolbox).
- comput.gpu = 1; %|1| use GPU device (required Parallel Computing Toolbox).
- %% Initialization: parallel processing
- if(license('test', 'Distrib_Computing_Toolbox') == 0)
- comput.gpu = 0;
- comput.Paral = 0;
- fprintf('Distrib_Computing_Toolbox not available: GPU and multicore capabilities can not be used\n');
- end
- % GPU device
- comput.gpuHandle = [];
- if(comput.gpu)
- for ii = 1:gpuDeviceCount
- g = gpuDevice(ii);
- if(str2double(g.ComputeCapability) >= 3)
- comput.gpuHandle = g;
- fprintf('Using GPU\n');
- break;
- end
- end
- fprintf('Not using GPU\n');
- else
- fprintf('Not using GPU\n');
- end
- % Cluster processing
- if(comput.Paral)
- parpool;
- p = gcp('nocreate'); % If no pool, do not create new one.
- fprintf('Using %d CPU\n', p.NumWorkers);
- % parpool('local', 4);
- else
- fprintf('Using 1 CPU\n');
- end
- %% GPU processing: matrix multiplication
- if(~isempty(comput.gpuHandle))
- try
- a = randn(100, 100, 'single', 'gpuArray');
- % Alternatively
- % a = randn(100, 100, 'single');
- % a = gpuArray(a);
- b = gather(a*a);
- catch
- warning('GPU computation is not possible, using CPU computation.\n');
- comput.gpuHandle = [];
- a = randn(100, 100, 'single');
- b = a*a;
- end
- else
- a = randn(100, 100, 'single');
- b = a*a;
- end
- %% Parallel loop processing: : matrix multiplication.
- b = cell(10,1);
- if(comput.Paral == 1)
- parfor i = 1:10
- a = randn(i, i, 'single');
- b{i} = a*a;
- end
- else
- for i = 1:10
- a = randn(i, i, 'single');
- b{i} = a*a;
- end
- end
Add Comment
Please, Sign In to add comment