diff --git a/.vscode/tasks.json b/.vscode/tasks.json index ca4bbd1..b56e9d6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,7 +10,8 @@ "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", - "-lrt" + "-lrt", + "-pthread" ], "options": { "cwd": "${fileDirname}" diff --git a/03-MatrixAddition (fork)/matAddFork.c b/03-MatrixAddition (fork)/matAddFork.c index ab73382..868eb0b 100644 --- a/03-MatrixAddition (fork)/matAddFork.c +++ b/03-MatrixAddition (fork)/matAddFork.c @@ -11,7 +11,6 @@ #include #define NS_PER_SEC 1000000000 -#define MAX 10 static inline uint64_t gettime_ns() { struct timespec ts; diff --git a/03-MatrixAddition (fork)/matAddSingle.c b/03-MatrixAddition (fork)/matAddSingle.c index cd7bcbd..17bd44c 100644 --- a/03-MatrixAddition (fork)/matAddSingle.c +++ b/03-MatrixAddition (fork)/matAddSingle.c @@ -3,10 +3,8 @@ #include #include - #define NS_PER_SEC 1000000000 - static inline uint64_t gettime_ns() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); diff --git a/04-MatrixAddition (pthread)/matAddThread b/04-MatrixAddition (pthread)/matAddThread new file mode 100644 index 0000000..4443b91 Binary files /dev/null and b/04-MatrixAddition (pthread)/matAddThread differ diff --git a/04-MatrixAddition (pthread)/matAddThread.c b/04-MatrixAddition (pthread)/matAddThread.c new file mode 100644 index 0000000..243fd92 --- /dev/null +++ b/04-MatrixAddition (pthread)/matAddThread.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#define NS_PER_SEC 1000000000 +#define OPS rows*cols +#define CORE 4 + +static int rows, cols; +int* matrix1; +int* matrix2; +int* result; + + + +void *addition(void *arg) { + int tid = *(int *)arg; + if(tid < OPS) { + for(int j = tid*(OPS/CORE); j < (tid*(OPS/CORE) + (OPS/CORE)); j++) { + result[j] = matrix1[j] + matrix2[j]; + } + } + free(arg); + return NULL; +} + + + +static inline uint64_t gettime_ns() { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return ts.tv_sec * NS_PER_SEC + ts.tv_nsec; +} + +int main(int argc, char* argv[]) { + + // Debug args + //argv[1] = "matrices/matA"; + //argv[2] = "matrices/matB"; + + uint64_t start, end; + + // Matrix 1 + FILE* input1 = fopen(argv[1], "r"); + fscanf(input1, "%d", &rows); + fscanf(input1, "%d", &cols); + matrix1 = (int *)malloc(rows*cols*sizeof(int)); + for(int i = 0; i < rows*cols; i++) { + int value; + fscanf(input1, "%d", &value); + matrix1[i] = value; + } + fclose(input1); + + // Matrix 2 + FILE* input2 = fopen(argv[2], "r"); + fscanf(input2, "%d", &rows); + fscanf(input2, "%d", &cols); + + matrix2 = (int *)malloc(rows*cols*sizeof(int)); + for(int i = 0; i < rows*cols; i++) { + int value; + fscanf(input2, "%d", &value); + matrix2[i] = value; + } + fclose(input2); + + result = (int *)malloc(rows*cols*sizeof(int)); + + // Threading functionality + + start = gettime_ns(); + + if(OPS >= CORE) { + pthread_t threads[CORE]; + int remainder = (OPS) % CORE; + // Create threads + for (int i = 0; i < CORE; i++) { + int* a = malloc(sizeof(int)); + *a = i; + if (pthread_create(&threads[i], NULL, addition, a)) { + perror("Error creating thread\n"); + return 1; + } + } + // Wait for threads + for (int i = 0; i < CORE; i++) { + if (pthread_join(threads[i], NULL)) { + perror("Error joining thread\n"); + return 1; + } + } + // Add remaining elements + if(remainder != 0) { + for (int i = (OPS); i < (OPS)+remainder; i++) { + result[i] = matrix1[i] + matrix2[i]; + } + } + } else { + for (int i = OPS; i < OPS; i++) { + result[i] = matrix1[i] + matrix2[i]; + } + } + + end = gettime_ns(); + + // Print result to console + for (int i = 0; i < rows * cols; i++) { + printf("%2d ", result[i]); + if ((i + 1) % cols == 0) { + printf("\n"); + } + } + + printf("Addition took %" PRIu64 " nanoseconds\n", end - start); + + free(matrix1); + free(matrix2); + free(result); +} \ No newline at end of file diff --git a/04-MatrixAddition (pthread)/matAddThread2 b/04-MatrixAddition (pthread)/matAddThread2 new file mode 100644 index 0000000..3b40ef6 Binary files /dev/null and b/04-MatrixAddition (pthread)/matAddThread2 differ diff --git a/04-MatrixAddition (pthread)/matAddThread2.c b/04-MatrixAddition (pthread)/matAddThread2.c new file mode 100644 index 0000000..1699959 --- /dev/null +++ b/04-MatrixAddition (pthread)/matAddThread2.c @@ -0,0 +1,72 @@ +#include +#include +#include + +#define NUM_THREADS 4 + +int *matrix1; +int *matrix2; +int *result; +int num_rows; +int num_cols; + +void *matrix_addition(void *arg) { + int thread_id = *(int *)arg; + int i, j; + for (i = thread_id; i < num_rows; i += NUM_THREADS) { + for (j = 0; j < num_cols; j++) { + int index = i * num_cols + j; + result[index] = matrix1[index] + matrix2[index]; + } + } + pthread_exit(NULL); +} + +int main() { + int i, j; + num_rows = 3; + num_cols = 3; + + // Allocate memory for matrices + matrix1 = malloc(num_rows * num_cols * sizeof(int)); + matrix2 = malloc(num_rows * num_cols * sizeof(int)); + result = malloc(num_rows * num_cols * sizeof(int)); + + // Initialize matrices + for (i = 0; i < num_rows; i++) { + for (j = 0; j < num_cols; j++) { + int index = i * num_cols + j; + matrix1[index] = rand() % 10; + matrix2[index] = rand() % 10; + } + } + + // Create threads + pthread_t threads[NUM_THREADS]; + int thread_ids[NUM_THREADS]; + for (i = 0; i < NUM_THREADS; i++) { + thread_ids[i] = i; + pthread_create(&threads[i], NULL, matrix_addition, &thread_ids[i]); + } + + // Join threads + for (i = 0; i < NUM_THREADS; i++) { + pthread_join(threads[i], NULL); + } + + // Print result matrix + printf("Result matrix:\n"); + for (i = 0; i < num_rows; i++) { + for (j = 0; j < num_cols; j++) { + printf("%d ", result[i * num_cols + j]); + } + printf("\n"); + } + + // Free memory + free(matrix1); + free(matrix2); + free(result); + + return 0; +} diff --git a/CS3841.code-workspace b/CS3841.code-workspace index 77c9d77..da569bc 100644 --- a/CS3841.code-workspace +++ b/CS3841.code-workspace @@ -10,7 +10,8 @@ "mataddsingle": "c", "stdint.h": "c", "inttypes.h": "c", - "wait.h": "c" + "wait.h": "c", + "stdlib.h": "c" } } } \ No newline at end of file