experiment 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,8 @@ lib/experiment/generator/Rakefile
12
12
  lib/experiment/base.rb
13
13
  lib/experiment/notify.rb
14
14
  lib/experiment/work_server.rb
15
- lib/experiment/distributed.rb
15
+ lib/experiment/distributed/master.rb
16
+ lib/experiment/distributed/slave.rb
16
17
  lib/experiment/factorial.rb
17
18
  lib/experiment/params.rb
18
19
  test/test_experiment.rb
@@ -8,7 +8,7 @@ require "rdoc/rdoc"
8
8
  require File.dirname(__FILE__) + "/../lib/experiment/runner"
9
9
 
10
10
  class App
11
- VERSION = '0.3.2'
11
+ VERSION = '0.3.3'
12
12
 
13
13
  attr_reader :options
14
14
 
@@ -2,5 +2,5 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Experiment
5
- VERSION = '0.3.2'
5
+ VERSION = '0.3.3'
6
6
  end
@@ -2,7 +2,8 @@ require File.dirname(__FILE__) + "/notify"
2
2
  require File.dirname(__FILE__) + "/stats/descriptive"
3
3
  require File.dirname(__FILE__) + "/config"
4
4
  require File.dirname(__FILE__) + "/params"
5
- require File.dirname(__FILE__) + "/distributed"
5
+ require File.dirname(__FILE__) + "/distributed/slave"
6
+ require File.dirname(__FILE__) + "/distributed/master"
6
7
  require 'benchmark'
7
8
  require "drb/drb"
8
9
  require "yaml"
@@ -12,8 +13,6 @@ module Experiment
12
13
  # @author Jakub Hampl
13
14
  # @see https://github.com/gampleman/Experiment/wiki/Designing-your-experiment
14
15
  class Base
15
-
16
- include Distributed
17
16
 
18
17
  @@cleanup_raw_files = false
19
18
 
@@ -30,22 +29,24 @@ module Experiment
30
29
  # Called internally by the framewrok
31
30
  # @private
32
31
  # @param [:normal, :master, :slave] mode
33
- # @param [String] experiment name
32
+ # @param [String] experiment Name of the experimental condition.
33
+ # @param [OpenStruct] options Most of the options passed from the CLI.
34
34
  def initialize(mode, experiment, options)
35
35
  @experiment = experiment
36
36
  @options = options
37
+ # a bit of dependency injection here
37
38
  case mode
38
-
39
39
  when :normal
40
40
  @abm = []
41
-
42
41
  when :master
43
42
  @abm = []
44
43
  extend DRb::DRbUndumped
44
+ extend Distributed::Master
45
45
  @done = false
46
46
  when :slave
47
-
47
+ extend Distributed::Slave
48
48
  end
49
+
49
50
  Experiment::Config::load(experiment, options.opts, options.env)
50
51
  @mode = mode
51
52
  end
@@ -79,7 +80,7 @@ module Experiment
79
80
 
80
81
  # runs the whole experiment, called by the framework
81
82
  # @private
82
- def normal_run!(cv)
83
+ def run!(cv)
83
84
  @cvs = cv || 1
84
85
  @results = {}
85
86
  Notify.started @experiment
@@ -0,0 +1,63 @@
1
+ module Experiment
2
+ # It incorporates most of the logic required for distributed
3
+ # computing support.
4
+ # @see https://github.com/gampleman/Experiment/wiki/Distributed-Mode
5
+ # @private
6
+ module Distributed
7
+ # this module is included into base when running with --distributed
8
+ module Master
9
+ # Send work from the master server
10
+ # @return [Hash, false] either a spec what work to carry out or false
11
+ # when no work available
12
+ def get_work()
13
+ if cv = @started.index(false)
14
+ @started[cv] = true
15
+ {:cv => cv, :input => @data[cv], :dir => @dir, :options => Experiment::Config.to_hash, :cvs => cvs }
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ # returns true if all work has been disseminated
22
+ def distribution_done?
23
+ @started.all?
24
+ end
25
+
26
+ # Sends the result of the computation back to the master server.
27
+ # Called on the master server object.
28
+ def submit_result(cv, result, performance)
29
+ @completed[cv] = true
30
+ array_merge(@results, result)
31
+ @abm << performance
32
+ Notify.cv_done @experiment, cv
33
+ master_done! if @completed.all?
34
+ end
35
+
36
+ # Strats up the master server
37
+ def run!(cv)
38
+
39
+ @cvs = cv || 1
40
+ @results = {}
41
+ Notify.started @experiment
42
+ split_up_data
43
+ write_dir!
44
+ @completed = (1..@cvs).map {|a| false }
45
+ @started = @completed.dup
46
+ end
47
+
48
+ # Cleans up the master server after all work is done
49
+ def master_done!
50
+ @done = true
51
+ specification! true
52
+ summarize_performance!
53
+ summarize_results! @results
54
+ cleanup!
55
+ Notify.completed @experiment
56
+
57
+ #sleep 1
58
+ #DRb.stop_service
59
+ end
60
+
61
+ end # module Master
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ module Experiment
2
+ module Distributed
3
+ # this module is included into base when running as worker
4
+ module Slave
5
+ # master server DRb object
6
+ attr_accessor :master
7
+
8
+ # Main function. Will continously request work from the server,
9
+ # execute it and send back results, then loops to the beggining.
10
+ def run!(not_used_arg)
11
+ while work = @master.get_work
12
+ puts work.inspect
13
+ Experiment::Config.set work[:options]
14
+ @current_cv = work[:cv]
15
+ @dir = work[:dir]
16
+ @data = work[:input]
17
+ #@data = work[:input]
18
+ File.open(@dir + "/raw-#{@current_cv}.txt", "w") do |output|
19
+ @ouptut_file = output
20
+ run_the_experiment
21
+ end
22
+ result = analyze_result!(@dir + "/raw-#{@current_cv}.txt", @dir + "/analyzed-#{@current_cv}.txt")
23
+ write_performance!
24
+ @master.submit_result @current_cv, result, @abm.first
25
+ end
26
+
27
+ end
28
+
29
+
30
+ def test_data
31
+ @data
32
+ end
33
+
34
+ end # Master
35
+ end
36
+ end
@@ -143,7 +143,7 @@ module Experiment
143
143
  require "./experiments/#{exp}/#{exp}"
