-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmult_seq.cpp
52 lines (46 loc) · 1.3 KB
/
matmult_seq.cpp
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
48
49
50
//
// Created by eva on 30.11.20.
//
#include <cstdio>
/**
* Multiplies two matrices A and B seuqentially
*/
void mult_seq(long *result, long *a, long *b, int height, int width, int widthA) {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
// walk through all cells of a row in matrixA and every cell in a column of matrixB
// and multiply
for (int i = 0; i < widthA; i++) {
result[row * width + col] += a[row * widthA + i] * b[i * width + col];
}
}
}
}
/**
* Multiplies two matrices A and B seuqentially
* But with switched inner loops
*/
void mult_seq_speed(long *result, long *a, long *b, int height, int width, int widthA) {
for (int row = 0; row < height; row++) {
for (int i = 0; i < widthA; i++) {
for (int col = 0; col < width; col++) {
result[row * width + col] += a[row * widthA + i] * b[i * width + col];
}
}
}
}
/**
* Multiplies two matrices A and B sequentially
* But with switched inner loops
*/
void mult_seq_speed_cache(long *result, long *a, long *b, int height, int width, int widthA) {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
long sum = 0;
for (int i = 0; i < widthA; i++) {
sum += a[row * widthA + i] * b[i * width + col];
}
result[row * width + col] = sum;
}
}
}