Class: NERA::RunLayerController

Inherits:
Object
  • Object
show all
Defined in:
lib/nera/nera_run_layer_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_db_folder, sim_name, param_id) ⇒ RunLayerController

Returns a new instance of RunLayerController.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nera/nera_run_layer_controller.rb', line 25

def initialize( path_db_folder, sim_name, param_id)
  raise ArgumentError unless path_db_folder.is_a?(String)

  @db_folders = NERA::DbFolders.new( path_db_folder)
  sim_records = NERA::SimulatorRecords.new( @db_folders.path_to_simulators_table)
  sim_records.set_yaml_file( @db_folders.path_to_simulators_yaml )
  
  found = sim_records.list.find do |rec|
    rec[:name] == sim_name.to_s
  end
  raise "No such simulator : #{sim_name.to_s}" unless found
  sim_id = found[:id]
  sim_class = NERA::Simulator.inherited_simulators.find do |sim|
    sim.to_s == sim_name.to_s
  end
  raise "No such simulator : #{sim_name.to_s}" unless sim_class
  
  @sim_instance = sim_class.new
  @param_id = param_id
  @param_records = NERA::ParameterRecords.new( @db_folders.path_to_parameters_table( sim_class), sim_records, sim_id)
  @param_records.set_yaml_file( @db_folders.path_to_parameters_yaml( sim_class) )
  found = @param_records.find_by_id( @param_id)
  raise "No such parameter : #{param_id}." unless found
  @sim_instance.class::Parameters.each do |pa|
    key = pa[0]
    if found[key]
      @sim_instance.param[key] = found[key]
    else
      @sim_instance.param[key] = pa[2]
    end
  end
  
  @run_records = NERA::RunRecords.new( @db_folders.path_to_runs_table( sim_class, @param_id), @param_records, @param_id )
  @run_records.set_yaml_file( @db_folders.path_to_runs_yaml( @sim_instance.class, @param_id) )
end

Instance Method Details

#all_runs_listObject



65
66
67
# File 'lib/nera/nera_run_layer_controller.rb', line 65

def all_runs_list
  return @run_records.list_all
end

#all_runs_list_in_csvObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nera/nera_run_layer_controller.rb', line 69

def all_runs_list_in_csv
  list = @run_records.list_all
  header = @run_records.keys.dup
  csv_list = []
  list.each do |r_hash|
    strings = []
    header.each do |key|
      if r_hash[key].is_a?(DateTime)
        strings << r_hash[key].to_s.split('T')[0]
      else
        strings << r_hash[key].to_s
      end
    end
    csv_list << strings.join(", ")
  end
  return header.join(", "), csv_list
end

#analysis_methodsObject



181
182
183
184
185
186
# File 'lib/nera/nera_run_layer_controller.rb', line 181

def analysis_methods
  found = @sim_instance.public_methods.find_all do |meth|
    meth =~ /^analyze_/
  end
  return found
end

#analyze(method_str) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/nera/nera_run_layer_controller.rb', line 188

def analyze( method_str)
  unless method_str.is_a?(String) or method_str.is_a?(Symbol)
    raise ArgumentError, "method_str must be a String or Symbol"
  end

  return nil unless method_str.to_s =~ /^analyze_/
  
  found = @sim_instance.public_methods.find do |m|
    m == method_str.to_s
  end
  return nil unless found

  FileUtils.cd( @db_folders.path_to_run_layer(@sim_instance.class, @param_id) ) {
    @sim_instance.__send__( method_str)
  }
  @db_folders.logger.info(self.class.to_s) {
    "#{method_str} (#{@sim_instance.class.to_s}/#{@param_id})"
  }

end

#analyze_allObject



209
210
211
212
213
214
215
216
217
218
# File 'lib/nera/nera_run_layer_controller.rb', line 209

def analyze_all
  found = @sim_instance.public_methods.find_all do |m|
    m =~ /^analyze_/
  end
  return nil unless found

  found.each do |meth|
    analyze( meth)
  end
end

#cancel_jobs(job_ids) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/nera/nera_run_layer_controller.rb', line 144

