Skip to content

Commit

Permalink
Combine array and mask approaches in one solution_2
Browse files Browse the repository at this point in the history
  • Loading branch information
rbergen committed Nov 7, 2024
1 parent df14de3 commit 67344d8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
3 changes: 2 additions & 1 deletion PrimeCPP/solution_2/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscore/**
.vscore/**
*.exe
14 changes: 10 additions & 4 deletions PrimeCPP/solution_2/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Check notice on line 9 in PrimeCPP/solution_2/Dockerfile

View workflow job for this annotation

GitHub Actions / build (PrimeCPP/solution_2)

Multiple consecutive `RUN` instructions. Consider consolidation.

Check notice on line 9 in PrimeCPP/solution_2/Dockerfile

View workflow job for this annotation

GitHub Actions / build (PrimeCPP/solution_2)

Multiple consecutive `RUN` instructions. Consider consolidation.

FROM ubuntu:22.04
COPY --from=build /opt/app/primes_par /usr/local/bin

ENTRYPOINT [ "primes_par", "-l", "1000000" ]
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" ]
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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; }
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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";
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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";
}

};
Expand Down Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions PrimeCPP/solution_2/benchmark.sh
Original file line number Diff line number Diff line change
@@ -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
Binary file removed PrimeCPP/solution_2/primes_par.exe
Binary file not shown.
11 changes: 9 additions & 2 deletions PrimeCPP/solution_2/run.cmd
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 13 additions & 2 deletions PrimeCPP/solution_2/run.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 0 additions & 13 deletions PrimeCPP/solution_2a/Dockerfile

This file was deleted.

2 changes: 0 additions & 2 deletions PrimeCPP/solution_2a/run.cmd

This file was deleted.

6 changes: 0 additions & 6 deletions PrimeCPP/solution_2a/run.sh

This file was deleted.

0 comments on commit 67344d8

Please sign in to comment.