-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmmul.cpp
More file actions
47 lines (35 loc) · 1013 Bytes
/
mmul.cpp
File metadata and controls
47 lines (35 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "matrix.hpp"
#include <sys/time.h>
using namespace std;
const size_t SIZE = 1024;
typedef Matrix& MatrixRef;
void MultiplyMatrices(const MatrixRef a, const MatrixRef b, MatrixRef c) {
for (size_t i = 0; i < SIZE; i++) {
for (size_t j = 0; j < SIZE; j++) {
for (size_t k = 0; k < SIZE; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main() {
Matrix a(SIZE);
Matrix b(SIZE);
Matrix c(SIZE);
struct timeval start, end;
gettimeofday(&start, NULL);
MultiplyMatrices(a, b, c);
gettimeofday(&end, NULL);
double elapsedtime_sec = double(end.tv_sec - start.tv_sec) +
double(end.tv_usec - start.tv_usec)/1000000.0;
cout << "Multiplication time (N=" << SIZE << "): " << elapsedtime_sec << std::endl;
float checksum = 0.0f;
for (size_t i = 0; i < SIZE; i++) {
for (size_t j = 0; j < SIZE; j++) {
checksum += c[i][j];
}
}
cout << "MMchecksum = " << checksum << endl;
return 0;
}