2024-10-22 23:26:09 +02:00
|
|
|
#include "matrix.h"
|
2024-09-26 14:32:20 +02:00
|
|
|
#include <stdio.h>
|
2024-10-04 01:03:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-10-04 13:05:02 +02:00
|
|
|
#include <time.h>
|
2024-09-26 14:32:20 +02:00
|
|
|
|
2024-10-22 23:26:09 +02:00
|
|
|
void print_matrix(const ulong row_length, const ulong column_length, const ulong matrix[row_length][column_length]) {
|
|
|
|
for (ulong column_index=0; column_index < column_length; column_index++) {
|
|
|
|
for (ulong row_index=0; row_index < row_length; row_index++) {
|
2024-09-26 14:32:20 +02:00
|
|
|
printf("%lu ", matrix[row_index][column_index]);
|
|
|
|
}
|
2024-10-08 22:18:36 +02:00
|
|
|
puts("");
|
2024-09-26 14:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-03 12:48:22 +02:00
|
|
|
|
2024-10-22 23:38:00 +02:00
|
|
|
void print_matrix_struct(const Matrix *matrix) {
|
|
|
|
for (ulong column_index=0; column_index < matrix->column_length; column_index++) {
|
|
|
|
for (ulong row_index=0; row_index < matrix->row_length; row_index++) {
|
|
|
|
printf("%lu ", matrix->values[row_index][column_index]);
|
|
|
|
}
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:26:09 +02:00
|
|
|
void gemm_basic(const ulong row_length1, const ulong column_length1, const ulong matrix1[row_length1][column_length1],
|
|
|
|
const ulong row_length2, const ulong column_length2, const ulong matrix2[row_length2][column_length2],
|
|
|
|
ulong output_matrix[row_length1][column_length2]) {
|
|
|
|
ulong sum;
|
2024-10-04 16:46:40 +02:00
|
|
|
|
2024-10-22 23:26:09 +02:00
|
|
|
for (ulong i = 0; i < row_length1; i++) {
|
|
|
|
for (ulong j = 0; j < column_length2; j++) {
|
2024-10-04 16:46:40 +02:00
|
|
|
sum = 0;
|
2024-10-04 01:03:39 +02:00
|
|
|
|
2024-10-22 23:26:09 +02:00
|
|
|
for (ulong k = 0; k < row_length1; k++) {
|
2024-10-04 01:03:39 +02:00
|
|
|
sum += matrix1[i][k] * matrix2[k][j];
|
|
|
|
}
|
|
|
|
|
|
|
|
output_matrix[i][j] = sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:38:00 +02:00
|
|
|
void gemm_basic_structs(const Matrix *matrix1, const Matrix *matrix2, Matrix *output_matrix) {
|
|
|
|
ulong sum;
|
|
|
|
|
|
|
|
for (ulong i = 0; i < matrix1->row_length; i++) {
|
|
|
|
for (ulong j = 0; j < matrix2->column_length; j++) {
|
|
|
|
sum = 0;
|
|
|
|
|
|
|
|
for (ulong k = 0; k < matrix1->row_length; k++) {
|
|
|
|
sum += matrix1->values[i][k] * matrix2->values[k][j];
|
|
|
|
}
|
|
|
|
|
|
|
|
output_matrix->values[i][j] = sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:26:09 +02:00
|
|
|
int read_csv(const char *file_name, const ulong row_length, const ulong column_length, ulong output_matrix[row_length][column_length]) {
|
2024-10-04 01:03:39 +02:00
|
|
|
FILE *file_ptr;
|
2024-10-22 23:26:09 +02:00
|
|
|
ulong bufsize = row_length*2+1; // have to account for delimiters
|
2024-10-04 13:05:02 +02:00
|
|
|
char buffer[bufsize];
|
|
|
|
char *value, *file_line;
|
2024-10-22 23:26:09 +02:00
|
|
|
ulong row_index = 0, column_index = 0;
|
2024-10-04 01:03:39 +02:00
|
|
|
|
|
|
|
file_ptr = fopen(file_name, "r");
|
|
|
|
if (file_ptr == NULL) {
|
2024-10-08 22:18:36 +02:00
|
|
|
puts("Unable to open csv");
|
2024-10-04 01:03:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-10-04 13:05:02 +02:00
|
|
|
while ((file_line = fgets(buffer, bufsize, file_ptr)) != NULL) {
|
|
|
|
// This shit is just needed and I dont know why
|
|
|
|
file_line[strcspn(file_line, "\n")] = 0;
|
|
|
|
value = strtok(file_line, ";,");
|
|
|
|
|
|
|
|
// for some reason there are two NULLs at the end of a line
|
|
|
|
// and I dont wanna increment the column_index
|
|
|
|
if (value == NULL) {
|
|
|
|
continue;
|
2024-10-04 01:03:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 13:05:02 +02:00
|
|
|
while (value != NULL) {
|
|
|
|
output_matrix[row_index++][column_index] = strtoul(value, NULL, 0);
|
|
|
|
value = strtok(NULL, ";,");
|
2024-10-04 01:03:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 13:05:02 +02:00
|
|
|
row_index = 0;
|
|
|
|
column_index++;
|
|
|
|
}
|
2024-10-04 01:03:39 +02:00
|
|
|
|
|
|
|
fclose(file_ptr);
|
2024-10-03 12:48:22 +02:00
|
|
|
|
2024-10-04 01:03:39 +02:00
|
|
|
return 0;
|
2024-10-03 12:48:22 +02:00
|
|
|
}
|
2024-11-19 11:46:06 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|