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



201
202
203
204
205
206
207
208
209
# File 'lib/pwrake/branch/branch.rb', line 201

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

#kill(sig = "INT") ⇒ Object



196
197
198
199
# File 'lib/pwrake/branch/branch.rb', line 196

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

#read_worker_progs(worker_progs) ⇒ Object



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

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
# File 'lib/pwrake/branch/branch.rb', line 40

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

#setup_fiberObject



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

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



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

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_shellObject



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/branch.rb', line 99

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



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

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