Compare commits

...

11 Commits
main ... simon

Author SHA1 Message Date
Simon Wiesend
2d12ecee4b
add some comments 2025-11-23 19:59:44 +01:00
Simon Wiesend
adf75b66d2
make matrix multiplication faster 2025-11-23 16:31:18 +01:00
Simon Wiesend
00ac1aa04a
initial prototype 2025-11-23 16:29:59 +01:00
Simon Wiesend
04768db522
update .gitignore 2025-11-23 15:57:18 +01:00
Simon Wiesend
01a13090a5
Merge remote-tracking branch 'origin/neuralNetworkTests' into simon 2025-11-23 15:53:22 +01:00
Simon Wiesend
92ad1e1c31
clean up and improve allocation error handling 2025-11-21 09:09:55 +01:00
Simon Wiesend
6137e45bdb
Merge branch 'main' into matrix 2025-11-17 18:35:46 +01:00
Simon Wiesend
0d7f380d87
implement matmul 2025-11-14 09:17:31 +01:00
Simon Wiesend
645d471860
first implementation of new broadcasting addition requirement 2025-11-11 19:05:03 +01:00
721f5cc2d1 merge upstream 2025-11-11 12:55:12 +00:00
Simon Wiesend
79ad1285b5
This implements all functions in matrix.c except multiply(). All tests from matrixTests.c are passing for the implemented functions. 2025-11-09 13:43:06 +01:00
4 changed files with 292 additions and 18 deletions

6
.gitignore vendored
View File

@ -1,4 +1,8 @@
mnist
runTests
*.o
*.exe
*.exe
runMatrixTests
runImageInputTests
runNeuralNetworkTests
.vscode

View File

