move init and free to functions
This commit is contained in:
parent
7b83d9eeb4
commit
50390f7118
41
main.c
41
main.c
@ -1,5 +1,4 @@
|
|||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
#include "matrix.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -266,40 +265,24 @@ void test_with_dfs() {
|
|||||||
void test_with_structs() {
|
void test_with_structs() {
|
||||||
ulong row_length = 5, column_length = 5;
|
ulong row_length = 5, column_length = 5;
|
||||||
|
|
||||||
ulong **values1 = (ulong**)malloc(row_length * sizeof(ulong*));
|
Matrix *adjacency_matrix1 = create_matrix(row_length, column_length);
|
||||||
ulong **values2 = (ulong**)malloc(row_length * sizeof(ulong*));
|
Matrix *adjacency_matrix2 = create_matrix(row_length, column_length);
|
||||||
ulong **values3 = (ulong**)malloc(row_length * sizeof(ulong*));
|
Matrix *dot_product = create_matrix(row_length, column_length);
|
||||||
|
|
||||||
for (ulong index = 0; index < row_length; index++) {
|
random_adjacency_struct(adjacency_matrix1);
|
||||||
values1[index] = (ulong*)malloc(column_length * sizeof(ulong));
|
print_matrix_struct(adjacency_matrix1);
|
||||||
values2[index] = (ulong*)malloc(column_length * sizeof(ulong));
|
|
||||||
values3[index] = (ulong*)malloc(column_length * sizeof(ulong));
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix adjacency_matrix1 = {row_length, column_length, values1};
|
|
||||||
Matrix adjacency_matrix2 = {row_length, column_length, values2};
|
|
||||||
Matrix dot_product = {row_length, column_length, values3};
|
|
||||||
|
|
||||||
random_adjacency_struct(&adjacency_matrix1);
|
|
||||||
print_matrix_struct(&adjacency_matrix1);
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
random_adjacency_struct(&adjacency_matrix2);
|
random_adjacency_struct(adjacency_matrix2);
|
||||||
print_matrix_struct(&adjacency_matrix2);
|
print_matrix_struct(adjacency_matrix2);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
gemm_basic_structs(&adjacency_matrix1, &adjacency_matrix2, &dot_product);
|
gemm_basic_structs(adjacency_matrix1, adjacency_matrix2,dot_product);
|
||||||
print_matrix_struct(&dot_product);
|
print_matrix_struct(dot_product);
|
||||||
putchar('\n');
|
|
||||||
|
|
||||||
for (ulong index = 0; index < row_length; index++) {
|
free_matrix(adjacency_matrix1);
|
||||||
free(values1[index]);
|
free_matrix(adjacency_matrix2);
|
||||||
free(values2[index]);
|
free_matrix(dot_product);
|
||||||
free(values3[index]);
|
|
||||||
}
|
|
||||||
free(values1);
|
|
||||||
free(values2);
|
|
||||||
free(values3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
24
matrix.c
24
matrix.c
@ -93,3 +93,27 @@ int read_csv(const char *file_name, const ulong row_length, const ulong column_l
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix* create_matrix(ulong row_length, ulong column_length) {
|
||||||
|
ulong **values = (ulong**)malloc(row_length * sizeof(ulong*));
|
||||||
|
|
||||||
|
|
||||||
|
for (ulong index = 0; index < row_length; index++) {
|
||||||
|
values[index] = (ulong*)malloc(column_length * sizeof(ulong));
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix *matrix = malloc(sizeof(Matrix));
|
||||||
|
matrix->row_length = row_length;
|
||||||
|
matrix->column_length = column_length;
|
||||||
|
matrix->values = values;
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_matrix(Matrix *matrix) {
|
||||||
|
for (ulong index = 0; index < matrix->row_length; index++) {
|
||||||
|
free(matrix->values[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(matrix->values);
|
||||||
|
}
|
||||||
|
4
matrix.h
4
matrix.h
@ -27,4 +27,8 @@ int read_csv(const char *file_name, const ulong row_length,
|
|||||||
const ulong column_length,
|
const ulong column_length,
|
||||||
ulong output_matrix[row_length][column_length]);
|
ulong output_matrix[row_length][column_length]);
|
||||||
|
|
||||||
|
Matrix* create_matrix(ulong row_length, ulong column_length);
|
||||||
|
|
||||||
|
void free_matrix(Matrix *matrix);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user