This repository has been archived by the owner on May 27, 2021. It is now read-only.
forked from silenthunter/CSE6230-CheckpointB
/
unittest_mm.c
204 lines (152 loc) · 4.22 KB
/
unittest_mm.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* \file unittest_mm.c
* \brief unittests for testing local_mm()
* \author Kent Czechowski <kentcz@gatech...>
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include "matrix_utils.h"
#include "local_mm.h"
void print_matrix_types() {
double *random, *ones, *zeros, *identity, *tri;
/* Allocate matrices */
random = random_matrix(6, 3);
identity = identity_matrix(5, 5);
ones = ones_matrix(4, 2);
zeros = zeros_matrix(2, 4);
tri = lowerTri_matrix(5, 5);
printf("\n\t\tMatrix Types\n");
printf("6x3 Random Matrix\n");
print_matrix(6, 3, random);
printf("\n\n");
printf("5x5 Identity Matrix\n");
print_matrix(5, 5, identity);
printf("\n\n");
printf("4x2 Ones Matrix\n");
print_matrix(4, 2, ones);
printf("\n\n");
printf("2x4 Zeros Matrix\n");
print_matrix(2, 4, zeros);
printf("\n\n");
printf("5x5 Lower Triangular Matrix\n");
print_matrix(5, 5, tri);
printf("\n\n");
/* deallocate memory */
deallocate_matrix(random);
deallocate_matrix(ones);
deallocate_matrix(zeros);
deallocate_matrix(identity);
deallocate_matrix(tri);
}
/**
* Verify that a matrix times the identity is itself
**/
void identity_test(int n) {
double *A, *B, *C;
printf("identity_test n=%d............", n);
/* Allocate matrices */
A = random_matrix(n, n);
B = identity_matrix(n, n);
C = zeros_matrix(n, n);
/* C = 1.0*(A*B) + 0.0*C */
local_mm(n, n, n, 1.0, A, n, B, n, 5.0, C, n);
/* Verfiy the results */
verify_matrix(n, n, A, C);
/* Backwards C = 1.0*(B*A) + 0.0*C */
local_mm(n, n, n, 1.0, B, n, A, n, 0.0, C, n);
/* Verfiy the results */
verify_matrix(n, n, A, C);
/* deallocate memory */
deallocate_matrix(A);
deallocate_matrix(B);
deallocate_matrix(C);
printf("passed\n");
}
/**
* Test the multiplication of two matrices of all ones
**/
void ones_test(int m, int n, int k) {
double *A, *B, *C, *C_ones, *C_zeros;
printf("ones_test m=%d n=%d k=%d............", m, n, k);
/* Allocate matrices */
A = ones_matrix(m, k);
B = ones_matrix(k, n);
C = ones_matrix(m, n);
C_ones = ones_matrix(m, n);
C_zeros = zeros_matrix(m, n);
/* C = (1.0/k)*(A*B) + 0.0*C */
local_mm(m, n, k, (1.0 / k), A, m, B, k, 0.0, C, m);
/* Verfiy the results */
verify_matrix(m, n, C, C_ones);
/* C = (1.0/k)*(A*B) + -1.0*C */
local_mm(m, n, k, (1.0 / k), A, m, B, k, -1.0, C, m);
/* Verfiy the results */
verify_matrix(m, n, C, C_zeros);
/* deallocate memory */
deallocate_matrix(A);
deallocate_matrix(B);
deallocate_matrix(C);
deallocate_matrix(C_ones);
deallocate_matrix(C_zeros);
printf("passed\n");
}
/**
* Test the multiplication of a lower triangular matrix
**/
void lower_triangular_test(int n) {
int i;
double *A, *B, *C;
printf("lower_triangular_test n=%d ............", n);
/* Allocate matrices */
A = lowerTri_matrix(n, n);
B = ones_matrix(n, 1);
C = ones_matrix(n, 1);
/* C = 1.0*(A*B) + 0.0*C */
local_mm(n, 1, n, 1.0, A, n, B, n, 0.0, C, n);
/* Loops over every element in C */
for (i = 0; i < n; i++) {
/* The elements of C should be [1 2 3 4...] */
assert(C[i] == (double) (i + 1.0));
}
/* C = 0.0*(A*B) + 1.0*C */
local_mm(n, 1, n, 0.0, A, n, B, n, 1.0, C, n);
/* Loops over every element in C */
for (i = 0; i < n; i++) {
/* The elements of C should be [1 2 3 4...] */
assert(C[i] == (double) (i + 1.0));
}
/* C = 1.0*(A*B) + 1.0*C */
local_mm(n, 1, n, 3.0, A, n, B, n, 1.0, C, n);
/* Loops over every element in C */
for (i = 0; i < n; i++) {
/* The elements of C should be [4 8 16 32...] */
assert(C[i] == (double) ((i + 1) * 4.0));
}
/* C = 0.0*(A*B) + 0.0*C */
local_mm(n, 1, n, 0.0, A, n, B, n, 0.0, C, n);
/* Loops over every element in C */
for (i = 0; i < n; i++) {
/* The elements of C should be 0 */
assert(C[i] == 0.0);
}
/* deallocate memory */
deallocate_matrix(A);
deallocate_matrix(B);
deallocate_matrix(C);
printf("passed\n");
}
int main() {
printf("Hello World\n");
identity_test(16);
identity_test(37);
identity_test(512);
ones_test(32, 32, 32);
ones_test(61, 128, 123);
lower_triangular_test(8);
lower_triangular_test(92);
lower_triangular_test(128);
return 0;
}