CSC 543 - Multiprocessing & Concurrent Programming, Spring 2024, Assignment 1 on intro to thread safety.

Assignment 1 due by 11:59 PM on Thursday February 15 via make turnitin
Dataflow diagram added to the bottom of this page on February 3, 2024

I added an example doc on using control-\ to see stack traces for a hung Java process on 2/6.
 

cd $HOME                  # places you into your login directory
mkdir multip              # all of your csc543 projects go into this directory
cd  ./multip                 # makes multip your current working directory
cp  ~parson/multip/CSC543s24DataflowAssn1.problem.zip  CSC543s24DataflowAssn1.problem.zip
unzip  CSC543s24DataflowAssn1.problem.zip    # unzips your working copy of the project directory
cd  ./CSC543s24DataflowAssn1                            # your project working directory

Perform all test execution on mcgonagall to avoid any platform-dependent output differences.
Also, large input and output files for your code reside in my file system to avoid overloading yours.
Here are the files of interest in this project directory. The ones through TupleExchange.java require
modification. You may need to understand the others.

CSC543s24DataflowAssn1.java                # Pipeline builder and main. Do this first to see make test explode.
MakeUniform.java                                      # The three Make* classes makes a uniform, normal, & exponential                            
MakeNormal.java                                       # distribution respectively of pseudo-random numbers.  They require
MakeExponential.java                                # added field modifiers to make them thread-safe + some annotations.
TenTuple.java                                             # A simple, immutable data container.
FiveTuple.java                                            # Slightly more complicated immutable data container.
DistributionGenerator.java                          # 1st stage active class that invokes one of the Make* generators above.
StatisticalAnalysisGenerator.java               # 2nd stage active class that analyzes / reduces stage 1's output.
StatisticalAnalysisCSVSaver.java              # 3rd stage in pipeline writes entries to a CSV file for diffing.
TupleExchange.java                                  # Implicit & explicit locks to synchronize pipeline data handoff.
    A mutex (lock) with no condition variable and a mutex with a condition variable.
IMakeDistribution.java                                # Interface for the above Make* classes.
Stats.java                                                   # Basic statistics & CSV writing because Java default libraries are not great.
makefile & makelib                                     # make files for make clean test and make turnitin.
CSC223f23DataflowAssn4.py                    # Python from last semester is the reference design for this project.
CSC223f23DataflowAssn4.ref                   # Reference files ending in .ref are the expected output CSV files.
CSC543s24multi.ref
CSC543s24single.ref
jcip-annotations.jar                                     # Annotations for the textbook denote design intent.

$ jar tvf jcip-annotations.jar
     0 Fri Dec 04 18:31:48 EST 2009 META-INF/
   102 Fri Dec 04 18:31:46 EST 2009 META-INF/MANIFEST.MF
     0 Fri Dec 04 18:31:48 EST 2009 net/
     0 Fri Dec 04 18:31:48 EST 2009 net/jcip/
     0 Fri Dec 04 18:31:48 EST 2009 net/jcip/annotations/
   436 Fri Dec 04 18:31:48 EST 2009 net/jcip/annotations/GuardedBy.class
   430 Fri Dec 04 18:31:48 EST 2009 net/jcip/annotations/Immutable.class
   438 Fri Dec 04 18:31:48 EST 2009 net/jcip/annotations/NotThreadSafe.class
   432 Fri Dec 04 18:31:48 EST 2009 net/jcip/annotations/ThreadSafe.class

Source files requiring your work contain STUDENT comments in upper case.

$ grep -l STUDENT *.java
CSC543s24DataflowAssn1.java
DistributionGenerator.java
FiveTuple.java
IMakeDistribution.java
MakeExponential.java
MakeNormal.java
MakeUniform.java
StatisticalAnalysisCSVSaver.java
StatisticalAnalysisGenerator.java
Stats.java
TenTuple.java
TupleExchange.java

Stats.java does not require changes, but it has an example line of code useful in
improving FiveTuple.java's thread safety.

Use make clean test to compile & test your code changes.
Testing must occur on mcgonagall.
$ ssh YOURLOGIN@mcgonagall    # You must log in from acad.
You can edit on acad.


Use make turnitin and hit the Enter key when prompted by the project deadline.
Late penalty is 10% per day, and I cannot accept a late assignment after I
go over the solution. At that point it is worth 0%.

We will walk through the spring 2023 solution to their Assignment 2 as example code.
Their solution is under ~parson/multip/prisonerd2j2023.solution.zip and prisonerd2j2023/.
A note from last year's Assignment 1:

Some basic guidelines for making Assignment 1's classes thread safe:
1. Make private any data field that can be private.
2. Inert data containers like TenTuple can have public fields i.f.f. those fields
    are immutable, for example primitive data types, or references to immutable
    objects such as String or most (all?) java.lang.Number objects.
    Mutable objects such as arrays should be private so as not to escape their objects.
3. Fields should be final when possible. Making a reference to a mutable object final
    does not make it immutable, but it does guarantee flushing to main memory when the
    constructor returns. A mutable field should be either volatile, or have every access
    guarded by an implicit or explicit lock, or have a final reference to a thread-safe
    object from package java.util.concurrent.atomic. We will cover atomic later.
4. Use the textbook @Immutable, @ThreadSafe, @NotThreadSafe, and
    @GuardedBy annotations as required in STUDENT comment to communicate
    design intent.

Here is what make clean test looks like from the handout directory:

