-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmultiplication.cpp
More file actions
66 lines (46 loc) · 1.24 KB
/
matrixmultiplication.cpp
File metadata and controls
66 lines (46 loc) · 1.24 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// #include <cstdlib>
#include <string>
#include <cstring>
#include "matrix_utility.cpp"
template <typename T>
T** matrixMultiplicationForPosition(T** a, int r_a, int c_a, T** b, int r_b, int c_b) {
if (c_a!=r_b) return NULL;
int r_c = r_a;
int c_c = c_b;
T* c_pool = (T*) malloc(r_c*c_c*sizeof(T));
memset(c_pool, 0, r_c*c_c*sizeof(T));
T** c = (T**) malloc(r_c*sizeof(T*));
for (int i=0; i<r_c; i++)
c[i] = &c_pool[c_c*i];
for (int i=0; i<r_a; i++)
for (int k=0; k<r_b; k++)
for (int j=0; j<c_b; j++)
c[i][j] += a[i][k]*b[k][j];
return c;
}
#ifndef MATRIX_TEST_CPP
// Testing functions
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// srand(time(0));
int squared = 512;
int r_a = squared;
int c_b = squared;
int n = squared;
int c_a = n;
int r_b = n;
float **a = allocateM<float>(r_a, c_a);
for (int i=0; i<r_a*c_a; i++)
a[i/c_a][i%c_a] = i;
// printM(a, r_a, c_a, "a");
float **b = allocateM<float>(r_b, c_b);
randomizeM(b, r_b, c_b, 4.0, 1.0);
// printM(b, r_b, c_b, "b");
float **c;
double t = measure_time_of(matrixMultiplicationForPosition, a, r_a, c_a, b, r_b, c_b, &c);
// cout << "matrix multiplication in " << t << " sec " << endl;
// printM(c, r_a, c_b, "c");
}
#endif