def cancel_jobs( job_ids)
  unless job_ids.is_a?(Array)
    raise ArgumentError, "job_ids must be an Array."
  end
  job_ids.each do |x|
    raise ArgumentError, "each element of job_ids must be an Integer" unless x.is_a?(Integer)
  end
  
  job_recs = NERA::JobRecords.new( @db_folders.path_to_jobs_table)
  job_recs.set_yaml_file( @db_folders.path_to_jobs_yaml)
  destroyed_jobids = []
  job_recs.transaction {
    @param_records.transaction {
      @run_records.transaction {
        job_ids.each do |jid|
          d = job_recs.destroy(jid)
          next unless d
          a = @run_records.destroy_job_id( jid)
          raise "must not happen" unless a
          FileUtils.rm( @db_folders.path_to_job_script(jid) )
          destroyed_jobids << jid
          @db_folders.logger.info(self.class.to_s) {
            "canceled a job (jobid:#{jid})"
          }
        end
        @param_records.update_num_runs( @param_id, @run_records.list_all_finished.size, @run_records.list_all.size)
      }
    }
  }
  destroyed_jobids = nil if destroyed_jobids.size == 0
  return destroyed_jobids
end

#create_jobs(num_jobs, num_runs_per_job = 1) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/nera/nera_run_layer_controller.rb', line 87

def create_jobs( num_jobs, num_runs_per_job = 1)
  unless num_jobs.is_a?(Integer) and num_jobs >= 0
    raise ArgumentError, "num_jobs must be equal to or larger than zero."
  end

  job_ids = []
  job_recs = NERA::JobRecords.new( @db_folders.path_to_jobs_table)
  job_recs.set_yaml_file( @db_folders.path_to_jobs_yaml)
  js = NERA::JobScript.new( @db_folders)
  job_recs.transaction {
    @param_records.transaction {
      @run_records.transaction {
        num_jobs.times do |j_idx|
          run_stat = @run_records.add( num_runs_per_job)
          rid = run_stat[0][:id]
          jid = job_recs.add( @sim_instance.class, @param_id, rid, num_runs_per_job)
          @run_records.set_job_id( rid, num_runs_per_job, jid)
          sim_records = NERA::SimulatorRecords.new( @db_folders.path_to_simulators_table)
          sim_records.set_yaml_file( @db_folders.path_to_simulators_yaml )
          found = sim_records.list.find do |rec| rec[:name] == @sim_instance.class.to_s end
          path_script = js.create_script( jid, found[:id], @param_id, @sim_instance, run_stat)
          job_ids << jid
          @db_folders.logger.info(self.class.to_s) {
            "created a job (#{@sim_instance.class.to_s}/#{@param_id}/#{rid}, #{num_runs_per_job} runs, jobid:#{jid})"
          }
        end
        @param_records.update_num_runs( @param_id, @run_records.list_all_finished.size, @run_records.list_all.size)
      }
    }
  }
  return job_ids
end

#find_similar_parameter_sets(parameter_key) ⇒ Object



177
178
179
# File 'lib/nera/nera_run_layer_controller.rb', line 177

def find_similar_parameter_sets(parameter_key)
  @param_records.find_similar_parameter_sets( @param_id, parameter_key) 
end

#not_finished_jobs_list_in_csvObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nera/nera_run_layer_controller.rb', line 120

def not_finished_jobs_list_in_csv
  list = @run_records.list_all_not_finished
  header = "job_id, created_at, run_id, num_runs, omp_threads, mpi_processes"

  jobids = list.map do |rec|
    rec[:job_id]
  end
  jobids = jobids.sort.uniq

  csv_list = []
  jobids.each do |jid|
    strings = [ jid]
    found = list.find_all do |r| r[:job_id] == jid end
    found_first = found.first
    strings << found_first[:created_at].to_s.split('T')[0]
    strings << found_first[:id]
    strings << found.size
    strings << found_first[:omp_threads]
    strings << found_first[:mpi_processes]
    csv_list << strings.join(", ")
  end
  return header, csv_list
end

#path_to_run_layerObject



61
62
63
# File 'lib/nera/nera_run_layer_controller.rb', line 61

def path_to_run_layer
  return @db_folders.path_to_run_layer( @sim_instance.class, @param_id)
end