diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..732e57b --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "CLOCK_REALTIME" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "c++98", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ca4bbd1 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc-9 build active file", + "command": "/usr/bin/gcc-9", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}", + "-lrt" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/03-MatrixAddition (fork)/mat b/03-MatrixAddition (fork)/mat deleted file mode 100644 index e69de29..0000000 diff --git a/03-MatrixAddition (fork)/matAddFork b/03-MatrixAddition (fork)/matAddFork new file mode 100644 index 0000000..dc780ad Binary files /dev/null and b/03-MatrixAddition (fork)/matAddFork differ diff --git a/03-MatrixAddition (fork)/matAddFork.c b/03-MatrixAddition (fork)/matAddFork.c index 5b9944e..ab73382 100644 --- a/03-MatrixAddition (fork)/matAddFork.c +++ b/03-MatrixAddition (fork)/matAddFork.c @@ -1,81 +1,121 @@ #include #include -#include +#include +#include +#include +#include #include +#include #include +#include #include -int main() { - int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX]; - int i, j, k, sum = 0; - int shmid; - int *shm; +#define NS_PER_SEC 1000000000 +#define MAX 10 - // Allocate shared memory - shmid = shmget(IPC_PRIVATE, MAX * MAX * sizeof(int), 0666 | IPC_CREAT); - if (shmid < 0) { - perror("shmget"); +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/matC"; + //argv[2] = "matrices/matD"; + + uint64_t start, end; + int rows, cols; + + // Matrix 1 + FILE* input1 = fopen(argv[1], "r"); + fscanf(input1, "%d", &rows); + fscanf(input1, "%d", &cols); + int * 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); + + int * 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); + + // Create shared memory object for result + int shm_fd = shm_open("MATRIX", O_CREAT | O_RDWR, 0666); + if (shm_fd == -1) { + perror("Error creating shared memory object"); exit(1); } - // Attach shared memory - shm = shmat(shmid, NULL, 0); - if (shm == (int *)-1) { - perror("shmat"); + // Resize shared memory object to fit result + if (ftruncate(shm_fd, rows*cols*sizeof(int))) { + perror("Error resizing shared memory object"); exit(1); } - // Initialize matrices A and B - for (i = 0; i < MAX; i++) { - for (j = 0; j < MAX; j++) { - A[i][j] = i + j; - B[i][j] = i - j; - } - } - - // Fork a child process - pid_t pid = fork(); - - if (pid == -1) { - perror("fork"); + // Map shared memory object to C + int *result = mmap(NULL, rows*cols*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (result == MAP_FAILED) { + perror("Error mapping shared memory object to C"); exit(1); } - else if (pid == 0) { // Child process - for (i = 0; i < MAX; i++) { - for (j = 0; j < MAX; j++) { - sum = 0; - for (k = 0; k < MAX; k++) { - sum += A[i][k] * B[k][j]; - } - *(shm + i * MAX + j) = sum; - } + + // Allocate Memory for pids + int* pids = (int*)malloc(rows*sizeof(pid_t)); + + + // Perform Forked Process Matrix Addition + start = gettime_ns(); + for (int i = 0; i < rows; i++) { + pids[i] = fork(); + if(pids[i] == -1) { + // Fork error + perror("Error with creating process\n"); + return 1; + } + if(pids[i] == 0) { + // Child process + for(int j = 0; j < cols; j++) { + result[i*cols+j] = matrix1[i*cols+j] + matrix2[i*cols+j]; + } + //printf("%d",pids[i]); + return 1; } - exit(0); } - else { // Parent process - wait(NULL); + end = gettime_ns(); + for (int i = 0; i < rows; i++) { + waitpid(pids[i], NULL, 0); + } + end = gettime_ns(); - // Retrieve result from shared memory - for (i = 0; i < MAX; i++) { - for (j = 0; j < MAX; j++) { - C[i][j] = *(shm + i * MAX + j); - } - } - - // Print result matrix C - printf("Matrix C:\n"); - for (i = 0; i < MAX; i++) { - for (j = 0; j < MAX; j++) { - printf("%d ", C[i][j]); - } + // Print result to console + for (int i = 0; i < rows * cols; i++) { + printf("%2d ", result[i]); + if ((i + 1) % cols == 0) { printf("\n"); } - - // Detach and remove shared memory - shmdt(shm); - shmctl(shmid, IPC_RMID, NULL); - munmap(shm); } - return 0; + printf("Addition took %" PRIu64 " nanoseconds\n", end - start); + + free(matrix1); + free(matrix2); + + // Unmap and unlink shared memory object + munmap(result, rows*cols*sizeof(int)); + close(shm_fd); + shm_unlink("MATRIX"); } \ No newline at end of file diff --git a/03-MatrixAddition (fork)/matAddSingle b/03-MatrixAddition (fork)/matAddSingle new file mode 100644 index 0000000..77ca066 Binary files /dev/null and b/03-MatrixAddition (fork)/matAddSingle differ diff --git a/03-MatrixAddition (fork)/matAddSingle.c b/03-MatrixAddition (fork)/matAddSingle.c new file mode 100644 index 0000000..cd7bcbd --- /dev/null +++ b/03-MatrixAddition (fork)/matAddSingle.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + + +#define NS_PER_SEC 1000000000 + + +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[]) +{ + uint64_t start, end; + int rows, cols; + + // Matrix 1 + FILE* input1 = fopen(argv[1], "r"); + fscanf(input1, "%d", &rows); + fscanf(input1, "%d", &cols); + int * 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); + + int * 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); + + + start = gettime_ns(); + int * result = (int *)malloc(rows*cols*sizeof(int)); + + // Perform Single Process Matrix Addition + for(int i= 0; i < rows*cols; 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); +} + + diff --git a/CS3841.code-workspace b/CS3841.code-workspace index 813e2f5..77c9d77 100644 --- a/CS3841.code-workspace +++ b/CS3841.code-workspace @@ -6,7 +6,11 @@ ], "settings": { "files.associations": { - "stdio.h": "c" + "stdio.h": "c", + "mataddsingle": "c", + "stdint.h": "c", + "inttypes.h": "c", + "wait.h": "c" } } } \ No newline at end of file