lab 3 working

This commit is contained in:
2023-02-21 18:22:20 -06:00
parent 60261c73cd
commit 904e96b703
8 changed files with 223 additions and 59 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -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
}

29
.vscode/tasks.json vendored Normal file
View File

@@ -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"
}

Binary file not shown.

View File

@@ -1,81 +1,121 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <inttypes.h>
#include <time.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
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");
}

Binary file not shown.

View File

@@ -0,0 +1,73 @@
#include <stdio.h>
#include <inttypes.h>
#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);
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);
}

View File

@@ -6,7 +6,11 @@
],
"settings": {
"files.associations": {
"stdio.h": "c"
"stdio.h": "c",
"mataddsingle": "c",
"stdint.h": "c",
"inttypes.h": "c",
"wait.h": "c"
}
}
}