{"id":3297,"date":"2021-03-02T10:00:13","date_gmt":"2021-03-02T10:00:13","guid":{"rendered":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/?page_id=3297"},"modified":"2022-12-24T01:13:55","modified_gmt":"2022-12-24T01:13:55","slug":"code-samples","status":"publish","type":"page","link":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/code-samples\/","title":{"rendered":"Code Samples"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-text-color has-black-color has-css-opacity has-black-background-color has-background is-style-wide\"\/>\n\n\n\n<p class=\"has-text-align-center\" style=\"font-size:28px\">Matrix Multiplication Using Parallel Computing in Matlab, with multithreading<a style=\"display:block; font-size:13px; line-height:3.5em;\"><span style=\"color:#424547\" class=\"has-inline-color\">March 5, 2021 \/ Lorand Gabriel Parajdi<\/span><\/a><\/p>\n\n\n\n<p style=\"font-size:15px\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u25ba<\/mark><\/strong> Save the following lines of code in a file called matrix_multiplication.m, then run it using Matlab. <a style=\"display:block;\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u25ba<\/mark><\/strong> <span style=\"color: rgb(30, 30, 30);\">Instead of using Matlab matrix multiplication (C = A * B), we have defined matrix multiplication using multiple <\/span><em style=\"color: rgb(30, 30, 30);\">for<\/em><span style=\"color: rgb(30, 30, 30);\"> loops. Matlab linear algebra library, used in A*B, is already parallelized, and we want to implement parallelization by ourselves.<\/span><\/a><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;octave&quot;,&quot;mime&quot;:&quot;text\/x-octave&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;fileName&quot;:&quot;matrix_multiplication.m&quot;,&quot;language&quot;:&quot;Octave&quot;,&quot;modeName&quot;:&quot;octave&quot;}\"> clear all; clc;\n  \n nthreads = [ 4 ];\n %nthreads = [ 1, 2, 3, 4, 8, 12 ];\n \n % set the size\n sizes = [ 1000 ];\n %sizes = [ 100 500 1000 ];\n \n for s=1:length(sizes)\n     n = sizes(s);               % set the matrix size\n     A = rand(n);                % create random matrix\n     B = rand(n);                % create another random matrix\n   for l=1:length(nthreads)\t     % vector implementation (may trigger multithreading)\n          nt=nthreads(l);\n          lastnt=maxNumCompThreads(nt);   % set the thread count\n       parpool('threads')\n        C = zeros(n);\n         tic                              % starts timer\n     parfor i = 1:n                       % matrix multiplication algorithm\n         for j = 1:n\n            sum = 0;\n          for k = 1:n\n            sum = sum + A(i,k) * B(k,j);\n          end\n            C(i,j) = sum;\n         end\n     end\n \n walltime(l) = toc;      \t\t         % wall clock time\n Speedup = walltime(1)\/walltime(l);\n Efficiency = 100*Speedup\/nt;\n printstring=sprintf(['size=%d time=%8.4f threads=%2d speedup=%4.1f efficiency=%5.1f%%'],\n n, walltime(l), nt, Speedup, Efficiency);\n disp(printstring);\n         \n par = gcp('nocreate');                  % release pool instance\n delete(par);\n \n   end\n    disp(' ');                           % to display matrix C --&gt; disp(C);\n end\n \n plot(nthreads,walltime)                 % plot of running time and no. of threads\n hold on\n plot(nthreads,walltime,'.','MarkerSize',25)\n xlabel('No. of threads');\n ylabel('Running time')\n grid on\n hold off<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-black-color has-css-opacity has-black-background-color has-background is-style-wide\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\" style=\"font-size:28px\">Matrix Multiplication on the CPU v.s. GPU in Matlab<a style=\"display:block; font-size:13px; line-height:3.5em;\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#424547\" class=\"has-inline-color\">March 3, 2021 \/ Lorand Gabriel Parajdi<\/mark><\/a><\/p>\n\n\n\n<p style=\"font-size:15px\"> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u25ba<\/mark><\/strong><\/mark> Save the following lines of code in a file called matrix_multip_cpu_vs_gpu.m, then run it using Matlab. <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;octave&quot;,&quot;mime&quot;:&quot;text\/x-octave&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;no&quot;,&quot;language&quot;:&quot;Octave&quot;,&quot;modeName&quot;:&quot;octave&quot;}\"> clear all; clc;\n\n v1 = 10:10:90;                            % set the matrix size\n v2 = 100:100:900;\n v3 = 1000:1000:9000;\n v = [v1 v2 v3];\n %v = [v1 v2];\n\n % CPU computations\n for i = 1:length(v)   \n     A = rand(v(i));                       % create random matrix\n     B = rand(v(i));                       % create another random matrix\n     f_cpu = @() bsxfun(@mtimes, A, B);\n     t(i) = timeit(f_cpu);\n     disp(strcat( 'CPU matrix_dimension: ', num2str(v(i)), ' time: ', num2str(t(i))))\n end\n\n % GPU computations\n for i = 1:length(v)\n     AA = rand(v(i),'gpuArray');           % create random matrix\n     BB = rand(v(i),'gpuArray');           % create another random matrix\n     f_gpu = @() bsxfun(@mtimes, AA, BB);\n     tt(i) = gputimeit(f_gpu);\n     disp(strcat( 'GPU matrix_dimension: ', num2str(v(i)), ' time: ', num2str(tt(i))))\n end\n\n save cpu_gpu.mat v t tt                  % save the data values of v, t and tt\n                                          % in the file named: cpu_gpu.mat\n\n loglog(v, t, 'b')                        \n hold on\n loglog(v, tt, 'r')\n loglog(v, t, 'b.', 'MarkerSize', 20)\n loglog(v, tt, 'r.', 'MarkerSize', 20)\n grid on\n xlabel('Log(Matrix size)')\n ylabel('Execution time')\n legend('CPU', 'GPU')\n hold off<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Matrix Multiplication Using Parallel Computing in Matlab, with multithreadingMarch 5, 2021 \/ Lorand Gabriel Parajdi \u25ba Save the following lines of code in a file called matrix_multiplication.m, then run it using Matlab. \u25ba Instead of using Matlab matrix multiplication (C = A * B), we have defined matrix multiplication using multiple for loops. Matlab linear &hellip; <a href=\"https:\/\/www.cs.ubbcluj.ro\/~lorand\/code-samples\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code Samples<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/pages\/3297"}],"collection":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/comments?post=3297"}],"version-history":[{"count":176,"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/pages\/3297\/revisions"}],"predecessor-version":[{"id":5315,"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/pages\/3297\/revisions\/5315"}],"wp:attachment":[{"href":"https:\/\/www.cs.ubbcluj.ro\/~lorand\/wp-json\/wp\/v2\/media?parent=3297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}