Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Week05/assignments_sirianni/Makefile
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions Week05/assignments_sirianni/assignment1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Assignment 1 - Hello World!
// IDEaS Workshop Week 5: HPC

//#include <cxxabi.h>
//#include <cstdio>
#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
#include <omp.h>

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);

}

}
41 changes: 41 additions & 0 deletions Week05/assignments_sirianni/assignment2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Assignment 2 - Basic for loop parallelization
// IDEaS Workshop Week 5: HPC

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
#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]);
}

}
40 changes: 40 additions & 0 deletions Week05/assignments_sirianni/assignment3.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Assignment 3 - Scheduling to balance workloads
// IDEaS Workshop Week 5: HPC

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
#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]);
}

}
61 changes: 61 additions & 0 deletions Week05/assignments_sirianni/assignment4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Assignment 4 - Resource sharing with prefix sum
// IDEaS Workshop Week 5: HPC

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
#include <vector>
#include <tuple>
#include <math.h>
#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<std::pair<size_t, size_t>> 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]);
}
}
64 changes: 64 additions & 0 deletions Week05/assignments_sirianni/assignment5.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Assignment 5 - Summing across one dimension of a matrix
// IDEaS Workshop Week 5: HPC

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
#include <vector>
#include <tuple>
#include <math.h>
#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<std::vector<int>> mat(N, std::vector<int>(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<int> 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]);
}
}
10 changes: 10 additions & 0 deletions Week05/assignments_sirianni/timer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "timer.h"
#include <stdlib.h>
#include <sys/time.h>

double time_in_seconds()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
}
1 change: 1 addition & 0 deletions Week05/assignments_sirianni/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
double time_in_seconds();