@ -6,17 +6,165 @@
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
typedef enum
{
IMG_SUCCESS = 0,
IMG_ERR_INVALID_HEADER, // Header did not match
IMG_ERR_READ, // Failed to read from file, maybe it's too short
IMG_ERR, // General Error
} ImageError;
static ImageError checkHeader(FILE *file);
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file);
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file);
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series);
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
// It's very important to call calloc here because otherwise clearSeries() might try to free random memory
GrayScaleImageSeries *series = calloc(1, sizeof(GrayScaleImageSeries));
if (series == NULL)
{
return NULL;
}
FILE *file = fopen(path, "rb");
// If fopen() failed
if (file == NULL)
{
clearSeries(series);
series = NULL;
return NULL;
}
// Try to parse the whole file. If anything fails memory is freed
if (parseImageFile(file, series))
{
clearSeries(series);
series = NULL; // Return NULL after the file is properly closed
}
fclose(file);
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
static ImageError parseImageFile(FILE *file, GrayScaleImageSeries *series)
{
if (checkHeader(file))
{
return IMG_ERR; // header check failed
}
unsigned short imgCNT, width, height;
if (readPictureParams(&imgCNT, &width, &height, file))
{
return IMG_ERR; // read failed
}
size_t pixels = width * height;
// The images contain more pointers, definitely use calloc here
series->images = calloc(imgCNT, sizeof(GrayScaleImage));
if (series->images == NULL)
{
return IMG_ERR;
}
series->labels = malloc(imgCNT * sizeof(unsigned char));
if (series->labels == NULL)
{
return IMG_ERR;
}
series->count = imgCNT;
// Read every image and it's label
for (size_t imageIdx = 0; imageIdx < imgCNT; imageIdx++)
{
GrayScaleImage *curImage = &series->images[imageIdx];
curImage->buffer = malloc(sizeof(GrayScalePixelType) * pixels);
if (curImage->buffer == NULL)
{
return IMG_ERR;
}
curImage->width = width;
curImage->height = height;
if (readImage(pixels, curImage->buffer, &series->labels[imageIdx], file))
{
return IMG_ERR;
}
}
return IMG_SUCCESS;
}
static ImageError checkHeader(FILE *file)
{
size_t len = strlen(FILE_HEADER_STRING);
char headerBuf[len + 1];
size_t charsRead = fread(headerBuf, 1, len, file);
// Check if the file is to short
if (charsRead < len)
{
return IMG_ERR_READ;
}
headerBuf[len] = '\0'; // Terminate string
return strcmp(headerBuf, FILE_HEADER_STRING) == 0 ? IMG_SUCCESS : IMG_ERR_INVALID_HEADER;
}
static ImageError readPictureParams(unsigned short *number, unsigned short *width, unsigned short *height, FILE *file)
{
// number of images
if (1 != fread(number, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
// image width
if (1 != fread(width, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
// height
if (1 != fread(height, sizeof(unsigned short), 1, file))
{
return IMG_ERR_READ;
}
return IMG_SUCCESS;
}
// Read 1 image and it's label
static ImageError readImage(size_t numPixels, GrayScalePixelType *pixelBuffer, unsigned char *label, FILE *file)
{
if (numPixels > fread(pixelBuffer, sizeof(GrayScalePixelType), numPixels, file))
{
return IMG_ERR_READ;
}
if (1 != fread(label, sizeof(unsigned char), 1, file))
{
return IMG_ERR_READ;
}
return IMG_SUCCESS;
}
// Frees memory for each image buffer, image, label and finally series
void clearSeries(GrayScaleImageSeries *series)
{
if (series)
{
int seriesLen = series->count;
for (size_t imageIdx = 0; imageIdx < seriesLen; imageIdx++)
{
free(series->images[imageIdx].buffer);
}
free(series->images);
free(series->labels);
free(series);
}
}

137
matrix.c
View File

@ -1,35 +1,152 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
#include <stdio.h>
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix mat = {.rows = rows, .cols = cols};
// If one dimension is 0, return both dimensions as 0 and don't init the array/buffer.
if (rows == 0 || cols == 0)
{
mat.rows = 0;
mat.cols = 0;
return mat;
}
// allocate contiguous and 0 initialized memory
mat.buffer = calloc(rows * cols, sizeof(MatrixType));
// check if calloc failed
if (mat.buffer == NULL)
{
clearMatrix(&mat);
perror("could not allocate memory");
}
return mat;
}
// reduce the dimensions to (0, 0) and free the memory
void clearMatrix(Matrix *matrix)
{
free(matrix->buffer);
matrix->buffer = NULL;
matrix->cols = 0;
matrix->rows = 0;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
// do nothing if idx is not in array or matrix buffer is NULL
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
{
return;
}
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
}
// return UNDEFINED_MATRIX_VALUE if idx is not in array or matrix buffer is NULL
if (!(rowIdx < matrix.rows) || !(colIdx < matrix.cols) || matrix.buffer == NULL)
{
return UNDEFINED_MATRIX_VALUE;
}
return matrix.buffer[rowIdx * matrix.cols + colIdx];
};
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix resMat = (matrix1.cols > matrix2.cols) ? createMatrix(matrix1.rows, matrix1.cols) : createMatrix(matrix2.rows, matrix2.cols);
if (resMat.buffer == NULL)
{
return createMatrix(0, 0);
}
if (matrix1.cols != matrix2.cols)
{
if (matrix1.rows != matrix2.rows)
{
clearMatrix(&resMat);
return resMat;
}
else if (matrix1.cols == 1)
{
// broadcast vector
for (size_t m = 0; m < matrix2.rows; m++)
{
for (size_t n = 0; n < matrix2.cols; n++)
{
setMatrixAt(getMatrixAt(matrix2, m, n) + getMatrixAt(matrix1, m, 0), resMat, m, n);
}
}
return resMat;
}
else if (matrix2.cols == 1)
{
// broadcast vector
for (size_t m = 0; m < matrix1.rows; m++)
{
for (size_t n = 0; n < matrix1.cols; n++)
{
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, 0), resMat, m, n);
}
}
return resMat;
}
else
{
clearMatrix(&resMat);
return resMat;
}
}
for (size_t m = 0; m < matrix1.rows; m++)
{
for (size_t n = 0; n < matrix1.cols; n++)
{
setMatrixAt(getMatrixAt(matrix1, m, n) + getMatrixAt(matrix2, m, n), resMat, m, n);
}
}
return resMat;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix A, const Matrix B)
{
if (A.cols != B.rows || A.buffer == NULL || B.buffer == NULL)
{
return createMatrix(0, 0);
}
int rows = A.rows, cols = B.cols;
Matrix C = createMatrix(rows, cols);
if (C.buffer == NULL)
{
return createMatrix(0, 0);
}
// M = Rows, K = Common Dim, N = Cols
size_t M = A.rows, K = A.cols, N = B.cols;
for (size_t i = 0; i < M; i++)
{
for (size_t k = 0; k < K; k++)
{
MatrixType valA = A.buffer[i * K + k];
for (size_t j = 0; j < N; j++)
{
// C[i, j] += A[i, k] * B[k, j];
// M x N, M x K, K x N
C.buffer[i * N + j] += valA * B.buffer[k * N + j];
}
}
}
return C;
}

View File

@ -5,8 +5,14 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
// Matrixtyp
typedef struct Matrix
{
size_t rows;
size_t cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
@ -15,5 +21,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif