Compile.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %% Author: Rodrigo de Barros Vimieiro
  2. % Date: April, 2018
  3. % rodrigo.vimieiro@gmail.com
  4. % =========================================================================
  5. %{
  6. %
  7. % Reference:
  8. % https://www.codefull.net/2014/11/tutorial-use-cuda-code-in-matlab/
  9. %
  10. % ---------------------------------------------------------------------
  11. % Copyright (C) <2019> <Rodrigo de Barros Vimieiro>
  12. %
  13. % This program is free software: you can redistribute it and/or modify
  14. % it under the terms of the GNU General Public License as published by
  15. % the Free Software Foundation, either version 3 of the License, or
  16. % (at your option) any later version.
  17. %
  18. % This program is distributed in the hope that it will be useful,
  19. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. % GNU General Public License for more details.
  22. %
  23. % You should have received a copy of the GNU General Public License
  24. % along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. %}
  26. % =========================================================================
  27. %% Compilation Code
  28. % Linux systems
  29. if isunix
  30. fprintf('--- Starting complilation ---\n');
  31. matlabRootPath = matlabroot;
  32. % ------------------------------------------
  33. pathVar{1} = 'backprojectionDD_mex';
  34. pathVar{2} = 'projectionDD_mex';
  35. for k=1:2
  36. mex('-largeArrayDims',[pathVar{k} filesep pathVar{k} '.cpp'],['-L' matlabRootPath '/bin/glnxa64'],'-outdir','..');
  37. end
  38. % ------------------------------------------
  39. pathVar{1} = 'backprojectionDDb_mex';
  40. pathVar{2} = 'projectionDDb_mex';
  41. for k=1:2
  42. mex('-largeArrayDims',[pathVar{k} filesep pathVar{k} '.cpp'],['-L' matlabRootPath '/bin/glnxa64'],'CFLAGS="$CFLAGS -fopenmp"', 'LDFLAGS="$LDFLAGS -fopenmp"','-outdir','..');
  43. end
  44. % ------------------------------------------
  45. % https://www.mathworks.com/matlabcentral/answers/394830-nvcc-command-not-found
  46. cudaPath = '/usr/local/cuda';
  47. if(~system('cd /usr/local/cuda'))
  48. p = getenv('PATH');
  49. setenv('PATH', [p ':' cudaPath '/bin'])
  50. pathVar{1} = 'backprojectionDDb_mex_CUDA'; sourceFilePath{1} = 'backprojectionDDb_mex_cuda.cpp';
  51. pathVar{2} = 'projectionDDb_mex_CUDA'; sourceFilePath{2} = 'projectionDDb_mex_cuda.cpp';
  52. for k=1:2
  53. % https://stackoverflow.com/a/25197234/8682939
  54. system(['nvcc -O3 -c ' pathVar{k} '/kernel.cu -Xcompiler -fPIC -I' matlabRootPath '/extern/include -I' matlabRootPath '/toolbox/distcomp/gpu/extern/include -I' cudaPath '/samples/common/inc']);
  55. mex('-R2018a',[pathVar{k} filesep sourceFilePath{k}],'kernel.o',['-I' cudaPath '/include'],['-I' cudaPath '/samples/common/inc'],['-L' cudaPath '/lib64'],['-L' matlabRootPath '/bin/glnxa64'],'-lcudart', '-lcufft', '-lmwgpu','-outdir','..');
  56. end
  57. system('rm kernel.o');
  58. flagCUDA = 1;
  59. else
  60. flagCUDA = 0;
  61. end
  62. % ------------------------------------------
  63. fprintf('\n\n\n\nC++ files compiled.\n');
  64. fprintf('C++ (OpenMP) files compiled.\n');
  65. if(flagCUDA)
  66. fprintf('CUDA files compiled.\n');
  67. else
  68. error('Either change CUDA toolkit path or install it.');
  69. end
  70. fprintf('\n--- Done!! --- \n');
  71. end