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 "matrix.h"
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
@ -266,40 +265,24 @@ void test_with_dfs() {
|
||||
void test_with_structs() {
|
||||
ulong row_length = 5, column_length = 5;
|
||||
|
||||
ulong **values1 = (ulong**)malloc(row_length * sizeof(ulong*));
|
||||
ulong **values2 = (ulong**)malloc(row_length * sizeof(ulong*));
|
||||
ulong **values3 = (ulong**)malloc(row_length * sizeof(ulong*));
|
||||
Matrix *adjacency_matrix1 = create_matrix(row_length, column_length);
|
||||
Matrix *adjacency_matrix2 = create_matrix(row_length, column_length);
|
||||
Matrix *dot_product = create_matrix(row_length, column_length);
|
||||
|
||||
for (ulong index = 0; index < row_length; index++) {
|
||||
values1[index] = (ulong*)malloc(column_length * sizeof(ulong));
|
||||
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);
|
||||
random_adjacency_struct(adjacency_matrix1);
|
||||
print_matrix_struct(adjacency_matrix1);
|
||||
putchar('\n');
|
||||
|
||||
random_adjacency_struct(&adjacency_matrix2);
|
||||
print_matrix_struct(&adjacency_matrix2);
|
||||
random_adjacency_struct(adjacency_matrix2);
|
||||
print_matrix_struct(adjacency_matrix2);
|
||||
putchar('\n');
|
||||
|
||||
gemm_basic_structs(&adjacency_matrix1, &adjacency_matrix2, &dot_product);
|
||||
print_matrix_struct(&dot_product);
|
||||
putchar('\n');
|
||||
gemm_basic_structs(adjacency_matrix1, adjacency_matrix2,dot_product);
|
||||
print_matrix_struct(dot_product);
|
||||
|
||||
for (ulong index = 0; index < row_length; index++) {
|
||||
free(values1[index]);
|
||||
free(values2[index]);
|
||||
free(values3[index]);
|
||||
}
|
||||
free(values1);
|
||||
free(values2);
|
||||
free(values3);
|
||||
free_matrix(adjacency_matrix1);
|
||||
free_matrix(adjacency_matrix2);
|
||||
free_matrix(dot_product);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user