Matrix Multiplication Tests in Octave (or Matlab)
I was recently implementing matrix multiplication on the GPU (using CUDA). For my application, I was generating random numbers and generating statistics about the performance of matrix multiplication variants (e.g. using shared memory vs naive multiplication). Some of the results tended to differ from the CPU’s results. Therefore, I decided to use deterministic matrices for the inputs to ensure my algorithm is correct. What I needed was a neutral (3rd party) matrix multiplication algorithm. This seems like a job for MATLAB. Unfortunately, my license expired a few years ago. My robotics professor at the University of Washington was a fan of Octave because it is open source and free. Here is the script I created to generate matrices with the positive integers.
A = 1:10000;
B = 10001:20000;
A = reshape(A, [100,100]);
B = reshape(B, [100,100]);
A = transpose(A);
B = transpose(B);
C = A * B;
# format short;
save 'mmult100x100.txt' C;
Backstory
It has been a while since I used MATLAB. Here are the searches I used to create the script.
- array 1 2 3 4 5 matlab – Google Search
- display full precision matlab – Google Search
- write matlab array to file – Google Search
- write matlab matrix to file – Google Search
- My matrix multiplication algorithms use floats so comparisons fail. use float32 in matlab – Search (bing.com)
- How to force MATLAb to use 32bit floating-point numbers in a script? – MATLAB Answers – MATLAB Central (mathworks.com) >
- How to set Matlab precision to Single – MATLAB Answers – MATLAB Central (mathworks.com) but that doesn’t sound convincing. Perhaps converting all the data to single before matrix multiplication might help as suggested by a comment there.
- check matlab matrix datatype – Google Search >
- How to get ‘type’ of a variable in matlab? – MATLAB Answers – MATLAB Central (mathworks.com)
class(A)
works in Octave as well.
- create matrix of float32 matlab – Google Search >
Leave a Reply