diff --git a/OpenMP2/run.sh b/OpenMP2/run.sh index f1405e7..93219fc 100755 --- a/OpenMP2/run.sh +++ b/OpenMP2/run.sh @@ -1,6 +1,6 @@ # run.sh compiler="g++" -flags="-fopenmp -std=c++11" +flags="-Xclang -fopenmp -std=c++11 -lomp" src="./src/main.cpp" build="./build" exe="$build/task" @@ -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" @@ -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" diff --git a/OpenMP2/src/main.cpp b/OpenMP2/src/main.cpp index 8dab215..bc0c25f 100644 --- a/OpenMP2/src/main.cpp +++ b/OpenMP2/src/main.cpp @@ -3,47 +3,50 @@ #include #include -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 \n"; - return 1; - } +int main(int argc, char **argv) { + // Check arguments + if (argc != 3) { + std::cout << "[Error] Usage \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; } diff --git a/OpenMP3/run.sh b/OpenMP3/run.sh index f1405e7..93219fc 100755 --- a/OpenMP3/run.sh +++ b/OpenMP3/run.sh @@ -1,6 +1,6 @@ # run.sh compiler="g++" -flags="-fopenmp -std=c++11" +flags="-Xclang -fopenmp -std=c++11 -lomp" src="./src/main.cpp" build="./build" exe="$build/task" @@ -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" @@ -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" diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index 1ca5a7a..0c76bc0 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -2,10 +2,37 @@ #include #include #include +#include +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) @@ -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(); diff --git a/OpenMP4/run.sh b/OpenMP4/run.sh index f1405e7..93219fc 100755 --- a/OpenMP4/run.sh +++ b/OpenMP4/run.sh @@ -1,6 +1,6 @@ # run.sh compiler="g++" -flags="-fopenmp -std=c++11" +flags="-Xclang -fopenmp -std=c++11 -lomp" src="./src/main.cpp" build="./build" exe="$build/task" @@ -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" @@ -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" diff --git a/OpenMP4/src/main.cpp b/OpenMP4/src/main.cpp index aa92f69..b8f1928 100644 --- a/OpenMP4/src/main.cpp +++ b/OpenMP4/src/main.cpp @@ -3,46 +3,59 @@ #include #include -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 \n"; - return 1; - } +int main(int argc, char **argv) { + // Check arguments + if (argc != 3) { + std::cout << "[Error] Usage \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; } diff --git a/OpenMP4/tests/00/input.txt b/OpenMP4/tests/00/input.txt new file mode 100644 index 0000000..2fb73a0 --- /dev/null +++ b/OpenMP4/tests/00/input.txt @@ -0,0 +1 @@ +1 1 diff --git a/OpenMP4/tests/00/output.txt b/OpenMP4/tests/00/output.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/OpenMP4/tests/00/output.txt @@ -0,0 +1 @@ +1 diff --git a/OpenMP4/tests/01/input.txt b/OpenMP4/tests/01/input.txt new file mode 100644 index 0000000..9ebabbc --- /dev/null +++ b/OpenMP4/tests/01/input.txt @@ -0,0 +1 @@ +1 8 diff --git a/OpenMP4/tests/01/output.txt b/OpenMP4/tests/01/output.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/OpenMP4/tests/01/output.txt @@ -0,0 +1 @@ +1 diff --git a/OpenMP4/tests/02/input.txt b/OpenMP4/tests/02/input.txt new file mode 100644 index 0000000..cba3471 --- /dev/null +++ b/OpenMP4/tests/02/input.txt @@ -0,0 +1 @@ +2 1 diff --git a/OpenMP4/tests/02/output.txt b/OpenMP4/tests/02/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP4/tests/02/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP4/tests/03/input.txt b/OpenMP4/tests/03/input.txt new file mode 100644 index 0000000..c0be89e --- /dev/null +++ b/OpenMP4/tests/03/input.txt @@ -0,0 +1 @@ +2 8 diff --git a/OpenMP4/tests/03/output.txt b/OpenMP4/tests/03/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP4/tests/03/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP4/tests/04/input.txt b/OpenMP4/tests/04/input.txt new file mode 100644 index 0000000..28ba9b1 --- /dev/null +++ b/OpenMP4/tests/04/input.txt @@ -0,0 +1 @@ +10000 1 diff --git a/OpenMP4/tests/04/output.txt b/OpenMP4/tests/04/output.txt new file mode 100644 index 0000000..ebb4380 --- /dev/null +++ b/OpenMP4/tests/04/output.txt @@ -0,0 +1 @@ +2.71828182845905 diff --git a/OpenMP4/tests/05/input.txt b/OpenMP4/tests/05/input.txt new file mode 100644 index 0000000..b22a23c --- /dev/null +++ b/OpenMP4/tests/05/input.txt @@ -0,0 +1 @@ +10000 8 diff --git a/OpenMP4/tests/05/output.txt b/OpenMP4/tests/05/output.txt new file mode 100644 index 0000000..ebb4380 --- /dev/null +++ b/OpenMP4/tests/05/output.txt @@ -0,0 +1 @@ +2.71828182845905 diff --git a/OpenMP4/tests/06/input.txt b/OpenMP4/tests/06/input.txt new file mode 100644 index 0000000..1e24de6 --- /dev/null +++ b/OpenMP4/tests/06/input.txt @@ -0,0 +1 @@ +100000000 1 diff --git a/OpenMP4/tests/06/output.txt b/OpenMP4/tests/06/output.txt new file mode 100644 index 0000000..ebb4380 --- /dev/null +++ b/OpenMP4/tests/06/output.txt @@ -0,0 +1 @@ +2.71828182845905 diff --git a/OpenMP4/tests/07/input.txt b/OpenMP4/tests/07/input.txt new file mode 100644 index 0000000..9e049b3 --- /dev/null +++ b/OpenMP4/tests/07/input.txt @@ -0,0 +1 @@ +100000000 8 diff --git a/OpenMP4/tests/07/output.txt b/OpenMP4/tests/07/output.txt new file mode 100644 index 0000000..ebb4380 --- /dev/null +++ b/OpenMP4/tests/07/output.txt @@ -0,0 +1 @@ +2.71828182845905