I’ve got a multi-threaded version of this algorithm working in D, Nim, and Rust, and am now trying to do a Crystal version.
The serial section looks like this:
cnts = [] of typeof(end_num)
lastwins = [] of typeof(end_num)
restwins.each_with_index do |r_hi, i|
l, c = twins_sieve(r_hi, kmin, kmax, kb, start_num, end_num, modpg, primes, resinvrs)
lastwins << l; cnts << c
print "\r#{i+1} of #{pairscnt} twinpairs done"
end
I tried to make it multi-threaded doing this:
cnts = [] of typeof(end_num)
lastwins = [] of typeof(end_num)
restwins.each_with_index do |r_hi, i| # sieve twinpair restracks
spawn do
l, c = twins_sieve(r_hi, kmin, kmax, kb, start_num, end_num, modpg, primes, resinvrs)
lastwins << l; cnts << c
print "\r#{i+1} of #{pairscnt} twinpairs done"
end
end
But get this type of error (using -Dpreview-mt)
Unhandled exception: Index out of bounds (IndexError)
from ???
from twinprimes_ssoz.cr:101:5 in 'twinprimes_ssoz'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:106:5 in 'main'
from __libc_start_main
from ../sysdeps/x86_64/start.S:122:0 in '_start'
from ???
But even when I try to simply print out the index like this
restwins.each_with_index do |r_hi, i|
spawn do
print "\r#{i+1} of #{pairscnt} twinpairs done"
end
end
I still get this error
135 of 135 twinpairs doneUnhandled exception: Index out of bounds (IndexError)
from ???
from twinprimes_ssoz.cr:101:5 in 'twinprimes_ssoz'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:106:5 in 'main'
from __libc_start_main
from ../sysdeps/x86_64/start.S:122:0 in '_start'
from ???
Everything compiled as:
$ crystal build test.cr -Ddisable_overflow -Dpreview_mt --release
$ crystal -v
Crystal 0.33.0 [612825a53] (2020-02-14)
LLVM: 8.0.0
Default target: x86_64-unknown-linux-gnu