-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_single.c
102 lines (84 loc) · 2.24 KB
/
matrix_single.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
#include <stdio.h>
#include <stdlib.h>
#define ALIGN 64
float** matrix_1 = NULL;
float** matrix_2 = NULL;
float** matrix_res = NULL;
int alloc_matrix(unsigned int matrix_size)
{
matrix_1 = (float**)aligned_alloc(ALIGN, matrix_size*sizeof(float*));
matrix_2 = (float**)aligned_alloc(ALIGN, matrix_size*sizeof(float*));
matrix_res = (float**)aligned_alloc(ALIGN, matrix_size*sizeof(float*));
if (matrix_1 == NULL || matrix_2 == NULL || matrix_res == NULL)
return 1;
for (int i = 0; i < matrix_size; i++)
{
matrix_1[i] = (float*)aligned_alloc(ALIGN, matrix_size*sizeof(float));
matrix_2[i] = (float*)aligned_alloc(ALIGN, matrix_size*sizeof(float));
matrix_res[i] = (float*)aligned_alloc(ALIGN, matrix_size*sizeof(float));
if (matrix_1[i] == NULL || matrix_2[i] == NULL || matrix_res[i] == NULL)
return 1;
}
for (int i = 0; i < matrix_size; i++)
{
for (int j = 0; j < matrix_size; j++)
{
matrix_1[i][j] = i*0.34 + 1;
matrix_2[i][j] = j*0.28 + 1;
}
}
return 0;
}
int mult_matrix(unsigned int matrix_size)
{
for (int i = 0; i < matrix_size; i++)
{
for (int j = 0; j < matrix_size; j++)
{
for (int k = 0; k < matrix_size; k++)
{
matrix_res[i][j] += matrix_1[i][k] * matrix_2[k][j];
}
}
}
return 0;
}
int free_matrix(unsigned int matrix_size)
{
for (int i = 0; i < matrix_size; i++)
{
free(matrix_res[i]);
free(matrix_1[i]);
free(matrix_2[i]);
}
free(matrix_res);
free(matrix_1);
free(matrix_2);
return 0;
}
int main(int argc, char** argv)
{
unsigned int matrix_size = 32;
if (argc == 2)
{
if (atoi(argv[1]) < 1)
{
printf("error: matrix_size < 1\n");
return -1;
}
matrix_size = atoi(argv[1]);
}
printf("matrix_size: %d\n", matrix_size);
if (matrix_size < 32)
{
printf("error: matrix_size too small\n");
return -1;
}
if (alloc_matrix(matrix_size))
{
printf("error: allocation issue\n");
}
mult_matrix(matrix_size);
free_matrix(matrix_size);
return 0;
}