lab 3 working
This commit is contained in:
18
.vscode/c_cpp_properties.json
vendored
Normal file
18
.vscode/c_cpp_properties.json
vendored
Normal 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
29
.vscode/tasks.json
vendored
Normal 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"
|
||||||
|
}
|
||||||
BIN
03-MatrixAddition (fork)/matAddFork
Normal file
BIN
03-MatrixAddition (fork)/matAddFork
Normal file
Binary file not shown.
@@ -1,81 +1,121 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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/shm.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main() {
|
#define NS_PER_SEC 1000000000
|
||||||
int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
|
#define MAX 10
|
||||||
int i, j, k, sum = 0;
|
|
||||||
int shmid;
|
|
||||||
int *shm;
|
|
||||||
|
|
||||||
// Allocate shared memory
|
static inline uint64_t gettime_ns() {
|
||||||
shmid = shmget(IPC_PRIVATE, MAX * MAX * sizeof(int), 0666 | IPC_CREAT);
|
struct timespec ts;
|
||||||
if (shmid < 0) {
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
perror("shmget");
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach shared memory
|
// Resize shared memory object to fit result
|
||||||
shm = shmat(shmid, NULL, 0);
|
if (ftruncate(shm_fd, rows*cols*sizeof(int))) {
|
||||||
if (shm == (int *)-1) {
|
perror("Error resizing shared memory object");
|
||||||
perror("shmat");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize matrices A and B
|
// Map shared memory object to C
|
||||||
for (i = 0; i < MAX; i++) {
|
int *result = mmap(NULL, rows*cols*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
|
||||||
for (j = 0; j < MAX; j++) {
|
if (result == MAP_FAILED) {
|
||||||
A[i][j] = i + j;
|
perror("Error mapping shared memory object to C");
|
||||||
B[i][j] = i - j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fork a child process
|
|
||||||
pid_t pid = fork();
|
|
||||||
|
|
||||||
if (pid == -1) {
|
|
||||||
perror("fork");
|
|
||||||
exit(1);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
else { // Parent process
|
|
||||||
wait(NULL);
|
|
||||||
|
|
||||||
// Retrieve result from shared memory
|
// Allocate Memory for pids
|
||||||
for (i = 0; i < MAX; i++) {
|
int* pids = (int*)malloc(rows*sizeof(pid_t));
|
||||||
for (j = 0; j < MAX; j++) {
|
|
||||||
C[i][j] = *(shm + i * MAX + j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print result matrix C
|
|
||||||
printf("Matrix C:\n");
|
// Perform Forked Process Matrix Addition
|
||||||
for (i = 0; i < MAX; i++) {
|
start = gettime_ns();
|
||||||
for (j = 0; j < MAX; j++) {
|
for (int i = 0; i < rows; i++) {
|
||||||
printf("%d ", C[i][j]);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end = gettime_ns();
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
waitpid(pids[i], NULL, 0);
|
||||||
|
}
|
||||||
|
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("\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");
|
||||||
}
|
}
|
||||||
BIN
03-MatrixAddition (fork)/matAddSingle
Normal file
BIN
03-MatrixAddition (fork)/matAddSingle
Normal file
Binary file not shown.
73
03-MatrixAddition (fork)/matAddSingle.c
Normal file
73
03-MatrixAddition (fork)/matAddSingle.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -6,7 +6,11 @@
|
|||||||
],
|
],
|
||||||
"settings": {
|
"settings": {
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"stdio.h": "c"
|
"stdio.h": "c",
|
||||||
|
"mataddsingle": "c",
|
||||||
|
"stdint.h": "c",
|
||||||
|
"inttypes.h": "c",
|
||||||
|
"wait.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user