$ make clean test
/bin/rm -f *.o *.class .jar core *.exe *.obj *.pyc __pycache__/*.pyc
/bin/rm -f junk* *.pyc
/bin/rm -f *.tmp *.o *.dif *.out __pycache__/*
/bin/rm -f DEBUG*arff* tmp*
/bin/rm -f *.class *.csv
PYTHONPATH=/home/kutztown.edu/parson:.:..:/usr/local/lib/python3.7 time /usr/local/bin/python3.7 CSC223f23DataflowAssn4.py 543343458 CSC223f23DataflowAssn4.csv
4.15user 1.78system 0:02.83elapsed 209%CPU (0avgtext+0avgdata 25672maxresident)k
0inputs+16outputs (1major+15215minor)pagefaults 0swaps
diff --ignore-trailing-space --strip-trailing-cr CSC223f23DataflowAssn4.csv CSC223f23DataflowAssn4.ref > CSC223f23DataflowAssn4.dif
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeUniform.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeNormal.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  TupleExchange.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeExponential.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  CSC543s24DataflowAssn1.java"
bash -c "CLASSPATH=..:./jcip-annotations.jar time /usr/bin/java CSC543s24DataflowAssn1.CSC543s24DataflowAssn1 343458543 test1.csv 0"
2.08user 0.21system 0:01.11elapsed 206%CPU (0avgtext+0avgdata 300160maxresident)k
0inputs+96outputs (0major+9793minor)pagefaults 0swaps
head -1 test1.csv > CSC543s24single.csv
grep uniform test1.csv >> CSC543s24single.csv
grep normal test1.csv >> CSC543s24single.csv
grep exponential test1.csv >> CSC543s24single.csv
diff --ignore-trailing-space --strip-trailing-cr CSC543s24single.csv CSC543s24single.ref > CSC543s24single.dif
bash -c "CLASSPATH=..:./jcip-annotations.jar time /usr/bin/java CSC543s24DataflowAssn1.CSC543s24DataflowAssn1 343458543 test2.csv 1"
0.12user 0.04system 0:00.14elapsed 115%CPU (0avgtext+0avgdata 33972maxresident)k
0inputs+72outputs (0major+6104minor)pagefaults 0swaps
head -1 test2.csv > CSC543s24multi.csv
grep uniform test2.csv >> CSC543s24multi.csv
make: *** [testjava] Error 1

My handout single-main-thread test passes, but the multi-threaded solution won't pass
until you complete your work. A successful test does not guarantee that all requirements
are met because a race condition that allows tests to pass sometimes is possible.
Please re-read all STUDENT requirements before turning it in. When make test
does not report an error, testing has succeeded.

$ cat CSC543s24multi.csv
Distribution,Param1,Param2,Count,Mean,Median,Mode,Pstdev,Min,Max

The multi-threaded output is missing its data. That is only a header line.

Use make turnitin and hit the Enter key when prompted by the project deadline.
Late penalty is 10% per day, and I cannot accept a late assignment after I
go over the solution. At that point it is worth 0%.

Here is a successful test run:

$ make clean test
/bin/rm -f *.o *.class .jar core *.exe *.obj *.pyc __pycache__/*.pyc
/bin/rm -f junk* *.pyc
/bin/rm -f *.tmp *.o *.dif *.out __pycache__/*
/bin/rm -f DEBUG*arff* tmp*
/bin/rm -f *.class *.csv
PYTHONPATH=/home/kutztown.edu/parson:.:..:/usr/local/lib/python3.7 time /usr/local/bin/python3.7 CSC223f23DataflowAssn4.py 543343458 CSC223f23DataflowAssn4.csv
4.33user 1.82system 0:03.16elapsed 194%CPU (0avgtext+0avgdata 25648maxresident)k
0inputs+16outputs (1major+14870minor)pagefaults 0swaps
diff --ignore-trailing-space --strip-trailing-cr CSC223f23DataflowAssn4.csv CSC223f23DataflowAssn4.ref > CSC223f23DataflowAssn4.dif
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeUniform.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeNormal.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  TupleExchange.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  MakeExponential.java"
/bin/bash -c "CLASSPATH=..:./jcip-annotations.jar /usr/bin/javac  CSC543s24DataflowAssn1.java"
bash -c "CLASSPATH=..:./jcip-annotations.jar time /usr/bin/java CSC543s24DataflowAssn1.CSC543s24DataflowAssn1 343458543 test1.csv 0"
2.13user 0.19system 0:01.10elapsed 210%CPU (0avgtext+0avgdata 300208maxresident)k (single threaded)
0inputs+96outputs (0major+10107minor)pagefaults 0swaps
head -1 test1.csv > CSC543s24single.csv
grep uniform test1.csv >> CSC543s24single.csv
grep normal test1.csv >> CSC543s24single.csv
grep exponential test1.csv >> CSC543s24single.csv
diff --ignore-trailing-space --strip-trailing-cr CSC543s24single.csv CSC543s24single.ref > CSC543s24single.dif
bash -c "CLASSPATH=..:./jcip-annotations.jar time /usr/bin/java CSC543s24DataflowAssn1.CSC543s24DataflowAssn1 343458543 test2.csv 1"
2.25user 0.22system 0:00.91elapsed 271%CPU (0avgtext+0avgdata 302388maxresident)k (multi-threaded)
0inputs+104outputs (0major+9917minor)pagefaults 0swaps
head -1 test2.csv > CSC543s24multi.csv
grep uniform test2.csv >> CSC543s24multi.csv
grep normal test2.csv >> CSC543s24multi.csv
grep exponential test2.csv >> CSC543s24multi.csv
diff --ignore-trailing-space --strip-trailing-cr CSC543s24multi.csv CSC543s24multi.ref > CSC543s24multi.dif

When that last diff does not report an error, it is working.
Multi-threading does not always accelerate this test run. We will work with acceleration later.
For now we are working on the basics of thread safety and using locks.

Dataflow Diagram for this Assignment 1:

CSC523f23DataflowAssn1DF.png