Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import threadpool, strformat, os, strutils, math
  2. type
  3. Tree = ref object
  4. left: Tree
  5. right: Tree
  6.  
  7. proc deepCopy[T](x: var T, y: T) =
  8. x = y
  9.  
  10. proc check(t: Tree): int32 =
  11. result = 1
  12. if (t.left != nil and t.right != nil):
  13. result += check t.left
  14. result += check t.right
  15.  
  16. proc make(depth: int32): Tree =
  17. result = Tree(left: nil, right: nil)
  18. if depth > 0:
  19. result.left = make(depth - 1)
  20. result.right = make(depth - 1)
  21.  
  22. proc shortLived(depth: int32, iterations: int32): string =
  23. {.push experimental: "parallel".}
  24. var ts = newSeq[FlowVar[Tree]](iterations)
  25. parallel:
  26. for i in 0 .. ts.len-1:
  27. ts[i] = spawn make depth
  28. var sum = 0
  29. for t in ts:
  30. sum += check ^t
  31. {.pop.}
  32. &"{iterations}\t trees of depth {depth}\t check: {sum}"
  33.  
  34. let minDepth = 4
  35. let maxDepth : int32 =
  36. if paramCount() == 0: 2
  37. else:
  38. try: parseInt(paramStr(1))
  39. except: 2
  40.  
  41. echo &"stretch tree of depth {maxDepth+1} check: {check(make(maxDepth+1))}"
  42.  
  43. let longLived = make maxDepth
  44. let numMessages = int(maxDepth/2)
  45.  
  46. var messages = newSeq[string](numMessages+1)
  47. for i in 2 .. int(maxDepth/2):
  48. let depth = int32(i*2)
  49. let iterations = int32(1 shl (maxDepth - depth + minDepth))
  50. echo iterations , " " , depth
  51. messages[i] = shortLived(depth, iterations)
  52.  
  53. echo messages
  54. echo check longLived
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement