Skip to content

Commit

Permalink
add a #nodes api to the job adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom committed Mar 27, 2024
1 parent 41fec50 commit 88a84a0
Show file tree
Hide file tree
Showing 5 changed files with 886 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/ood_core/job/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ def accounts
def queues
[]
end

# Return the list of nodes for this scheduler.
#
# Subclasses that do not implement this will return empty arrays.
# @return [Array<NodeInfo>]
def nodes
[]
end
end
end
end
28 changes: 28 additions & 0 deletions lib/ood_core/job/adapters/slurm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ def queues
end
end

def all_sinfo_node_fields
{
procs: '%c',
name: '%n',
features: '%f'
}
end

def nodes
args = all_sinfo_node_fields.values.join(UNIT_SEPARATOR)
output = call('sinfo', '-ho', "#{RECORD_SEPARATOR}#{args}")

output.each_line(RECORD_SEPARATOR).map do |line|
values = line.chomp(RECORD_SEPARATOR).strip.split(UNIT_SEPARATOR)

next if values.empty?

data = Hash[all_sinfo_node_fields.keys.zip(values)]
data[:name] = data[:name].to_s.split(',').first
data[:features] = data[:features].to_s.split(',')
NodeInfo.new(**data)
end.compact
end

private
def str_to_acct_info(line)
hsh = line.split(' ').map do |token|
Expand Down Expand Up @@ -669,6 +693,10 @@ def queues
@slurm.queues
end

def nodes
@slurm.nodes
end

private
# Convert duration to seconds
def duration_in_seconds(time)
Expand Down
13 changes: 11 additions & 2 deletions lib/ood_core/job/node_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ class NodeInfo
# @return [Integer, nil] number of procs
attr_reader :procs

# The features associated with this node.
# @return [Array<String>, []]
attr_reader :features

# @param name [#to_s] node name
# @param procs [#to_i, nil] number of procs
def initialize(name:, procs: nil, **_)
# @param features [#to_a, []] list of features
def initialize(name:, procs: nil, features: [], **_)
@name = name.to_s
@procs = procs && procs.to_i
@features = features.to_a
end

# Convert object to hash
# @return [Hash] object as hash
def to_h
{ name: name, procs: procs }
instance_variables.map do |var|
name = var.to_s.gsub('@', '').to_sym
[name, send(name)]
end.to_h
end

# The comparison operator
Expand Down
Loading

0 comments on commit 88a84a0

Please sign in to comment.