Measuring performance of matrix multiplication

This page contains details on how I measured the performance of matrix multiplication in R and Matlab.

The tests were measuring the time requred to multiply two 4096 by 4096 matrices. In R, both %*% and crossprod functions were tested. Matlab test includes both A*B and A*B' matrix multiplications. Each multiplication was performed 10 times and the fastest time was reported.

The tests were performed using the following software:

Batch file:

..\R-2.14.1_BLAS\bin\x64\Rscript.exe --vanilla test.R > 1_blas_64_Japan_time.txt
..\R-2.14.1_BLAS\bin\i386\Rscript.exe --vanilla test.R > 1_blas_32_ATLAS_time.txt
..\R-2.14.1\bin\x64\Rscript.exe --vanilla test.R > 1_noblas_64_slow_time_usual_R.txt
..\R-2.14.1\bin\i386\Rscript.exe --vanilla test.R > 1_noblas_32_slow_time_usual_R.txt
"C:\Revolution\R-Enterprise-4.3\R-2.12.2\bin\x64\Rscript.exe" --vanilla test.R > 1_blas_revo_4.3.txt
matlab -nodesktop -nojvm -wait -r "datatype='double';Matlab_GEMM;quit()"
matlab -nodesktop -nojvm -wait -r "datatype='single';Matlab_GEMM;quit()"
matlab -nodesktop -nojvm -wait -r "datatype='double';Matlab_GPU;quit()"
matlab -nodesktop -nojvm -wait -r "datatype='single';Matlab_GPU;quit()"

Code for tests in R (test.R):

m1 = 2^12;
m2 = 2^12;
n = 2^12;

z1 = runif(m1*n); dim(z1) = c(m1,n);
z2 = runif(m2*n); dim(z2) = c(m2,n);

btimes = Inf;
for( i in 1:10 ) {
    a = proc.time()[3];
    z3 = z1 %*% t(z2);
    b = proc.time()[3];
    b-a
    btimes = min( btimes, b-a );
}

bcross = Inf;
for( i in 1:10 ) {
    a = proc.time()[3];
    z4 = crossprod(z1,z2);
    b = proc.time()[3];
    bcross = min( bcross, b-a );
}

cat("%*%\t", btimes, "\tcrossprod\t", bcross );

Code for tests in Matlab (Matlab_GEMM.m):

% datatype = 'double';
% datatype = 'single';

n = 2^12;
m1 = 2^12;
m2 = 2^12;

z1 = rand(m1,n,datatype);
z2 = rand(m2,n,datatype);

best = Inf;
for i=1:10
    a = tic;
    z3 = z1*z2;
    t = toc(a);
    best = min(best,t);
end

bestt = Inf;
for i=1:10
    a = tic;
    z3 = z1*z2';
    t = toc(a);
    bestt = min(bestt,t);
end

clear z1 z2 z3;
bb = [best;bestt];
disp(min(bb));
disp(2*m1*m2*n / min(bb) /1e9);

save(['1_blas_Matlab_' datatype '.txt'],'bb','-ascii');

Code for GPU tests in Matlab (Matlab_GPU.m):

% datatype = 'double';
% datatype = 'single';

n = 2^12;
m1 = 2^12;
m2 = 2^12;

tic; z1 = parallel.gpu.GPUArray.randn(m1,n,datatype); toc;
tic; z2 = parallel.gpu.GPUArray.randn(m1,n,datatype); toc;

best = Inf;
for i=1:10
    a = tic;
    z3 = z1*z2;
    t = toc(a);
    best = min(best,t);
end

bestt = Inf;
for i=1:10
    a = tic;
    z3 = z1*z2';
    t = toc(a);
    bestt = min(bestt,t);
end

clear z1 z2 z3;
bb = [best;bestt];
disp(bb);
disp(2*m1*m2*n / min(bb) /1e9);

save(['1_blas_Matlab_GPU_' datatype '.txt'],'bb','-ascii');


Back to Matrix eQTL page.

By Andrey Shabalin