Comments (3)
If anyone is having trouble reproducing and would like extra debugging information, let me know what I can do to supplement.
from nim.
I'd like to blame it on sharing a ref
across threads given the following works on my end.
import md5
import std/[atomics, cpuinfo, monotimes, strformat, strutils, sugar, terminal, times]
const batchSize = 8000
type
SharedObj = object
prefix: string
done: Atomic[bool]
counter: Atomic[uint32]
sol: Atomic[uint32]
Shared = ref SharedObj
proc worker(arg: (ptr SharedObj, int)) {.thread.} =
let (s, v) = arg
while not s.done.load(moRelaxed):
let offset = s.counter.fetchAdd(batchSize, moRelaxed)
for n in 0'u32 ..< batchSize:
let i = offset + n
let h = (s.prefix & $i).toMD5
if h[0] == 0 and h[1] == 0:
var val = s.sol.load(moRelaxed)
while i < val and not s.sol.compareExchangeWeak(val, i, moRelaxed):
discard
s.done.store(true, moRelaxed)
proc solve(input: string): uint32 =
let s = Shared(prefix: input)
s.sol.store(uint32.high)
var thr: array[20, Thread[(ptr SharedObj, int)]]
doAssert countProcessors() < thr.len
for thr in thr.toOpenArray(0, countProcessors() - 1).mitems:
createThread(thr, worker, (cast[ptr SharedObj](s), 15))
joinThreads(thr.toOpenArray(0, countProcessors() - 1))
s.sol.load
proc part1(input: string): string =
$input.solve
proc colorizeTime(t: float): string =
# Crash happens here
let s = fmt"{t:.3f}"
if t < 0.5:
ansiForegroundColorCode(fgGreen) & s & ansiResetCode
elif t < 1.0:
ansiForegroundColorCode(fgYellow) & s & ansiResetCode
else:
ansiForegroundColorCode(fgRed) & s & ansiResetCode
proc timeit(f: (string) -> string, inp: string): (string, float) =
let startT = getMonoTime()
var ans = f(inp)
let endT = getMonoTime()
let t = endT - startT
return (ans, float(t.inMicroseconds) / 1_000_000)
proc run() =
# Needs the strip call to fail
let contents = readFile("input.txt").strip
let (ans, t) = timeit(part1, contents)
stdout.write fmt"Part 1: "
echo "$1 Elapsed time $2 seconds".format(align(ans, 54), colorizeTime(t))
run()
from nim.
Ah yeah that seems to be the problem. Switching to a SharedPtr in threading removes the crash. Perhaps this can be signaled more prominently in the threading docs? An example in https://nim-lang.org/docs/typedthreads.html for something that isn't copied by value would be helpful, I had to search around to find that library, and https://forum.nim-lang.org/t/9378 which I found more helpful than the docs.
from nim.
Related Issues (20)
- Default object values silently override generic types
- Enum Overloading Regression? HOT 4
- async `and` swallows errors
- Async `finally` swallows errors when it contains an `await` wrapped by `try/except`
- The `when nimvm` statement fails to compile when in a file called nimvm.nim HOT 4
- Regression from 2.0 to `devel` with `dirty` template HOT 2
- Async memory leak when raising exception on ORC HOT 1
- unicode.splitWhitespace() and strutils.splitWhitespace() have different results for ASCII string
- Wrong stack trace when exception is raised in template HOT 1
- Error: cannot evaluate at compile time: foo HOT 4
- nim check crashes
- Any differnece between value types and reference types for `=sink` ? HOT 1
- Simple destructor code gives invalid C HOT 4
- Compiler crashes with infinite recursion for nested generic instantiation with static[int]
- The stdout.write doesn't print to terminal until new line symbol is sent HOT 1
- Gc_ref(x: string) and Gc_ref[T](x: seq[T]) doesn't exists anymore ? HOT 2
- tasks.toTask Doesn't Expect a Dot Expression
- [potential bug] SIGILL at `renderer.atom()`
- SIGSEGV due to freeing of uninitialized local string, during exception handling HOT 1
- Parameterless Conversions and Upcasting Are Broken
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim.