Class: SimControl::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/SimControl/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, simulation_description, scenario_description, results_directory, args = {}) ⇒ Controller

Returns a new instance of Controller.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/SimControl/controller.rb', line 5

def initialize(hostname, simulation_description, scenario_description, results_directory, args = {})
  @hosts = args.delete(:hosts) || SimControl::Hosts.new
  @scenario_klass = args.delete(:scenario_klass) || SimControl::Scenario

  @hostname = hostname
  @simulation_description = simulation_description
  @scenario_description = scenario_description
  @results_directory = results_directory

  @scenarios = []

  @meta_seed = 13
  @max_seed = 2**(32 - 1) - 1
end

Instance Attribute Details

#current_simulationObject (readonly)

Returns the value of attribute current_simulation.



3
4
5
# File 'lib/SimControl/controller.rb', line 3

def current_simulation
  @current_simulation
end

Class Method Details

.execute(hostname, simulation_description, scenario_description, results_directory) ⇒ Object



70
71
72
73
# File 'lib/SimControl/controller.rb', line 70

def execute(hostname, simulation_description, scenario_description, results_directory)
  controller = SimControl::Controller.new(hostname, simulation_description, scenario_description, results_directory)
  controller.run
end

Instance Method Details

#all_scenariosObject



65
66
67
# File 'lib/SimControl/controller.rb', line 65

def all_scenarios
  @scenarios
end

#create_scenario(*args) ⇒ Object



20
21
22
# File 'lib/SimControl/controller.rb', line 20

def create_scenario(*args)
  @scenario_klass.new(*args)
end

#hosts(&hosts_block) ⇒ Object



48
49
50
# File 'lib/SimControl/controller.rb', line 48

def hosts(&hosts_block)
  @hosts.process &hosts_block
end

#repetitions(number_of_repetitions) ⇒ Object



44
45
46
# File 'lib/SimControl/controller.rb', line 44

def repetitions(number_of_repetitions)
  @number_of_repetitions = number_of_repetitions
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/SimControl/controller.rb', line 24

def run
  instance_eval(@simulation_description)
  instance_eval(@scenario_description)

  host_scenarios = @hosts.partition(all_scenarios, @hostname)

  host_scenarios.each do |scenarios_per_core|
    threads = []
    scenarios_per_core.each do |scenario|
      threads << Thread.new do
        current_simulation.simulate(scenario, seeds)
      end

      threads.each do |thread|
        thread.join
      end
    end
  end
end

#seedsObject



56
57
58
59
# File 'lib/SimControl/controller.rb', line 56

def seeds
  @rng = Random.new(@meta_seed)
  (1..@number_of_repetitions).map { @rng.rand(@max_seed) }
end

#simulate(*scenario) ⇒ Object



61
62
63
# File 'lib/SimControl/controller.rb', line 61

def simulate(*scenario)
  @scenarios << (create_scenario *scenario)
end

#simulation(klass, script, arguments) ⇒ Object



52
53
54
# File 'lib/SimControl/controller.rb', line 52

def simulation(klass, script, arguments)
  @current_simulation = klass.new script, @results_directory, arguments
end