Code Samples


Matrix Multiplication Using Parallel Computing in Matlab, with multithreadingMarch 5, 2021 / Lorand Gabriel Parajdi

Save the following lines of code in a file called matrix_multiplication.m, then run it using Matlab. Instead of using Matlab matrix multiplication (C = A * B), we have defined matrix multiplication using multiple for loops. Matlab linear algebra library, used in A*B, is already parallelized, and we want to implement parallelization by ourselves.

 clear all; clc;
  
 nthreads = [ 4 ];
 %nthreads = [ 1, 2, 3, 4, 8, 12 ];
 
 % set the size
 sizes = [ 1000 ];
 %sizes = [ 100 500 1000 ];
 
 for s=1:length(sizes)
     n = sizes(s);               % set the matrix size
     A = rand(n);                % create random matrix
     B = rand(n);                % create another random matrix
   for l=1:length(nthreads)	     % vector implementation (may trigger multithreading)
          nt=nthreads(l);
          lastnt=maxNumCompThreads(nt);   % set the thread count
       parpool('threads')
        C = zeros(n);
         tic                              % starts timer
     parfor i = 1:n                       % matrix multiplication algorithm
         for j = 1:n
            sum = 0;
          for k = 1:n
            sum = sum + A(i,k) * B(k,j);
          end
            C(i,j) = sum;
         end
     end
 
 walltime(l) = toc;      		         % wall clock time
 Speedup = walltime(1)/walltime(l);
 Efficiency = 100*Speedup/nt;
 printstring=sprintf(['size=%d time=%8.4f threads=%2d speedup=%4.1f efficiency=%5.1f%%'],
 n, walltime(l), nt, Speedup, Efficiency);
 disp(printstring);
         
 par = gcp('nocreate');                  % release pool instance
 delete(par);
 
   end
    disp(' ');                           % to display matrix C --> disp(C);
 end
 
 plot(nthreads,walltime)                 % plot of running time and no. of threads
 hold on
 plot(nthreads,walltime,'.','MarkerSize',25)
 xlabel('No. of threads');
 ylabel('Running time')
 grid on
 hold off

Matrix Multiplication on the CPU v.s. GPU in MatlabMarch 3, 2021 / Lorand Gabriel Parajdi

Save the following lines of code in a file called matrix_multip_cpu_vs_gpu.m, then run it using Matlab.

 clear all; clc;

 v1 = 10:10:90;                            % set the matrix size
 v2 = 100:100:900;
 v3 = 1000:1000:9000;
 v = [v1 v2 v3];
 %v = [v1 v2];

 % CPU computations
 for i = 1:length(v)   
     A = rand(v(i));                       % create random matrix
     B = rand(v(i));                       % create another random matrix
     f_cpu = @() bsxfun(@mtimes, A, B);
     t(i) = timeit(f_cpu);
     disp(strcat( 'CPU matrix_dimension: ', num2str(v(i)), ' time: ', num2str(t(i))))
 end

 % GPU computations
 for i = 1:length(v)
     AA = rand(v(i),'gpuArray');           % create random matrix
     BB = rand(v(i),'gpuArray');           % create another random matrix
     f_gpu = @() bsxfun(@mtimes, AA, BB);
     tt(i) = gputimeit(f_gpu);
     disp(strcat( 'GPU matrix_dimension: ', num2str(v(i)), ' time: ', num2str(tt(i))))
 end

 save cpu_gpu.mat v t tt                  % save the data values of v, t and tt
                                          % in the file named: cpu_gpu.mat

 loglog(v, t, 'b')                        
 hold on
 loglog(v, tt, 'r')
 loglog(v, t, 'b.', 'MarkerSize', 20)
 loglog(v, tt, 'r.', 'MarkerSize', 20)
 grid on
 xlabel('Log(Matrix size)')
 ylabel('Execution time')
 legend('CPU', 'GPU')
 hold off