Class: Pwrake::Branch

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

Constant Summary collapse

@@io_class =
IO

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, r, w) ⇒ Branch

Returns a new instance of Branch.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pwrake/branch/branch.rb', line 18

def initialize(opts,r,w)
  Thread.abort_on_exception = true
  @option = opts
  @task_q = {}  # worker_id => FiberQueue.new
  @shells = []
  @ior = r
  @iow = w
  @selector = NBIO::Selector.new(@@io_class)
  @master_rd = NBIO::Reader.new(@selector,@ior)
  @master_wt = NBIO::Writer.new(@selector,@iow)
  @shell_start_interval = @option['SHELL_START_INTERVAL']

  # init_logger
  Log.set_logger(@option)
  if dir = @option['LOG_DIR']
    fn = File.join(dir,@option["COMMAND_CSV_FILE"])
    Shell.profiler.open(fn,@option['GNU_TIME'],@option['PLOT_PARALLELISM'])
  end
end

Class Method Details

.io_class=(io_class) ⇒ Object



14
15
16
# File 'lib/pwrake/branch/branch.rb', line 14

def self.io_class=(io_class)
  @@io_class = io_class
end

Instance Method Details

#finishObject



225
226
227
228
229
230
231
232
233
# File 'lib/pwrake/branch/branch.rb', line 225

def finish
  return if @finished
  @finished = true
  #Log.debug "Branch#finish: begin"
  @cs.exit
  Log.debug "Branch#finish: worker exited"
  @master_wt.put_line "exited"
  Log.debug "Branch#finish: sent 'exited' to master"
end

#invoke(t, args) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/pwrake/branch/branch.rb', line 208

def invoke(t,args)
  Log.debug "Branch#invoke start: #{t.class}[#{t.name}]"
  r,w = IO.pipe
  rd = NBIO::Reader.new(@selector,r)
  @search_que.enq([t,args,w])
  task_name = rd.get_line.chomp
  if t.name != task_name
    raise "t.name=#{t.name} != task_name=#{task_name}"
  end
  Log.debug "Branch#invoke end: #{t.class}[#{t.name}]"
end

#kill(sig = "INT") ⇒ Object



220
221
222
223
# File 'lib/pwrake/branch/branch.rb', line 220

def kill(sig="INT")
  Log.warn "Branch#kill #{sig}"
  @cs.kill(sig)
end

#read_worker_progs(worker_progs) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pwrake/branch/branch.rb', line 74

def read_worker_progs(worker_progs)
  code = ""
  modpath = {}
  worker_progs.each do |f|
    m = f.split(/\//).first
    if !modpath[m]
      $LOAD_PATH.each do |x|
        if File.directory?(File.join(x,m))
          modpath[m] = x
          break
        end
      end
      if !modpath[m]
        raise RuntimeError,"load path for module #{m} not found"
      end
    end
    path = File.join(modpath[m],f)
    path += '.rb' if /\.rb$/ !~ path
    if !File.exist?(path)
      raise RuntimeError,"program file #{path} not found"
    end
    code << IO.read(path) + "\n"
  end
  code
end

#runObject

Rakefile is loaded after ‘init’ before ‘run’



40
41
42
43
44
45
46
47
48
# File 'lib/pwrake/branch/branch.rb', line 40

def run
  setup_worker
  setup_shell
  setup_fiber
  setup_master_channel
  setup_search_thread
  @cs.run("task execution")
  Log.debug "Branch#run end"
end

#setup_fiberObject



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

def setup_fiber
  # start fibers
  @shells.each do |shell|
    shell.create_fiber(@master_wt).resume
  end
  Log.debug "all fiber started"

  @cs.each_value do |comm|
    #comm.start_default_fiber
    Fiber.new do
      while s = comm.reader.get_line
        break unless comm.common_line(s)
      end
      Log.debug "Branch#setup_fiber: end of fiber for default channel"
    end.resume
  end

  # setup end
  @cs.each_value do |comm|
    comm.writer.put_line "setup_end"
  end

  @master_wt.put_line "branch_setup:done"
  Log.debug "Branch#setup_fiber: setup end"
end

#setup_master_channelObject



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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/pwrake/branch/branch.rb', line 149

def setup_master_channel
  Fiber.new do
    while s = @master_rd.get_line
      # receive command from main pwrake
      Log.debug "Branch:recv #{s.inspect} from master"
      case s
        #
      when /^(\d+):(.+)$/o
        id, tname = $1,$2
        begin
          task_name = tname.sub(/^\d+:/,"")
          @task_q[id].enq(tname)
        rescue => e
          Log.error Log.bt(e)
          ret="taskfail:#{id}:#{task_name}"
          Log.debug "fail to enq task_q[#{id}], ret=#{ret}"
          @master_wt.put_line(ret)
        end
        #
      when /^exit$/
        #@task_q.each_value{|q| q.finish}
        #@cs.drop_all
        @cs.finish_shells

        #@shells.each{|shell| shell.exit} # just for comfirm
        #@selector.halt # should halt after exited
        break
        #
      when /^drop:(.*)$/o
        id = $1
        taskq = @task_q.delete(id)
        Log.debug "drop @task_q[#{id}]=#{taskq.inspect}"
        @cs.drop(id)
        break if @cs.empty?
        #
      when /^kill:(.*)$/o
        sig = $1
        kill(sig)
        break
        #
      else
        Log.debug "Branch: invalid line from master: #{s}"
      end
    end
    Log.debug "Branch#setup_master_channel: end of fiber for master channel"
  end.resume
end

#setup_search_threadObject



197
198
199
200
201
202
203
204
205
206
# File 'lib/pwrake/branch/branch.rb', line 197

def setup_search_thread
  @search_que = Queue.new
  Thread.new do
    while a = @search_que.deq
      t,args,w = *a
      t.pw_search_tasks(args)
      w.puts(t.name)
    end
  end
end

#setup_shellObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pwrake/branch/branch.rb', line 100

def setup_shell
  @shells = []
  @cs.each_value do |comm|
    @task_q[comm.id] = task_q = FiberQueue.new
    comm.ncore.times do
      chan = comm.new_channel
      shell = Shell.new(chan,comm,task_q,@option.worker_option)
      # wait for remote shell open
      Fiber.new do
        if shell.open
          @shells << shell
        else
          @master_wt.put_line "retire:#{comm.id}"
        end
        Log.debug "Branch#setup_shells: end of fiber to open shell"
      end.resume
      sleep @shell_start_interval
    end
  end

  @cs.run("setup shells")
end

#setup_workerObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pwrake/branch/branch.rb', line 50

def setup_worker
  @cs = CommunicatorSet.new(@master_rd,@selector,@option.worker_option)
  @cs.create_communicators
  worker_code = read_worker_progs(@option.worker_progs)
  @cs.each_value do |comm|
    Fiber.new do
      comm.connect(worker_code)
    end.resume
  end
  @cs.run("connect to workers")
  #
  Fiber.new do
    @cs.each_value do |comm|
      # set WorkerChannel#ncore at Master
      @master_wt.put_line "ncore:#{comm.id}:#{comm.ncore}"
      comm.ipaddr.each do |ipa|
        @master_wt.put_line "ip:#{comm.id}:#{ipa}"
      end
    end
    @master_wt.put_line "ncore:done"
  end.resume
  @selector.run
end