large matrices working

This commit is contained in:
2023-02-22 22:17:40 -06:00
parent 904e96b703
commit 21736c114b
8 changed files with 199 additions and 5 deletions

3
.vscode/tasks.json vendored
View File

@@ -10,7 +10,8 @@
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lrt"
"-lrt",
"-pthread"
],
"options": {
"cwd": "${fileDirname}"

View File

@@ -11,7 +11,6 @@
#include <unistd.h>
#define NS_PER_SEC 1000000000
#define MAX 10
static inline uint64_t gettime_ns() {
struct timespec ts;

View File

@@ -3,10 +3,8 @@
#include <stdlib.h>
#include <time.h>
#define NS_PER_SEC 1000000000
static inline uint64_t gettime_ns() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);

Binary file not shown.

View File

@@ -0,0 +1,123 @@
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include <pthread.h>
#include <stdatomic.h>
#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);
}

Binary file not shown.

View File

@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#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;
}

View File

@@ -10,7 +10,8 @@
"mataddsingle": "c",
"stdint.h": "c",
"inttypes.h": "c",
"wait.h": "c"
"wait.h": "c",
"stdlib.h": "c"
}
}
}