144
144
  cla = eval(as_class_name(exp))
145
145
  experiment = cla.new :normal, exp, @options
146
- experiment.normal_run! @options.cv
146
+ experiment.run! @options.cv
147
147
  end
148
148
  Notify::done
149
149
  end
@@ -199,7 +199,7 @@ module Experiment
199
199
  cla = eval(as_class_name(exp))
200
200
  experiment = cla.new :slave, exp, @options
201
201
  experiment.master = @master.instance item
202
- experiment.slave_run!
202
+ experiment.run! 0
203
203
  end
204
204
  end
205
205
  end
@@ -38,7 +38,7 @@ module Experiment
38
38
  @experiment_instances += subs
39
39
  return i + 1
40
40
  else
41
- experiment.master_run! @options.cv
41
+ experiment.run! @options.cv
42
42
  @experiment_instances[i] = experiment
43
43
  return i
44
44
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jakub Hampl
@@ -75,7 +75,8 @@ files:
75
75
  - lib/experiment/base.rb
76
76
  - lib/experiment/notify.rb
77
77
  - lib/experiment/work_server.rb
78
- - lib/experiment/distributed.rb
78
+ - lib/experiment/distributed/master.rb
79
+ - lib/experiment/distributed/slave.rb
79
80
  - lib/experiment/factorial.rb
80
81
  - lib/experiment/params.rb
81
82
  - test/test_experiment.rb
@@ -1,98 +0,0 @@
1
- module Experiment
2
- # this module is included in Experiment::Base
3
- # It incorporates most of the logic required for distributed
4
- # computing support.
5
- # @see https://github.com/gampleman/Experiment/wiki/Distributed-Mode
6
- # @private
7
- module Distributed
8
-
9
-
10
- # @group Called on slave
11
-
12
- # master server DRb object
13
- attr_accessor :master
14
-
15
- # Main function. Will continously request work from the server,
16
- # execute it and send back results, then loops to the beggining.
17
- def slave_run!
18
- while work = @master.get_work
19
- puts work.inspect
20
- Experiment::Config.set work[:options]
21
- @current_cv = work[:cv]
22
-
23
- @dir = work[:dir]
24
- #@data = work[:input]
25
- File.open(@dir + "/raw-#{@current_cv}.txt", "w") do |output|
26
- @ouptut_file = output
27
- run_the_experiment
28
- end
29
- result = analyze_result!(@dir + "/raw-#{@current_cv}.txt", @dir + "/analyzed-#{@current_cv}.txt")
30
- write_performance!
31
- @master.submit_result @current_cv, result, @abm.first
32
- end
33
-
34
- end
35
-
36
-
37
- # @endgroup
38
-
39
- # @group Called on master
40
-
41
- # Send work from the master server
42
- # @return [Hash, false] either a spec what work to carry out or false
43
- # when no work available
44
- def get_work()
45
- if cv = @started.index(false)
46
- @started[cv] = true
47
- {:cv => cv, :input => @data[cv], :dir => @dir, :options => Experiment::Config.to_hash }
48
- else
49
- false
50
- end
51
- end
52
-
53
- # returns true if all work has been disseminated
54
- def distribution_done?
55
- @started.all?
56
- end
57
-
58
- # Sends the result of the computation back to the master server.
59
- # Called on the master server object.
60
- def submit_result(cv, result, performance)
61
- @completed[cv] = true
62
- array_merge(@results, result)
63
- @abm << performance
64
- Notify.cv_done @experiment, cv
65
- master_done! if @completed.all?
66
- end
67
-
68
-
69
-
70
- # Strats up the master server
71
- def master_run!(cv)
72
-
73
- @cvs = cv || 1
74
- @results = {}
75
- Notify.started @experiment
76
- split_up_data
77
- write_dir!
78
- @completed = (1..@cvs).map {|a| false }
79
- @started = @completed.dup
80
- end
81
-
82
- # Cleans up the master server after all work is done
83
- def master_done!
84
- @done = true
85
- specification! true
86
- summarize_performance!
87
- summarize_results! @results
88
- cleanup!
89
- Notify.completed @experiment
90
-
91
- #sleep 1
92
- #DRb.stop_service
93
- end
94
-
95
- # @endgroup
96
-
97
- end
98
- end