From 7d9696dd29422cddccfbc1ccc0b6013b9b5a7a8f Mon Sep 17 00:00:00 2001 From: "Dominic A. Sirianni" Date: Thu, 17 Aug 2017 19:00:52 -0400 Subject: [PATCH] DAS code for assignment5 --- Week05/assignments_sirianni/Makefile | 27 +++++++++ Week05/assignments_sirianni/assignment1.cc | 26 +++++++++ Week05/assignments_sirianni/assignment2.cc | 41 ++++++++++++++ Week05/assignments_sirianni/assignment3.cc | 40 ++++++++++++++ Week05/assignments_sirianni/assignment4.cc | 61 +++++++++++++++++++++ Week05/assignments_sirianni/assignment5.cc | 64 ++++++++++++++++++++++ Week05/assignments_sirianni/timer.cc | 10 ++++ Week05/assignments_sirianni/timer.h | 1 + 8 files changed, 270 insertions(+) create mode 100644 Week05/assignments_sirianni/Makefile create mode 100644 Week05/assignments_sirianni/assignment1.cc create mode 100644 Week05/assignments_sirianni/assignment2.cc create mode 100644 Week05/assignments_sirianni/assignment3.cc create mode 100644 Week05/assignments_sirianni/assignment4.cc create mode 100644 Week05/assignments_sirianni/assignment5.cc create mode 100644 Week05/assignments_sirianni/timer.cc create mode 100644 Week05/assignments_sirianni/timer.h diff --git a/Week05/assignments_sirianni/Makefile b/Week05/assignments_sirianni/Makefile new file mode 100644 index 0000000..43a945a --- /dev/null +++ b/Week05/assignments_sirianni/Makefile @@ -0,0 +1,27 @@ + +# Edit these according to your environment +COMPILER=icpc # icpc or g++ +VERSION=-std=c++11 # -std=c++11 or -std=gnu++11 +OPENMP=-qopenmp # -qopenmp or -fopenmp + +RPATH=-Wl,-rpath,${CONDA_PREFIX}/lib + +a1: assignment1.cc timer.cc + $(COMPILER) $(VERSION) $(OPENMP) $(RPATH) assignment1.cc timer.cc -o a1 + +a2: assignment2.cc timer.cc + $(COMPILER) $(VERSION) $(OPENMP) $(RPATH) assignment2.cc timer.cc -o a2 + +a3: assignment3.cc timer.cc + $(COMPILER) $(VERSION) $(OPENMP) $(RPATH) assignment3.cc timer.cc -o a3 + +a4: assignment4.cc timer.cc + $(COMPILER) $(VERSION) $(OPENMP) $(RPATH) assignment4.cc timer.cc -o a4 + +a5: assignment5.cc timer.cc + $(COMPILER) $(VERSION) $(OPENMP) $(RPATH) assignment5.cc timer.cc -o a5 + +clean: + rm -f *.o a1 a2 a3 a4 a5 + +.PHONY: clean diff --git a/Week05/assignments_sirianni/assignment1.cc b/Week05/assignments_sirianni/assignment1.cc new file mode 100644 index 0000000..5c6d758 --- /dev/null +++ b/Week05/assignments_sirianni/assignment1.cc @@ -0,0 +1,26 @@ +// Assignment 1 - Hello World! +// IDEaS Workshop Week 5: HPC + +//#include +//#include +#include +#include +//#include +#include + +int main() +{ + // OpenMP function + int nthreads = omp_get_max_threads(); + + // OpenMP pragma and clause + #pragma omp parallel num_threads(nthreads) + { + + int thread_id = omp_get_thread_num(); +// cout << "HI" << endl; + printf("Hello World! ~ from thread %d\n", thread_id); + + } + +} diff --git a/Week05/assignments_sirianni/assignment2.cc b/Week05/assignments_sirianni/assignment2.cc new file mode 100644 index 0000000..c3ce663 --- /dev/null +++ b/Week05/assignments_sirianni/assignment2.cc @@ -0,0 +1,41 @@ +// Assignment 2 - Basic for loop parallelization +// IDEaS Workshop Week 5: HPC + +#include +#include +#include +#include +#include "timer.h" + +#define ITERATIONS 100 +#define WORK_TIME 1000 + +int main() +{ + // setup ~ + int max_threads = omp_get_max_threads(); + double times[max_threads]; + + for(int m = 0; m < max_threads; m++){ + + // get nthreads ~ use this in your pragma! + int nthreads = m + 1; + + double t0 = time_in_seconds(); + + // ==> Drop a pragma on the loop below! <== // + #pragma omp parallel for num_threads(nthreads) + for(int i = 0; i < ITERATIONS; i++){ + // emulate work + usleep(WORK_TIME); + } + + // ==> Drop a pragma on the loop above! <== // + + double t1 = time_in_seconds(); + times[m] = t1 - t0; + printf("(%d): Time: %f, Speedup: %f\n", m + 1, times[m], + times[0]/times[m]); + } + +} diff --git a/Week05/assignments_sirianni/assignment3.cc b/Week05/assignments_sirianni/assignment3.cc new file mode 100644 index 0000000..de5cbdd --- /dev/null +++ b/Week05/assignments_sirianni/assignment3.cc @@ -0,0 +1,40 @@ +// Assignment 3 - Scheduling to balance workloads +// IDEaS Workshop Week 5: HPC + +#include +#include +#include +#include +#include "timer.h" + +#define ITERATIONS 3000 + +int main() +{ + // setup ~ + int max_threads = omp_get_max_threads(); + double times[max_threads]; + + for(int m = 0; m < max_threads; m++){ + + // get nthreads ~ use this in your pragma! + int nthreads = m + 1; + + double t0 = time_in_seconds(); + + // ==> Drop a pragma and schedule clause to the loop below! <== // + #pragma omp parallel for num_threads(nthreads) schedule(dynamic) + for(int i = 0; i < ITERATIONS; i++){ + // emulate work + usleep(i); + } + + // ==> Drop a pragma and schedule clause to the loop above! <== // + + double t1 = time_in_seconds(); + times[m] = t1 - t0; + printf("(%d): Time: %f, Speedup: %f\n", m + 1, times[m], + times[0]/times[m]); + } + +} diff --git a/Week05/assignments_sirianni/assignment4.cc b/Week05/assignments_sirianni/assignment4.cc new file mode 100644 index 0000000..54bc2ec --- /dev/null +++ b/Week05/assignments_sirianni/assignment4.cc @@ -0,0 +1,61 @@ +// Assignment 4 - Resource sharing with prefix sum +// IDEaS Workshop Week 5: HPC + +#include +#include +#include +#include +#include +#include +#include +#include "timer.h" + +// N > max_threads +#define N 10000000 + +int main() +{ + // setup ~ + int max_threads = omp_get_max_threads(); + double times[max_threads]; + + for(int m = 0; m < max_threads; m++){ + + // init sum + unsigned long long int sum = 0; + + int nthreads = m + 1; + double t0 = time_in_seconds(); + + // ==> Improve strategy below! <== // + + // determine blocking + std::vector> blocks; + for(int i = 0, start = 1, size; i < nthreads; i++, start += size){ + size = ((N % nthreads) / (i + 1) ? ceil((double)N / nthreads) : + floor((double)N / nthreads)); + blocks.push_back(std::make_pair(start, start + size)); + } + + #pragma omp parallel num_threads(nthreads) + { + // get block info + int id = omp_get_thread_num(); + size_t start = std::get<0>(blocks[id]); + size_t stop = std::get<1>(blocks[id]); + + #pragma omp parallel for reduction(+:sum) + for(int i = start; i < stop; i++){ + sum += 1; + } + + } + + // ==> Improve strategy above! <== // + + double t1 = time_in_seconds(); + times[m] = t1 - t0; + printf("(%d): Answer: %llu, Time: %f, Speedup: %f\n", m, sum, times[m], + times[0]/times[m]); + } +} diff --git a/Week05/assignments_sirianni/assignment5.cc b/Week05/assignments_sirianni/assignment5.cc new file mode 100644 index 0000000..3462c1e --- /dev/null +++ b/Week05/assignments_sirianni/assignment5.cc @@ -0,0 +1,64 @@ +// Assignment 5 - Summing across one dimension of a matrix +// IDEaS Workshop Week 5: HPC + +#include +#include +#include +#include +#include +#include +#include +#include "timer.h" + +// N > max_threads +#define N 20000 + +// How much RAM? +#define MEMORY 50 + +int main() +{ + // memory check + if((double)(N / 1e9) * N * 4 > MEMORY * 0.8){ + printf("Not enough memory! ~~ "); + printf("Needed: %6.3fGB\n", N / 1e9 * N * 4 / 0.8); + exit(1); + } + + // setup ~ + int max_threads = omp_get_max_threads(); + double times[max_threads]; + + // NxN matrix to be collapsed to N vector (initialized to all 1s) + std::vector> mat(N, std::vector(N, 1)); + + for(int m = 0; m < max_threads; m++){ + + int nthreads = m + 1; + double t0 = time_in_seconds(); + + // write answer to this vec (initialized to all 0s) + std::vector vec(N, 0); + + // ==> implement solution below! <== // + + #pragma omp parallel for collapse(2) num_threads(nthreads) + for(int k = 0; k < N; k++){ + for(int l = 0; l < N; l++){ + vec[k] += mat[k][l]; + } + } + + // ==> implement solution above! <== // + + //check solution + bool correct = 0; + for(int i = 0; i < N; i++) + correct = (vec[i] == N ? true : false); + + double t1 = time_in_seconds(); + times[m] = t1 - t0; + printf("(%d): Correct? %d, Time: %f, Speedup: %f\n", m, correct, times[m], + times[0]/times[m]); + } +} diff --git a/Week05/assignments_sirianni/timer.cc b/Week05/assignments_sirianni/timer.cc new file mode 100644 index 0000000..caad2be --- /dev/null +++ b/Week05/assignments_sirianni/timer.cc @@ -0,0 +1,10 @@ +#include "timer.h" +#include +#include + +double time_in_seconds() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; +} diff --git a/Week05/assignments_sirianni/timer.h b/Week05/assignments_sirianni/timer.h new file mode 100644 index 0000000..5b52c36 --- /dev/null +++ b/Week05/assignments_sirianni/timer.h @@ -0,0 +1 @@ +double time_in_seconds();