From 04a8712b0f190c74427dda1ebfcd4c893d881577 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Fri, 4 Oct 2024 01:03:39 +0200 Subject: [PATCH] read_csv doesn't work --- graph.c | 3 +- graph.h | 4 ++- main.c | 62 +++++++++++++++++++++++++++++++++++------ matrix.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++-------- matrix.h | 17 +++++++++--- 5 files changed, 145 insertions(+), 26 deletions(-) diff --git a/graph.c b/graph.c index 5ce9629..949ca26 100644 --- a/graph.c +++ b/graph.c @@ -3,12 +3,13 @@ #include void random_adjacency(const uint64_t row_length, const uint64_t column_length, uint64_t matrix[row_length][column_length]) { + srand(time(NULL)); for (uint8_t row_index=0; row_index < row_length; row_index++) { for (uint8_t column_index=0; column_index < column_length; column_index++) { if(column_index == row_index) { - matrix[row_index][column_index] = 1; + matrix[row_index][column_index] = 0; continue; } matrix[row_index][column_index] = rand()%2; diff --git a/graph.h b/graph.h index c5d37dc..fe895c4 100644 --- a/graph.h +++ b/graph.h @@ -3,6 +3,8 @@ #include -void random_adjacency(const uint64_t row_length, const uint64_t column_length, uint64_t matrix[row_length][column_length]); +void random_adjacency(const uint64_t row_length, + const uint64_t column_length, + uint64_t matrix[row_length][column_length]); #endif diff --git a/main.c b/main.c index 29f623d..ffc2a65 100644 --- a/main.c +++ b/main.c @@ -1,16 +1,60 @@ #include "graph.h" #include "matrix.h" #include +#include +#include +void benchmark() { + const uint64_t row_length1 = 100; + const uint64_t column_length1 = 100; + uint64_t matrix1[row_length1][column_length1]; + + const uint64_t row_length2 = 100; + const uint64_t column_length2 = 100; + uint64_t matrix2[row_length2][column_length2]; + + uint64_t new_matrix[row_length1][column_length2]; + + double elapsed_time = 0.0; + const uint64_t iterations = 10000; + clock_t start_time; + + for (uint64_t i = 0; i < iterations; i++) { + random_adjacency(row_length1, column_length1, matrix1); + random_adjacency(row_length2, column_length2, matrix2); + + start_time = clock(); + + matrix_multiply_basic(row_length1, column_length1, matrix1, + row_length2, column_length2, matrix2, + new_matrix); + + elapsed_time += (double)(clock() - start_time) / CLOCKS_PER_SEC; + } + + printf("%lu iterations of matrix_multiply_basic took roughly %f seconds\n", iterations, elapsed_time); + printf("1 iteration of matrix_multiply_basic took on average roughly %f seconds\n", elapsed_time/iterations); +} + +void test() { + const uint64_t row_length1 = 5; + const uint64_t column_length1 = 5; + uint64_t matrix1[row_length1][column_length1]; + + read_csv("csv/graph.csv", row_length1, column_length1, matrix1); + + // print_matrix(row_length1, column_length1, matrix1); + + // uint64_t new_matrix[row_length1][column_length1]; + // + // matrix_multiply_basic(row_length1, column_length1, matrix1, + // row_length1, column_length1, matrix1, + // new_matrix); + // + // print_matrix(row_length1, column_length1, new_matrix); +} int main(void) { - const uint64_t row_length = 5; - const uint64_t column_length = 5; - uint64_t matrix1[row_length][column_length]; - uint64_t matrix2[row_length*2][column_length*2]; - - random_adjacency(row_length, column_length, matrix1); - random_adjacency(row_length*2, column_length*2, matrix2); - - print_matrix(row_length, column_length, matrix1); + test(); + // benchmark(); } diff --git a/matrix.c b/matrix.c index 8ae8974..4def30d 100644 --- a/matrix.c +++ b/matrix.c @@ -1,9 +1,10 @@ +#include #include #include +#include +#include -void print_matrix(const uint64_t row_length, const uint64_t column_length, - const uint64_t matrix[row_length][column_length]) { - +void print_matrix(const uint64_t row_length, const uint64_t column_length, const uint64_t matrix[row_length][column_length]) { for (uint64_t column_index=0; column_index < column_length; column_index++) { for (uint64_t row_index=0; row_index < row_length; row_index++) { printf("%lu ", matrix[row_index][column_index]); @@ -12,15 +13,77 @@ void print_matrix(const uint64_t row_length, const uint64_t column_length, } } -int matrix_multiply_basic(const uint64_t row_length1, - const uint64_t column_length1, - const uint64_t matrix1[row_length1][column_length1], - const uint64_t row_length2, - const uint64_t column_length2, - const uint64_t matrix2[row_length2][column_length2], +int matrix_multiply_basic(const uint64_t row_length1, const uint64_t column_length1, const uint64_t matrix1[row_length1][column_length1], + const uint64_t row_length2, const uint64_t column_length2, const uint64_t matrix2[row_length2][column_length2], uint64_t output_matrix[row_length1][column_length2]) { - int ret = 0; + if (row_length1 != column_length2) { + return 1; + } - return ret; + for (uint64_t i = 0; i < row_length1; i++) { + for(uint64_t j = 0; j < column_length2; j++) { + uint64_t sum = 0; + + for (uint64_t k = 0; k -void print_matrix(const uint64_t row_length, const uint64_t column_length, +void print_matrix(const uint64_t row_length, + const uint64_t column_length, const uint64_t matrix[row_length][column_length]); -// Takes three matrices, the first two will be multiplied and restult will be written to output_matrix. -// Matrix requirements are as specified in the parameters. -// Function return 0 on success and 1 on failure. +/* + First two matrices will be multiplied and + restult will be written to output_matrix. + Matrix requirements are as specified in the parameters. + Function return 0 on success and 1 on failure. +*/ int matrix_multiply_basic(const uint64_t row_length1, const uint64_t column_length1, const uint64_t matrix1[row_length1][column_length1], @@ -17,4 +21,9 @@ int matrix_multiply_basic(const uint64_t row_length1, const uint64_t matrix2[row_length2][column_length2], uint64_t output_matrix[row_length1][column_length2]); +int read_csv(char *file_name, + uint64_t row_length, + uint64_t column_length, + uint64_t output_matrix[row_length][column_length]); + #endif