From c14ac9ffc62e77af3c40d8f7a030c81fd357c10b Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Thu, 5 Aug 2021 18:08:55 -0400 Subject: [PATCH 01/41] OpenMP examples using Pyjama in Java: 00 and 01 --- Patternlets/pyjama/00.forkJoin/ForkJoin.java | 28 +++++++++++++ Patternlets/pyjama/00.forkJoin/Makefile | 16 ++++++++ .../pyjama/01.forkJoin2/ForkJoin2.java | 41 +++++++++++++++++++ Patternlets/pyjama/01.forkJoin2/Makefile | 16 ++++++++ 4 files changed, 101 insertions(+) create mode 100644 Patternlets/pyjama/00.forkJoin/ForkJoin.java create mode 100755 Patternlets/pyjama/00.forkJoin/Makefile create mode 100644 Patternlets/pyjama/01.forkJoin2/ForkJoin2.java create mode 100755 Patternlets/pyjama/01.forkJoin2/Makefile diff --git a/Patternlets/pyjama/00.forkJoin/ForkJoin.java b/Patternlets/pyjama/00.forkJoin/ForkJoin.java new file mode 100644 index 0000000..5bda427 --- /dev/null +++ b/Patternlets/pyjama/00.forkJoin/ForkJoin.java @@ -0,0 +1,28 @@ +/* ForkJoin.java + * ... illustrates the fork-join pattern + * using OpenMP's parallel directive. + * + * Joel Adams, Calvin College, November 2009. + * Adapted for Java by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run + * + * Exercise: + * - Compile & run, uncomment the pragma, + * recompile & run, compare results. + */ + +class ForkJoin { + + public static void main(String[] args) { + + System.out.println("\nBefore..."); + + //#omp parallel num_threads(4) + System.out.println("\nDuring..."); + + System.out.println("\nAfter...\n"); + } + +} \ No newline at end of file diff --git a/Patternlets/pyjama/00.forkJoin/Makefile b/Patternlets/pyjama/00.forkJoin/Makefile new file mode 100755 index 0000000..297857f --- /dev/null +++ b/Patternlets/pyjama/00.forkJoin/Makefile @@ -0,0 +1,16 @@ +PROG = ForkJoin +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java b/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java new file mode 100644 index 0000000..83601c1 --- /dev/null +++ b/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java @@ -0,0 +1,41 @@ +/* ForkJoin2.java + * ... illustrates the fork-join pattern + * using multiple OpenMP parallel directives, + * and changing the number of threads two ways. + * + * Joel Adams, Calvin College, May 2013. + * Adapted for Java by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run + * + * Exercise: + * - Compile & run, compare results to source. + * - Predict how many threads will be used in 'Part IV'? + * - Uncomment 'Part IV', recompile, rerun. + */ + +class ForkJoin2 { + public static void main(String[] args) { + + System.out.print("\nBeginning\n"); + + //#omp parallel + System.out.print("\nPart I"); + + System.out.print("\n\nBetween I and II...\n"); + + Pyjama.omp_set_num_threads(3); + + //#omp parallel + System.out.print("\nPart II..."); + + System.out.print("\n\nBetween II and III...\n"); + + //#omp parallel num_threads(5) + System.out.print("\nPart III..."); + + System.out.print("\n\nEnd\n\n"); + } +} + diff --git a/Patternlets/pyjama/01.forkJoin2/Makefile b/Patternlets/pyjama/01.forkJoin2/Makefile new file mode 100755 index 0000000..38cba30 --- /dev/null +++ b/Patternlets/pyjama/01.forkJoin2/Makefile @@ -0,0 +1,16 @@ +PROG = ForkJoin2 +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(CLASS) *~ *# + From 98b2cc46e73d47653f94f4be8daa15042fdd7beb Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Fri, 6 Aug 2021 23:21:17 -0400 Subject: [PATCH 02/41] More patternlets --- Patternlets/pyjama/01.forkJoin2/Makefile | 2 +- Patternlets/pyjama/02.spmd/Makefile | 16 +++++++++++ Patternlets/pyjama/02.spmd/Spmd.java | 27 ++++++++++++++++++ Patternlets/pyjama/03.spmd2/Makefile | 16 +++++++++++ Patternlets/pyjama/03.spmd2/Spmd2.java | 36 ++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 Patternlets/pyjama/02.spmd/Makefile create mode 100644 Patternlets/pyjama/02.spmd/Spmd.java create mode 100755 Patternlets/pyjama/03.spmd2/Makefile create mode 100644 Patternlets/pyjama/03.spmd2/Spmd2.java diff --git a/Patternlets/pyjama/01.forkJoin2/Makefile b/Patternlets/pyjama/01.forkJoin2/Makefile index 38cba30..42110bc 100755 --- a/Patternlets/pyjama/01.forkJoin2/Makefile +++ b/Patternlets/pyjama/01.forkJoin2/Makefile @@ -12,5 +12,5 @@ $(CLASS): $(SRC) $(CC) $(SRC) clean: - rm -f $(CLASS) *~ *# + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/02.spmd/Makefile b/Patternlets/pyjama/02.spmd/Makefile new file mode 100755 index 0000000..c036a85 --- /dev/null +++ b/Patternlets/pyjama/02.spmd/Makefile @@ -0,0 +1,16 @@ +PROG = Spmd +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/02.spmd/Spmd.java b/Patternlets/pyjama/02.spmd/Spmd.java new file mode 100644 index 0000000..c2a4b01 --- /dev/null +++ b/Patternlets/pyjama/02.spmd/Spmd.java @@ -0,0 +1,27 @@ +/* Spmd.java + * ... illustrates the single-program-multiple-data (SPMD) + * pattern using two basic OpenMP commands... + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./spmd + * + * Exercise: + * - Compile & run + * - Uncomment pragma, recompile & run, compare results + */ + +class Spmd { + public static void main(String[] args) { + System.out.println(); + + //#omp parallel + { + int id = Pyjama.omp_get_thread_num(); + int numThreads = Pyjama.omp_get_num_threads(); + System.out.println("Hello from thread " + id + " of " + numThreads); + } + + System.out.println(); + } +} diff --git a/Patternlets/pyjama/03.spmd2/Makefile b/Patternlets/pyjama/03.spmd2/Makefile new file mode 100755 index 0000000..a0dbedd --- /dev/null +++ b/Patternlets/pyjama/03.spmd2/Makefile @@ -0,0 +1,16 @@ +PROG = Spmd2 +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/03.spmd2/Spmd2.java b/Patternlets/pyjama/03.spmd2/Spmd2.java new file mode 100644 index 0000000..6966f35 --- /dev/null +++ b/Patternlets/pyjama/03.spmd2/Spmd2.java @@ -0,0 +1,36 @@ +/* Spmd2.java + * ... illustrates the SPMD pattern in OpenMP, + * using the commandline arguments + * to control the number of threads. + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./spmd2 [numThreads] + * + * Exercise: + * - Compile & run with no commandline args + * - Rerun with different commandline args, + * until you see a problem with thread ids + * - Fix the race condition + * (if necessary, compare to 02.spmd) + */ + +class Spmd2 { + static int id, numThreads; + + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); + } + System.out.println(); + + //#omp parallel + { + id = Pyjama.omp_get_thread_num(); + numThreads = Pyjama.omp_get_num_threads(); + System.out.println("Hello from thread "+ id +" of " + numThreads); + } + + System.out.println(); + } +} From 107c36ee5e48666b96798d44719148ed8b2e4bfb Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sat, 7 Aug 2021 12:59:51 -0400 Subject: [PATCH 03/41] Added Barrier and MasterWorker. Added a Makefile to pull Pyjama lib. --- Patternlets/pyjama/00.forkJoin/Makefile | 0 Patternlets/pyjama/01.forkJoin2/Makefile | 0 Patternlets/pyjama/02.spmd/Makefile | 0 Patternlets/pyjama/03.spmd2/Makefile | 0 Patternlets/pyjama/04.barrier/Barrier.java | 35 ++++++++++++++++++ Patternlets/pyjama/04.barrier/Makefile | 16 ++++++++ Patternlets/pyjama/05.masterWorker/Makefile | 16 ++++++++ .../pyjama/05.masterWorker/MasterWorker.java | 37 +++++++++++++++++++ Patternlets/pyjama/lib/Makefile | 14 +++++++ 9 files changed, 118 insertions(+) mode change 100755 => 100644 Patternlets/pyjama/00.forkJoin/Makefile mode change 100755 => 100644 Patternlets/pyjama/01.forkJoin2/Makefile mode change 100755 => 100644 Patternlets/pyjama/02.spmd/Makefile mode change 100755 => 100644 Patternlets/pyjama/03.spmd2/Makefile create mode 100644 Patternlets/pyjama/04.barrier/Barrier.java create mode 100644 Patternlets/pyjama/04.barrier/Makefile create mode 100644 Patternlets/pyjama/05.masterWorker/Makefile create mode 100644 Patternlets/pyjama/05.masterWorker/MasterWorker.java create mode 100644 Patternlets/pyjama/lib/Makefile diff --git a/Patternlets/pyjama/00.forkJoin/Makefile b/Patternlets/pyjama/00.forkJoin/Makefile old mode 100755 new mode 100644 diff --git a/Patternlets/pyjama/01.forkJoin2/Makefile b/Patternlets/pyjama/01.forkJoin2/Makefile old mode 100755 new mode 100644 diff --git a/Patternlets/pyjama/02.spmd/Makefile b/Patternlets/pyjama/02.spmd/Makefile old mode 100755 new mode 100644 diff --git a/Patternlets/pyjama/03.spmd2/Makefile b/Patternlets/pyjama/03.spmd2/Makefile old mode 100755 new mode 100644 diff --git a/Patternlets/pyjama/04.barrier/Barrier.java b/Patternlets/pyjama/04.barrier/Barrier.java new file mode 100644 index 0000000..325c96c --- /dev/null +++ b/Patternlets/pyjama/04.barrier/Barrier.java @@ -0,0 +1,35 @@ +/* barrier.c + * ... illustrates the use of the OpenMP barrier command, + * using the commandline to control the number of threads... + * + * Joel Adams, Calvin College, May 2013. + * + * Usage: ./barrier [numThreads] + * + * Exercise: + * - Compile & run several times, noting interleaving of outputs. + * - Remove the barrier directive, recompile, rerun, + * and note the change in the outputs. + */ +class Barrier { + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads( Integer.parseInt(args[0]) ); + } + + System.out.println(); + //#omp parallel + { + int id = Pyjama.omp_get_thread_num(); + int numThreads = Pyjama.omp_get_num_threads(); + System.out.println("Thread "+id + " of "+numThreads+" is BEFORE the barrier."); + + //Try this with and without the barrier + //#omp barrier + + System.out.println("Thread "+id + " of "+numThreads+" is AFTER the barrier."); + } + + System.out.println(); + } +} diff --git a/Patternlets/pyjama/04.barrier/Makefile b/Patternlets/pyjama/04.barrier/Makefile new file mode 100644 index 0000000..beef171 --- /dev/null +++ b/Patternlets/pyjama/04.barrier/Makefile @@ -0,0 +1,16 @@ +PROG = Barrier +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/05.masterWorker/Makefile b/Patternlets/pyjama/05.masterWorker/Makefile new file mode 100644 index 0000000..949dbc0 --- /dev/null +++ b/Patternlets/pyjama/05.masterWorker/Makefile @@ -0,0 +1,16 @@ +PROG = MasterWorker +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/05.masterWorker/MasterWorker.java b/Patternlets/pyjama/05.masterWorker/MasterWorker.java new file mode 100644 index 0000000..59a83f8 --- /dev/null +++ b/Patternlets/pyjama/05.masterWorker/MasterWorker.java @@ -0,0 +1,37 @@ +/* masterWorker.c + * ... illustrates the master-worker pattern in OpenMP + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: make run + * + * Exercise: + * - Compile and run as is. + * - Uncomment #pragma directive, re-compile and re-run + * - Compare and trace the different executions. + */ + +class MasterWorker { + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); + } + System.out.println(); + + + //#omp parallel + { + int id = Pyjama.omp_get_thread_num(); + int numThreads = Pyjama.omp_get_num_threads(); + + if ( id == 0 ) { // thread with ID 0 is master + System.out.println("Greetings from the master, #"+ id +" of " + numThreads + " threads"); + } else { // threads with IDs > 0 are workers + System.out.println("Greetings from a worker, #"+ id +" of " + numThreads + " threads"); + } + } + + System.out.println(); + } +} + diff --git a/Patternlets/pyjama/lib/Makefile b/Patternlets/pyjama/lib/Makefile new file mode 100644 index 0000000..ecfc694 --- /dev/null +++ b/Patternlets/pyjama/lib/Makefile @@ -0,0 +1,14 @@ +URL = https://www.csc.tntech.edu/pdcincs/resources/modules/tools/updated/Pyjama.zip +PJ = Pyjama +JAR = $(PJ).jar + +$(PJ): + wget $(URL) + unzip -o $(PJ).zip + cp $(PJ)/*.jar $(JAR) + rm -r -f Pyjama.zip Pyjama + +clean: + rm $(JAR) + + From c92990868b17416d4d3ee600bd31f59ba9841620 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 8 Aug 2021 19:52:29 -0400 Subject: [PATCH 04/41] Added ParallelLoopEqualChunks --- Patternlets/pyjama/00.forkJoin/ForkJoin.java | 2 +- .../pyjama/01.forkJoin2/ForkJoin2.java | 2 +- Patternlets/pyjama/02.spmd/Spmd.java | 4 ++- Patternlets/pyjama/03.spmd2/Makefile | 2 +- Patternlets/pyjama/03.spmd2/Spmd2.java | 4 ++- Patternlets/pyjama/04.barrier/Barrier.java | 7 ++-- Patternlets/pyjama/04.barrier/Makefile | 4 +-- Patternlets/pyjama/05.masterWorker/Makefile | 2 +- .../pyjama/05.masterWorker/MasterWorker.java | 8 +++-- .../06.parallelLoop-equalChunks/Makefile | 16 +++++++++ .../ParallelLoopEqualChunks.java | 35 +++++++++++++++++++ 11 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile create mode 100644 Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java diff --git a/Patternlets/pyjama/00.forkJoin/ForkJoin.java b/Patternlets/pyjama/00.forkJoin/ForkJoin.java index 5bda427..985a9aa 100644 --- a/Patternlets/pyjama/00.forkJoin/ForkJoin.java +++ b/Patternlets/pyjama/00.forkJoin/ForkJoin.java @@ -3,7 +3,7 @@ * using OpenMP's parallel directive. * * Joel Adams, Calvin College, November 2009. - * Adapted for Java by Ruth Kurniawati, Westfield State University, July, 2021 + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 * * Usage: * make run diff --git a/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java b/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java index 83601c1..8e9eccb 100644 --- a/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java +++ b/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java @@ -4,7 +4,7 @@ * and changing the number of threads two ways. * * Joel Adams, Calvin College, May 2013. - * Adapted for Java by Ruth Kurniawati, Westfield State University, July, 2021 + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 * * Usage: * make run diff --git a/Patternlets/pyjama/02.spmd/Spmd.java b/Patternlets/pyjama/02.spmd/Spmd.java index c2a4b01..560e5aa 100644 --- a/Patternlets/pyjama/02.spmd/Spmd.java +++ b/Patternlets/pyjama/02.spmd/Spmd.java @@ -3,8 +3,10 @@ * pattern using two basic OpenMP commands... * * Joel Adams, Calvin College, November 2009. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 * - * Usage: ./spmd + * Usage: + * make run * * Exercise: * - Compile & run diff --git a/Patternlets/pyjama/03.spmd2/Makefile b/Patternlets/pyjama/03.spmd2/Makefile index a0dbedd..d31991f 100644 --- a/Patternlets/pyjama/03.spmd2/Makefile +++ b/Patternlets/pyjama/03.spmd2/Makefile @@ -6,7 +6,7 @@ CC = java -jar $(PJ) VM = java -cp $(PJ):. run: $(CLASS) - $(VM) $(PROG) + $(VM) $(PROG) $(ARGS) $(CLASS): $(SRC) $(CC) $(SRC) diff --git a/Patternlets/pyjama/03.spmd2/Spmd2.java b/Patternlets/pyjama/03.spmd2/Spmd2.java index 6966f35..8d9332e 100644 --- a/Patternlets/pyjama/03.spmd2/Spmd2.java +++ b/Patternlets/pyjama/03.spmd2/Spmd2.java @@ -5,7 +5,9 @@ * * Joel Adams, Calvin College, November 2009. * - * Usage: ./spmd2 [numThreads] + * Usage: + * make run ARGS=[numThreads] + * Example: make run ARGS=4 * * Exercise: * - Compile & run with no commandline args diff --git a/Patternlets/pyjama/04.barrier/Barrier.java b/Patternlets/pyjama/04.barrier/Barrier.java index 325c96c..67349f9 100644 --- a/Patternlets/pyjama/04.barrier/Barrier.java +++ b/Patternlets/pyjama/04.barrier/Barrier.java @@ -1,10 +1,13 @@ -/* barrier.c +/* Barrier.java * ... illustrates the use of the OpenMP barrier command, * using the commandline to control the number of threads... * * Joel Adams, Calvin College, May 2013. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 * - * Usage: ./barrier [numThreads] + * Usage: + * make run ARGS=[numThreads] + * Example: make run ARGS=8 * * Exercise: * - Compile & run several times, noting interleaving of outputs. diff --git a/Patternlets/pyjama/04.barrier/Makefile b/Patternlets/pyjama/04.barrier/Makefile index beef171..481913b 100644 --- a/Patternlets/pyjama/04.barrier/Makefile +++ b/Patternlets/pyjama/04.barrier/Makefile @@ -6,10 +6,10 @@ CC = java -jar $(PJ) VM = java -cp $(PJ):. run: $(CLASS) - $(VM) $(PROG) + $(VM) $(PROG) $(ARGS) $(CLASS): $(SRC) - $(CC) $(SRC) + $(CC) $(SRC) clean: rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/05.masterWorker/Makefile b/Patternlets/pyjama/05.masterWorker/Makefile index 949dbc0..d8c85a9 100644 --- a/Patternlets/pyjama/05.masterWorker/Makefile +++ b/Patternlets/pyjama/05.masterWorker/Makefile @@ -6,7 +6,7 @@ CC = java -jar $(PJ) VM = java -cp $(PJ):. run: $(CLASS) - $(VM) $(PROG) + $(VM) $(PROG) $(ARGS) $(CLASS): $(SRC) $(CC) $(SRC) diff --git a/Patternlets/pyjama/05.masterWorker/MasterWorker.java b/Patternlets/pyjama/05.masterWorker/MasterWorker.java index 59a83f8..310911a 100644 --- a/Patternlets/pyjama/05.masterWorker/MasterWorker.java +++ b/Patternlets/pyjama/05.masterWorker/MasterWorker.java @@ -1,13 +1,15 @@ -/* masterWorker.c +/* MasterWorker.java * ... illustrates the master-worker pattern in OpenMP * * Joel Adams, Calvin College, November 2009. * - * Usage: make run + * Usage: + * make run [numThreads] + * Example: make run 4 * * Exercise: * - Compile and run as is. - * - Uncomment #pragma directive, re-compile and re-run + * - Remove the #omp directive, re-compile and re-run * - Compare and trace the different executions. */ diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile b/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile new file mode 100644 index 0000000..de758cf --- /dev/null +++ b/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile @@ -0,0 +1,16 @@ +PROG = ParallelLoopEqualChunks +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java new file mode 100644 index 0000000..062aefc --- /dev/null +++ b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java @@ -0,0 +1,35 @@ +/* ParallelLoopEqualChunks.java + * ... illustrates the use of OpenMP's default parallel for loop in which + * threads iterate through equal sized chunks of the index range + * (cache-beneficial when accessing adjacent memory locations). + * + * Joel Adams, Calvin College, November 2009. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run [numThreads] + * Example: make run 4 + * + * Exercise + * - Compile and run, comparing output to source code + * - try with different numbers of threads, e.g.: 2, 3, 4, 6, 8 + */ + +class ParallelLoopEqualChunks { + final static int REPS = 16; + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); + } + System.out.println(); + + //#omp parallel for + for (int i = 0; i < REPS; i++) { + int id = Pyjama.omp_get_thread_num(); + System.out.println("Thread "+id+" performed iteration "+i); + } + + System.out.println(); + } +} + From 01a9e86112fcbec854f3b8d58078ef2360e49559 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 8 Aug 2021 21:52:11 -0400 Subject: [PATCH 05/41] Added ParallelLoopChunksOf1 --- .../pyjama/07.parallelLoop-chunksOf1/Makefile | 15 ++++++ .../ParallelLoopChunksOf1.java | 49 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile create mode 100644 Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java diff --git a/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile b/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile new file mode 100644 index 0000000..1c64fed --- /dev/null +++ b/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile @@ -0,0 +1,15 @@ +PROG = ParallelLoopChunksOf1 +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java b/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java new file mode 100644 index 0000000..ec33f24 --- /dev/null +++ b/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java @@ -0,0 +1,49 @@ +/* ParallelLoopChunksOf1.java + * ... illustrates how to make OpenMP map threads to + * parallel loop iterations in chunks of size 1 + * (use when not accesssing memory). + * + * Joel Adams, Calvin College, November 2009. + * + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run [numThreads] + * Example: make run 4 + * + * Exercise: + * 1. Compile and run, comparing output to source code, + * and to the output of the 'equal chunks' version. + * 2. Uncomment the "commented out" code below, + * and verify that both loops produce the same output. + * The first loop is simpler but more restrictive; + * the second loop is more complex but less restrictive. + */ + +class ParallelLoopChunksOf1 { + final static int REPS = 16; + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); + } + System.out.println(); + + //#omp parallel for schedule(static,1) + for (int i = 0; i < REPS; i++) { + int id = Pyjama.omp_get_thread_num(); + System.out.println("Thread "+id+" performed iteration "+i); + } + + /* + System.out.println("--\n\n"); + + //#omp parallel for + for (int i = 0; i < REPS; i++) { + int id = Pyjama.omp_get_thread_num(); + System.out.println("Thread "+id+" performed iteration "+i); + } + */ + System.out.println(); + } +} + From 194d287ece7c3019d231c91b81836ae00e01e2b5 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Mon, 9 Aug 2021 23:58:04 -0400 Subject: [PATCH 06/41] Added reduction example. Use shared clause instead of static for Spmd2, added code to add random delay. Clean up ParallelLoopEqualChunks.java --- Patternlets/pyjama/03.spmd2/Spmd2.java | 18 +++-- .../ParallelLoopEqualChunks.java | 2 +- Patternlets/pyjama/08.reduction/Makefile | 15 ++++ .../pyjama/08.reduction/Reduction.java | 57 +++++++++++++++ Patternlets/pyjama/08.reduction/reduction.c | 70 +++++++++++++++++++ 5 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 Patternlets/pyjama/08.reduction/Makefile create mode 100644 Patternlets/pyjama/08.reduction/Reduction.java create mode 100755 Patternlets/pyjama/08.reduction/reduction.c diff --git a/Patternlets/pyjama/03.spmd2/Spmd2.java b/Patternlets/pyjama/03.spmd2/Spmd2.java index 8d9332e..cf50743 100644 --- a/Patternlets/pyjama/03.spmd2/Spmd2.java +++ b/Patternlets/pyjama/03.spmd2/Spmd2.java @@ -17,19 +17,29 @@ * (if necessary, compare to 02.spmd) */ -class Spmd2 { - static int id, numThreads; +import java.util.Random; +class Spmd2 { public static void main(String[] args) { if (args.length >= 1) { Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); } System.out.println(); - //#omp parallel + + int id, numThreads; + Random rand; + //#omp parallel shared(id, numThreads) private(rand) { - id = Pyjama.omp_get_thread_num(); + // To make it easier to observe the race condition, uncomment the code below: + // + rand = new Random(); // use a fixed seed for reproduceability + try { + Thread.sleep(rand.nextInt()); + } catch(Exception ex) {}; + numThreads = Pyjama.omp_get_num_threads(); + id = Pyjama.omp_get_thread_num(); System.out.println("Hello from thread "+ id +" of " + numThreads); } diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java index 062aefc..33e97cf 100644 --- a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java +++ b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java @@ -15,7 +15,7 @@ * - try with different numbers of threads, e.g.: 2, 3, 4, 6, 8 */ -class ParallelLoopEqualChunks { +public class ParallelLoopEqualChunks { final static int REPS = 16; public static void main(String[] args) { if (args.length >= 1) { diff --git a/Patternlets/pyjama/08.reduction/Makefile b/Patternlets/pyjama/08.reduction/Makefile new file mode 100644 index 0000000..48816c3 --- /dev/null +++ b/Patternlets/pyjama/08.reduction/Makefile @@ -0,0 +1,15 @@ +PROG = Reduction +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/08.reduction/Reduction.java b/Patternlets/pyjama/08.reduction/Reduction.java new file mode 100644 index 0000000..5870c83 --- /dev/null +++ b/Patternlets/pyjama/08.reduction/Reduction.java @@ -0,0 +1,57 @@ +/* Reduction.java + * ... illustrates the OpenMP parallel-for loop's reduction clause + * + * Joel Adams, Calvin College, November 2009. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run [numThreads] + * Example: make run 4 + * + * Exercise: + * - Compile and run. Note that incorrect output is produced by parallelSum() + * - Uncomment 'reduction(+:sum)' clause of #omp in parallelSum() + * - Recompile and rerun. Note that correct output is produced again. + */ + +import java.util.Random; + +class Reduction { + final static int SIZE=1000000; + + public static void main(String[] args) { + if (args.length >= 1) { + Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); + } + System.out.println(); + + // generate SIZE random values in [0..1000) range + int[] array = new Random().ints(SIZE, 0, 1000).toArray(); + System.out.println("Seq. sum: \t" + sequentialSum(array)); + System.out.println("Par. sum: \t" + parallelSum(array)); + } + + + /* sum the array sequentially */ + static int sequentialSum(int[] a) { + int sum = 0; + int i; + for (i = 0; i < a.length; i++) { + sum += a[i]; + } + return sum; + } + + /* sum the array using multiple threads */ + static int parallelSum(int[] a) { + int sum = 0; + //#omp parallel shared(a,sum) + { + //#omp for /* reduction(+:sum) */ + for(int i = 0; i < a.length;i++) { + sum += a[i]; + } + } + return sum; + } +} \ No newline at end of file diff --git a/Patternlets/pyjama/08.reduction/reduction.c b/Patternlets/pyjama/08.reduction/reduction.c new file mode 100755 index 0000000..036af0e --- /dev/null +++ b/Patternlets/pyjama/08.reduction/reduction.c @@ -0,0 +1,70 @@ +/* reduction.c + * ... illustrates the OpenMP parallel-for loop's reduction clause + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./reduction + * + * Exercise: + * - Compile and run. Note that correct output is produced. + * - Uncomment #pragma in function parallelSum(), + * but leave its reduction clause commented out + * - Recompile and rerun. Note that correct output is NOT produced. + * - Uncomment 'reduction(+:sum)' clause of #pragma in parallelSum() + * - Recompile and rerun. Note that correct output is produced again. + */ + +#include // printf() +#include // OpenMP +#include // rand() + +void initialize(int* a, int n); +int sequentialSum(int* a, int n); +int parallelSum(int* a, int n); + +#define SIZE 1000000 + +int main(int argc, char** argv) { + int array[SIZE]; + + if (argc > 1) { + omp_set_num_threads( atoi(argv[1]) ); + } + + initialize(array, SIZE); + printf("\nSeq. sum: \t%d\nPar. sum: \t%d\n\n", + sequentialSum(array, SIZE), + parallelSum(array, SIZE) ); + + return 0; +} + +/* fill array with random values */ +void initialize(int* a, int n) { + int i; + for (i = 0; i < n; i++) { + a[i] = rand() % 1000; + } +} + +/* sum the array sequentially */ +int sequentialSum(int* a, int n) { + int sum = 0; + int i; + for (i = 0; i < n; i++) { + sum += a[i]; + } + return sum; +} + +/* sum the array using multiple threads */ +int parallelSum(int* a, int n) { + int sum = 0; + int i; +// #pragma omp parallel for // reduction(+:sum) + for (i = 0; i < n; i++) { + sum += a[i]; + } + return sum; +} + From 769fec9524c27a6d2128f6ef95c19bef634df8f0 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Mon, 9 Aug 2021 23:58:31 -0400 Subject: [PATCH 07/41] Removed c source code --- Patternlets/pyjama/08.reduction/reduction.c | 70 --------------------- 1 file changed, 70 deletions(-) delete mode 100755 Patternlets/pyjama/08.reduction/reduction.c diff --git a/Patternlets/pyjama/08.reduction/reduction.c b/Patternlets/pyjama/08.reduction/reduction.c deleted file mode 100755 index 036af0e..0000000 --- a/Patternlets/pyjama/08.reduction/reduction.c +++ /dev/null @@ -1,70 +0,0 @@ -/* reduction.c - * ... illustrates the OpenMP parallel-for loop's reduction clause - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: ./reduction - * - * Exercise: - * - Compile and run. Note that correct output is produced. - * - Uncomment #pragma in function parallelSum(), - * but leave its reduction clause commented out - * - Recompile and rerun. Note that correct output is NOT produced. - * - Uncomment 'reduction(+:sum)' clause of #pragma in parallelSum() - * - Recompile and rerun. Note that correct output is produced again. - */ - -#include // printf() -#include // OpenMP -#include // rand() - -void initialize(int* a, int n); -int sequentialSum(int* a, int n); -int parallelSum(int* a, int n); - -#define SIZE 1000000 - -int main(int argc, char** argv) { - int array[SIZE]; - - if (argc > 1) { - omp_set_num_threads( atoi(argv[1]) ); - } - - initialize(array, SIZE); - printf("\nSeq. sum: \t%d\nPar. sum: \t%d\n\n", - sequentialSum(array, SIZE), - parallelSum(array, SIZE) ); - - return 0; -} - -/* fill array with random values */ -void initialize(int* a, int n) { - int i; - for (i = 0; i < n; i++) { - a[i] = rand() % 1000; - } -} - -/* sum the array sequentially */ -int sequentialSum(int* a, int n) { - int sum = 0; - int i; - for (i = 0; i < n; i++) { - sum += a[i]; - } - return sum; -} - -/* sum the array using multiple threads */ -int parallelSum(int* a, int n) { - int sum = 0; - int i; -// #pragma omp parallel for // reduction(+:sum) - for (i = 0; i < n; i++) { - sum += a[i]; - } - return sum; -} - From 5d84e27868bae4619cfda222f06312cefb2375eb Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 16:47:36 -0400 Subject: [PATCH 08/41] vscode config files --- Patternlets/pyjama/.vscode/launch.json | 18 ++++++++++++++++++ Patternlets/pyjama/.vscode/tasks.json | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 Patternlets/pyjama/.vscode/launch.json create mode 100644 Patternlets/pyjama/.vscode/tasks.json diff --git a/Patternlets/pyjama/.vscode/launch.json b/Patternlets/pyjama/.vscode/launch.json new file mode 100644 index 0000000..4fcec4c --- /dev/null +++ b/Patternlets/pyjama/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch Current File", + "request": "launch", + "classPaths": ["${fileDirname}/../lib/Pyjama.jar", "${fileDirname}"], + "args" : ["4"], + "cwd" : "${fileDirname}", + "mainClass": "${file}", + "preLaunchTask": "build" + } + ] +} \ No newline at end of file diff --git a/Patternlets/pyjama/.vscode/tasks.json b/Patternlets/pyjama/.vscode/tasks.json new file mode 100644 index 0000000..40ab3fb --- /dev/null +++ b/Patternlets/pyjama/.vscode/tasks.json @@ -0,0 +1,25 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "java", + "args": [ + "-jar", + "../lib/Pyjama.jar", + "${file}" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [], + "options": { + "cwd": "${fileDirname}" + } + } + ] +} \ No newline at end of file From 4a18b1f937c7a72c6d3afb5e865c110bad38edd9 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 16:54:37 -0400 Subject: [PATCH 09/41] Failed attempt for custom reduction --- .../09.reduction-userDefined/Reduction2.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Patternlets/pyjama/09.reduction-userDefined/Reduction2.java diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java new file mode 100644 index 0000000..8864bbb --- /dev/null +++ b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java @@ -0,0 +1,91 @@ +/* Reduction2.java computes a table of factorial values, + * OpenMP's user-defined reductions. + * + * Joel Adams, Calvin College, December 2015. + * + * Usage: ./reduction2 [numThreads] [n] + * + * Exercise: + * - Build and run, record sequential time in a spreadsheet + * - Uncomment #pragma omp parallel for directive, rebuild, + * and read the error message carefully. + * - Uncomment the #pragma omp declare directive, rebuild, + * and note the user-defined * reduction for a BigInt. + * - Rerun, using 2, 4, 6, 8, ... threads, recording + * the times in the spreadsheet. + * - Create a chart that plots the times vs the # of threads. + * - Experiment with different n values + */ + +import java.math.BigInteger; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.stream.IntStream; + +public class Reduction2 { + static class MyBigInteger { + private BigInteger myValue; + + public MyBigInteger() { + myValue = BigInteger.valueOf(1); + } + + public MyBigInteger(int val) { + myValue = BigInteger.valueOf(val); + } + + public MyBigInteger(BigInteger val) { + myValue = val; + } + + public MyBigInteger multiply(MyBigInteger arg) { + return new MyBigInteger(myValue.multiply(arg.myValue)); + } + + @Override + public String toString() { + return myValue.toString(); + } + } + + static MyBigInteger multiply(MyBigInteger arg1, MyBigInteger arg2) { + return arg1.multiply(arg2); + } + + static MyBigInteger factorial(int numThreads, int n) { + MyBigInteger acc = new MyBigInteger(1); + + //#omp parallel shared(acc, n) num_threads(numThreads) + { + //#omp for reduction(multiply:acc) + for(int i=2; i <= n; i++) { + MyBigInteger x = new MyBigInteger(i); + acc = acc.multiply(x); + } + } + return acc; + } + + public static void main(String[] args) { + // check and parse argument + int numThreads = Runtime.getRuntime().availableProcessors(); + if (args.length < 1) { + System.out.println("Usage " + Reduction2.class.getName() + " numThreads n."); + System.out.println("Using default number of Threads " + numThreads); + } else { + numThreads = Integer.parseInt(args[0]); + } + + int n = 32; + if (args.length == 2) { + n = Integer.parseInt(args[1]); + } + + long startTime = System.currentTimeMillis(); + MyBigInteger result = factorial(numThreads, n); + long duration = System.currentTimeMillis() - startTime; + + System.out.println("Result = " + result.toString()); + System.out.println("Time = " + duration + " ms"); + } +} From 3b019e3215d50e91c0739ddb4d95c94614b06db1 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 21:34:39 -0400 Subject: [PATCH 10/41] Using long in the factorial calc and multiplication operator in the reduction. Not very interesting since we will get overflow when n > 20 --- .../09.reduction-userDefined/Reduction2.java | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java index 8864bbb..bcd0a76 100644 --- a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java +++ b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java @@ -1,10 +1,15 @@ /* Reduction2.java computes a table of factorial values, - * OpenMP's user-defined reductions. + * OpenMP's reduction using the multiplication operator. * * Joel Adams, Calvin College, December 2015. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 * - * Usage: ./reduction2 [numThreads] [n] + * Usage: + * make run + * make run ARGS="numThreads n" + * For example: make run ARGS="4 100" * + * * Exercise: * - Build and run, record sequential time in a spreadsheet * - Uncomment #pragma omp parallel for directive, rebuild, @@ -17,50 +22,19 @@ * - Experiment with different n values */ -import java.math.BigInteger; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.stream.IntStream; public class Reduction2 { - static class MyBigInteger { - private BigInteger myValue; - - public MyBigInteger() { - myValue = BigInteger.valueOf(1); - } - - public MyBigInteger(int val) { - myValue = BigInteger.valueOf(val); - } - - public MyBigInteger(BigInteger val) { - myValue = val; - } - - public MyBigInteger multiply(MyBigInteger arg) { - return new MyBigInteger(myValue.multiply(arg.myValue)); - } - - @Override - public String toString() { - return myValue.toString(); - } - } - - static MyBigInteger multiply(MyBigInteger arg1, MyBigInteger arg2) { - return arg1.multiply(arg2); - } - - static MyBigInteger factorial(int numThreads, int n) { - MyBigInteger acc = new MyBigInteger(1); + static long factorial(int numThreads, int n) { + long acc = 1; //#omp parallel shared(acc, n) num_threads(numThreads) { - //#omp for reduction(multiply:acc) + //#omp for reduction(*:acc) for(int i=2; i <= n; i++) { - MyBigInteger x = new MyBigInteger(i); - acc = acc.multiply(x); + acc *= i; } } return acc; @@ -76,16 +50,20 @@ public static void main(String[] args) { numThreads = Integer.parseInt(args[0]); } - int n = 32; + int n = 20; if (args.length == 2) { n = Integer.parseInt(args[1]); + if (n > 20) { + System.out.println("n cannot be greater than 20 (overflow will happen)"); + return; + } } long startTime = System.currentTimeMillis(); - MyBigInteger result = factorial(numThreads, n); + long result = factorial(numThreads, n); long duration = System.currentTimeMillis() - startTime; - System.out.println("Result = " + result.toString()); + System.out.println("Result = " + result); System.out.println("Time = " + duration + " ms"); } } From 506225801e3585150a079f3f8afb6746288b6feb Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 23:34:48 -0400 Subject: [PATCH 11/41] Use reduction with max function (example from Thomas Hines) --- .../pyjama/09.reduction-userDefined/Makefile | 15 ++++ .../09.reduction-userDefined/Reduction2.java | 71 ++++++++----------- 2 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 Patternlets/pyjama/09.reduction-userDefined/Makefile diff --git a/Patternlets/pyjama/09.reduction-userDefined/Makefile b/Patternlets/pyjama/09.reduction-userDefined/Makefile new file mode 100644 index 0000000..c8746ed --- /dev/null +++ b/Patternlets/pyjama/09.reduction-userDefined/Makefile @@ -0,0 +1,15 @@ +PROG = Reduction2 +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java index bcd0a76..67899a1 100644 --- a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java +++ b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java @@ -1,69 +1,58 @@ -/* Reduction2.java computes a table of factorial values, - * OpenMP's reduction using the multiplication operator. +/* Reduction2.java computes the maximum using reduction. * - * Joel Adams, Calvin College, December 2015. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * Pyjama OpenMP's reduction using the max function. + * Note that Pyjama currently does NOT support arbitrary + * user-defined function in the reduction clause. + * + * Adapted from Thomas Hines (Tennessee Tech) Pyjama example + * by Ruth Kurniawati, Westfield State University, July, 2021 * * Usage: * make run * make run ARGS="numThreads n" * For example: make run ARGS="4 100" - * * * Exercise: * - Build and run, record sequential time in a spreadsheet - * - Uncomment #pragma omp parallel for directive, rebuild, - * and read the error message carefully. * - Uncomment the #pragma omp declare directive, rebuild, - * and note the user-defined * reduction for a BigInt. + * and note the user-defined max function used in the reduction. * - Rerun, using 2, 4, 6, 8, ... threads, recording * the times in the spreadsheet. * - Create a chart that plots the times vs the # of threads. * - Experiment with different n values */ -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ForkJoinPool; -import java.util.stream.IntStream; +import java.util.concurrent.ThreadLocalRandom; public class Reduction2 { - static long factorial(int numThreads, int n) { - long acc = 1; - - //#omp parallel shared(acc, n) num_threads(numThreads) - { - //#omp for reduction(*:acc) - for(int i=2; i <= n; i++) { - acc *= i; - } - } - return acc; - } public static void main(String[] args) { - // check and parse argument - int numThreads = Runtime.getRuntime().availableProcessors(); - if (args.length < 1) { - System.out.println("Usage " + Reduction2.class.getName() + " numThreads n."); - System.out.println("Using default number of Threads " + numThreads); - } else { + + int numThreads = Pyjama.omp_get_num_procs(); + if (args.length > 1) { numThreads = Integer.parseInt(args[0]); } - - int n = 20; - if (args.length == 2) { + int n = 1000000000; + if (args.length > 2) { n = Integer.parseInt(args[1]); - if (n > 20) { - System.out.println("n cannot be greater than 20 (overflow will happen)"); - return; + } + + int max_val = 0; + int [] arr = new int[n]; + for (int i = 0; i < n; i++) + arr[i] = ThreadLocalRandom.current().nextInt(0, 101); + + //#omp parallel num_threads(numThreads) shared(n, arr) reduction(max:max_val) + { + //#omp for + for (int i = 0; i < n; i++) + { + if (arr[i] > max_val) + max_val = arr[i]; } } - long startTime = System.currentTimeMillis(); - long result = factorial(numThreads, n); - long duration = System.currentTimeMillis() - startTime; - - System.out.println("Result = " + result); - System.out.println("Time = " + duration + " ms"); + for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); + System.out.println("\nmax value = " + max_val); } } From 8815cf2981e79c1f8dc1776cfaa02439013c0295 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 23:49:45 -0400 Subject: [PATCH 12/41] reduce array size - we run out of heap space with n = 100,000,000 --- .../pyjama/09.reduction-userDefined/Reduction2.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java index 67899a1..8bfc872 100644 --- a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java +++ b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java @@ -32,7 +32,7 @@ public static void main(String[] args) { if (args.length > 1) { numThreads = Integer.parseInt(args[0]); } - int n = 1000000000; + int n = 100000000; if (args.length > 2) { n = Integer.parseInt(args[1]); } @@ -42,6 +42,8 @@ public static void main(String[] args) { for (int i = 0; i < n; i++) arr[i] = ThreadLocalRandom.current().nextInt(0, 101); + long startTime = System.currentTimeMillis(); + //#omp parallel num_threads(numThreads) shared(n, arr) reduction(max:max_val) { //#omp for @@ -52,7 +54,10 @@ public static void main(String[] args) { } } - for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); + //for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); + long endTime = System.currentTimeMillis(); + System.out.println("\nmax value = " + max_val); + System.out.println("Time = " + (endTime-startTime) + " ms"); } } From 7c0b88193ae77083b00fa0b9f1ec978613d47e3e Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Tue, 10 Aug 2021 23:58:42 -0400 Subject: [PATCH 13/41] Fixed Reduction2. Added an example to explore dynamic scheduling --- .../09.reduction-userDefined/Reduction2.java | 4 +- .../DynamicScheduling.java | 60 +++++++++++++++++++ .../10.parallelLoop-dynamicSchedule/Makefile | 15 +++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java create mode 100644 Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java index 8bfc872..1791e0b 100644 --- a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java +++ b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java @@ -29,11 +29,11 @@ public class Reduction2 { public static void main(String[] args) { int numThreads = Pyjama.omp_get_num_procs(); - if (args.length > 1) { + if (args.length >= 1) { numThreads = Integer.parseInt(args[0]); } int n = 100000000; - if (args.length > 2) { + if (args.length >= 2) { n = Integer.parseInt(args[1]); } diff --git a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java new file mode 100644 index 0000000..d6610ec --- /dev/null +++ b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java @@ -0,0 +1,60 @@ +/* DynamicScheduling.java + * Explore OpenMP's schedule() clause by counting the number of + * prime numbers between 0 and n. + * + * Adapted from OpenMP example from Shaikh Ghafoor and Mike Rogers (Tennessee Tech) + * CDER workshop summer 2021. + * Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run ARGS="numThreads n" + * Example: make run ARGS=4 10000000 + * + * Exercise: + * - Build and run, record sequential run time in a spreadsheet + * - Uncomment #pragma omp parallel for, rebuild, + * run using 2, 4, 6, 8, ... threads, record run times. + * - Uncomment schedule(dynamic), rebuild, + * run using 2, 4, 6, 8, ... threads, record run times. + * - Create a line chart plotting run times vs # of threads. + */ + +class DynamicScheduling { + + static boolean isPrime(int n) { + if (n == 2) return true; + if (n % 2 == 0) return false; + int half = n / 2; + for(int i = 3; i < half; i+=2) { + if (n % i == 0) return false; + if (i*i > n) break; + } + return true; + } + + public static void main(String[] args) { + + int numThreads = Pyjama.omp_get_num_procs(); + if (args.length >= 1) { + numThreads = Integer.parseInt(args[0]); + } + + int n = 1000000; // one million, cannot 1_000_000 since we're limited to Java 1.5 syntax + if (args.length >= 2) { + n = Integer.parseInt(args[1]); + } + + long startTime = System.currentTimeMillis(); + int count = 1; + //#omp parallel for shared(n) num_threads(numThreads) reduction(+:count) /* schedule(dynamic) */ + for(int i = 3; i <= n; i++) { + if (isPrime(i)) count++; + } + + long endTime = System.currentTimeMillis(); + + System.out.println("The number between of prime numbers between 0 and "+ n + " is " + count); + System.out.println("Time = " + (endTime-startTime) + " ms"); + + } +} \ No newline at end of file diff --git a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile new file mode 100644 index 0000000..c71c51f --- /dev/null +++ b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile @@ -0,0 +1,15 @@ +PROG = DynamicScheduling +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# From e8f635cec7c89bd2679ddc3c85546726fe15bea7 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 00:01:53 -0400 Subject: [PATCH 14/41] make class not public to be consistent with other examples --- .../06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java index 33e97cf..062aefc 100644 --- a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java +++ b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java @@ -15,7 +15,7 @@ * - try with different numbers of threads, e.g.: 2, 3, 4, 6, 8 */ -public class ParallelLoopEqualChunks { +class ParallelLoopEqualChunks { final static int REPS = 16; public static void main(String[] args) { if (args.length >= 1) { From 477544005b5cbe45c5fe3d653ac5ea642cadce33 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 00:10:02 -0400 Subject: [PATCH 15/41] Added example on omp section --- Patternlets/pyjama/16.sections/Makefile | 15 +++++++ Patternlets/pyjama/16.sections/Sections.java | 46 ++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Patternlets/pyjama/16.sections/Makefile create mode 100644 Patternlets/pyjama/16.sections/Sections.java diff --git a/Patternlets/pyjama/16.sections/Makefile b/Patternlets/pyjama/16.sections/Makefile new file mode 100644 index 0000000..3ac2c39 --- /dev/null +++ b/Patternlets/pyjama/16.sections/Makefile @@ -0,0 +1,15 @@ +PROG = Sections +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/16.sections/Sections.java b/Patternlets/pyjama/16.sections/Sections.java new file mode 100644 index 0000000..b84cb9f --- /dev/null +++ b/Patternlets/pyjama/16.sections/Sections.java @@ -0,0 +1,46 @@ +/* sections.java + * ... illustrates the use of OpenMP's parallel section/sections directives, + * which can be used for task parallelism... + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./sections + * + * Exercise: Compile, run (several times), compare output to source code. + */ + +public class Sections { + + public static void main(String[] args) { + + int numThreads = Pyjama.omp_get_num_procs(); + if (args.length >= 1) { + numThreads = Integer.parseInt(args[0]); + } + + System.out.println("\nBefore...\n"); + + //#omp parallel sections num_threads(4) + { + //#omp section + { + System.out.println("Task/section A performed by thread " + Pyjama.omp_get_thread_num() ); + } + //#omp section + { + System.out.println("Task/section B performed by thread " + Pyjama.omp_get_thread_num() ); + } + //#omp section + { + System.out.println("Task/section C performed by thread " + Pyjama.omp_get_thread_num() ); + } + //#omp section + { + System.out.println("Task/section D performed by thread " + Pyjama.omp_get_thread_num() ); + } + } + + System.out.println("\nAfter...\n"); + } +} + From 3b0349a360812e60e74310e877bf1b25ab405d48 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 00:31:13 -0400 Subject: [PATCH 16/41] Added example of private omp clause --- Patternlets/pyjama/11.private/Makefile | 15 ++++++ Patternlets/pyjama/11.private/Private.java | 53 ++++++++++++++++++++++ Patternlets/pyjama/11.private/private.c | 50 ++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 Patternlets/pyjama/11.private/Makefile create mode 100644 Patternlets/pyjama/11.private/Private.java create mode 100755 Patternlets/pyjama/11.private/private.c diff --git a/Patternlets/pyjama/11.private/Makefile b/Patternlets/pyjama/11.private/Makefile new file mode 100644 index 0000000..1a96bcc --- /dev/null +++ b/Patternlets/pyjama/11.private/Makefile @@ -0,0 +1,15 @@ +PROG = Private +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/11.private/Private.java b/Patternlets/pyjama/11.private/Private.java new file mode 100644 index 0000000..a0d787a --- /dev/null +++ b/Patternlets/pyjama/11.private/Private.java @@ -0,0 +1,53 @@ +import pj.Pyjama; + +/* private.c + * ... illustrates why private variables are needed with OpenMP's parallel for loop + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./private + * + * Exercise: + * - Run, noting that the sequential program produces correct results + * - Uncomment line A, recompile/run and compare + * - Recomment line A, uncomment line B, recompile/run and compare + */ + +/* there is a bug in Pyjama when loop control variable i is declared as private (private(i)) */ + +class Private { + final static int SIZE = 1000; + + public static void main(String[] args) { + int j; + boolean ok = true; + int[][] m = new int[SIZE][SIZE]; + + System.out.println(); + // set all array entries to 1 + + /* //#omp parallel for shared(m,j) // A */ + /* //#omp parallel for private(j) shared(m) // B */ + for (int i = 0; i < SIZE; i++) { + for (j = 0; j < SIZE; j++) { + //System.out.println("Thread " + Pyjama.omp_get_thread_num() + " setting m["+i+","+j+"]"); + m[i][j] = 1; + } + } + + // test (without using threads) + for (int ii = 0; ii < SIZE; ii++) { + for (int jj = 0; jj < SIZE; jj++) { + if ( m[ii][jj] != 1 ) { + System.out.println("Element ["+ii+","+jj+"] not set... \n"); + ok = false; + } + } + } + + if ( ok ) { + System.out.println("\nAll elements correctly set to 1\n"); + } + } +} + diff --git a/Patternlets/pyjama/11.private/private.c b/Patternlets/pyjama/11.private/private.c new file mode 100755 index 0000000..3d87fa1 --- /dev/null +++ b/Patternlets/pyjama/11.private/private.c @@ -0,0 +1,50 @@ +/* private.c + * ... illustrates why private variables are needed with OpenMP's parallel for loop + * + * Joel Adams, Calvin College, November 2009. + * + * Usage: ./private + * + * Exercise: + * - Run, noting that the sequential program produces correct results + * - Uncomment line A, recompile/run and compare + * - Recomment line A, uncomment line B, recompile/run and compare + */ + +#include +#include +#include + +#define SIZE 100 + +int main(int argc, char** argv) { + int i, j, ok = 1; + int m[SIZE][SIZE]; + + printf("\n"); + // set all array entries to 1 +// #pragma omp parallel for // A +// #pragma omp parallel for private(i,j) // B + for (i = 0; i < SIZE; i++) { + for (j = 0; j < SIZE; j++) { + m[i][j] = 1; + } + } + + // test (without using threads) + for (i = 0; i < SIZE; i++) { + for (j = 0; j < SIZE; j++) { + if ( m[i][j] != 1 ) { + printf("Element [%d,%d] not set... \n", i, j); + ok = 0; + } + } + } + + if ( ok ) { + printf("\nAll elements correctly set to 1\n\n"); + } + + return 0; +} + From 433d5bdef59c3b6af2dec7dd03eab3e14800cd0a Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 00:31:36 -0400 Subject: [PATCH 17/41] remove c code --- Patternlets/pyjama/11.private/private.c | 50 ------------------------- 1 file changed, 50 deletions(-) delete mode 100755 Patternlets/pyjama/11.private/private.c diff --git a/Patternlets/pyjama/11.private/private.c b/Patternlets/pyjama/11.private/private.c deleted file mode 100755 index 3d87fa1..0000000 --- a/Patternlets/pyjama/11.private/private.c +++ /dev/null @@ -1,50 +0,0 @@ -/* private.c - * ... illustrates why private variables are needed with OpenMP's parallel for loop - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: ./private - * - * Exercise: - * - Run, noting that the sequential program produces correct results - * - Uncomment line A, recompile/run and compare - * - Recomment line A, uncomment line B, recompile/run and compare - */ - -#include -#include -#include - -#define SIZE 100 - -int main(int argc, char** argv) { - int i, j, ok = 1; - int m[SIZE][SIZE]; - - printf("\n"); - // set all array entries to 1 -// #pragma omp parallel for // A -// #pragma omp parallel for private(i,j) // B - for (i = 0; i < SIZE; i++) { - for (j = 0; j < SIZE; j++) { - m[i][j] = 1; - } - } - - // test (without using threads) - for (i = 0; i < SIZE; i++) { - for (j = 0; j < SIZE; j++) { - if ( m[i][j] != 1 ) { - printf("Element [%d,%d] not set... \n", i, j); - ok = 0; - } - } - } - - if ( ok ) { - printf("\nAll elements correctly set to 1\n\n"); - } - - return 0; -} - From 8842921415d268adad2e89cb97ab22aa02655aea Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 08:44:34 -0400 Subject: [PATCH 18/41] Added examples for the critical and atomic clause, with an explanation that the support for atomic clause in Pyjama is not equivalent to native support --- .../12.mutualExclusion-atomic/Atomic.java | 42 ++++++++++++++++++ .../pyjama/12.mutualExclusion-atomic/Makefile | 15 +++++++ .../13.mutualExclusion-critical/Critical.java | 44 +++++++++++++++++++ .../13.mutualExclusion-critical/Makefile | 24 ++++++++++ .../{16.sections => 14.sections}/Makefile | 0 .../Sections.java | 0 6 files changed, 125 insertions(+) create mode 100644 Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java create mode 100644 Patternlets/pyjama/12.mutualExclusion-atomic/Makefile create mode 100644 Patternlets/pyjama/13.mutualExclusion-critical/Critical.java create mode 100644 Patternlets/pyjama/13.mutualExclusion-critical/Makefile rename Patternlets/pyjama/{16.sections => 14.sections}/Makefile (100%) rename Patternlets/pyjama/{16.sections => 14.sections}/Sections.java (100%) diff --git a/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java b/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java new file mode 100644 index 0000000..43b9f3f --- /dev/null +++ b/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java @@ -0,0 +1,42 @@ +/* Atomic.java + * ... illustrates a race condition when multiple threads read from / + * write to a shared variable (and explores OpenMP atomic operations). + * + * NOTE: In Pyjama, atomic is translated to a critical region, instead of atomic hardware operation. + * There is not a real support for atomic operations in Java. + * In native OpenMP library, support for atomic the clause will depend on the available atomic operations + * on the machine - make sure that you check the documentation. + * + * Joel Adams, Calvin College, November 2009. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run + * + * Exercise: + * - Compile and run 10 times; note that it always produces the correct balance: $1,000,000.00 + * - To parallelize, uncomment A, recompile and rerun multiple times, compare results + * - To fix: uncomment B, recompile and rerun, compare + */ + +class Atomic { + final static int REPS = 1000000; + + public static void main(String[] args) { + double balance = 0.0; + + System.out.println("\nYour starting bank account balance is "+ + balance); + + // simulate many deposits + /* //#omp parallel for shared(balance) // A */ + for (int i = 0; i < REPS; i++) { + /* //#omp atomic // B */ + balance += 1.0; + } + + System.out.println("\nAfter "+REPS+" $1 deposits, your balance is "+balance+"\n"); + + } +} + diff --git a/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile b/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile new file mode 100644 index 0000000..dacfc3d --- /dev/null +++ b/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile @@ -0,0 +1,15 @@ +PROG = Atomic +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java b/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java new file mode 100644 index 0000000..3420a70 --- /dev/null +++ b/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java @@ -0,0 +1,44 @@ +/* Critical.java + * ... fixes a race condition when multiple threads read from / + * write to a shared variable using the OpenMP critical directive. + * + * NOTE: In Pyjama, atomic is translated to a critical region, instead of atomic hardware operation. + * There is not a real support for atomic operations in Java. + * + * Joel Adams, Calvin College, November 2009. + * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run + * + * Exercise: + * - Compile and run several times; note that it always produces the correct balance $1,000,000.00 + * - Comment out A; recompile/run, and note incorrect result + * - To compare: uncomment B1+B2+B3, recompile and rerun, compare + * - Compare the code generated using A vs B1/B2/B3 by running "make j2j". The generated code can be found in the "gen" subdirectory. + */ + +class Critical { + final static int REPS = 1000000; + + public static void main(String[] args) { + double balance = 0.0; + + System.out.println("\nYour starting bank account balance is "+ + balance); + + // simulate many deposits + //#omp parallel for shared(balance) + for (int i = 0; i < REPS; i++) { + //#omp atomic // A + // //#omp critical // B1 + // { // B2 + balance += 1.0; + // } // B3 + } + + System.out.println("\nAfter "+REPS+" $1 deposits, your balance is "+balance+"\n"); + } +} + + diff --git a/Patternlets/pyjama/13.mutualExclusion-critical/Makefile b/Patternlets/pyjama/13.mutualExclusion-critical/Makefile new file mode 100644 index 0000000..8c10ae9 --- /dev/null +++ b/Patternlets/pyjama/13.mutualExclusion-critical/Makefile @@ -0,0 +1,24 @@ +PROG = Critical +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. +GEN = gen + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# + rm -r -f $(GEN) + +gen: + mkdir gen + +j2j: gen + $(CC) -j2j -d $(GEN) $(SRC) + diff --git a/Patternlets/pyjama/16.sections/Makefile b/Patternlets/pyjama/14.sections/Makefile similarity index 100% rename from Patternlets/pyjama/16.sections/Makefile rename to Patternlets/pyjama/14.sections/Makefile diff --git a/Patternlets/pyjama/16.sections/Sections.java b/Patternlets/pyjama/14.sections/Sections.java similarity index 100% rename from Patternlets/pyjama/16.sections/Sections.java rename to Patternlets/pyjama/14.sections/Sections.java From 2107580e2d6ccbc5352cdaeb1bcd6bb67d747c7e Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 11 Aug 2021 18:53:59 +0000 Subject: [PATCH 19/41] Use ThreadLocalRandom instead of a shared Random --- Patternlets/pyjama/03.spmd2/Spmd2.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Patternlets/pyjama/03.spmd2/Spmd2.java b/Patternlets/pyjama/03.spmd2/Spmd2.java index cf50743..c868443 100644 --- a/Patternlets/pyjama/03.spmd2/Spmd2.java +++ b/Patternlets/pyjama/03.spmd2/Spmd2.java @@ -7,7 +7,7 @@ * * Usage: * make run ARGS=[numThreads] - * Example: make run ARGS=4 + * Example: make run ARGS=10 * * Exercise: * - Compile & run with no commandline args @@ -17,7 +17,8 @@ * (if necessary, compare to 02.spmd) */ -import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; +import pj.Pyjama; class Spmd2 { public static void main(String[] args) { @@ -26,17 +27,12 @@ public static void main(String[] args) { } System.out.println(); - int id, numThreads; - Random rand; - //#omp parallel shared(id, numThreads) private(rand) + //#omp parallel shared(id, numThreads) { - // To make it easier to observe the race condition, uncomment the code below: + // To make it easier to observe the race condition, uncomment the code below that will make the thread sleep for 1-2 ms. // - rand = new Random(); // use a fixed seed for reproduceability - try { - Thread.sleep(rand.nextInt()); - } catch(Exception ex) {}; + // try { Thread.sleep(ThreadLocalRandom.current().nextInt(1, 3)); } catch(InterruptedException e) {} numThreads = Pyjama.omp_get_num_threads(); id = Pyjama.omp_get_thread_num(); From 29e267b13bcb7cdd4257649fda32403e4f035f22 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Thu, 12 Aug 2021 03:02:12 +0000 Subject: [PATCH 20/41] fixed the commented code -- now it is equivalent to the for loop with schedule(static,1) --- .../ParallelLoopChunksOf1.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java b/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java index ec33f24..9cca99b 100644 --- a/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java +++ b/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java @@ -1,3 +1,5 @@ +import pj.Pyjama; + /* ParallelLoopChunksOf1.java * ... illustrates how to make OpenMP map threads to * parallel loop iterations in chunks of size 1 @@ -37,12 +39,16 @@ public static void main(String[] args) { /* System.out.println("--\n\n"); - //#omp parallel for - for (int i = 0; i < REPS; i++) { + //#omp parallel + { + int numThreads = Pyjama.omp_get_num_threads(); int id = Pyjama.omp_get_thread_num(); - System.out.println("Thread "+id+" performed iteration "+i); + for (int i = id; i < REPS; i+=numThreads) { + System.out.println("Thread "+id+" performed iteration "+i); + } } */ + System.out.println(); } } From 4b2933b0ad4c089814efd82dc0204a7f07d6aa3f Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Thu, 12 Aug 2021 03:24:09 +0000 Subject: [PATCH 21/41] Added a simpler example of dynamic scheduling --- .../Makefile | 15 ++++++ .../SimpleDynamicScheduling.java | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile create mode 100644 Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java diff --git a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile new file mode 100644 index 0000000..2e875e9 --- /dev/null +++ b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile @@ -0,0 +1,15 @@ +PROG = SimpleDynamicScheduling +CLASS = $(PROG).class +SRC = $(PROG).java +PJ = ../lib/Pyjama.jar +CC = java -jar $(PJ) +VM = java -cp $(PJ):. + +run: $(CLASS) + $(VM) $(PROG) $(ARGS) + +$(CLASS): $(SRC) + $(CC) $(SRC) + +clean: + rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java new file mode 100644 index 0000000..c2ed8a6 --- /dev/null +++ b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java @@ -0,0 +1,48 @@ +/* SimpleDynamicScheduling.java + * Explore OpenMP's schedule() clause by counting the number of + * prime numbers between 0 and n. + * + * Ruth Kurniawati, Westfield State University, July, 2021 + * + * Usage: + * make run ARGS=numThreads + * Example: make run ARGS=4 + * + * Exercise: + * - Build and run, record sequential run time in a spreadsheet + * - Uncomment #pragma omp parallel for, rebuild, + * run using 2, 4, ... threads, record run times. + * - Uncomment schedule(dynamic), rebuild, + * run using 2, 4, ... threads, record run times. + * - Create a line chart plotting run times vs # of threads. + */ + +class SimpleDynamicScheduling { + + static void sleepALittle(int numMillis) { + try { + Thread.sleep(numMillis); + } catch(InterruptedException e) { + // do nothing + } + } + + public static void main(String[] args) { + int numThreads = Pyjama.omp_get_num_procs(); + if (args.length >= 1) { + numThreads = Integer.parseInt(args[0]); + } + + long startTime = System.currentTimeMillis(); + int count = 1; + + //#omp parallel for num_threads(numThreads) schedule(dynamic) + for(int i = 1; i <= 100; i++) { + sleepALittle(i); + } + + long endTime = System.currentTimeMillis(); + System.out.println("Time = " + (endTime-startTime) + " ms"); + + } +} \ No newline at end of file From c8c290e081e68f08911ca67020b493b2c37b3232 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 15 Aug 2021 02:54:36 +0000 Subject: [PATCH 22/41] Removed pyjama patternlets from CSinParallel repo, getting ready to use submodule instead --- Patternlets/pyjama/.vscode/launch.json | 18 ------ Patternlets/pyjama/.vscode/tasks.json | 25 -------- Patternlets/pyjama/00.forkJoin/ForkJoin.java | 28 --------- Patternlets/pyjama/00.forkJoin/Makefile | 16 ----- .../pyjama/01.forkJoin2/ForkJoin2.java | 41 ------------ Patternlets/pyjama/01.forkJoin2/Makefile | 16 ----- Patternlets/pyjama/02.spmd/Makefile | 16 ----- Patternlets/pyjama/02.spmd/Spmd.java | 29 --------- Patternlets/pyjama/03.spmd2/Makefile | 16 ----- Patternlets/pyjama/03.spmd2/Spmd2.java | 44 ------------- Patternlets/pyjama/04.barrier/Barrier.java | 38 ----------- Patternlets/pyjama/04.barrier/Makefile | 16 ----- Patternlets/pyjama/05.masterWorker/Makefile | 16 ----- .../pyjama/05.masterWorker/MasterWorker.java | 39 ------------ .../06.parallelLoop-equalChunks/Makefile | 16 ----- .../ParallelLoopEqualChunks.java | 35 ----------- .../pyjama/07.parallelLoop-chunksOf1/Makefile | 15 ----- .../ParallelLoopChunksOf1.java | 55 ---------------- Patternlets/pyjama/08.reduction/Makefile | 15 ----- .../pyjama/08.reduction/Reduction.java | 57 ----------------- .../pyjama/09.reduction-userDefined/Makefile | 15 ----- .../09.reduction-userDefined/Reduction2.java | 63 ------------------- .../DynamicScheduling.java | 60 ------------------ .../10.parallelLoop-dynamicSchedule/Makefile | 15 ----- .../Makefile | 15 ----- .../SimpleDynamicScheduling.java | 48 -------------- Patternlets/pyjama/11.private/Makefile | 15 ----- Patternlets/pyjama/11.private/Private.java | 53 ---------------- .../12.mutualExclusion-atomic/Atomic.java | 42 ------------- .../pyjama/12.mutualExclusion-atomic/Makefile | 15 ----- .../13.mutualExclusion-critical/Critical.java | 44 ------------- .../13.mutualExclusion-critical/Makefile | 24 ------- Patternlets/pyjama/14.sections/Makefile | 15 ----- Patternlets/pyjama/14.sections/Sections.java | 46 -------------- Patternlets/pyjama/lib/Makefile | 14 ----- 35 files changed, 1035 deletions(-) delete mode 100644 Patternlets/pyjama/.vscode/launch.json delete mode 100644 Patternlets/pyjama/.vscode/tasks.json delete mode 100644 Patternlets/pyjama/00.forkJoin/ForkJoin.java delete mode 100644 Patternlets/pyjama/00.forkJoin/Makefile delete mode 100644 Patternlets/pyjama/01.forkJoin2/ForkJoin2.java delete mode 100644 Patternlets/pyjama/01.forkJoin2/Makefile delete mode 100644 Patternlets/pyjama/02.spmd/Makefile delete mode 100644 Patternlets/pyjama/02.spmd/Spmd.java delete mode 100644 Patternlets/pyjama/03.spmd2/Makefile delete mode 100644 Patternlets/pyjama/03.spmd2/Spmd2.java delete mode 100644 Patternlets/pyjama/04.barrier/Barrier.java delete mode 100644 Patternlets/pyjama/04.barrier/Makefile delete mode 100644 Patternlets/pyjama/05.masterWorker/Makefile delete mode 100644 Patternlets/pyjama/05.masterWorker/MasterWorker.java delete mode 100644 Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile delete mode 100644 Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java delete mode 100644 Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile delete mode 100644 Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java delete mode 100644 Patternlets/pyjama/08.reduction/Makefile delete mode 100644 Patternlets/pyjama/08.reduction/Reduction.java delete mode 100644 Patternlets/pyjama/09.reduction-userDefined/Makefile delete mode 100644 Patternlets/pyjama/09.reduction-userDefined/Reduction2.java delete mode 100644 Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java delete mode 100644 Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile delete mode 100644 Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile delete mode 100644 Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java delete mode 100644 Patternlets/pyjama/11.private/Makefile delete mode 100644 Patternlets/pyjama/11.private/Private.java delete mode 100644 Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java delete mode 100644 Patternlets/pyjama/12.mutualExclusion-atomic/Makefile delete mode 100644 Patternlets/pyjama/13.mutualExclusion-critical/Critical.java delete mode 100644 Patternlets/pyjama/13.mutualExclusion-critical/Makefile delete mode 100644 Patternlets/pyjama/14.sections/Makefile delete mode 100644 Patternlets/pyjama/14.sections/Sections.java delete mode 100644 Patternlets/pyjama/lib/Makefile diff --git a/Patternlets/pyjama/.vscode/launch.json b/Patternlets/pyjama/.vscode/launch.json deleted file mode 100644 index 4fcec4c..0000000 --- a/Patternlets/pyjama/.vscode/launch.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "java", - "name": "Launch Current File", - "request": "launch", - "classPaths": ["${fileDirname}/../lib/Pyjama.jar", "${fileDirname}"], - "args" : ["4"], - "cwd" : "${fileDirname}", - "mainClass": "${file}", - "preLaunchTask": "build" - } - ] -} \ No newline at end of file diff --git a/Patternlets/pyjama/.vscode/tasks.json b/Patternlets/pyjama/.vscode/tasks.json deleted file mode 100644 index 40ab3fb..0000000 --- a/Patternlets/pyjama/.vscode/tasks.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "java", - "args": [ - "-jar", - "../lib/Pyjama.jar", - "${file}" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "problemMatcher": [], - "options": { - "cwd": "${fileDirname}" - } - } - ] -} \ No newline at end of file diff --git a/Patternlets/pyjama/00.forkJoin/ForkJoin.java b/Patternlets/pyjama/00.forkJoin/ForkJoin.java deleted file mode 100644 index 985a9aa..0000000 --- a/Patternlets/pyjama/00.forkJoin/ForkJoin.java +++ /dev/null @@ -1,28 +0,0 @@ -/* ForkJoin.java - * ... illustrates the fork-join pattern - * using OpenMP's parallel directive. - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * - * Exercise: - * - Compile & run, uncomment the pragma, - * recompile & run, compare results. - */ - -class ForkJoin { - - public static void main(String[] args) { - - System.out.println("\nBefore..."); - - //#omp parallel num_threads(4) - System.out.println("\nDuring..."); - - System.out.println("\nAfter...\n"); - } - -} \ No newline at end of file diff --git a/Patternlets/pyjama/00.forkJoin/Makefile b/Patternlets/pyjama/00.forkJoin/Makefile deleted file mode 100644 index 297857f..0000000 --- a/Patternlets/pyjama/00.forkJoin/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = ForkJoin -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java b/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java deleted file mode 100644 index 8e9eccb..0000000 --- a/Patternlets/pyjama/01.forkJoin2/ForkJoin2.java +++ /dev/null @@ -1,41 +0,0 @@ -/* ForkJoin2.java - * ... illustrates the fork-join pattern - * using multiple OpenMP parallel directives, - * and changing the number of threads two ways. - * - * Joel Adams, Calvin College, May 2013. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * - * Exercise: - * - Compile & run, compare results to source. - * - Predict how many threads will be used in 'Part IV'? - * - Uncomment 'Part IV', recompile, rerun. - */ - -class ForkJoin2 { - public static void main(String[] args) { - - System.out.print("\nBeginning\n"); - - //#omp parallel - System.out.print("\nPart I"); - - System.out.print("\n\nBetween I and II...\n"); - - Pyjama.omp_set_num_threads(3); - - //#omp parallel - System.out.print("\nPart II..."); - - System.out.print("\n\nBetween II and III...\n"); - - //#omp parallel num_threads(5) - System.out.print("\nPart III..."); - - System.out.print("\n\nEnd\n\n"); - } -} - diff --git a/Patternlets/pyjama/01.forkJoin2/Makefile b/Patternlets/pyjama/01.forkJoin2/Makefile deleted file mode 100644 index 42110bc..0000000 --- a/Patternlets/pyjama/01.forkJoin2/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = ForkJoin2 -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/02.spmd/Makefile b/Patternlets/pyjama/02.spmd/Makefile deleted file mode 100644 index c036a85..0000000 --- a/Patternlets/pyjama/02.spmd/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = Spmd -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/02.spmd/Spmd.java b/Patternlets/pyjama/02.spmd/Spmd.java deleted file mode 100644 index 560e5aa..0000000 --- a/Patternlets/pyjama/02.spmd/Spmd.java +++ /dev/null @@ -1,29 +0,0 @@ -/* Spmd.java - * ... illustrates the single-program-multiple-data (SPMD) - * pattern using two basic OpenMP commands... - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * - * Exercise: - * - Compile & run - * - Uncomment pragma, recompile & run, compare results - */ - -class Spmd { - public static void main(String[] args) { - System.out.println(); - - //#omp parallel - { - int id = Pyjama.omp_get_thread_num(); - int numThreads = Pyjama.omp_get_num_threads(); - System.out.println("Hello from thread " + id + " of " + numThreads); - } - - System.out.println(); - } -} diff --git a/Patternlets/pyjama/03.spmd2/Makefile b/Patternlets/pyjama/03.spmd2/Makefile deleted file mode 100644 index d31991f..0000000 --- a/Patternlets/pyjama/03.spmd2/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = Spmd2 -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/03.spmd2/Spmd2.java b/Patternlets/pyjama/03.spmd2/Spmd2.java deleted file mode 100644 index c868443..0000000 --- a/Patternlets/pyjama/03.spmd2/Spmd2.java +++ /dev/null @@ -1,44 +0,0 @@ -/* Spmd2.java - * ... illustrates the SPMD pattern in OpenMP, - * using the commandline arguments - * to control the number of threads. - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: - * make run ARGS=[numThreads] - * Example: make run ARGS=10 - * - * Exercise: - * - Compile & run with no commandline args - * - Rerun with different commandline args, - * until you see a problem with thread ids - * - Fix the race condition - * (if necessary, compare to 02.spmd) - */ - -import java.util.concurrent.ThreadLocalRandom; -import pj.Pyjama; - -class Spmd2 { - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); - } - System.out.println(); - - int id, numThreads; - //#omp parallel shared(id, numThreads) - { - // To make it easier to observe the race condition, uncomment the code below that will make the thread sleep for 1-2 ms. - // - // try { Thread.sleep(ThreadLocalRandom.current().nextInt(1, 3)); } catch(InterruptedException e) {} - - numThreads = Pyjama.omp_get_num_threads(); - id = Pyjama.omp_get_thread_num(); - System.out.println("Hello from thread "+ id +" of " + numThreads); - } - - System.out.println(); - } -} diff --git a/Patternlets/pyjama/04.barrier/Barrier.java b/Patternlets/pyjama/04.barrier/Barrier.java deleted file mode 100644 index 67349f9..0000000 --- a/Patternlets/pyjama/04.barrier/Barrier.java +++ /dev/null @@ -1,38 +0,0 @@ -/* Barrier.java - * ... illustrates the use of the OpenMP barrier command, - * using the commandline to control the number of threads... - * - * Joel Adams, Calvin College, May 2013. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run ARGS=[numThreads] - * Example: make run ARGS=8 - * - * Exercise: - * - Compile & run several times, noting interleaving of outputs. - * - Remove the barrier directive, recompile, rerun, - * and note the change in the outputs. - */ -class Barrier { - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads( Integer.parseInt(args[0]) ); - } - - System.out.println(); - //#omp parallel - { - int id = Pyjama.omp_get_thread_num(); - int numThreads = Pyjama.omp_get_num_threads(); - System.out.println("Thread "+id + " of "+numThreads+" is BEFORE the barrier."); - - //Try this with and without the barrier - //#omp barrier - - System.out.println("Thread "+id + " of "+numThreads+" is AFTER the barrier."); - } - - System.out.println(); - } -} diff --git a/Patternlets/pyjama/04.barrier/Makefile b/Patternlets/pyjama/04.barrier/Makefile deleted file mode 100644 index 481913b..0000000 --- a/Patternlets/pyjama/04.barrier/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = Barrier -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/05.masterWorker/Makefile b/Patternlets/pyjama/05.masterWorker/Makefile deleted file mode 100644 index d8c85a9..0000000 --- a/Patternlets/pyjama/05.masterWorker/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = MasterWorker -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/05.masterWorker/MasterWorker.java b/Patternlets/pyjama/05.masterWorker/MasterWorker.java deleted file mode 100644 index 310911a..0000000 --- a/Patternlets/pyjama/05.masterWorker/MasterWorker.java +++ /dev/null @@ -1,39 +0,0 @@ -/* MasterWorker.java - * ... illustrates the master-worker pattern in OpenMP - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: - * make run [numThreads] - * Example: make run 4 - * - * Exercise: - * - Compile and run as is. - * - Remove the #omp directive, re-compile and re-run - * - Compare and trace the different executions. - */ - -class MasterWorker { - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); - } - System.out.println(); - - - //#omp parallel - { - int id = Pyjama.omp_get_thread_num(); - int numThreads = Pyjama.omp_get_num_threads(); - - if ( id == 0 ) { // thread with ID 0 is master - System.out.println("Greetings from the master, #"+ id +" of " + numThreads + " threads"); - } else { // threads with IDs > 0 are workers - System.out.println("Greetings from a worker, #"+ id +" of " + numThreads + " threads"); - } - } - - System.out.println(); - } -} - diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile b/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile deleted file mode 100644 index de758cf..0000000 --- a/Patternlets/pyjama/06.parallelLoop-equalChunks/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -PROG = ParallelLoopEqualChunks -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - diff --git a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java b/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java deleted file mode 100644 index 062aefc..0000000 --- a/Patternlets/pyjama/06.parallelLoop-equalChunks/ParallelLoopEqualChunks.java +++ /dev/null @@ -1,35 +0,0 @@ -/* ParallelLoopEqualChunks.java - * ... illustrates the use of OpenMP's default parallel for loop in which - * threads iterate through equal sized chunks of the index range - * (cache-beneficial when accessing adjacent memory locations). - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run [numThreads] - * Example: make run 4 - * - * Exercise - * - Compile and run, comparing output to source code - * - try with different numbers of threads, e.g.: 2, 3, 4, 6, 8 - */ - -class ParallelLoopEqualChunks { - final static int REPS = 16; - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); - } - System.out.println(); - - //#omp parallel for - for (int i = 0; i < REPS; i++) { - int id = Pyjama.omp_get_thread_num(); - System.out.println("Thread "+id+" performed iteration "+i); - } - - System.out.println(); - } -} - diff --git a/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile b/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile deleted file mode 100644 index 1c64fed..0000000 --- a/Patternlets/pyjama/07.parallelLoop-chunksOf1/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = ParallelLoopChunksOf1 -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java b/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java deleted file mode 100644 index 9cca99b..0000000 --- a/Patternlets/pyjama/07.parallelLoop-chunksOf1/ParallelLoopChunksOf1.java +++ /dev/null @@ -1,55 +0,0 @@ -import pj.Pyjama; - -/* ParallelLoopChunksOf1.java - * ... illustrates how to make OpenMP map threads to - * parallel loop iterations in chunks of size 1 - * (use when not accesssing memory). - * - * Joel Adams, Calvin College, November 2009. - * - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run [numThreads] - * Example: make run 4 - * - * Exercise: - * 1. Compile and run, comparing output to source code, - * and to the output of the 'equal chunks' version. - * 2. Uncomment the "commented out" code below, - * and verify that both loops produce the same output. - * The first loop is simpler but more restrictive; - * the second loop is more complex but less restrictive. - */ - -class ParallelLoopChunksOf1 { - final static int REPS = 16; - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); - } - System.out.println(); - - //#omp parallel for schedule(static,1) - for (int i = 0; i < REPS; i++) { - int id = Pyjama.omp_get_thread_num(); - System.out.println("Thread "+id+" performed iteration "+i); - } - - /* - System.out.println("--\n\n"); - - //#omp parallel - { - int numThreads = Pyjama.omp_get_num_threads(); - int id = Pyjama.omp_get_thread_num(); - for (int i = id; i < REPS; i+=numThreads) { - System.out.println("Thread "+id+" performed iteration "+i); - } - } - */ - - System.out.println(); - } -} - diff --git a/Patternlets/pyjama/08.reduction/Makefile b/Patternlets/pyjama/08.reduction/Makefile deleted file mode 100644 index 48816c3..0000000 --- a/Patternlets/pyjama/08.reduction/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = Reduction -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/08.reduction/Reduction.java b/Patternlets/pyjama/08.reduction/Reduction.java deleted file mode 100644 index 5870c83..0000000 --- a/Patternlets/pyjama/08.reduction/Reduction.java +++ /dev/null @@ -1,57 +0,0 @@ -/* Reduction.java - * ... illustrates the OpenMP parallel-for loop's reduction clause - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run [numThreads] - * Example: make run 4 - * - * Exercise: - * - Compile and run. Note that incorrect output is produced by parallelSum() - * - Uncomment 'reduction(+:sum)' clause of #omp in parallelSum() - * - Recompile and rerun. Note that correct output is produced again. - */ - -import java.util.Random; - -class Reduction { - final static int SIZE=1000000; - - public static void main(String[] args) { - if (args.length >= 1) { - Pyjama.omp_set_num_threads(Integer.parseInt(args[0])); - } - System.out.println(); - - // generate SIZE random values in [0..1000) range - int[] array = new Random().ints(SIZE, 0, 1000).toArray(); - System.out.println("Seq. sum: \t" + sequentialSum(array)); - System.out.println("Par. sum: \t" + parallelSum(array)); - } - - - /* sum the array sequentially */ - static int sequentialSum(int[] a) { - int sum = 0; - int i; - for (i = 0; i < a.length; i++) { - sum += a[i]; - } - return sum; - } - - /* sum the array using multiple threads */ - static int parallelSum(int[] a) { - int sum = 0; - //#omp parallel shared(a,sum) - { - //#omp for /* reduction(+:sum) */ - for(int i = 0; i < a.length;i++) { - sum += a[i]; - } - } - return sum; - } -} \ No newline at end of file diff --git a/Patternlets/pyjama/09.reduction-userDefined/Makefile b/Patternlets/pyjama/09.reduction-userDefined/Makefile deleted file mode 100644 index c8746ed..0000000 --- a/Patternlets/pyjama/09.reduction-userDefined/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = Reduction2 -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java b/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java deleted file mode 100644 index 1791e0b..0000000 --- a/Patternlets/pyjama/09.reduction-userDefined/Reduction2.java +++ /dev/null @@ -1,63 +0,0 @@ -/* Reduction2.java computes the maximum using reduction. - * - * Pyjama OpenMP's reduction using the max function. - * Note that Pyjama currently does NOT support arbitrary - * user-defined function in the reduction clause. - * - * Adapted from Thomas Hines (Tennessee Tech) Pyjama example - * by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * make run ARGS="numThreads n" - * For example: make run ARGS="4 100" - * - * Exercise: - * - Build and run, record sequential time in a spreadsheet - * - Uncomment the #pragma omp declare directive, rebuild, - * and note the user-defined max function used in the reduction. - * - Rerun, using 2, 4, 6, 8, ... threads, recording - * the times in the spreadsheet. - * - Create a chart that plots the times vs the # of threads. - * - Experiment with different n values - */ - -import java.util.concurrent.ThreadLocalRandom; - -public class Reduction2 { - - public static void main(String[] args) { - - int numThreads = Pyjama.omp_get_num_procs(); - if (args.length >= 1) { - numThreads = Integer.parseInt(args[0]); - } - int n = 100000000; - if (args.length >= 2) { - n = Integer.parseInt(args[1]); - } - - int max_val = 0; - int [] arr = new int[n]; - for (int i = 0; i < n; i++) - arr[i] = ThreadLocalRandom.current().nextInt(0, 101); - - long startTime = System.currentTimeMillis(); - - //#omp parallel num_threads(numThreads) shared(n, arr) reduction(max:max_val) - { - //#omp for - for (int i = 0; i < n; i++) - { - if (arr[i] > max_val) - max_val = arr[i]; - } - } - - //for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); - long endTime = System.currentTimeMillis(); - - System.out.println("\nmax value = " + max_val); - System.out.println("Time = " + (endTime-startTime) + " ms"); - } -} diff --git a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java deleted file mode 100644 index d6610ec..0000000 --- a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/DynamicScheduling.java +++ /dev/null @@ -1,60 +0,0 @@ -/* DynamicScheduling.java - * Explore OpenMP's schedule() clause by counting the number of - * prime numbers between 0 and n. - * - * Adapted from OpenMP example from Shaikh Ghafoor and Mike Rogers (Tennessee Tech) - * CDER workshop summer 2021. - * Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run ARGS="numThreads n" - * Example: make run ARGS=4 10000000 - * - * Exercise: - * - Build and run, record sequential run time in a spreadsheet - * - Uncomment #pragma omp parallel for, rebuild, - * run using 2, 4, 6, 8, ... threads, record run times. - * - Uncomment schedule(dynamic), rebuild, - * run using 2, 4, 6, 8, ... threads, record run times. - * - Create a line chart plotting run times vs # of threads. - */ - -class DynamicScheduling { - - static boolean isPrime(int n) { - if (n == 2) return true; - if (n % 2 == 0) return false; - int half = n / 2; - for(int i = 3; i < half; i+=2) { - if (n % i == 0) return false; - if (i*i > n) break; - } - return true; - } - - public static void main(String[] args) { - - int numThreads = Pyjama.omp_get_num_procs(); - if (args.length >= 1) { - numThreads = Integer.parseInt(args[0]); - } - - int n = 1000000; // one million, cannot 1_000_000 since we're limited to Java 1.5 syntax - if (args.length >= 2) { - n = Integer.parseInt(args[1]); - } - - long startTime = System.currentTimeMillis(); - int count = 1; - //#omp parallel for shared(n) num_threads(numThreads) reduction(+:count) /* schedule(dynamic) */ - for(int i = 3; i <= n; i++) { - if (isPrime(i)) count++; - } - - long endTime = System.currentTimeMillis(); - - System.out.println("The number between of prime numbers between 0 and "+ n + " is " + count); - System.out.println("Time = " + (endTime-startTime) + " ms"); - - } -} \ No newline at end of file diff --git a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile b/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile deleted file mode 100644 index c71c51f..0000000 --- a/Patternlets/pyjama/10.parallelLoop-dynamicSchedule/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = DynamicScheduling -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile deleted file mode 100644 index 2e875e9..0000000 --- a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = SimpleDynamicScheduling -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java b/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java deleted file mode 100644 index c2ed8a6..0000000 --- a/Patternlets/pyjama/10.parallelLoop-simpleDynamicSchedule/SimpleDynamicScheduling.java +++ /dev/null @@ -1,48 +0,0 @@ -/* SimpleDynamicScheduling.java - * Explore OpenMP's schedule() clause by counting the number of - * prime numbers between 0 and n. - * - * Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run ARGS=numThreads - * Example: make run ARGS=4 - * - * Exercise: - * - Build and run, record sequential run time in a spreadsheet - * - Uncomment #pragma omp parallel for, rebuild, - * run using 2, 4, ... threads, record run times. - * - Uncomment schedule(dynamic), rebuild, - * run using 2, 4, ... threads, record run times. - * - Create a line chart plotting run times vs # of threads. - */ - -class SimpleDynamicScheduling { - - static void sleepALittle(int numMillis) { - try { - Thread.sleep(numMillis); - } catch(InterruptedException e) { - // do nothing - } - } - - public static void main(String[] args) { - int numThreads = Pyjama.omp_get_num_procs(); - if (args.length >= 1) { - numThreads = Integer.parseInt(args[0]); - } - - long startTime = System.currentTimeMillis(); - int count = 1; - - //#omp parallel for num_threads(numThreads) schedule(dynamic) - for(int i = 1; i <= 100; i++) { - sleepALittle(i); - } - - long endTime = System.currentTimeMillis(); - System.out.println("Time = " + (endTime-startTime) + " ms"); - - } -} \ No newline at end of file diff --git a/Patternlets/pyjama/11.private/Makefile b/Patternlets/pyjama/11.private/Makefile deleted file mode 100644 index 1a96bcc..0000000 --- a/Patternlets/pyjama/11.private/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = Private -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/11.private/Private.java b/Patternlets/pyjama/11.private/Private.java deleted file mode 100644 index a0d787a..0000000 --- a/Patternlets/pyjama/11.private/Private.java +++ /dev/null @@ -1,53 +0,0 @@ -import pj.Pyjama; - -/* private.c - * ... illustrates why private variables are needed with OpenMP's parallel for loop - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: ./private - * - * Exercise: - * - Run, noting that the sequential program produces correct results - * - Uncomment line A, recompile/run and compare - * - Recomment line A, uncomment line B, recompile/run and compare - */ - -/* there is a bug in Pyjama when loop control variable i is declared as private (private(i)) */ - -class Private { - final static int SIZE = 1000; - - public static void main(String[] args) { - int j; - boolean ok = true; - int[][] m = new int[SIZE][SIZE]; - - System.out.println(); - // set all array entries to 1 - - /* //#omp parallel for shared(m,j) // A */ - /* //#omp parallel for private(j) shared(m) // B */ - for (int i = 0; i < SIZE; i++) { - for (j = 0; j < SIZE; j++) { - //System.out.println("Thread " + Pyjama.omp_get_thread_num() + " setting m["+i+","+j+"]"); - m[i][j] = 1; - } - } - - // test (without using threads) - for (int ii = 0; ii < SIZE; ii++) { - for (int jj = 0; jj < SIZE; jj++) { - if ( m[ii][jj] != 1 ) { - System.out.println("Element ["+ii+","+jj+"] not set... \n"); - ok = false; - } - } - } - - if ( ok ) { - System.out.println("\nAll elements correctly set to 1\n"); - } - } -} - diff --git a/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java b/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java deleted file mode 100644 index 43b9f3f..0000000 --- a/Patternlets/pyjama/12.mutualExclusion-atomic/Atomic.java +++ /dev/null @@ -1,42 +0,0 @@ -/* Atomic.java - * ... illustrates a race condition when multiple threads read from / - * write to a shared variable (and explores OpenMP atomic operations). - * - * NOTE: In Pyjama, atomic is translated to a critical region, instead of atomic hardware operation. - * There is not a real support for atomic operations in Java. - * In native OpenMP library, support for atomic the clause will depend on the available atomic operations - * on the machine - make sure that you check the documentation. - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * - * Exercise: - * - Compile and run 10 times; note that it always produces the correct balance: $1,000,000.00 - * - To parallelize, uncomment A, recompile and rerun multiple times, compare results - * - To fix: uncomment B, recompile and rerun, compare - */ - -class Atomic { - final static int REPS = 1000000; - - public static void main(String[] args) { - double balance = 0.0; - - System.out.println("\nYour starting bank account balance is "+ - balance); - - // simulate many deposits - /* //#omp parallel for shared(balance) // A */ - for (int i = 0; i < REPS; i++) { - /* //#omp atomic // B */ - balance += 1.0; - } - - System.out.println("\nAfter "+REPS+" $1 deposits, your balance is "+balance+"\n"); - - } -} - diff --git a/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile b/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile deleted file mode 100644 index dacfc3d..0000000 --- a/Patternlets/pyjama/12.mutualExclusion-atomic/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = Atomic -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java b/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java deleted file mode 100644 index 3420a70..0000000 --- a/Patternlets/pyjama/13.mutualExclusion-critical/Critical.java +++ /dev/null @@ -1,44 +0,0 @@ -/* Critical.java - * ... fixes a race condition when multiple threads read from / - * write to a shared variable using the OpenMP critical directive. - * - * NOTE: In Pyjama, atomic is translated to a critical region, instead of atomic hardware operation. - * There is not a real support for atomic operations in Java. - * - * Joel Adams, Calvin College, November 2009. - * Adapted for Java/Pyjama by Ruth Kurniawati, Westfield State University, July, 2021 - * - * Usage: - * make run - * - * Exercise: - * - Compile and run several times; note that it always produces the correct balance $1,000,000.00 - * - Comment out A; recompile/run, and note incorrect result - * - To compare: uncomment B1+B2+B3, recompile and rerun, compare - * - Compare the code generated using A vs B1/B2/B3 by running "make j2j". The generated code can be found in the "gen" subdirectory. - */ - -class Critical { - final static int REPS = 1000000; - - public static void main(String[] args) { - double balance = 0.0; - - System.out.println("\nYour starting bank account balance is "+ - balance); - - // simulate many deposits - //#omp parallel for shared(balance) - for (int i = 0; i < REPS; i++) { - //#omp atomic // A - // //#omp critical // B1 - // { // B2 - balance += 1.0; - // } // B3 - } - - System.out.println("\nAfter "+REPS+" $1 deposits, your balance is "+balance+"\n"); - } -} - - diff --git a/Patternlets/pyjama/13.mutualExclusion-critical/Makefile b/Patternlets/pyjama/13.mutualExclusion-critical/Makefile deleted file mode 100644 index 8c10ae9..0000000 --- a/Patternlets/pyjama/13.mutualExclusion-critical/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -PROG = Critical -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. -GEN = gen - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# - rm -r -f $(GEN) - -gen: - mkdir gen - -j2j: gen - $(CC) -j2j -d $(GEN) $(SRC) - diff --git a/Patternlets/pyjama/14.sections/Makefile b/Patternlets/pyjama/14.sections/Makefile deleted file mode 100644 index 3ac2c39..0000000 --- a/Patternlets/pyjama/14.sections/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -PROG = Sections -CLASS = $(PROG).class -SRC = $(PROG).java -PJ = ../lib/Pyjama.jar -CC = java -jar $(PJ) -VM = java -cp $(PJ):. - -run: $(CLASS) - $(VM) $(PROG) $(ARGS) - -$(CLASS): $(SRC) - $(CC) $(SRC) - -clean: - rm -f $(PROG)*class *~ *# diff --git a/Patternlets/pyjama/14.sections/Sections.java b/Patternlets/pyjama/14.sections/Sections.java deleted file mode 100644 index b84cb9f..0000000 --- a/Patternlets/pyjama/14.sections/Sections.java +++ /dev/null @@ -1,46 +0,0 @@ -/* sections.java - * ... illustrates the use of OpenMP's parallel section/sections directives, - * which can be used for task parallelism... - * - * Joel Adams, Calvin College, November 2009. - * - * Usage: ./sections - * - * Exercise: Compile, run (several times), compare output to source code. - */ - -public class Sections { - - public static void main(String[] args) { - - int numThreads = Pyjama.omp_get_num_procs(); - if (args.length >= 1) { - numThreads = Integer.parseInt(args[0]); - } - - System.out.println("\nBefore...\n"); - - //#omp parallel sections num_threads(4) - { - //#omp section - { - System.out.println("Task/section A performed by thread " + Pyjama.omp_get_thread_num() ); - } - //#omp section - { - System.out.println("Task/section B performed by thread " + Pyjama.omp_get_thread_num() ); - } - //#omp section - { - System.out.println("Task/section C performed by thread " + Pyjama.omp_get_thread_num() ); - } - //#omp section - { - System.out.println("Task/section D performed by thread " + Pyjama.omp_get_thread_num() ); - } - } - - System.out.println("\nAfter...\n"); - } -} - diff --git a/Patternlets/pyjama/lib/Makefile b/Patternlets/pyjama/lib/Makefile deleted file mode 100644 index ecfc694..0000000 --- a/Patternlets/pyjama/lib/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -URL = https://www.csc.tntech.edu/pdcincs/resources/modules/tools/updated/Pyjama.zip -PJ = Pyjama -JAR = $(PJ).jar - -$(PJ): - wget $(URL) - unzip -o $(PJ).zip - cp $(PJ)/*.jar $(JAR) - rm -r -f Pyjama.zip Pyjama - -clean: - rm $(JAR) - - From d7a608bb0573d447a1b8f7b423253310130fe4ee Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 15 Aug 2021 02:56:15 +0000 Subject: [PATCH 23/41] Added the pyjama submodule --- .gitmodules | 3 +++ Patternlets/pyjama | 1 + 2 files changed, 4 insertions(+) create mode 160000 Patternlets/pyjama diff --git a/.gitmodules b/.gitmodules index f97ad74..a99822c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "Patternlets/MPI/notebook"] path = Patternlets/MPI/notebook url = https://github.com/rkurniawati/mpiC.git +[submodule "Patternlets/pyjama"] + path = Patternlets/pyjama + url = https://github.com/rkurniawati/pyjama-patternlets.git diff --git a/Patternlets/pyjama b/Patternlets/pyjama new file mode 160000 index 0000000..c405175 --- /dev/null +++ b/Patternlets/pyjama @@ -0,0 +1 @@ +Subproject commit c405175851c96893547d9811c805fffd095b5e4b From 84bbee8980f791b1a5863ebfe11374fe0985a542 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 15 Aug 2021 03:02:58 +0000 Subject: [PATCH 24/41] update submodules --- Patternlets/MPI/notebook | 2 +- Patternlets/java-OpenMPI/notebook | 2 +- Patternlets/pyjama | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Patternlets/MPI/notebook b/Patternlets/MPI/notebook index 7c6811d..de38a5e 160000 --- a/Patternlets/MPI/notebook +++ b/Patternlets/MPI/notebook @@ -1 +1 @@ -Subproject commit 7c6811de23e540c719b7aade6e14fa31bbb54520 +Subproject commit de38a5e8e137397aa96acd2b4b10a4bffbf9458d diff --git a/Patternlets/java-OpenMPI/notebook b/Patternlets/java-OpenMPI/notebook index 7a4069b..dec419a 160000 --- a/Patternlets/java-OpenMPI/notebook +++ b/Patternlets/java-OpenMPI/notebook @@ -1 +1 @@ -Subproject commit 7a4069b39ee6a3259a050f115b26ddc1564fe451 +Subproject commit dec419a7979361a965afc95605198190b7794ffd diff --git a/Patternlets/pyjama b/Patternlets/pyjama index c405175..1567a57 160000 --- a/Patternlets/pyjama +++ b/Patternlets/pyjama @@ -1 +1 @@ -Subproject commit c405175851c96893547d9811c805fffd095b5e4b +Subproject commit 1567a57ff449cd49d63407f2cf18614ded74f53d From f80109ea31a6b18f06d40fb34c059e69d1c10360 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Mon, 16 Aug 2021 17:38:08 +0000 Subject: [PATCH 25/41] update pyjama submodule --- Patternlets/pyjama | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Patternlets/pyjama b/Patternlets/pyjama index 1567a57..5a08554 160000 --- a/Patternlets/pyjama +++ b/Patternlets/pyjama @@ -1 +1 @@ -Subproject commit 1567a57ff449cd49d63407f2cf18614ded74f53d +Subproject commit 5a08554d1db9f695fb98be25dfa5062a8d94079c From ba511387862a981ef161dd286f59763d2f11a899 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 01:37:32 +0000 Subject: [PATCH 26/41] Initial version of Drug Design Exemplar in Java with Pyjama --- Exemplars/DrugDesign/DDPyjama.java | 135 +++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Exemplars/DrugDesign/DDPyjama.java diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java new file mode 100644 index 0000000..49d4a6e --- /dev/null +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -0,0 +1,135 @@ +import java.util.Random; + +public class DDPyjama { + static Random rand = new Random(42); + + static String[] cannedLigands = + {"razvex", "qudgy", "afrs", "sst", "pgfht", "rt", + "id", "how", "aaddh", "df", "os", "hid", + "sad", "fl", "rd", "edp", "dfgt", "spa"}; + + // Ligand Score pair + static class LSPair { + String ligand; + int score; + + public LSPair(String ligand, int score) { + this.ligand = ligand; + this.score = score; + } + + @Override + public String toString() { + return "["+ligand+","+score+"]"; + } + } + + // returns arbitrary string of lower-case letters of length at most max_ligand + static String makeLigand(int maxLigandLength) { + + int len = rand.nextInt(maxLigandLength+1); + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < len; i++) + sb.append((char) ('a' + rand.nextInt(26))); + return sb.toString(); + } + + private static String[] generateLigands(int numLigands, int maxLigandLength) { + // If numLigands <=18, create a pre-determined set of example ligands. + // Otherwise, create a set of ligands whose length randomly varies from 2 + // to args.maxLigand + + String[] result = new String[numLigands]; + if (numLigands<=18) { + for(int i = 0; i < numLigands; i++) { + result[i] = cannedLigands[i]; + } + return result; + + } + for(int i = 0; i < numLigands; i++) { + result[i] = makeLigand(maxLigandLength); + } + return result; + } + + public static void main(String[] args) { + + if (args.length != 4) { + System.out.println("Usage DDPyjama numThreads numLigands maxLigandLength protein"); + } + + int numThreads = 4; + if (args.length > 1) { + numThreads = Integer.parseInt(args[0]); + } + + int numLigands = 12; + if (args.length > 2) { + numLigands = Integer.parseInt(args[1]); + } + + int maxLigandLength = 7; + if (args.length > 3) { + maxLigandLength = Integer.parseInt(args[2]); + } + + String protein = "the cat in the hat wore the hat to the cat hat party"; + + if (args.length > 4) { + protein = args[3]; + } + + // Things to do: + // 1. Generate the requested numLigands w/ maxLigandLength + // 2. Calculate the matching score for each ligand vs the given protein + // Score is calculated based on the number of character in the ligand that + // appears in the same order in the protein. + // 3. Find the ligand(s) with the highest score + + String ligands[] = generateLigands(numLigands, maxLigandLength); + + // map each ligand to (ligand, score) + // also keep track of the maxScore + LSPair[] ligandsWScore = new LSPair[numLigands]; + int maxScore = 0; + for(int i = 0; i < numLigands; i++) { + int score = calcScore(ligands[i], protein); + ligandsWScore[i] = new LSPair(ligands[i], score); + maxScore = Math.max(maxScore, score); + } + + // find the ligands whose score is maxScore + // this is a reduce operation + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < numLigands; i++) { + if (ligandsWScore[i].score == maxScore) { + if (sb.length() > 0) sb.append(", "); + sb.append(ligandsWScore[i].ligand); + } + } + + System.out.println("The maximum score is " + maxScore); + System.out.println("Achieved by ligand(s) "+ sb.toString()); + + } + + /** + * Match a ligand (str1) and the protein. Count the number of characters in str1 + * that appear in the same seq in str2 (there can be any number of intervening chars) + * @param str1 first string + * @param str2 second string + * @return number of matches + */ + private static int calcScore(String str1, String str2) { + // no match if either is empty string + if (str1.length() == 0 || str2.length() == 0) return 0; + + if (str1.charAt(0) == str2.charAt(0)) { + return 1 + calcScore(str1.substring(1), str2.substring(1)); + } + return Math.max( + calcScore(str1, str2.substring(1)), calcScore(str1.substring(1), str2)); + } +} \ No newline at end of file From d746c1f2bbe6c5951c82e6c1d28a71ecb110aa64 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 01:56:28 +0000 Subject: [PATCH 27/41] Added omp directive, track timing --- Exemplars/DrugDesign/DDPyjama.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index 49d4a6e..d7302c9 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -70,7 +70,7 @@ public static void main(String[] args) { numLigands = Integer.parseInt(args[1]); } - int maxLigandLength = 7; + int maxLigandLength = 6; if (args.length > 3) { maxLigandLength = Integer.parseInt(args[2]); } @@ -81,6 +81,12 @@ public static void main(String[] args) { protein = args[3]; } + System.out.println("Number of threads: " + numThreads); + System.out.println("Number of ligans: "+numLigands); + System.out.println("Max ligand length: "+ maxLigandLength); + System.out.println("Protein: "+ protein); + + // Things to do: // 1. Generate the requested numLigands w/ maxLigandLength // 2. Calculate the matching score for each ligand vs the given protein @@ -88,15 +94,19 @@ public static void main(String[] args) { // appears in the same order in the protein. // 3. Find the ligand(s) with the highest score - String ligands[] = generateLigands(numLigands, maxLigandLength); + long start = System.currentTimeMillis(); + String[] ligands = generateLigands(numLigands, maxLigandLength); // map each ligand to (ligand, score) // also keep track of the maxScore LSPair[] ligandsWScore = new LSPair[numLigands]; int maxScore = 0; + + //#omp parallel for num_threads(numThreads) shared(numLigands, ligands, ligandsWScore, protein) reduction(max:maxScore) schedule(dynamic) for(int i = 0; i < numLigands; i++) { - int score = calcScore(ligands[i], protein); - ligandsWScore[i] = new LSPair(ligands[i], score); + String ligand = ligands[i]; + int score = calcScore(ligand, protein); + ligandsWScore[i] = new LSPair(ligand, score); maxScore = Math.max(maxScore, score); } @@ -110,9 +120,10 @@ public static void main(String[] args) { } } + long end = System.currentTimeMillis(); System.out.println("The maximum score is " + maxScore); System.out.println("Achieved by ligand(s) "+ sb.toString()); - + System.out.println("Calculation time " + (end-start) + " ms"); } /** From 687738eb1d9293e93e710532c9f3897d067cf6ce Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 01:56:41 +0000 Subject: [PATCH 28/41] Added rules for DDPyjama --- Exemplars/DrugDesign/Makefile | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Exemplars/DrugDesign/Makefile b/Exemplars/DrugDesign/Makefile index 2886e7b..fac056c 100644 --- a/Exemplars/DrugDesign/Makefile +++ b/Exemplars/DrugDesign/Makefile @@ -17,6 +17,16 @@ dd_threads: dd_threads.cpp dd_mpi: dd_mpi.cpp mpiCC -std=c++11 -o dd_mpi dd_mpi.cpp +Pyjama.jar: + wget https://www.csc.tntech.edu/pdcincs/resources/modules/tools/updated/Pyjama.zip + unzip -o Pyjama.zip + cp Pyjama/Pyjama*.jar Pyjama.jar + rm -r -f Pyjama.zip Pyjama + +DDPyjama: DDPyjama.java Pyjama.jar + java -jar Pyjama.jar DDPyjama.java + java -cp .:Pyjama.jar DDPyjama $(ARGS) + clean: - rm -f dd_serial dd_omp_static dd_omp_dynamic dd_threads dd_mpi + rm -f dd_serial dd_omp_static dd_omp_dynamic dd_threads dd_mpi Pyjama.jar DDPyjama*class From c50eb71c1307d3abc8dc712cb5ef85bf7ad81050 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 03:09:20 +0000 Subject: [PATCH 29/41] Minor changes --- Exemplars/DrugDesign/DDPyjama.java | 4 ++-- Exemplars/DrugDesign/Makefile | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index d7302c9..491a301 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -82,10 +82,10 @@ public static void main(String[] args) { } System.out.println("Number of threads: " + numThreads); - System.out.println("Number of ligans: "+numLigands); + System.out.println("Number of ligands: "+numLigands); System.out.println("Max ligand length: "+ maxLigandLength); System.out.println("Protein: "+ protein); - + System.out.println(); // Things to do: // 1. Generate the requested numLigands w/ maxLigandLength diff --git a/Exemplars/DrugDesign/Makefile b/Exemplars/DrugDesign/Makefile index fac056c..c3759d1 100644 --- a/Exemplars/DrugDesign/Makefile +++ b/Exemplars/DrugDesign/Makefile @@ -23,8 +23,10 @@ Pyjama.jar: cp Pyjama/Pyjama*.jar Pyjama.jar rm -r -f Pyjama.zip Pyjama -DDPyjama: DDPyjama.java Pyjama.jar +DDPyjama.class: DDPyjama.java Pyjama.jar java -jar Pyjama.jar DDPyjama.java + +DDPyjama: DDPyjama.class java -cp .:Pyjama.jar DDPyjama $(ARGS) clean: From 7a1ae1825915f652097689229703570706926ef9 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 03:17:13 +0000 Subject: [PATCH 30/41] fixed args issue --- Exemplars/DrugDesign/DDPyjama.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index 491a301..5aef089 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -41,7 +41,7 @@ private static String[] generateLigands(int numLigands, int maxLigandLength) { // to args.maxLigand String[] result = new String[numLigands]; - if (numLigands<=18) { + if (numLigands <= cannedLigands.length) { for(int i = 0; i < numLigands; i++) { result[i] = cannedLigands[i]; } @@ -61,23 +61,23 @@ public static void main(String[] args) { } int numThreads = 4; - if (args.length > 1) { + if (args.length >= 1) { numThreads = Integer.parseInt(args[0]); } int numLigands = 12; - if (args.length > 2) { + if (args.length >= 2) { numLigands = Integer.parseInt(args[1]); } int maxLigandLength = 6; - if (args.length > 3) { + if (args.length >= 3) { maxLigandLength = Integer.parseInt(args[2]); } String protein = "the cat in the hat wore the hat to the cat hat party"; - if (args.length > 4) { + if (args.length >= 4) { protein = args[3]; } From 8d659e0f3b5438504e89962d686e9be3f7ffc57b Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 29 Aug 2021 03:42:08 +0000 Subject: [PATCH 31/41] Use the latest version of the Pyjama submodule --- Patternlets/pyjama | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Patternlets/pyjama b/Patternlets/pyjama index 5a08554..e5fe441 160000 --- a/Patternlets/pyjama +++ b/Patternlets/pyjama @@ -1 +1 @@ -Subproject commit 5a08554d1db9f695fb98be25dfa5062a8d94079c +Subproject commit e5fe4413d5271da1ae0ff4daaf3abfad417d613e From 42841212ab749c33680447f82523bf97403fe98c Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 1 Dec 2021 09:25:37 -0500 Subject: [PATCH 32/41] Updated pyjama submodule to the latest version --- Patternlets/pyjama | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Patternlets/pyjama b/Patternlets/pyjama index e5fe441..f2d452f 160000 --- a/Patternlets/pyjama +++ b/Patternlets/pyjama @@ -1 +1 @@ -Subproject commit e5fe4413d5271da1ae0ff4daaf3abfad417d613e +Subproject commit f2d452f76c8220e0146437b4c0c0d916159668d0 From 055b5eb61a85413da7086406912222af99e712b7 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 1 Dec 2021 10:13:18 -0500 Subject: [PATCH 33/41] added option to skip the canned ligands and print ligands --- Exemplars/DrugDesign/DDPyjama.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index 5aef089..ca56b62 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -35,19 +35,21 @@ static String makeLigand(int maxLigandLength) { return sb.toString(); } - private static String[] generateLigands(int numLigands, int maxLigandLength) { + private static String[] generateLigands(int numLigands, int maxLigandLength, boolean useCanned) { // If numLigands <=18, create a pre-determined set of example ligands. // Otherwise, create a set of ligands whose length randomly varies from 2 // to args.maxLigand String[] result = new String[numLigands]; - if (numLigands <= cannedLigands.length) { + + if (useCanned && numLigands <= cannedLigands.length) { for(int i = 0; i < numLigands; i++) { result[i] = cannedLigands[i]; } return result; } + for(int i = 0; i < numLigands; i++) { result[i] = makeLigand(maxLigandLength); } @@ -57,7 +59,10 @@ private static String[] generateLigands(int numLigands, int maxLigandLength) { public static void main(String[] args) { if (args.length != 4) { - System.out.println("Usage DDPyjama numThreads numLigands maxLigandLength protein"); + System.out.println("Usage DDPyjama numThreads numLigands maxLigandLength protein useCanned printLigands"); + + // the example string below is one of Dijkstra's famous quotes + System.out.println(" Example: java DDPyjama 4 10 8 \"Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it\" false true\n"); } int numThreads = 4; @@ -95,8 +100,16 @@ public static void main(String[] args) { // 3. Find the ligand(s) with the highest score long start = System.currentTimeMillis(); - String[] ligands = generateLigands(numLigands, maxLigandLength); + String[] ligands = generateLigands(numLigands, maxLigandLength, args.length >= 5 && args[4].equals("true")); + // print the ligands if desired + if (args.length >= 6 && args[5].equals("true")) { + System.out.println("Here are the ligands"); + for(String l : ligands) { + System.out.println(l); + } + } + // map each ligand to (ligand, score) // also keep track of the maxScore LSPair[] ligandsWScore = new LSPair[numLigands]; From a699793ec6258552716aaf66d3a4556f9a7ae94a Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 1 Dec 2021 10:21:12 -0500 Subject: [PATCH 34/41] Add longish canned ligands to allow for more imbalance in the dynamic scheduling --- Exemplars/DrugDesign/DDPyjama.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index ca56b62..272bf9e 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -6,7 +6,7 @@ public class DDPyjama { static String[] cannedLigands = {"razvex", "qudgy", "afrs", "sst", "pgfht", "rt", "id", "how", "aaddh", "df", "os", "hid", - "sad", "fl", "rd", "edp", "dfgt", "spa"}; + "sad", "fl", "rd", "edp", "dfgt", "spa", "qdsayjbjp", "txtzunerdjenklh"}; // Ligand Score pair static class LSPair { From b28795c634ec12ea55d0c40ee0fb4d041f3dc8c2 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Wed, 1 Dec 2021 10:40:04 -0500 Subject: [PATCH 35/41] shorten the additional ligands --- Exemplars/DrugDesign/DDPyjama.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index 272bf9e..e199efe 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -6,7 +6,7 @@ public class DDPyjama { static String[] cannedLigands = {"razvex", "qudgy", "afrs", "sst", "pgfht", "rt", "id", "how", "aaddh", "df", "os", "hid", - "sad", "fl", "rd", "edp", "dfgt", "spa", "qdsayjbjp", "txtzunerdjenklh"}; + "sad", "fl", "rd", "edp", "dfgt", "spa", "qdsayjbp", "zunerd"}; // Ligand Score pair static class LSPair { From 323c1b3b32ed9e16a0ee47e37a97a20b86fc4a93 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 5 Dec 2021 23:52:01 -0500 Subject: [PATCH 36/41] Let generateLigands to mix canned and random ligands --- Exemplars/DrugDesign/DDPyjama.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index e199efe..ff6f643 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -36,21 +36,19 @@ static String makeLigand(int maxLigandLength) { } private static String[] generateLigands(int numLigands, int maxLigandLength, boolean useCanned) { - // If numLigands <=18, create a pre-determined set of example ligands. - // Otherwise, create a set of ligands whose length randomly varies from 2 + // If numLigands <=cannedLigands.length, create a pre-determined set of example ligands. + // Otherwise, create a set of ligands whose length randomly varies from 1 // to args.maxLigand String[] result = new String[numLigands]; - if (useCanned && numLigands <= cannedLigands.length) { - for(int i = 0; i < numLigands; i++) { + if (useCanned) { + for(int i = 0; i < Math.min(numLigands, cannedLigands.length); i++) { result[i] = cannedLigands[i]; } - return result; - } - for(int i = 0; i < numLigands; i++) { + for(int i = useCanned ? cannedLigands.length : 0; i < numLigands; i++) { result[i] = makeLigand(maxLigandLength); } return result; From 0a57cabbb443c7b065d1d9e5cb40b277ee1d4560 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 5 Dec 2021 23:58:06 -0500 Subject: [PATCH 37/41] Removed the additional longer ligands. Fixed comments. --- Exemplars/DrugDesign/DDPyjama.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index ff6f643..d31b45b 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -36,9 +36,8 @@ static String makeLigand(int maxLigandLength) { } private static String[] generateLigands(int numLigands, int maxLigandLength, boolean useCanned) { - // If numLigands <=cannedLigands.length, create a pre-determined set of example ligands. - // Otherwise, create a set of ligands whose length randomly varies from 1 - // to args.maxLigand + // If we use canned ligands, use as many of them as we can, then fill the rest with randomly generated ligands. + // Otherwise, create a set of ligands whose length randomly varies from 1 to args.maxLigand String[] result = new String[numLigands]; From 7061576c07bef24aba113c66c5386a2771e2e360 Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Sun, 5 Dec 2021 23:58:40 -0500 Subject: [PATCH 38/41] Removed the additional longer ligands --- Exemplars/DrugDesign/DDPyjama.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index d31b45b..4807a48 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -6,7 +6,7 @@ public class DDPyjama { static String[] cannedLigands = {"razvex", "qudgy", "afrs", "sst", "pgfht", "rt", "id", "how", "aaddh", "df", "os", "hid", - "sad", "fl", "rd", "edp", "dfgt", "spa", "qdsayjbp", "zunerd"}; + "sad", "fl", "rd", "edp", "dfgt", "spa"}; // Ligand Score pair static class LSPair { From e88a186e76977a2640b64f97b099a9513be36b5f Mon Sep 17 00:00:00 2001 From: Ruth Kurniawati Date: Mon, 6 Dec 2021 00:01:48 -0500 Subject: [PATCH 39/41] Fixed usage instruction --- Exemplars/DrugDesign/DDPyjama.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exemplars/DrugDesign/DDPyjama.java b/Exemplars/DrugDesign/DDPyjama.java index 4807a48..333ef89 100644 --- a/Exemplars/DrugDesign/DDPyjama.java +++ b/Exemplars/DrugDesign/DDPyjama.java @@ -59,7 +59,7 @@ public static void main(String[] args) { System.out.println("Usage DDPyjama numThreads numLigands maxLigandLength protein useCanned printLigands"); // the example string below is one of Dijkstra's famous quotes - System.out.println(" Example: java DDPyjama 4 10 8 \"Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it\" false true\n"); + System.out.println(" Example: java -cp .:Pyjama.jar DDPyjama 4 10 8 \"Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it\" false true\n"); } int numThreads = 4; From 924242a46629a442b205cf8bfc05098f36c8ccef Mon Sep 17 00:00:00 2001 From: Ruth K Date: Tue, 15 Mar 2022 21:56:17 -0400 Subject: [PATCH 40/41] don't create zero length ligands --- Exemplars/DrugDesign/DDPyjama$LSPair.class | Bin 0 -> 910 bytes Exemplars/DrugDesign/DDPyjama.class | Bin 0 -> 4035 bytes Exemplars/DrugDesign/DDPyjama.java | 1 + 3 files changed, 1 insertion(+) create mode 100644 Exemplars/DrugDesign/DDPyjama$LSPair.class create mode 100644 Exemplars/DrugDesign/DDPyjama.class diff --git a/Exemplars/DrugDesign/DDPyjama$LSPair.class b/Exemplars/DrugDesign/DDPyjama$LSPair.class new file mode 100644 index 0000000000000000000000000000000000000000..395698449f4c31162c606611f4147da87bb2fe64 GIT binary patch literal 910 zcmaJYtP&Ksg zc%4dEoHda{p0F6SlrITXv$zq<#1fVXPx_)KtIBH${n^o1{1s^7>430ToaL*PW@Cia zxyTDbysFy#$6Vd<WJBx*SPr6?I5hF3#mlH`LH?$zvykXYPkS3@7CBQyJ(J z6=(yS?A>R1nIDwWf%e7VqtsWb9pn;tk;Y5BGEfL7-XRnow&Zx%swc})E71~;MN2Dx zMEE*A)C3rjp76F^85HVD^@fAW>_(;{{}i5&267Jkata?wpfbIZ2}M=7Zo|>ClEfZA z<|Q4_7lK*yJn2_m5d@OoED?#qz&>GTX3lx-8+c9F{69409nXqw)>Dkt6CRc$TZH0b z8v{3m6}GGFy$LlQ=IkFJd;b=Oy?%?#cMh@S*jk9Q&9i-s6nj~I8^TkpO$c_!gh1B5 z;o2Ky?ih$~f9n&B12e_(jPDqpV|{#YV2kf0XSez6qR5`b`6M<_Vr%07hgkaqD8Jn8 literal 0 HcmV?d00001 diff --git a/Exemplars/DrugDesign/DDPyjama.class b/Exemplars/DrugDesign/DDPyjama.class new file mode 100644 index 0000000000000000000000000000000000000000..1a68877e7a51b3886e5708f4a3ef29b28c8fac38 GIT binary patch literal 4035 zcmaJ^YgAm-75)x0%)N8x^2o)(3=q7DQDO2RVnhXk8lr|2lIUO}Nn5$hT$r28D>HWp zSgSUT_0gBLwZ_=0iBwZrh(!Vd zO2(dY2Qoa&-!{-bpx^?53l}Vjxp^luv9;)=5_Vog1ALNPsADA}w9m@rYU6=i(^Q@<(wpJZRwvuBn;vv|Y1>NosJK!fc5A_!u+2*R%uF#o zoFqF@kkj(T%(OLCnhx5T2{&ox@>$n*GG?)0_j?}T7b-4`Yq$zmE4W5SH+tw4@5bjJ z&RBapQoMS&ZRyS0_o&z+@U&@~H%wXST*~e?r5bZXJZEm~E+yN>16F>)^3CSuo6M`t zH8F?cPTX;)O{ZX5<^*r!nv+i6E!yT-(KQ{{%-e^GPTnqs@ylU7c{XVB6(EmP8%%Wn{;fK8FQh` znEq^rI`%qllCy$qW!!=Rftw5#IEfE~QhG?>(&gN~G!inHyJRE-8nf+Y9k(FHTgHp| zyq$4}owPmVq*4x(pSZ9?#jPw}&o5}jULCh#gr!M$(_tS7bqQE{+V{z<->>7t_=rH= zgq^YTbaOeE1Xi*#_O&llSSoI3;cmC-A*<}ARH+aZ5Qyq3IlXo;w%c+D7%JjA60ik= zaVr&%#pzsu%a-xOQa8xj9L0o$N7iiPGJ6Kv_sCK|pyQydb02Fa$Sr9d8Dv@Dh0<|? ziW+c8M;>%YAmtU5=R9UXQJ`wQDFqHoRGSjmu*^wIk)Vu;J@UXibbJhVO4Xh--iP8v z5Y}VBFO#7fg9ZY)M`pw)WOQ~cRZaYA#UV+K>bMu5qT)EK=K3;7_e;+GIv$V-7%UXW z3LZ#TGcDy$RD%9zrPn?uL4Aj;>xZZ!Z{0CzPpNpAjyY6JOiZiz5(TXBd_l#R>5f9d zRdI~m+{Ad2)8q6^p5s?IItdj|P$ro@tl}xkSXLsDRB@8L#JGyDam-TBvrr1j$!wxf7r>7+{1`t`@KYJfp9!p8fFUO{nLTKC zlv-U09&WVaZZseY6i@uzXhO8;G^vx_4m&f@v8|v;- z*YeiCVYG&ctE^HdT}+!^b;^c8{l6oWUSH}333F_^tgWrkE}4H|ozTj7F;(sjL_kxR zX~sn#;A0@)pRx)CTh?|7cNF|Z;F2ZbSkB7|{wC1+|3#%nzJ zLYzgz$Qgu(e0{5$Poimky+x7wCQ6JxJcGu)M}6YCb8oJnLDLJ|U*(<93Wf7lXt;<^ zzSW4(Jd*}q&haX&#dpcAhwmM%5bw&@WP^Ac&6H*%DBE*_)<`HBYI+5&J6j&Z#l9|o z*xz&-=KL(yoa;;jt}240FFm9OIw z8#voY13PHmMzrG!zKJ*E296&>muF}r0`EdsFhe6-wClt>5S%q|-x*jrw~Xt(Ms%e9~h&F@V69`2#!=1t8-iTIaPs`QO5N&u}-cy?-bvTh7DR@-y9+gr<0C?)C~V-EB%dcAB{*P@Y6xs~W=>TY94zljg` z?UWe6l^Dd;7(yR*GBDh}YE zd>Z_l<6Ag{|L_6uUp9P!NutVOv4Y)xHKs+B-!|bcaSiSkTXB!r#m;;?j)*kZ1-9QQ z+$ZkE{o)ZkAfCa4UT$Vl*Z2-%Sbgg_97nb9RMa=&5LBTKdj!OovnNN2H0d03IPcJWBjJ#`-*tO?aHKIqu!K z8opZ5LhL&GKFl5SkPEo;ENVwCz=LP-nU@Gr29aaLXM8?3;y1n!8>u$F7#j%~kHkh4 zc(@VHO4ncYmIM_))~)})*Ig; zU17W!dkN+Oy2}5jL5pf=pC8}%F7^uKKihu#ZImei;;w=pa`khM{=%cLdGvLUzTwf| Zdi3}BBX`J0{3vhzC;t5zf5qPs`5%1z Date: Tue, 15 Mar 2022 21:56:44 -0400 Subject: [PATCH 41/41] remove *.class files --- Exemplars/DrugDesign/DDPyjama$LSPair.class | Bin 910 -> 0 bytes Exemplars/DrugDesign/DDPyjama.class | Bin 4035 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Exemplars/DrugDesign/DDPyjama$LSPair.class delete mode 100644 Exemplars/DrugDesign/DDPyjama.class diff --git a/Exemplars/DrugDesign/DDPyjama$LSPair.class b/Exemplars/DrugDesign/DDPyjama$LSPair.class deleted file mode 100644 index 395698449f4c31162c606611f4147da87bb2fe64..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 910 zcmaJYtP&Ksg zc%4dEoHda{p0F6SlrITXv$zq<#1fVXPx_)KtIBH${n^o1{1s^7>430ToaL*PW@Cia zxyTDbysFy#$6Vd<WJBx*SPr6?I5hF3#mlH`LH?$zvykXYPkS3@7CBQyJ(J z6=(yS?A>R1nIDwWf%e7VqtsWb9pn;tk;Y5BGEfL7-XRnow&Zx%swc})E71~;MN2Dx zMEE*A)C3rjp76F^85HVD^@fAW>_(;{{}i5&267Jkata?wpfbIZ2}M=7Zo|>ClEfZA z<|Q4_7lK*yJn2_m5d@OoED?#qz&>GTX3lx-8+c9F{69409nXqw)>Dkt6CRc$TZH0b z8v{3m6}GGFy$LlQ=IkFJd;b=Oy?%?#cMh@S*jk9Q&9i-s6nj~I8^TkpO$c_!gh1B5 z;o2Ky?ih$~f9n&B12e_(jPDqpV|{#YV2kf0XSez6qR5`b`6M<_Vr%07hgkaqD8Jn8 diff --git a/Exemplars/DrugDesign/DDPyjama.class b/Exemplars/DrugDesign/DDPyjama.class deleted file mode 100644 index 1a68877e7a51b3886e5708f4a3ef29b28c8fac38..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4035 zcmaJ^YgAm-75)x0%)N8x^2o)(3=q7DQDO2RVnhXk8lr|2lIUO}Nn5$hT$r28D>HWp zSgSUT_0gBLwZ_=0iBwZrh(!Vd zO2(dY2Qoa&-!{-bpx^?53l}Vjxp^luv9;)=5_Vog1ALNPsADA}w9m@rYU6=i(^Q@<(wpJZRwvuBn;vv|Y1>NosJK!fc5A_!u+2*R%uF#o zoFqF@kkj(T%(OLCnhx5T2{&ox@>$n*GG?)0_j?}T7b-4`Yq$zmE4W5SH+tw4@5bjJ z&RBapQoMS&ZRyS0_o&z+@U&@~H%wXST*~e?r5bZXJZEm~E+yN>16F>)^3CSuo6M`t zH8F?cPTX;)O{ZX5<^*r!nv+i6E!yT-(KQ{{%-e^GPTnqs@ylU7c{XVB6(EmP8%%Wn{;fK8FQh` znEq^rI`%qllCy$qW!!=Rftw5#IEfE~QhG?>(&gN~G!inHyJRE-8nf+Y9k(FHTgHp| zyq$4}owPmVq*4x(pSZ9?#jPw}&o5}jULCh#gr!M$(_tS7bqQE{+V{z<->>7t_=rH= zgq^YTbaOeE1Xi*#_O&llSSoI3;cmC-A*<}ARH+aZ5Qyq3IlXo;w%c+D7%JjA60ik= zaVr&%#pzsu%a-xOQa8xj9L0o$N7iiPGJ6Kv_sCK|pyQydb02Fa$Sr9d8Dv@Dh0<|? ziW+c8M;>%YAmtU5=R9UXQJ`wQDFqHoRGSjmu*^wIk)Vu;J@UXibbJhVO4Xh--iP8v z5Y}VBFO#7fg9ZY)M`pw)WOQ~cRZaYA#UV+K>bMu5qT)EK=K3;7_e;+GIv$V-7%UXW z3LZ#TGcDy$RD%9zrPn?uL4Aj;>xZZ!Z{0CzPpNpAjyY6JOiZiz5(TXBd_l#R>5f9d zRdI~m+{Ad2)8q6^p5s?IItdj|P$ro@tl}xkSXLsDRB@8L#JGyDam-TBvrr1j$!wxf7r>7+{1`t`@KYJfp9!p8fFUO{nLTKC zlv-U09&WVaZZseY6i@uzXhO8;G^vx_4m&f@v8|v;- z*YeiCVYG&ctE^HdT}+!^b;^c8{l6oWUSH}333F_^tgWrkE}4H|ozTj7F;(sjL_kxR zX~sn#;A0@)pRx)CTh?|7cNF|Z;F2ZbSkB7|{wC1+|3#%nzJ zLYzgz$Qgu(e0{5$Poimky+x7wCQ6JxJcGu)M}6YCb8oJnLDLJ|U*(<93Wf7lXt;<^ zzSW4(Jd*}q&haX&#dpcAhwmM%5bw&@WP^Ac&6H*%DBE*_)<`HBYI+5&J6j&Z#l9|o z*xz&-=KL(yoa;;jt}240FFm9OIw z8#voY13PHmMzrG!zKJ*E296&>muF}r0`EdsFhe6-wClt>5S%q|-x*jrw~Xt(Ms%e9~h&F@V69`2#!=1t8-iTIaPs`QO5N&u}-cy?-bvTh7DR@-y9+gr<0C?)C~V-EB%dcAB{*P@Y6xs~W=>TY94zljg` z?UWe6l^Dd;7(yR*GBDh}YE zd>Z_l<6Ag{|L_6uUp9P!NutVOv4Y)xHKs+B-!|bcaSiSkTXB!r#m;;?j)*kZ1-9QQ z+$ZkE{o)ZkAfCa4UT$Vl*Z2-%Sbgg_97nb9RMa=&5LBTKdj!OovnNN2H0d03IPcJWBjJ#`-*tO?aHKIqu!K z8opZ5LhL&GKFl5SkPEo;ENVwCz=LP-nU@Gr29aaLXM8?3;y1n!8>u$F7#j%~kHkh4 zc(@VHO4ncYmIM_))~)})*Ig; zU17W!dkN+Oy2}5jL5pf=pC8}%F7^uKKihu#ZImei;;w=pa`khM{=%cLdGvLUzTwf| Zdi3}BBX`J0{3vhzC;t5zf5qPs`5%1z