MilesCranmer commited on
Commit
2a2a517
1 Parent(s): b364345

Working parallelism with normal threads module

Browse files
Files changed (1) hide show
  1. paralleleureqa.jl +57 -17
paralleleureqa.jl CHANGED
@@ -1,33 +1,26 @@
1
- using Distributed
2
- addprocs(8)
3
- @everywhere const nthreads = 8
4
-
5
- @everywhere include("eureqa.jl")
6
 
7
  println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
8
- @everywhere const npop = 100
9
- @everywhere const annealing = false
10
- @everywhere const niterations = 30
11
- @everywhere const ncyclesperiteration = 10000
 
 
12
 
13
  # Generate random initial populations
14
-
15
- # Create a mapping for running the algorithm on all processes
16
- @everywhere f = (pop,)->run(pop, ncyclesperiteration, annealing)
17
-
18
-
19
  allPops = [Population(npop, 3) for j=1:nthreads]
20
  bestScore = Inf
21
  # Repeat this many evolutions; we collect and migrate the best
22
  # each time.
23
  for k=1:4
24
  # Spawn independent evolutions
25
- futures = [@spawnat :any f(allPops[i]) for i=1:nthreads]
26
 
27
  # Gather them
28
- for i=1:nthreads
29
- allPops[i] = fetch(futures[i])
30
  end
 
31
  # Get best 10 models for each processes. Copy because we re-assign later.
32
  bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
33
  bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
@@ -43,6 +36,53 @@ for k=1:4
43
  end
44
  end
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
 
 
1
+ include("eureqa.jl")
 
 
 
 
2
 
3
  println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
4
+ const nthreads = Threads.nthreads()
5
+ println("Running with $nthreads threads")
6
+ const npop = 100
7
+ const annealing = true
8
+ const niterations = 30
9
+ const ncyclesperiteration = 10000
10
 
11
  # Generate random initial populations
 
 
 
 
 
12
  allPops = [Population(npop, 3) for j=1:nthreads]
13
  bestScore = Inf
14
  # Repeat this many evolutions; we collect and migrate the best
15
  # each time.
16
  for k=1:4
17
  # Spawn independent evolutions
 
18
 
19
  # Gather them
20
+ @inbounds Threads.@threads for i=1:nthreads
21
+ allPops[i] = run(allPops[i], ncyclesperiteration, annealing)
22
  end
23
+
24
  # Get best 10 models for each processes. Copy because we re-assign later.
25
  bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
26
  bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
 
36
  end
37
  end
38
 
39
+ ## Possibly calls once for every thread? But works.
40
+
41
+ # using Distributed
42
+ # addprocs(8)
43
+ # @everywhere const nthreads = 8
44
+
45
+ # @everywhere include("eureqa.jl")
46
+
47
+ # println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
48
+ # @everywhere const npop = 100
49
+ # @everywhere const annealing = false
50
+ # @everywhere const niterations = 30
51
+ # @everywhere const ncyclesperiteration = 10000
52
+
53
+ # # Generate random initial populations
54
+
55
+ # # Create a mapping for running the algorithm on all processes
56
+ # @everywhere f = (pop,)->run(pop, ncyclesperiteration, annealing)
57
+
58
+
59
+ # @everywhere allPops = [Population(npop, 3) for j=1:nthreads]
60
+ # @everywhere bestScore = Inf
61
+ # # Repeat this many evolutions; we collect and migrate the best
62
+ # # each time.
63
+ # for k=1:4
64
+ # # Spawn independent evolutions
65
+ # @everywhere futures = [@spawnat :any f(allPops[i]) for i=1:nthreads]
66
+
67
+ # # Gather them
68
+ # for i=1:nthreads
69
+ # @everywhere allPops[i] = fetch(futures[i])
70
+ # end
71
+ # # Get best 10 models for each processes. Copy because we re-assign later.
72
+ # @everywhere bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
73
+ # @everywhere bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
74
+ # @everywhere bestCurScore = bestPops.members[bestCurScoreIdx].score
75
+ # println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
76
+
77
+ # # Migration
78
+ # for j=1:nthreads
79
+ # for k in rand(1:npop, 50)
80
+ # # Copy in case one gets copied twice
81
+ # @everywhere allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
82
+ # end
83
+ # end
84
+ # end
85
+
86
 
87
 
88