uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 677acf9f5d5842a59a0b17105351b48b52bcb6fa
parent 16cbb42e669a02c33571b67b76a9670a4c1264a9
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu, 10 Dec 2020 22:11:55 +0200

new mips assignment

Diffstat:
Ac-parallel-computing/ex2/doc.pdf | 0
Ac-parallel-computing/ex2/doc.tex | 24++++++++++++++++++++++++
Ac-parallel-computing/ex2/ex2.c | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmips-architecture/lab3_ex1.asm | 6+++---
Mmips-architecture/lab3_ex2.asm | 2+-
Mmips-architecture/lab4_ex1.asm | 8++++----
Mmips-architecture/lab4_ex2.asm | 2--
Amips-architecture/lab5_ex1.asm | 42++++++++++++++++++++++++++++++++++++++++++
Amips-architecture/lab5_ex2.asm | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Amips-architecture/lab5_ex3.asm | 25+++++++++++++++++++++++++
Amips-architecture/lab5_ex4.asm | 36++++++++++++++++++++++++++++++++++++
Amips-architecture/lab5_ex5.asm | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 354 insertions(+), 10 deletions(-)

diff --git a/c-parallel-computing/ex2/doc.pdf b/c-parallel-computing/ex2/doc.pdf Binary files differ. diff --git a/c-parallel-computing/ex2/doc.tex b/c-parallel-computing/ex2/doc.tex @@ -0,0 +1,24 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage[backend=biber]{biblatex} + +\title{Εργαστήριο Παράλληλου Υπολογισμού - Εργασία 2} +\author{Χρήστος Μαργιώλης} +\date{Δεκέμβριος 2020} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\renewcommand{\contentsname}{Περιεχόμενα} +\tableofcontents + +\end{document} diff --git a/c-parallel-computing/ex2/ex2.c b/c-parallel-computing/ex2/ex2.c @@ -0,0 +1,105 @@ +#include <stdio.h> +#include <stdlib.h> +#include <mpi.h> + +static float input(const char *, int); +static void readvec(float *, int); +static float calcavg(float *, int); +static void *emalloc(size_t); + +static float +input(const char *fmt, int i) +{ + char buf[48]; + float n; + + sprintf(buf, fmt, i); + printf("%s", buf); + scanf("%f", &n); + getchar(); + return n; +} + +static void +readvec(float *vec, int n) +{ + int i = 0; + + for (; i < n; i++) + vec[i] = input("vec[%d]: ", i); +} + +static void * +emalloc(size_t nb) +{ + void *p; + + if ((p = malloc(nb)) == NULL) { + fputs("ex2: cannot allocate memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +static float +calcavg(float *vec, int n) +{ + float sum = 0.0f; + int i = 0; + + for (; i < n; i++) + sum += vec[i]; + return (sum / (float)n); +} + +int +main(int argc, char *argv[]) +{ + int rank, nproc, root = 0; + int n, localn; + float *vec, *localvec; + float localsum, sum; + float *avgs, localavg, finalavg; + float var; + int i; + + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &nproc); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + if (rank == root) { + n = input("N: ", 0); + vec = emalloc(n * sizeof(float)); + readvec(vec, n); + } + + /* part 0 - calc avg */ + MPI_Bcast(&n, 1, MPI_INT, root, MPI_COMM_WORLD); + localn = n / nproc; + localvec = emalloc(localn * sizeof(float)); + MPI_Scatter(vec, localn, MPI_FLOAT, localvec, localn, MPI_FLOAT, root, MPI_COMM_WORLD); + + avgs = emalloc(nproc * sizeof(float)); + localavg = calcavg(localvec, localn); + MPI_Gather(&localavg, 1, MPI_FLOAT, avgs, 1, MPI_FLOAT, root, MPI_COMM_WORLD); + + if (rank == root) { + finalavg = calcavg(avgs, nproc); + } + + /*MPI_Bcast(&finalavg, 1, MPI_FLOAT, root, MPI_COMM_WORLD);*/ + /*MPI_Scatter(vec, localn, MPI_FLOAT, localvec, localn, MPI_FLOAT, root, MPI_COMM_WORLD);*/ + + if (rank == root) { + printf("avg: %.2f\n", finalavg); + } + + /* move up? */ + free(vec); + free(localvec); + free(avgs); + + MPI_Finalize(); + + return 0; +} diff --git a/mips-architecture/lab3_ex1.asm b/mips-architecture/lab3_ex1.asm @@ -7,9 +7,9 @@ .eqv SYS_READ_CHAR 12 .macro endl() - li $v0, SYS_PRINT_CHAR - li $a0, 0xa - syscall +li $v0, SYS_PRINT_CHAR +li $a0, 0xa +syscall .end_macro .data diff --git a/mips-architecture/lab3_ex2.asm b/mips-architecture/lab3_ex2.asm @@ -45,7 +45,7 @@ main: # 3. read a string and print its 2nd char li $v0, SYS_READ_STRING la $a0, name - la $a1, 64 + li $a1, 64 syscall lb $a0, name + 1 li $v0, SYS_PRINT_CHAR diff --git a/mips-architecture/lab4_ex1.asm b/mips-architecture/lab4_ex1.asm @@ -9,7 +9,7 @@ .text .globl main -main: +main: li $v0, SYS_READ_WORD syscall @@ -23,9 +23,8 @@ main: nonzero: li $v0, SYS_PRINT_STRING la $a0, nzstr - syscall - j exit + syscall exit: li $v0, SYS_EXIT - syscall + syscall+ \ No newline at end of file diff --git a/mips-architecture/lab4_ex2.asm b/mips-architecture/lab4_ex2.asm @@ -28,8 +28,6 @@ pos: la $a0, posstr syscall - j exit - exit: li $v0, SYS_EXIT syscall diff --git a/mips-architecture/lab5_ex1.asm b/mips-architecture/lab5_ex1.asm @@ -0,0 +1,42 @@ +.eqv SYS_PRINT_WORD 1 +.eqv SYS_READ_STRING 8 +.eqv SYS_EXIT 10 + +.data + str: .space 17 # + 2 bytes to include '\n' and '\0' + len: .asciiz "strlen: " + revstr: .asciiz "reverse: " + +.text +.globl main + +main: + # read input string + li $v0, SYS_READ_STRING + la $a0, str + li $a1, 17 + syscall + + # init strlen counter + li $t0, 0 + # load string to $t1 because we want to use $a0 + la $t1, str + +strlen: + lb $a0, 0($t1) + beqz $a0, exit + addi $t0, $t0, 1 + addi $t1, $t1, 1 + j strlen + +exit: + li $v0, SYS_PRINT_STRING + la $a0, len + syscall + + li $v0, SYS_PRINT_WORD + la $a0, 0($t0) + syscall + + li $v0, SYS_EXIT + syscall diff --git a/mips-architecture/lab5_ex2.asm b/mips-architecture/lab5_ex2.asm @@ -0,0 +1,50 @@ +.eqv SYS_PRINT_STRING 4 +.eqv SYS_READ_STRING 8 +.eqv SYS_EXIT 10 +.eqv SYS_PRINT_CHAR 11 + +.data + str: .space 17 # + 2 bytes to include '\n' and '\0' + revstr: .asciiz "reverse: " + +.text +.globl main + +main: + # read input string + li $v0, SYS_READ_STRING + la $a0, str + li $a1, 17 + syscall + + # init strlen counter + li $t0, 0 + # load string to $t1 because we want to use $a0 + la $t1, str + +strlen: + lb $a0, 0($t1) + beqz $a0, strrev + addi $t0, $t0, 1 + addi $t1, $t1, 1 + j strlen + + # subtract 2 so that we skip '\n' and '\0' and get to the + # last character in the string + addi $t1, $t1, -2 + +strrev: + # if $t0 is 0 is means we reached str[0] + beqz $t0, exit + # in the first loop, $t1 is at the last actual character, + # so we'll go backwards to get to the beginning + lb $a0, 0($t1) + li $v0, SYS_PRINT_CHAR + syscall + addi $t0, $t0, -1 + addi $t1, $t1, -1 + j strrev + +exit: + li $v0, SYS_EXIT + syscall diff --git a/mips-architecture/lab5_ex3.asm b/mips-architecture/lab5_ex3.asm @@ -0,0 +1,25 @@ +.eqv SYS_EXIT 10 + +.data + size: .byte 10 + arr: .byte 1, 15, 0, -3, 99, 48, -17, -9, 20, 15 + +.text +.globl main + +main: + # init loop, sum counters and define iterations + li $t0, 0 + li $t1, 0 + lb $t2, size + +calcsum: + beq $t0, $t2, exit + lb $t3, arr($t0) + add $t1, $t1, $t3 + addi $t0, $t0, 1 + j calcsum + +exit: + li $v0, SYS_EXIT + syscall diff --git a/mips-architecture/lab5_ex4.asm b/mips-architecture/lab5_ex4.asm @@ -0,0 +1,36 @@ +.eqv SYS_EXIT 10 + +.data + val: .word 0xabcd + # 'val' is 2 bytes + first: .space 2 + second: .space 2 + third: .space 2 + fourth: .space 2 + +.text +.globl main + +main: + # init loop counter + li $t0, 0 + # 4 iterations because we'll store 'val' 4 times + li $t1, 4 + # this is our offset, we'll increment it by 2 + # in each iteration so that we store 0xabcd in + # the appropriate label + li $t2, 0 + lh $t3, val + +loop: + beq $t0, $t1, exit + # store it as half word so that it and doesn't + # there aren't any zeros between that and the next 'val' + sh $t3, first($t2) + addi $t0, $t0, 1 + addi $t2, $t2, 2 + j loop + +exit: + li $v0, SYS_EXIT + syscall diff --git a/mips-architecture/lab5_ex5.asm b/mips-architecture/lab5_ex5.asm @@ -0,0 +1,64 @@ +.eqv SYS_PRINT_WORD 1 +.eqv SYS_EXIT 10 +.eqv SYS_READ_CHAR 12 + +.macro isnotdigit(%ch, %lbl) + # the character must be 0x30 <= c <= 0x39 + # for it to be a digit, otherwise, stop + blt %ch, 0x30, %lbl + bgt %ch, 0x39, %lbl +.end_macro + +.data + # max word can be 10 digits long but à + # we add + 1 byte in case there is '\n' + str: .space 11 + +.text +.globl main + +main: + # init strlen/iterations counter + li $t1, 0 + +getdigit: + li $v0, SYS_READ_CHAR + syscall + # copy $v0 to $t0 + addu $t0, $v0, $zero + isnotdigit($t0, atoi) + # we don't read more than 10 digits + beq $t1, 10, atoi + # save the digit in memory so that we can + # retrieve it later + sb $t0, str($t1) + addi $t1, $t1, 1 + j getdigit + +atoi: + li $t0, 0 + # init sum + li $t3, 0 + +atoiloop: + lb $t2, str($t0) + # $t1 is the strlen of str so loop as + # long as $t0 < $t1 + beq $t0, $t1, exit + # in case we hit '\n' or '\0' + isnotdigit($t2, exit) + + # get decimal value + addi $t2, $t2, -0x30 + mul $t3, $t3, 10 + add $t3, $t3, $t2 + addi $t0, $t0, 1 + j atoiloop + +exit: + li $v0, SYS_PRINT_WORD + la $a0, 0($t3) + syscall + + li $v0, SYS_EXIT + syscall