Skip to content

Commit

Permalink
Align matmul-c tests with matmul-julia tests
Browse files Browse the repository at this point in the history
Updates the matmul-c square matrix scaling tests to align with mhauru's
Julia tests.

The benchmarks now perform 19 test with increasingly large square
matrices. Single-threaded and multi-threaded matrix multiplication tests
are both performed. The results are stored in the same results.csv file
so that everything can be plotted together.
  • Loading branch information
llewelld committed Aug 9, 2024
1 parent c0faf90 commit ea63cb1
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 48 deletions.
1 change: 1 addition & 0 deletions implementation/matmul-c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ matmul-c: \
src/threadpool.c \
src/utils.c \
src/benchmarks.c \
src/store.c \
src/tests.c
$(CC) $(CFLAGS) -o$@ $^ $(CLIBS)

Expand Down
3 changes: 2 additions & 1 deletion implementation/matmul-c/include/benchmarks.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand All @@ -19,6 +19,7 @@ void benchmarks_end(Benchmark *benchmark);
void benchmark_set_quiet(Benchmark *benchmark, bool quiet);
void benchmarks_multiply_big(ThreadPool *pool);
void benchmarks_multiply_small(Matrices *a, Matrices *b, Matrices *d);
void benchmark_multiply_square(ThreadPool *pool);

#endif /* __MATRIX_BENCHMARKS_H */

2 changes: 1 addition & 1 deletion implementation/matmul-c/include/load.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand Down
6 changes: 4 additions & 2 deletions implementation/matmul-c/include/matrix.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */

#include <stdint.h>

#include "utils.h"

#ifndef __MATRIX_MATRIX_H
#define __MATRIX_MATRIX_H (1)

Expand All @@ -18,6 +20,6 @@ Matrix * new_matrix(uint16_t height, uint16_t width);
Matrix * delete_matrix(Matrix *A);
Matrix * new_matrix_identity(uint16_t height, uint16_t width);
void matrix_print(Matrix *A);
void matrix_fill(Matrix *A, uint32_t seed);
void matrix_fill(Matrix *A, Rand * const rand);

#endif /* __MATRIX_MATRIX_H */
2 changes: 1 addition & 1 deletion implementation/matmul-c/include/operations.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand Down
2 changes: 1 addition & 1 deletion implementation/matmul-c/include/parse_header.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand Down
30 changes: 30 additions & 0 deletions implementation/matmul-c/include/store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */

#include <stdint.h>
#include <stdbool.h>

#ifndef __MATRIX_STORE_H
#define __MATRIX_STORE_H (1)

typedef struct _Store {
// Pointer to the data
char * data;
// The allocated size
size_t size;
// The size of the data store in the allocation
size_t length;
// The chunk size to allocate blocks in
size_t chunk_size;
} Store;

Store * new_store(size_t chunk_size);
Store * delete_store(Store *store);
bool store_append(Store * const store, char const * const data, size_t length);
bool store_setsize(Store * const store, size_t size);
size_t store_printf (Store * const store, char const * const format, ...);
size_t store_printf_append (Store * const store, char const * const format, ...);

#endif /* __MATRIX_STORE_H */
2 changes: 1 addition & 1 deletion implementation/matmul-c/include/tests.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand Down
5 changes: 3 additions & 2 deletions implementation/matmul-c/include/threadpool.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand All @@ -17,8 +17,9 @@

typedef struct _ThreadPool ThreadPool;

ThreadPool * new_threadpool();
ThreadPool * new_threadpool(uint32_t threads);
ThreadPool * delete_threadpool(ThreadPool *pool);
bool multiply_parallel(ThreadPool *pool, Matrix *result, Matrix *A, Matrix *B);
uint32_t threadpool_threads(ThreadPool *pool);

#endif /* __MATRIX_THREADPOOL_H */
4 changes: 2 additions & 2 deletions implementation/matmul-c/include/utils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand All @@ -14,6 +14,6 @@ Rand * new_rand();
Rand * delete_rand(Rand *rand);
void rand_seed(Rand *rand, uint32_t seed);
double rand_next(Rand *rand);
double rand_digit(Rand *rand);
double rand_value(Rand *rand);

#endif /* __MATRIX_UTILS_H */
16 changes: 12 additions & 4 deletions implementation/matmul-c/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* vim: noet:ts=2:sts=2:sw=2 */
/* vim: noet:ts=2:sts=2:sw=2 */

/* SPDX-License-Identifier: MIT */
/* Copyright © 2024 David Llewellyn-Jones */
Expand All @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
bool result;
uint32_t total;

ThreadPool *pool = new_threadpool();
ThreadPool *pool = new_threadpool(10);

// Play around with the API
printf("Example matrix manipulation...\n");
Expand Down Expand Up @@ -55,11 +55,19 @@ int main(int argc, char *argv[]) {
// Perform 512 multiplications and compare against the results from NumPy
tests_compare(a, b, c, d, pool);

// Benchmark square matrix multiplications single-threaded
printf("Square matrix benchmark single-threaded\n");
benchmark_multiply_square(NULL);

// Benchmark square matrix multiplications using threads
printf("Square matrix benchmark multi-threaded\n");
benchmark_multiply_square(pool);

// Benchmark large matrix multiplications
benchmarks_multiply_big(pool);
//benchmarks_multiply_big(pool);

// Measure time taken to perform 16777216 multiplications
benchmarks_multiply_small(a, b, d);
//benchmarks_multiply_small(a, b, d);

a = delete_matrices(a);
b = delete_matrices(b);
Expand Down
Loading

0 comments on commit ea63cb1

Please sign in to comment.