-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixmul.hpp
106 lines (88 loc) · 3.03 KB
/
matrixmul.hpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Author: Nathan Crock - http://www.mathnathan.com
Date: 8/26/2010 @ 19:33
Program Description: A matrix multiplication function written for my
Numerical Analysis class. This program takes in
two matrices of any size and any type and
multiplies them using the standard row by column
matrix multiplication technique.
*/
# include <fstream>
# include <stdio.h>
using namespace std;
// --------------Matrix data structure-----------------
// It has three members: data - a pointer to an array
// height - the number of rows
// width - the number of columns
//
// I'm using a one dimensional array and listing the rows
// one after another. Then I can use math tricks to simplify
// the multiplication algorithm. And ultimately use the arrays
// to rewrite this in OpenCL and optimize the computation time.
template <typename T>
struct matrix {
T* data;
unsigned int height;
unsigned int width;
matrix(unsigned int h, unsigned int w);
~matrix();
};
// Constructor - Initializes matrix of size (h x w) with
template <typename T>
matrix<T>::matrix(unsigned int h = 0, unsigned int w = 0): height(h), width(w) {
data = new T[height*width];
}
template <typename T>
matrix<T>::~matrix() {
delete [] data;
}
/* -------------Multiplication Function-----------------
Without using sparse matrices, this is roughly as
quick as you'll get when brute force multiplying
matrices. The first for loop goes through every
element of the resultant matrix, while the nested
for loop goes over the columns. The products of all the
elements in the respective rows and columns are summed
up in each entry of the resultant matrix. */
template <typename T>
void matMul(const matrix<T> & A, const matrix<T> & B, matrix<T> & C) {
int row, col;
for (int entry = 0; entry < A.height*B.width; entry++) {
row = entry / B.width;
col = entry % B.width;
for (int elem = 0; elem < A.width; elem++) {
C.data[entry] += A.data[row*A.width + elem]*B.data[col + elem*B.width];
}
}
}
// Print a matrix to the screen
template <typename T>
void print_matrix_screen(const matrix<T> & A) {
printf("\n| ");
for(int i = 0; i < A.height; i++) {
if(i > 0 && i < A.height) {
printf("|\n");
printf("| ");
}
for(int j = 0; j < A.width; j++) {
cout << A.data[i*A.width + j] << " ";
}
}
printf("|\n");
}
// Print a matrix to a file
template <typename T>
void print_matrix_file(const matrix<T> & A, const char* filename) {
std::ofstream output;
output.open(filename);
output << "| ";
for(int i = 0; i < A.height; i++) {
if(i > 0 && i < A.height) {
output << "|\n";
output << "| ";}
for(int j = 0; j < A.width; j++) {
output << A.data[i*A.width + j] << " ";
}
}
output << "|\n";
output.close();
}