Subject Computer Science C-Family Programming


Write an efficient cache friendly code in C/C++ for matrix matrix multiplication. Subtasks are as following:

- Create 100x100, 250x250, 500x500, and 1000x1000 random matrices. The random values for each of these matrices will be int, float, and double types. So, you will create total of 4 different size and 3 different data type, total 4*3=12 matrices.

- Write a simple regular matrix multiplication code for all of these different sized matrices and compare timings for regular v.s. cache-friendly matrix multiplications.

- (Optional, Bonus: 10 Points) You can write and read your random matrices in to a binary file.

Note: When you design cache friendly version, recall that C/C++ is row major language and be aware of strided memory accesses.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include <fstream>

using namespace std;

// Matrix declarations
// There is A, B,C for all sizes and types

int iA1[100][100], iB1[100][100], iC1[100][100];
int iA2[250][250], iB2[250][250], iC2[250][250];
int iA3[500][500], iB3[500][500], iC3[500][500];
int iA4[1000][1000], iB4[1000][1000], iC4[1000][1000];
float fA1[100][100], fB1[100][100], fC1[100][100];
float fA2[250][250], fB2[250][250], fC2[250][250];
float fA3[500][500], fB3[500][500], fC3[500][500];
float fA4[1000][1000], fB4[1000][1000], fC4[1000][1000];
double dA1[100][100], dB1[100][100], dC1[100][100];
double dA2[250][250], dB2[250][250], dC2[250][250];
double dA3[500][500], dB3[500][500], dC3[500][500];
double dA4[1000][1000], dB4[1000][1000], dC4[1000][1000];

float frand()
return 1000 * ((float)rand() / (float)RAND_MAX); // float value between 0 and 1000

double drand()
return 1000 * ((double)rand() / (double)RAND_MAX); // double value between 0 and 1000

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats