Skip to content

Instantly share code, notes, and snippets.

@mkaczanowski mkaczanowski/e_prime.nim

Last active Apr 14, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.