From 9839664a8bd80775a200bf810b1fa5d8799ef37a Mon Sep 17 00:00:00 2001 From: pavel Date: Fri, 9 Oct 2020 00:57:30 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=202,3,?= =?UTF-8?q?4=20=D0=B2=20run.sh=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20macos=20=D0=B2=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=D0=B5=203=20=D0=BD=D0=B5=20=D1=85=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE=20<<=20std::endl=20--=20=D0=B8=D0=B7-=D0=B7?= =?UTF-8?q?=D0=B0=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE=20=D0=BD=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D0=BB=D0=B8=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenMP2/run.sh | 10 +++--- OpenMP2/src/main.cpp | 77 +++++++++++++++++++++++--------------------- OpenMP3/run.sh | 10 +++--- OpenMP3/src/main.cpp | 31 ++++++++++++++++-- 4 files changed, 79 insertions(+), 49 deletions(-) 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(); From 70df9d64b5cfb2b6555d0741da3dd930b404c5ee Mon Sep 17 00:00:00 2001 From: pavel Date: Fri, 9 Oct 2020 03:10:12 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenMP4/run.sh | 10 ++--- OpenMP4/src/main.cpp | 83 +++++++++++++++++++++---------------- OpenMP4/tests/00/input.txt | 1 + OpenMP4/tests/00/output.txt | 1 + OpenMP4/tests/01/input.txt | 1 + OpenMP4/tests/01/output.txt | 1 + OpenMP4/tests/02/input.txt | 1 + OpenMP4/tests/02/output.txt | 1 + OpenMP4/tests/03/input.txt | 1 + OpenMP4/tests/03/output.txt | 1 + OpenMP4/tests/04/input.txt | 1 + OpenMP4/tests/04/output.txt | 1 + OpenMP4/tests/05/input.txt | 1 + OpenMP4/tests/05/output.txt | 1 + OpenMP4/tests/06/input.txt | 1 + OpenMP4/tests/06/output.txt | 1 + OpenMP4/tests/07/input.txt | 1 + OpenMP4/tests/07/output.txt | 1 + 18 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 OpenMP4/tests/00/input.txt create mode 100644 OpenMP4/tests/00/output.txt create mode 100644 OpenMP4/tests/01/input.txt create mode 100644 OpenMP4/tests/01/output.txt create mode 100644 OpenMP4/tests/02/input.txt create mode 100644 OpenMP4/tests/02/output.txt create mode 100644 OpenMP4/tests/03/input.txt create mode 100644 OpenMP4/tests/03/output.txt create mode 100644 OpenMP4/tests/04/input.txt create mode 100644 OpenMP4/tests/04/output.txt create mode 100644 OpenMP4/tests/05/input.txt create mode 100644 OpenMP4/tests/05/output.txt create mode 100644 OpenMP4/tests/06/input.txt create mode 100644 OpenMP4/tests/06/output.txt create mode 100644 OpenMP4/tests/07/input.txt create mode 100644 OpenMP4/tests/07/output.txt 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