Class: Pwrake::Communicator
- Inherits:
-
Object
- Object
- Pwrake::Communicator
- Defined in:
- lib/pwrake/branch/communicator.rb,
lib/pwrake/mpi/branch.rb
Defined Under Namespace
Classes: ConnectError
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#ipaddr ⇒ Object
readonly
Returns the value of attribute ipaddr.
-
#ncore ⇒ Object
readonly
Returns the value of attribute ncore.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#shells ⇒ Object
readonly
Returns the value of attribute shells.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
- #common_line(s) ⇒ Object
- #connect(worker_code) ⇒ Object
- #dropout(exc = nil) ⇒ Object
- #finish_shells ⇒ Object
-
#initialize(set, id, host, ncore, selector, option) ⇒ Communicator
constructor
A new instance of Communicator.
- #inspect ⇒ Object
- #new_channel ⇒ Object
- #setup_pipe(worker_code) ⇒ Object
Constructor Details
#initialize(set, id, host, ncore, selector, option) ⇒ Communicator
Returns a new instance of Communicator.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pwrake/branch/communicator.rb', line 43 def initialize(set,id,host,ncore,selector,option) @set = set @id = id @host = host @ncore = @ncore_given = ncore @selector = selector @option = option @shells = {} @ipaddr = [] end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
38 39 40 |
# File 'lib/pwrake/branch/communicator.rb', line 38 def channel @channel end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
39 40 41 |
# File 'lib/pwrake/branch/communicator.rb', line 39 def handler @handler end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
38 39 40 |
# File 'lib/pwrake/branch/communicator.rb', line 38 def host @host end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
38 39 40 |
# File 'lib/pwrake/branch/communicator.rb', line 38 def id @id end |
#ipaddr ⇒ Object (readonly)
Returns the value of attribute ipaddr.
41 42 43 |
# File 'lib/pwrake/branch/communicator.rb', line 41 def ipaddr @ipaddr end |
#ncore ⇒ Object (readonly)
Returns the value of attribute ncore.
38 39 40 |
# File 'lib/pwrake/branch/communicator.rb', line 38 def ncore @ncore end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
39 40 41 |
# File 'lib/pwrake/branch/communicator.rb', line 39 def reader @reader end |
#shells ⇒ Object (readonly)
Returns the value of attribute shells.
40 41 42 |
# File 'lib/pwrake/branch/communicator.rb', line 40 def shells @shells end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
39 40 41 |
# File 'lib/pwrake/branch/communicator.rb', line 39 def writer @writer end |
Instance Method Details
#common_line(s) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pwrake/branch/communicator.rb', line 121 def common_line(s) x = "Communicator#common_line(id=#{@id},host=#{@host})" case s when /^heartbeat$/ Log.debug "#{x}: #{s.inspect}" when /^exited$/ Log.debug "#{x}: #{s.inspect}" return false when /^log:(.*)$/ Log.info "#{x}: log>#{$1}" when String Log.warn "#{x}: out>#{s.inspect}" when Exception Log.warn "#{x}: err>#{s.class}: #{s.}" dropout(s) return false else raise ConnectError, "#{x}: invalid for read: #{s.inspect}" end true end |
#connect(worker_code) ⇒ Object
82 83 84 85 86 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 119 |
# File 'lib/pwrake/branch/communicator.rb', line 82 def connect(worker_code) setup_pipe(worker_code) # send ncore and options opts = Marshal.dump(@option) s = [@ncore||0, opts.size].pack("V2") @iow.write(s) @iow.write(opts) sel = @set.selector @reader = NBIO::MultiReader.new(sel,@ior) @writer = NBIO::Writer.new(sel,@iow) @handler = NBIO::Handler.new(@reader,@writer,@host) # read ncore while s = @reader.get_line case s when /^ip:(.*)$/ a = $1 @ipaddr.push(a) Log.debug "ip=#{a} @#{@host}" when /^ncore:(.*)$/ a = $1 Log.debug "ncore=#{a} @#{@host}" if /^(\d+)$/ =~ a @ncore = $1.to_i return false else raise ConnectError, "invalid for ncore: #{a.inspect}" end else return false if !common_line(s) end end raise ConnectError, "lost connection to #{@host} during setup" rescue => e dropout(e) end |
#dropout(exc = nil) ⇒ Object
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 176 177 178 179 180 |
# File 'lib/pwrake/branch/communicator.rb', line 147 def dropout(exc=nil) # Finish worker begin finish_shells if @handler @handler.exit @handler = nil end rescue => e m = Log.bt(e) $stderr.puts(m) Log.error(m) end # Error output from worker if @ioe err_out = ["standard error from worker:"] while s = @ioe.gets err_out << s.chomp end if err_out.size > 1 m = err_out.join("\n ") $stderr.puts(m) Log.error(m) end end # Exception message if exc m = Log.bt(exc) $stderr.puts(m) Log.error(m) end ensure @set.delete(self) end |
#finish_shells ⇒ Object
143 144 145 |
# File 'lib/pwrake/branch/communicator.rb', line 143 def finish_shells @shells.keys.each{|sh| sh.finish_task_q} end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/pwrake/branch/communicator.rb', line 54 def inspect "#<#{self.class} @id=#{@id},@host=#{@host},@ncore=#{@ncore}>" end |
#new_channel ⇒ Object
58 59 60 61 |
# File 'lib/pwrake/branch/communicator.rb', line 58 def new_channel i,q = @reader.new_queue CommChannel.new(@host,i,q,@writer,[@ior,@iow,@ioe]) end |
#setup_pipe(worker_code) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pwrake/branch/communicator.rb', line 63 def setup_pipe(worker_code) rb_cmd = "ruby -e 'eval ARGF.read(#{worker_code.size})'" if %w[127.0.0.1 ::1].include?(IPSocket.getaddress(@host)) cmd = rb_cmd else cmd = "ssh -x -T #{@option[:ssh_option]} #{@host} \"#{rb_cmd}\"" end # @ior,w0 = IO.pipe @ioe,w1 = IO.pipe r2,@iow = IO.pipe @pid = Kernel.spawn(cmd,:pgroup=>true,:out=>w0,:err=>w1,:in=>r2) w0.close w1.close r2.close # send worker_code @iow.write(worker_code) end |