tparfind.nim 677 B

1234567891011121314151617181920212223242526272829
  1. discard """
  2. output: "500"
  3. """
  4. import threadpool, sequtils
  5. {.experimental.}
  6. proc linearFind(a: openArray[int]; x, offset: int): int =
  7. for i, y in a:
  8. if y == x: return i+offset
  9. result = -1
  10. proc parFind(a: seq[int]; x: int): int =
  11. var results: array[4, int]
  12. parallel:
  13. if a.len >= 4:
  14. let chunk = a.len div 4
  15. results[0] = spawn linearFind(a[0 ..< chunk], x, 0)
  16. results[1] = spawn linearFind(a[chunk ..< chunk*2], x, chunk)
  17. results[2] = spawn linearFind(a[chunk*2 ..< chunk*3], x, chunk*2)
  18. results[3] = spawn linearFind(a[chunk*3 ..< a.len], x, chunk*3)
  19. result = max(results)
  20. let data = toSeq(0..1000)
  21. echo parFind(data, 500)