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
10 changes: 5 additions & 5 deletions OpenMP2/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# run.sh <number of repetitions>
compiler="g++"
flags="-fopenmp -std=c++11"
flags="-Xclang -fopenmp -std=c++11 -lomp"
src="./src/main.cpp"
build="./build"
exe="$build/task"
Expand Down Expand Up @@ -28,9 +28,9 @@ for test_dir in $tests_dir/*; do
test=$(basename $test_dir)
printf "\n[TEST $test]\n"
echo " $exe $test_dir/input.txt $build/$test.txt"
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/$test.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$((($END - $START)/1000000))
if [ ! $? -eq 0 ]; then
echo "[TEST $test] RUNTIME FAIL"
Expand All @@ -41,9 +41,9 @@ for test_dir in $tests_dir/*; do
RES="OK"
if [ -n "$1" ]; then
for ((i=1; i < $1; i++)); do
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/${test}_$i.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$(($DIFF + ($END - $START)/1000000))
if ! cmp -s $build/${test}_$i.txt $test_dir/output.txt; then
RES="FAIL"
Expand Down
77 changes: 40 additions & 37 deletions OpenMP2/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,50 @@
#include <fstream>
#include <omp.h>

double calc(uint32_t x_last, uint32_t num_threads)
{
return 0;
double calc(uint32_t x_last, uint32_t num_threads) {
double res = 0.0;
#pragma omp parallel for num_threads(num_threads)
{
for (int i = x_last; i > 0; i--) {
double f = i;
res += 1.0 / f;
}
}
return res;
}

int main(int argc, char** argv)
{
// Check arguments
if (argc != 3)
{
std::cout << "[Error] Usage <inputfile> <output file>\n";
return 1;
}
int main(int argc, char **argv) {
// Check arguments
if (argc != 3) {
std::cout << "[Error] Usage <inputfile> <output file>\n";
return 1;
}

// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open())
{
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
return 1;
}
// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open()) {
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
return 1;
}

// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open())
{
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
input.close();
return 1;
}
// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open()) {
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
input.close();
return 1;
}

// Read arguments from input
uint32_t x_last = 0, num_threads = 0;
input >> x_last >> num_threads;
// Calculation
double res = calc(x_last, num_threads);
// Read arguments from input
uint32_t x_last = 0, num_threads = 0;
input >> x_last >> num_threads;
// Calculation
double res = calc(x_last, num_threads);

// Write result
output << std::setprecision(15) << res << std::endl;
// Prepare to exit
output.close();
input.close();
return 0;
// Write result
output << std::setprecision(15) << res << std::endl;
// Prepare to exit
output.close();
input.close();
return 0;
}
10 changes: 5 additions & 5 deletions OpenMP3/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# run.sh <number of repetitions>
compiler="g++"
flags="-fopenmp -std=c++11"
flags="-Xclang -fopenmp -std=c++11 -lomp"
src="./src/main.cpp"
build="./build"
exe="$build/task"
Expand Down Expand Up @@ -28,9 +28,9 @@ for test_dir in $tests_dir/*; do
test=$(basename $test_dir)
printf "\n[TEST $test]\n"
echo " $exe $test_dir/input.txt $build/$test.txt"
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/$test.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$((($END - $START)/1000000))
if [ ! $? -eq 0 ]; then
echo "[TEST $test] RUNTIME FAIL"
Expand All @@ -41,9 +41,9 @@ for test_dir in $tests_dir/*; do
RES="OK"
if [ -n "$1" ]; then
for ((i=1; i < $1; i++)); do
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/${test}_$i.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$(($DIFF + ($END - $START)/1000000))
if ! cmp -s $build/${test}_$i.txt $test_dir/output.txt; then
RES="FAIL"
Expand Down
31 changes: 29 additions & 2 deletions OpenMP3/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@
#include <iomanip>
#include <fstream>
#include <omp.h>
#include <math.h>
double func(double x) {
return sin(x);
}

double create_task(float a, float b, float dx) {
double res = 0;
int len = (int) ((b - a) / dx);
for (int i = 0; i < len; i++) {
double x = a + (double) i * dx + dx / 2;
res += dx * func(x);
}
return res;
}

double calc(double x0, double x1, double dx, uint32_t num_threads)
{
return 0;
float dx_thread = (x1 - x0) / num_threads;
int len = (int) ((x1 - x0) / dx);
double res = 0;
#pragma omp parallel for num_threads(num_threads) reduction(+:res)
{
for (int i = 0; i < num_threads; i++) {
res += create_task(x0 + i * dx_thread, x0 + (i + 1) * dx_thread, dx);
}
// for (int i = 0; i < len; i++) {
// double x = x0 + (double) i * dx + dx / 2;
// res += dx * func(x);
// }
}
return round(res);
}

int main(int argc, char** argv)
Expand Down Expand Up @@ -43,7 +70,7 @@ int main(int argc, char** argv)
double res = calc(x0, x1, dx, num_threads);

// Write result
output << std::setprecision(15) << res;
output << std::setprecision(15) << res << std::endl;
// Prepare to exit
output.close();
input.close();
Expand Down
10 changes: 5 additions & 5 deletions OpenMP4/run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# run.sh <number of repetitions>
compiler="g++"
flags="-fopenmp -std=c++11"
flags="-Xclang -fopenmp -std=c++11 -lomp"
src="./src/main.cpp"
build="./build"
exe="$build/task"
Expand Down Expand Up @@ -28,9 +28,9 @@ for test_dir in $tests_dir/*; do
test=$(basename $test_dir)
printf "\n[TEST $test]\n"
echo " $exe $test_dir/input.txt $build/$test.txt"
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/$test.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$((($END - $START)/1000000))
if [ ! $? -eq 0 ]; then
echo "[TEST $test] RUNTIME FAIL"
Expand All @@ -41,9 +41,9 @@ for test_dir in $tests_dir/*; do
RES="OK"
if [ -n "$1" ]; then
for ((i=1; i < $1; i++)); do
START=$(date +%s%N)
START=$(gdate +%s%N)
$exe $test_dir/input.txt $build/${test}_$i.txt
END=$(date +%s%N)
END=$(gdate +%s%N)
DIFF=$(($DIFF + ($END - $START)/1000000))
if ! cmp -s $build/${test}_$i.txt $test_dir/output.txt; then
RES="FAIL"
Expand Down
83 changes: 48 additions & 35 deletions OpenMP4/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,59 @@
#include <fstream>
#include <omp.h>

double calc()
{
return 0;
double calc(uint32_t x_last, uint32_t num_threads) {
double fct = 1;
double *facts = (double *) malloc(sizeof(double) * x_last);
facts[0] = 1;
double start = omp_get_wtime();
for (int i = 1; i <= x_last; i++) {
fct *= (double) i;
facts[i] = fct;
}
double res = 0;
#pragma omp parallel for num_threads(num_threads) reduction(+:res)
{
for (int i = x_last - 1; i >= 0; i--) {
res += 1 / facts[i];
}
}
free(facts);
return res;
}

int main(int argc, char** argv)
{
// Check arguments
if (argc != 3)
{
std::cout << "[Error] Usage <inputfile> <output file>\n";
return 1;
}
int main(int argc, char **argv) {
// Check arguments
if (argc != 3) {
std::cout << "[Error] Usage <inputfile> <output file>\n";
return 1;
}

// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open())
{
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
return 1;
}
// Prepare input file
std::ifstream input(argv[1]);
if (!input.is_open()) {
std::cout << "[Error] Can't open " << argv[1] << " for write\n";
return 1;
}

// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open())
{
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
input.close();
return 1;
}
// Prepare output file
std::ofstream output(argv[2]);
if (!output.is_open()) {
std::cout << "[Error] Can't open " << argv[2] << " for read\n";
input.close();
return 1;
}

// Read arguments from input
// Read arguments from input
uint32_t x_last = 0, num_threads = 0;
input >> x_last >> num_threads;

// Calculation
double res = calc();
// Calculation
double res = calc(x_last, num_threads);

// Write result
output << std::setprecision(15) << res;
// Prepare to exit
output.close();
input.close();
return 0;
// Write result
output << std::setprecision(15) << res << std::endl;
// Prepare to exit
output.close();
input.close();
return 0;
}
1 change: 1 addition & 0 deletions OpenMP4/tests/00/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 1
1 change: 1 addition & 0 deletions OpenMP4/tests/00/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions OpenMP4/tests/01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 8
1 change: 1 addition & 0 deletions OpenMP4/tests/01/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions OpenMP4/tests/02/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 1
1 change: 1 addition & 0 deletions OpenMP4/tests/02/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions OpenMP4/tests/03/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 8
1 change: 1 addition & 0 deletions OpenMP4/tests/03/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions OpenMP4/tests/04/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10000 1
1 change: 1 addition & 0 deletions OpenMP4/tests/04/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.71828182845905
1 change: 1 addition & 0 deletions OpenMP4/tests/05/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10000 8
1 change: 1 addition & 0 deletions OpenMP4/tests/05/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.71828182845905
1 change: 1 addition & 0 deletions OpenMP4/tests/06/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100000000 1
1 change: 1 addition & 0 deletions OpenMP4/tests/06/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.71828182845905
1 change: 1 addition & 0 deletions OpenMP4/tests/07/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100000000 8
1 change: 1 addition & 0 deletions OpenMP4/tests/07/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.71828182845905