Class: Pwrake::Communicator

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/branch/communicator.rb,
lib/pwrake/mpi/branch.rb

Defined Under Namespace

Classes: ConnectError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



38
39
40
# File 'lib/pwrake/branch/communicator.rb', line 38

def channel
  @channel
end

#handlerObject (readonly)

Returns the value of attribute handler.



39
40
41
# File 'lib/pwrake/branch/communicator.rb', line 39

def handler
  @handler
end

#hostObject (readonly)

Returns the value of attribute host.



38
39
40
# File 'lib/pwrake/branch/communicator.rb', line 38

def host
  @host
end

#idObject (readonly)

Returns the value of attribute id.



38
39
40
# File 'lib/pwrake/branch/communicator.rb', line 38

def id
  @id
end

#ipaddrObject (readonly)

Returns the value of attribute ipaddr.



41
42
43
# File 'lib/pwrake/branch/communicator.rb', line 41

def ipaddr
  @ipaddr
end

#ncoreObject (readonly)

Returns the value of attribute ncore.



38
39
40
# File 'lib/pwrake/branch/communicator.rb', line 38

def ncore
  @ncore
end

#readerObject (readonly)

Returns the value of attribute reader.



39
40
41
# File 'lib/pwrake/branch/communicator.rb', line 39

def reader
  @reader
end

#shellsObject (readonly)

Returns the value of attribute shells.



40
41
42
# File 'lib/pwrake/branch/communicator.rb', line 40

def shells
  @shells
end

#writerObject (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



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

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.message}"
    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
120
# 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)
  @rd_err = NBIO::Reader.new(sel,@ioe)
  @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



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 148

def dropout(exc=nil)
  # Error output
  err_out = []
  begin
    finish_shells
    if @handler
      @handler.exit
      @handler = nil
    end
    if @rd_err
      while s = @rd_err.get_line
        err_out << s
      end
    end
  rescue => e
    m = Log.bt(e)
    #$stderr.puts m
    Log.error(m)
  end
  # Error output
  if !err_out.empty?
    $stderr.puts err_out.join("\n")
    Log.error((["process error output:"]+err_out).join("\n "))
  end
  # Exception
  if exc
    m = Log.bt(exc)
    #$stderr.puts m
    Log.error m
  end
ensure
  @set.delete(self)
end

#finishObject



182
183
184
185
186
187
188
189
190
# File 'lib/pwrake/branch/communicator.rb', line 182

def finish
  @iow.close
  while s=@ior.gets
    puts "out=#{s.chomp}"
  end
  while s=@ioe.gets
    puts "err=#{s.chomp}"
  end
end

#finish_shellsObject



144
145
146
# File 'lib/pwrake/branch/communicator.rb', line 144

def finish_shells
  @shells.keys.each{|sh| sh.finish_task_q}
end

#inspectObject



54
55
56
# File 'lib/pwrake/branch/communicator.rb', line 54

def inspect
  "#<#{self.class} @id=#{@id},@host=#{@host},@ncore=#{@ncore}>"
end

#new_channelObject



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