diff --git a/PrimeCPP/solution_2/.gitignore b/PrimeCPP/solution_2/.gitignore index 5b1fd1a1c..2cafdba9f 100644 --- a/PrimeCPP/solution_2/.gitignore +++ b/PrimeCPP/solution_2/.gitignore @@ -1 +1,2 @@ -.vscore/** \ No newline at end of file +.vscore/** +*.exe \ No newline at end of file diff --git a/PrimeCPP/solution_2/Dockerfile b/PrimeCPP/solution_2/Dockerfile index f6c0ceb2f..d56ddc06a 100644 --- a/PrimeCPP/solution_2/Dockerfile +++ b/PrimeCPP/solution_2/Dockerfile @@ -1,13 +1,19 @@ FROM ubuntu:22.04 AS build RUN apt-get update -qq \ - && apt-get install -y clang + && apt-get install -y bash clang WORKDIR /opt/app COPY *.cpp . -RUN clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_PAR.cpp -oprimes_par +RUN clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_array.cpp -oprimes_array +RUN clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_mask.cpp -oprimes_mask FROM ubuntu:22.04 -COPY --from=build /opt/app/primes_par /usr/local/bin -ENTRYPOINT [ "primes_par", "-l", "1000000" ] \ No newline at end of file +COPY --from=build /opt/app/primes_array /opt/app/primes_mask /opt/app/ + +WORKDIR /opt/app +COPY benchmark.sh . + +ENTRYPOINT [ "./benchmark.sh"] +CMD ["both", "-l", "1000000" ] \ No newline at end of file diff --git a/PrimeCPP/solution_2a/PrimeCPP_PAR.cpp b/PrimeCPP/solution_2/PrimeCPP_array.cpp similarity index 96% rename from PrimeCPP/solution_2a/PrimeCPP_PAR.cpp rename to PrimeCPP/solution_2/PrimeCPP_array.cpp index 017e8220a..4edd24af0 100644 --- a/PrimeCPP/solution_2a/PrimeCPP_PAR.cpp +++ b/PrimeCPP/solution_2/PrimeCPP_array.cpp @@ -25,7 +25,6 @@ const uint64_t DEFAULT_UPPER_LIMIT = 10'000'000LLU; class BitArray { uint8_t *array; - size_t arrSize; size_t logicalSize; static constexpr size_t arraySize(size_t size) @@ -41,9 +40,9 @@ class BitArray { public: explicit BitArray(size_t size) : logicalSize(size) { - arrSize = (size + 1) / 2; // Only store bits for odd numbers + auto arrSize = (size + 1) / 2; // Only store bits for odd numbers array = new uint8_t[arraySize(arrSize)]; - fill_n(array, arrSize, 0x00); + std::memset(array, 0x00, arraySize(arrSize)); } ~BitArray() { delete[] array; } @@ -103,14 +102,20 @@ class prime_sieve while (factor <= q) { // Find the next prime number - for (NULL; factor <= q; factor += 2) + for (; factor <= q; factor += 2) + { if (Bits.get(factor)) + { break; + } + } // Mark multiples of the prime number as not prime uint64_t start = factor * factor; for (uint64_t num = start; num <= Bits.size(); num += factor * 2) + { Bits.set(num); + } factor += 2; } @@ -201,7 +206,7 @@ class prime_sieve // Following 2 lines added by rbergen to conform to drag race output format cout << "\n"; - cout << "davepl_par;" << passes << ";" << duration << ";" << threads << ";algorithm=base,faithful=yes,bits=1\n"; + cout << "davepl_array;" << passes << ";" << duration << ";" << threads << ";algorithm=base,faithful=yes,bits=1\n"; } }; diff --git a/PrimeCPP/solution_2/PrimeCPP_PAR.cpp b/PrimeCPP/solution_2/PrimeCPP_mask.cpp similarity index 96% rename from PrimeCPP/solution_2/PrimeCPP_PAR.cpp rename to PrimeCPP/solution_2/PrimeCPP_mask.cpp index be5864816..58ea0dac6 100644 --- a/PrimeCPP/solution_2/PrimeCPP_PAR.cpp +++ b/PrimeCPP/solution_2/PrimeCPP_mask.cpp @@ -70,6 +70,15 @@ class BitArray { return ~mask; } + uint32_t rol(uint32_t value, size_t bits) + { + bits %= 32; + if (bits == 0) + return value; + // Ensure that the number of bits to rotate is within 0-31 + return (value << bits) | (value >> (32 - bits)); + } + void setFlagsFalse(size_t n, size_t skip) { if (skip <= 12) { @@ -78,7 +87,7 @@ class BitArray { size_t bit_pos = n % 32; size_t curr_n = n; - while (curr_n < arrSize) + while (curr_n < size()) { // Build mask for current word starting at bit_pos uint32_t mask = buildSkipMask(skip, bit_pos); @@ -90,7 +99,7 @@ class BitArray { size_t bits_remaining = 32 - bit_pos; curr_n += ((bits_remaining + skip - 1) / skip) * skip; - if (curr_n >= arrSize) break; + if (curr_n >= size()) break; word_idx = index(curr_n); bit_pos = curr_n % 32; @@ -101,7 +110,7 @@ class BitArray { // Original implementation for larger skips auto rolling_mask = ~uint32_t(1 << (n % 32)); auto roll_bits = skip % 32; - while (n < arrSize) { + while (n < size()) { array[index(n)] &= rolling_mask; n += skip; rolling_mask = rol(rolling_mask, roll_bits); @@ -254,7 +263,7 @@ class prime_sieve // Following 2 lines added by rbergen to conform to drag race output format cout << "\n"; - cout << "davepl_par;" << passes << ";" << duration << ";" << threads << ";algorithm=base,faithful=yes,bits=1\n"; + cout << "davepl_mask;" << passes << ";" << duration << ";" << threads << ";algorithm=base,faithful=yes,bits=1\n"; } }; @@ -429,4 +438,4 @@ int main(int argc, char **argv) // On success return the count of primes found; on failure, return 0 return (int) result; -} \ No newline at end of file +} diff --git a/PrimeCPP/solution_2/benchmark.sh b/PrimeCPP/solution_2/benchmark.sh new file mode 100755 index 000000000..a1cd0ea64 --- /dev/null +++ b/PrimeCPP/solution_2/benchmark.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +if [[ $1 == both || $1 == 1 || $1 == array ]]; then + ./primes_array ${@:2} +fi + +if [[ $1 == both || $1 == 2 || $1 == mask ]]; then + ./primes_mask ${@:2} +fi diff --git a/PrimeCPP/solution_2/primes_par.exe b/PrimeCPP/solution_2/primes_par.exe deleted file mode 100755 index c815cbfcf..000000000 Binary files a/PrimeCPP/solution_2/primes_par.exe and /dev/null differ diff --git a/PrimeCPP/solution_2/run.cmd b/PrimeCPP/solution_2/run.cmd index 3390a0df2..aee24f625 100644 --- a/PrimeCPP/solution_2/run.cmd +++ b/PrimeCPP/solution_2/run.cmd @@ -1,2 +1,9 @@ -g++ -Ofast PrimeCPP_PAR.cpp -std=c++17 -lstdc++ -oPrimes_par_gcc.exe -.\Primes_par_gcc.exe +IF "%1" == "" || "%1" == "1" || "%1" == "array" ( + g++ -Ofast PrimeCPP_array.cpp -std=c++17 -lstdc++ -oPrimes_array.exe + .\Primes_array.exe +) + +IF "%1" == "" || "%1" == "2" || "%1" == "mask" ( + g++ -Ofast PrimeCPP_mask.cpp -std=c++17 -lstdc++ -oPrimes_mask.exe + .\Primes_mask.exe +) diff --git a/PrimeCPP/solution_2/run.sh b/PrimeCPP/solution_2/run.sh index ea150e106..70f698fa5 100755 --- a/PrimeCPP/solution_2/run.sh +++ b/PrimeCPP/solution_2/run.sh @@ -1,6 +1,17 @@ +#!/bin/bash + # g++ -Ofast -std=c++17 -lc++ PrimeCPP.cpp -oPrimes.exe # gcc -Ofast -std=c++17 PrimeCPP.cpp -lc++ -oPrimes_gcc.exe # clang -Ofast -std=c++17 -lc++ PrimeCPP.cpp -oPrimes_clang.exe -clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_PAR.cpp -oprimes_par.exe -./primes_par.exe +if [[ "$#" -eq "0" -o "$1" -eq "1" -o "$1" -eq "array" ]]; then + echo "Building and running the array approach" + clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_array.cpp -oprimes_array.exe + ./primes_array.exe +fi + +if [[ "$#" -eq "0" -o "$1" -eq "2" -o "$1" -eq "mask" ]]; then + echo "Building and running the mask approach" + clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_mask.cpp -oprimes_mask.exe + ./primes_mask.exe +fi diff --git a/PrimeCPP/solution_2a/Dockerfile b/PrimeCPP/solution_2a/Dockerfile deleted file mode 100644 index f6c0ceb2f..000000000 --- a/PrimeCPP/solution_2a/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM ubuntu:22.04 AS build - -RUN apt-get update -qq \ - && apt-get install -y clang - -WORKDIR /opt/app -COPY *.cpp . -RUN clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_PAR.cpp -oprimes_par - -FROM ubuntu:22.04 -COPY --from=build /opt/app/primes_par /usr/local/bin - -ENTRYPOINT [ "primes_par", "-l", "1000000" ] \ No newline at end of file diff --git a/PrimeCPP/solution_2a/run.cmd b/PrimeCPP/solution_2a/run.cmd deleted file mode 100644 index 3390a0df2..000000000 --- a/PrimeCPP/solution_2a/run.cmd +++ /dev/null @@ -1,2 +0,0 @@ -g++ -Ofast PrimeCPP_PAR.cpp -std=c++17 -lstdc++ -oPrimes_par_gcc.exe -.\Primes_par_gcc.exe diff --git a/PrimeCPP/solution_2a/run.sh b/PrimeCPP/solution_2a/run.sh deleted file mode 100755 index dc59d3612..000000000 --- a/PrimeCPP/solution_2a/run.sh +++ /dev/null @@ -1,6 +0,0 @@ -# g++ -Ofast -std=c++17 -lc++ PrimeCPP.cpp -oPrimes.exe -# gcc -Ofast -std=c++17 PrimeCPP.cpp -lc++ -oPrimes_gcc.exe -# clang -Ofast -std=c++17 -lc++ PrimeCPP.cpp -oPrimes_clang.exe - -clang++ -march=native -mtune=native -pthread -Ofast -std=c++17 PrimeCPP_PAR.cpp -oprimes_par.exe -./primes_par.exe -t 1