Skip to content

Instantly share code, notes, and snippets.

@mkaczanowski
Last active April 14, 2020 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkaczanowski/c03ed98313ce118586d17208e883a9ee to your computer and use it in GitHub Desktop.
Save mkaczanowski/c03ed98313ce118586d17208e883a9ee to your computer and use it in GitHub Desktop.
proc e_get_coreid(): cint {.header: "e_lib.h"}
proc is_prime(number: uint32): bool =
for i in 2..number div 2:
if number mod i == 0:
return false
return true
var
count = cast[ptr uint32](0x7000)
num = cast[ptr uint32](0x7008)
primes = cast[ptr uint32](0x7010)
max_tests = cast[ptr uint32](0x7020)
proc main() =
let
core_id = int(e_get_coreid())
row = (coreid shr 6) and 0x3f
col = coreid and 0x3f
# Assign the starting value for each core (3,5,7,..33)
# Each core starts with a unique odd number (2 is the only even prime number)
var number: uint32 = uint32(2 + ((2*row*4) + (2*col+1)))
# Initialize this core iteration count for stats
count[] = 0
# Initialize the number of found primes counter
primes[] = 0
while count[] < max_tests[]:
if is_prime(number):
primes[] = primes[] + 1
# Skip to the next odd number for this core to test, assuming total of 16 cores
# Core (0,0) started with 3 on the first iteration, and next test 35
# Core (0,1) started with 5 on the first iteration, and next test 37
# etc
number += (2*16);
num[] = number;
count[] = count[] + 1
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment