forked from freudenreichan/info2Praktikum-NeuronalesNetz
refactor: change storage type of matrix to flat storage
This commit is contained in:
1 parent
3ee900784e
commit
5e1a1fdf72
2 files changed
+23
-11
No files matched your search
+21
-10
@@ -4,29 +4,39 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx)
|
||||||
|
{
|
||||||
|
return rowIdx * cols + colIdx;
|
||||||
|
}
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int));
|
Matrix* m = malloc(sizeof(Matrix));
|
||||||
const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage};
|
m->rows = rows;
|
||||||
|
m->cols = cols;
|
||||||
return matrix;
|
m->storage = malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
matrix->rows = 0;
|
for (int i = 0; i < matrix->rows; i++)
|
||||||
matrix->cols = 0;
|
{
|
||||||
__builtin_free(matrix->storage);
|
for (int j = 0; j < matrix->cols; j++)
|
||||||
|
{
|
||||||
|
setMatrixElement(matrix, i, j, UNDEFINED_MATRIX_VALUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
matrix.storage[rowIdx][colIdx] = value;
|
matrix.storage[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
return matrix.storage[rowIdx][colIdx];
|
return matrix.storage[getMatrixIndex(matrix->cols, rowIdx, colIdx)];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
@@ -42,7 +52,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < matrix1.cols; j++)
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
{
|
{
|
||||||
result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j];
|
unsigned int index = getMatrixIndex(matrix1.cols, i, j);
|
||||||
|
result.storage[index] = matrix1.storage[index] + matrix2.storage[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -6,11 +6,12 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
|
// Flat Storage: Nur ein Pointer, sorgt für zusammenhängendes array, kein verstreuter Speicher -> bessere Performance
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
|
|
||||||
MatrixType** storage;
|
MatrixType* storage;
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
Reference in new issue
Block a user