commit 38620e1c79ee698e1c9dbf7fd72028db3bf372fb
parent 46fa27a69c174bfb2dfd026bc8fa859a7b1d5a3e
Author: Christos Margiolis <christos@margiolis.net>
Date: Mon, 28 Dec 2020 01:30:28 +0200
minor progress
Diffstat:
9 files changed, 231 insertions(+), 40 deletions(-)
diff --git a/c-parallel-computing/ex2/data b/c-parallel-computing/ex2/data
@@ -0,0 +1,5 @@
+4
+1
+2
+3
+4
diff --git a/c-parallel-computing/ex2/ex2.c b/c-parallel-computing/ex2/ex2.c
@@ -2,11 +2,28 @@
#include <stdlib.h>
#include <mpi.h>
+/* constants */
+#define FIND_XMIN 1 << 0
+#define FIND_XMAX 1 << 1
+#define COUNT_BELOW_AVG 1 << 0
+#define COUNT_ABOVE_AVG 1 << 1
+
+/* function declarations */
static float input(const char *, int);
static void readvec(float *, int);
-static float calcavg(float *, int);
+static float find(int);
+static float findavg(float *, int);
+static float calcavg(void);
+static int count(float, int);
+static float calcvar(float);
static void *emalloc(size_t);
+/* global variables */
+static int rank, nproc, root = 0;
+static int n, localn;
+static float *vec, *localvec;
+
+/* function implementations */
static float
input(const char *fmt, int i)
{
@@ -29,20 +46,36 @@ readvec(float *vec, int n)
vec[i] = input("vec[%d]: ", i);
}
-static void *
-emalloc(size_t nb)
+static float
+find(int flag)
{
- void *p;
+ float localres = *localvec;
+ float finalres = localres;
+ float *res;
+ int i;
- if ((p = malloc(nb)) == NULL) {
- fputs("ex2: cannot allocate memory", stderr);
- exit(EXIT_FAILURE);
- }
- return p;
+ for (i = 0; i < localn; i++)
+ if ((flag & FIND_XMIN && localvec[i] < localres)
+ || (flag & FIND_XMAX && localvec[i] > localres))
+ localres = localvec[i];
+
+ res = emalloc(nproc * sizeof(float));
+ MPI_Gather(&localres, 1, MPI_FLOAT, res, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+
+ if (rank == root)
+ for (i = 0; i < nproc; i++)
+ if ((flag & FIND_XMIN && res[i] < finalres)
+ || (flag & FIND_XMAX && res[i] > finalres))
+ finalres = res[i];
+
+ MPI_Bcast(&finalres, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+ free(res);
+
+ return finalres;
}
static float
-calcavg(float *vec, int n)
+findavg(float *vec, int n)
{
float sum = 0.0f;
int i = 0;
@@ -52,17 +85,101 @@ calcavg(float *vec, int n)
return (sum / (float)n);
}
-int
-main(int argc, char *argv[])
+static float
+calcavg(void)
{
- int rank, nproc, root = 0;
- int n, localn;
- float *vec, *localvec;
- float localsum, sum;
float *avgs, localavg, finalavg;
- float var;
+
+ avgs = emalloc(nproc * sizeof(float));
+ localavg = findavg(localvec, localn);
+ MPI_Gather(&localavg, 1, MPI_FLOAT, avgs, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+
+ if (rank == root)
+ finalavg = findavg(avgs, nproc);
+ MPI_Bcast(&finalavg, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+ free(avgs);
+
+ return finalavg;
+}
+
+static int
+count(float avg, int flag)
+{
+ int *res, localres = 0, finalres = 0, i;
+
+ for (i = 0; i < localn; i++)
+ if ((flag & COUNT_BELOW_AVG && localvec[i] < avg)
+ || (flag & COUNT_ABOVE_AVG && localvec[i] > avg))
+ localres++;
+
+ res = emalloc(nproc * sizeof(int));
+ MPI_Gather(&localres, 1, MPI_INT, res, 1, MPI_INT, root, MPI_COMM_WORLD);
+
+ if (rank == root)
+ for (i = 0; i < nproc; i++)
+ finalres += res[i];
+ MPI_Bcast(&finalres, 1, MPI_INT, root, MPI_COMM_WORLD);
+ free(res);
+
+ return finalres;
+}
+
+static float
+calcvar(float avg)
+{
+ float *vars, localvar = 0.0f, finalvar = 0.0f;
+ int i;
+
+ for (i = 0; i < localn; i++)
+ localvar += (localvec[i] - avg) * (localvec[i] - avg);
+
+ vars = emalloc(nproc * sizeof(float));
+ MPI_Gather(&localvar, 1, MPI_FLOAT, vars, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+
+ if (rank == root) {
+ for (i = 0; i < nproc; i++)
+ finalvar += vars[i];
+ finalvar /= (float)n - 1;
+ }
+ MPI_Bcast(&finalvar, 1, MPI_FLOAT, root, MPI_COMM_WORLD);
+ free(vars);
+
+ return finalvar;
+}
+
+static float *
+calcd(float xmin, float xmax)
+{
+ float *locald, *finald = NULL;
int i;
+ locald = emalloc(localn * sizeof(float));
+ for (i = 0; i < localn; i++)
+ locald[i] = ((localvec[i] - xmin) / (xmax - xmin)) * 100;
+
+ free(locald);
+
+ return finald;
+}
+
+static void *
+emalloc(size_t nb)
+{
+ void *p;
+
+ if ((p = malloc(nb)) == NULL) {
+ fputs("cannot allocate memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+int
+main(int argc, char *argv[])
+{
+ float *d, avg, var, xmin, xmax;
+ int belowavg, aboveavg;
+
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
@@ -73,31 +190,51 @@ main(int argc, char *argv[])
readvec(vec, n);
}
- /* part 0 - calc avg */
+ /* move? */
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);
+ /* part 0.1 - calculate min and max */
+ xmin = find(FIND_XMIN);
+ xmax = find(FIND_XMAX);
- if (rank == root) {
- finalavg = calcavg(avgs, nproc);
- }
+ /* part 0.2 - calculate average */
+ avg = calcavg();
+
+ /* part 1 - find how many elements are above or below average */
+ belowavg = count(avg, COUNT_BELOW_AVG);
+ aboveavg = count(avg, COUNT_ABOVE_AVG);
- /*MPI_Bcast(&finalavg, 1, MPI_FLOAT, root, MPI_COMM_WORLD);*/
- /*MPI_Scatter(vec, localn, MPI_FLOAT, localvec, localn, MPI_FLOAT, root, MPI_COMM_WORLD);*/
+ /* part 2 - calculate variance */
+ var = calcvar(avg);
+ /*
+ * part 3 - make a new vector where each element is:
+ * ((xi - xmin) / (xmax - xmin)) * 100
+ */
+ d = calcd(xmin, xmax);
+
+ /* print all results */
if (rank == root) {
- printf("avg: %.2f\n", finalavg);
+ printf("\n");
+ printf("Average: %.4f\n", avg);
+ printf("Xmin: %.4f\n", xmin);
+ printf("Xmax: %.4f\n", xmax);
+ printf("Below Average: %d\n", belowavg);
+ printf("Above Average: %d\n", aboveavg);
+ printf("Variance: %.4f\n", var);
+
+ /* TODO: cleanup, very ugly */
+ printf("Vector D: [");
+ for (int i = 0; i < n; i++)
+ printf("%.4f%s", var, i != n-1 ? ", " : "");
+ printf("]\n");
}
- /* move up? */
free(vec);
free(localvec);
- free(avgs);
MPI_Finalize();
diff --git a/mips-architecture/lab6_ex2.asm b/mips-architecture/lab6_ex2.asm
@@ -16,7 +16,7 @@ main:
syscall
li $v0, SYS_READ_WORD
- syscall
+ syscall
# get 2 lowest bits
andi $t0, $v0, 3
diff --git a/mips-architecture/lab6_ex5.asm b/mips-architecture/lab6_ex5.asm
@@ -46,7 +46,7 @@ main:
# 00110010 & 0x0f = 00000010 = 2
andi $t1, $t0, 0x0f
- # Grint second portion (4 bits)
+ # Print second portion (4 bits)
lb $a0, hex($t1)
syscall
diff --git a/sh-os1/ex2/bck b/sh-os1/ex2/bck
@@ -18,8 +18,7 @@ main() {
test ${dst##*.} = "tar" && tar -rf ${dst} ${src}
}
-usage() {
- echo "usage: ${0##*/} username src dst" 1>&2
+usage() { echo "usage: ${0##*/} username src dst" 1>&2
exit 1
}
diff --git a/sh-os1/ex2/mfproc b/sh-os1/ex2/mfproc
@@ -2,13 +2,14 @@
main() {
while getopts u:s: arg; do
- case "${arg}" in
- u) test ! -z "$(grep -w "^${OPTARG}" /etc/passwd)" ||
- err "'${OPTARG}' is not in /etc/passwd"
- uid=$(id -u ${OPTARG}) ;;
- s) state="${OPTARG}" ;;
- *) usage
- esac
+ case "${arg}" in
+ u) test ! -z "$(grep -w "^${OPTARG}" /etc/passwd)" ||
+ err "'${OPTARG}' is not in /etc/passwd"
+ uid=$(id -u ${OPTARG}) ;;
+ # TODO: error check
+ s) state="${OPTARG}" ;;
+ *) usage
+ esac
done
printf "Name\tPID\tPPID\tUID\tGID\tState\n" | expand -t 16
diff --git a/sh-os1/ex3/avgsort.awk b/sh-os1/ex3/avgsort.awk
@@ -0,0 +1,10 @@
+#!/usr/bin/env -S awk -f
+
+{
+ sum = cnt = 0;
+ for (i = 4; i <= NF; i++) {
+ sum += $i;
+ cnt++;
+ }
+ print "Student", NR",", $1, $2",", sum / cnt
+}
diff --git a/sh-os1/ex3/cmds b/sh-os1/ex3/cmds
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+# TODO: make error checks: grades: 0 - 100
+
+echo "First names starting with K or N:"
+awk '$1 ~ /^K|N/ {print $2, $1}' grades
+echo ""
+
+echo "Last names starting with C or L:"
+awk '$2 ~ /^C|L/ {print $1, $2}' grades
+echo ""
+
+echo "Students with their second grade being > 85:"
+awk '{if ($5 > 85 && $5 > 85) print}' grades
+echo ""
+
+echo "Students in the humanities:"
+awk '{if ($3 == "Theo") print}' grades
+echo ""
+
+echo "Average grades:"
+awk '{
+sum = cnt = 0;
+for (i = 4; i <= NF; i++) {
+ sum += $i;
+ cnt++;
+}
+print $1, $2, sum / cnt}' grades
+echo ""
+
+echo "Students with their third grade being < 70:"
+awk 'BEGIN {cnt = 0} {if ($6 < 70) cnt++;} END {print cnt}' grades
+echo ""
+
+echo "How many student names end in 'is'?"
+awk 'BEGIN {cnt = 0} {if ($2 ~ /is$/) cnt++} END {print cnt}' grades
diff --git a/sh-os1/ex3/grades b/sh-os1/ex3/grades
@@ -0,0 +1,3 @@
+Thomas Georgiou Texn 96 89 81 84
+Mary Makridi Thet 67 75 56 74
+Nick Aliferis Theo 81 87 75 94