Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. close all; clearvars; clc; delete(gcp('nocreate'));
  2.  
  3. %% Parameters: Parallel computation and gpu
  4. global comput
  5. comput.Paral = 1; % |1| use parallel computation (required Parallel Computing Toolbox).
  6. comput.gpu = 1; %|1| use GPU device (required Parallel Computing Toolbox).
  7.  
  8. %% Initialization: parallel processing
  9. if(license('test', 'Distrib_Computing_Toolbox') == 0)
  10. comput.gpu = 0;
  11. comput.Paral = 0;
  12. fprintf('Distrib_Computing_Toolbox not available: GPU and multicore capabilities can not be used\n');
  13. end
  14. % GPU device
  15. comput.gpuHandle = [];
  16. if(comput.gpu)
  17. for ii = 1:gpuDeviceCount
  18. g = gpuDevice(ii);
  19. if(str2double(g.ComputeCapability) >= 3)
  20. comput.gpuHandle = g;
  21. fprintf('Using GPU\n');
  22. break;
  23. end
  24. end
  25. fprintf('Not using GPU\n');
  26. else
  27. fprintf('Not using GPU\n');
  28. end
  29. % Cluster processing
  30. if(comput.Paral)
  31. parpool;
  32. p = gcp('nocreate'); % If no pool, do not create new one.
  33. fprintf('Using %d CPU\n', p.NumWorkers);
  34. % parpool('local', 4);
  35. else
  36. fprintf('Using 1 CPU\n');
  37. end
  38.  
  39. %% GPU processing: matrix multiplication
  40. if(~isempty(comput.gpuHandle))
  41. try
  42. a = randn(100, 100, 'single', 'gpuArray');
  43. % Alternatively
  44. % a = randn(100, 100, 'single');
  45. % a = gpuArray(a);
  46. b = gather(a*a);
  47. catch
  48. warning('GPU computation is not possible, using CPU computation.\n');
  49. comput.gpuHandle = [];
  50. a = randn(100, 100, 'single');
  51. b = a*a;
  52. end
  53. else
  54. a = randn(100, 100, 'single');
  55. b = a*a;
  56. end
  57.  
  58. %% Parallel loop processing: : matrix multiplication.
  59. b = cell(10,1);
  60. if(comput.Paral == 1)
  61. parfor i = 1:10
  62. a = randn(i, i, 'single');
  63. b{i} = a*a;
  64. end
  65. else
  66. for i = 1:10
  67. a = randn(i, i, 'single');
  68. b{i} = a*a;
  69. end
  70. end
Add Comment
Please, Sign In to add comment