Determine a matrix size that you can comfortably fit into your available RAM. For example, if you have a 4 GB machine, you should be able to comfortably store a matrix that occupies about 800MB. Store this value in a variable
Mb
. Use the following information to compute a maximum matrix dimension N that you can store in Mb megabytes of memory.
A megabyte has
1024
kilobytesA kilobyte is
1024
bytesA floating point number is
8 bytes
.An
N × N
matrix containsN^2
floating point numbers.Call the N you compute
nmax
.(b) Create two random matrices
A
andB
each of sizeNmax × Nmax
. Using the MATLAB functionstic
andtoc
, determine how much time (seconds) it takes to compute the productAB
. Determine the number of floating point operations (additions and multiplications) it takes to compute theNmax × Nmax
matrix-matrix product(2/3)n^3.
Use this number to estimate the number of floating point operations per second (’flops’) your computer can carry out. Call this flop rateflops
.
% Part A
nmax = sqrt((1600*1024*1024)/8); % 8GB of RAM
% Part B
A = (nmax:nmax);
B = (nmax:nmax);
tic
prod = A*B;
prod_time = toc
flops = (2/3)*(prod).^3
Everything runs fine but I feel like I am not creating a matrix for the values A
and B
. What am I doing wrong?
Two main things: you messed up your matrix assignment; c:c
where c
is a constant just returns the constant. The colon, :
, creates arrays, such as
c = 5;
N = 1:c
1 2 3 4 5
Feeding the colon operator the same start- and endpoint obviously just returns that point.
Second: the total number of operations was proportional to the number of elements, not the actual result of the matrix product (which is irrelevant actually, we're just interested in the time). So first calculate the total number of FLoating point Operations.
Remember that we used tic/toc
? Well, perhaps we should find out what the total time was, which is stored in prod_time
. This is the number of seconds it took to perform the matrix multiplication. Dividing Totflops
by prod_time
gives you the FLoating point Operations Per Second, i.e. FLOPS.
[~,systemview] = memory; % Get RAM info
tmp = systemview.PhysicalMemory;
% tmp.Total stores the total system RAM
Mb = 0.2*((tmp.Total/(1024^2))); % 20% of the total RAM
% floor your nmax to force it to be integer
nmax = floor(sqrt((Mb*1024^2/8))); % create your nmax
A = rand(nmax); % random nmax x nmax matrix
B = rand(nmax); % random nmax x nmax matrix
tic
prod = A*B;
prod_time = toc;
% Total flops
Totflops = (2/3)*(nmax).^3;
flops = Totflops/prod_time; % flops/sec
Which on my system (8GB RAM and an i5 750 2.66GHz) gives flops = 1.0617e+10