Class: NERA::RunRecords

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

Constant Summary collapse

ATTRIBUTES =

keys of run records

[ [:id, Integer], [:state, Symbol], [:created_at, DateTime],
  [:job_id, Integer], [:seed, Integer], 
  [:start_at, DateTime], [:finish_at, DateTime], 
  [:included_at, DateTime], [:real_time, Float], 
  [:user_time, Float], [:host_name, String], 
  [:omp_threads, Integer], [:mpi_processes, Integer]
]
SEED_MAX =

Maximum of the seed

2**31

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_file, param_recs, param_id) ⇒ RunRecords

run record

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/nera/nera_run_records.rb', line 27

def initialize( table_file, param_recs, param_id)
  raise ArgumentError unless param_recs.is_a?( NERA::ParameterRecords) and param_id.is_a?(Integer)
  
  @keys = ATTRIBUTES.map do |k| k[0] end
  @db = NERA::Database.new( table_file, param_id)
  @db.add_observer( param_recs)
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



24
25
26
# File 'lib/nera/nera_run_records.rb', line 24

def keys
  @keys
end

Class Method Details

.create_table(table_file) ⇒ Object



40
41
42
# File 'lib/nera/nera_run_records.rb', line 40

def self.create_table(table_file)
  NERA::Database.create_table( table_file)
end

Instance Method Details

#add(num_runs, omp_threads = 1, mpi_procs = 1) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nera/nera_run_records.rb', line 45

def add( num_runs, omp_threads = 1, mpi_procs = 1)
  # check arguments
  unless num_runs.is_a?(Integer) and num_runs > 0
    raise ArgumentError, "Number of runs must be a postive integer."
  end
  unless (omp_threads.is_a?(Integer) and omp_threads > 0) or omp_threads == nil
    raise ArgumentError, "omp_threads must be a postive integer or nil"
  end
  unless (mpi_procs.is_a?(Integer) and mpi_procs > 0) or mpi_procs == nil
    raise ArgumentError, "mpi_procs must be a postive integer or nil"
  end

  stats = []
  @db.transaction {
    seeds = find_unique_seed( num_runs)
    d = DateTime.now
    num_runs.times do |i|
      h = {:state => :not_finished, :created_at => d, :job_id => nil, :seed => seeds[i], :omp_threads => omp_threads, :mpi_processes => mpi_procs}
      new_id = @db.add( h)
      found = @db.find_by_id( new_id)
      stat = Hash.new
      stat[:id] = found[:id]
      stat[:seed] = found[:seed]
      stat[:omp_threads] = found[:omp_threads]
      stat[:mpi_processes] = found[:mpi_processes]
      stats << stat
    end
  }
  return stats
end

#destroy(id) ⇒ Object

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nera/nera_run_records.rb', line 157

def destroy( id)
  raise ArgumentError unless id.is_a?(Integer)

  flag = nil
  @db.transaction {
    found = @db.find_by_id(id)
    return nil unless found
    return nil if found[:state] == :finished
    flag = @db.destroy(id)
  }
  return flag
end

#destroy_job_id(job_id) ⇒ Object

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/nera/nera_run_records.rb', line 170

def destroy_job_id( job_id)
  raise ArgumentError unless job_id.is_a?(Integer)

  @db.transaction {
    found = list_all_not_finished.find_all do |rec|
      rec[:job_id] == job_id
    end
    return nil if found.size == 0
    found.each do |rec|
      f = @db.destroy( rec[:id])
      raise "must not happen" unless f
    end
  }
  return true
end

#list_allObject



109
110
111
112
113
114
# File 'lib/nera/nera_run_records.rb', line 109

def list_all
  matched = @db.find_all do |rec|
    true
  end
  return matched.to_a
end

#list_all_finishedObject



116
117
118
119
120
121
# File 'lib/nera/nera_run_records.rb', line 116

def list_all_finished
  matched = @db.find_all do |rec|
    rec[:state] == :finished
  end
  return matched.to_a
end

#list_all_not_finishedObject



123
124
125
126
127
128
# File 'lib/nera/nera_run_records.rb', line 123

def list_all_not_finished
  matched = @db.find_all do |rec|
    rec[:state] == :not_finished
  end
  return matched.to_a
end

#set_job_id(id, num_runs, j_id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nera/nera_run_records.rb', line 90

def set_job_id( id, num_runs, j_id)
  # check arguments
  unless id.is_a?(Integer) and num_runs.is_a?(Integer) and j_id.is_a?(Integer)
    raise ArgumentError, "Arguments must be Integers."
  end

  @db.transaction {
    for i in id...(id+num_runs) do
      r = @db.find_by_id(i)
      raise RuntimeError, "The record #{id} is not found." unless r
      raise RuntimeError, "The record #{id} already has job_id." if r[:job_id]
      r[:job_id] = j_id
      @db.update(r)
    end
    flag = true
  }
  return j_id
end

#set_yaml_file(yaml_path) ⇒ Object

argument is the path to runs.yml



36
37
38
# File 'lib/nera/nera_run_records.rb', line 36

def set_yaml_file( yaml_path)
  @db.set_yaml_file( yaml_path)
end

#transactionObject



186
187
188
189
190
# File 'lib/nera/nera_run_records.rb', line 186

def transaction
  @db.transaction{
    yield
  }
end

#update_state_to_finished(id, start_at, finish_at, included_at, real_time, user_time, host_name) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nera/nera_run_records.rb', line 130

def update_state_to_finished( id, start_at, finish_at, included_at, real_time, user_time, host_name)
  unless id.is_a?(Integer) and start_at.is_a?(DateTime) and finish_at.is_a?(DateTime)
    p id, start_at, finish_at
    raise ArgumentError
  end
  unless included_at.is_a?(DateTime) and real_time.is_a?(Float) and user_time.is_a?(Float) and host_name.is_a?(String)
    p included_at, real_time, user_time, host_name
    raise ArgumentError
  end

  flag = nil
  @db.transaction {
    found = @db.find_by_id( id)
    return nil unless found
    return nil if found[:state] == :finished
    found[:start_at] = start_at
    found[:finish_at] = finish_at
    found[:included_at] = included_at
    found[:real_time] = real_time
    found[:user_time] = user_time
    found[:host_name] = host_name
    found[:state] = :finished
    flag = @db.update( found)
  }
  return flag
end