Module: Pwrake::Parallelism

Defined in:
lib/pwrake/report/parallelism.rb

Class Method Summary collapse

Class Method Details

.count_start_end_by_pattern(csvtable, pattern) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/pwrake/report/parallelism.rb', line 239

def count_start_end_by_pattern(csvtable, pattern)
  h = Hash.new
  start_time = []

  csvtable.each do |row|
    cmd = get_command_key(row['command'], pattern)
    push_time_by_key(h, row, cmd, start_time)
  end

  h.each do |k,a|
    a.sort!{|x,y| x[0]<=>y[0]}
  end
  h
end

.count_start_end_from_csv(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pwrake/report/parallelism.rb', line 22

def count_start_end_from_csv(file)
  a = []
  start_time = []

  CSV.foreach(file,:headers=>true) do |row|
    push_time_event(a, row, start_time)
  end

  a.sort{|x,y| x[0]<=>y[0]}
end

.count_start_end_from_csv_table(csvtable) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/pwrake/report/parallelism.rb', line 33

def count_start_end_from_csv_table(csvtable)
  a = []
  start_time = []

  csvtable.each do |row|
    push_time_event(a, row, start_time)
  end

  a.sort{|x,y| x[0]<=>y[0]}
end

.exec_density(a) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pwrake/report/parallelism.rb', line 44

def exec_density(a)
  reso = 0.1
  delta = 1/reso
  t_end = a.last[0]
  n = (t_end/reso).round + 1
  d = (n+1).times.map{|i| [reso*i,0]}
  i = 0
  a.each do |x|
    while d[i+1][0] <= x[0]
      i += 1
    end
    if x[1] > 0
      d[i][1] += delta
    end
  end
  d
end

.get_command_key(s, pattern = []) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/pwrake/report/parallelism.rb', line 216

def get_command_key(s, pattern=[])
  pattern.each do |cmd,regex|
    if regex =~ s
      return cmd
    end
  end
  case s
  when /^\s*\((.*)$/
    get_command_key($1)
  when /^\s*\w+=\S+\s+(.*)$/
    get_command_key($1)
  when /^\s*([\w.,~^\/=+-]+)(.*)$/
    cmd, rest = $1, $2
    if cmd == "cd" && /[^;]*;(.*)$/ =~ rest
      return get_command_key($1)
    else
      cmd
    end
  else
    s[0..15]
  end
end

.plot_parallelism(file, fmt) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pwrake/report/parallelism.rb', line 62

def plot_parallelism(file,fmt)
  a = count_start_end_from_csv(file)
  return if a.size < 4

  #density = exec_density(a)

  base = file.sub(/\.csv$/,"")
  fpara = base+"_para.dat"

  n = a.size
  y = 0
  y_max = 0

  File.open(fpara,"w") do |f|
    i = 0
    t = 0
    y_pre = 0
    begin
      while i < n
        if a[i][0]-t > 0.001
          f.printf "%.3f %d\n", t, y_pre
          t = a[i][0]
          f.printf "%.3f %d\n", t, y
        end
        y += a[i][1]
        y_pre = y
        y_max = y if y > y_max
        i += 1
      end
    rescue
      p a[i]
    end
  end

  t_end = (a.last)[0]

  IO.popen("gnuplot","r+") do |f|
    f.puts "
set terminal #{fmt}
set output '#{base}.#{fmt}'
#set rmargin 10
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'

set arrow 1 from #{t_end},#{y_max*0.5} to #{t_end},0 linecolor rgb 'blue'
set label 1 at first #{t_end},#{y_max*0.5} right \"#{t_end}\\nsec\" textcolor rgb 'blue'

plot '#{fpara}' w l axis x1y1 title 'parallelism'
"
  end

  #puts "Parallelism plot: #{base}.#{fmt}"
end

.plot_parallelism2(csvtable, base, fmt) ⇒ Object



118
119
120
121
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
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
181
182
183
# File 'lib/pwrake/report/parallelism.rb', line 118

def plot_parallelism2(csvtable, base, fmt)
  a = count_start_end_from_csv_table(csvtable)
  return if a.size < 4

  density = exec_density(a)

  fimg = base+'/parallelism.'+fmt

  n = a.size
  i = 0
  y = 0
  y_max = 0

  para = []
  begin
    t = 0
    y_pre = 0
    while i < n
      if a[i][0]-t > 0.001
        para.push "%.3f %d" % [t, y_pre]
        t = a[i][0]
        para.push "%.3f %d" % [t, y]
      end
      y += a[i][1]
      y_pre = y
      y_max = y if y > y_max
      i += 1
    end
  rescue
    p a[i]
  end

  t_end = (a.last)[0]

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    f.print "
set terminal #{fmt}
set output '#{fimg}'
#set rmargin 10
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'
set y2tics
set ytics nomirror
set y2label 'exec/sec'

set arrow 1 from #{t_end},#{y_max*0.5} to #{t_end},0 linecolor rgb 'blue'
set label 1 \"#{t_end}\\nsec\" at first #{t_end},#{y_max*0.5} right front textcolor rgb 'blue'

plot '-' w l axis x1y1 title 'parallelism', '-' w l axis x1y2 title 'exec/sec'
"
    para.each do |x|
      f.puts x
    end
    f.puts "e"

    density.each do |t,d|
      f.puts "#{t} #{d}"
    end
  end
  end

  #puts "Parallelism plot: #{fimg}"
  fimg
end

.plot_parallelism_by_host(csvtable, base, fmt) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/pwrake/report/parallelism.rb', line 333

def plot_parallelism_by_host(csvtable,base,fmt)
  fimg = base+"/para_host."+fmt
  data = read_time_by_host_from_csv(csvtable)
  return fimg if data.size == 0

  grid = []
  hosts = data.keys.sort
  hosts.each do |h|
    a = timeline_to_grid(data[h])
    grid << a
  end

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    f.puts "
set terminal #{fmt}
set output '#{fimg}'
#set rmargin 7
set lmargin 16
set pm3d map
set pm3d corners2color c1
set xlabel 'time (sec)'
set ytics nomirror
set ticslevel 0
set format y ''
"
    hosts.each_with_index do |h,i|
      if /^([^.]+)\./ =~ h
        h = $1
      end
      f.puts "set ytics add ('#{h}' #{i+0.5})"
    end
    f.puts "splot '-' using 2:1:3 with pm3d title ''"

    grid.each_with_index do |a,j|
      a.each do |x|
        f.printf "%g %g %d\n", j, x[0], x[1]
      end
      f.printf "\n"
    end
    j = grid.size
    grid.last.each do |x|
      f.printf "%g %g %d\n", j, x[0], x[1]
    end
    f.printf "e\n"
  end
  end
  fimg
end

.plot_parallelism_by_pattern(csvtable, base, pattern, fmt) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/pwrake/report/parallelism.rb', line 254

def plot_parallelism_by_pattern(csvtable, base, pattern, fmt)
  y_max = 0
  t_end = 0
  para = {}
  t_pat = count_start_end_by_pattern(csvtable, pattern)
  t_pat.each do |cmd,a|
    n = a.size
    i = 0
    y = 0
    para[cmd] = dat = []
    begin
      t = 0
      y_pre = 0
      while i < n
        if a[i][0]-t > 0.001
          dat.push "%.3f %d" % [t, y_pre]
          t = a[i][0]
          dat.push "%.3f %d" % [t, y]
        end
        y += a[i][1]
        y_pre = y
        y_max = y if y > y_max
        i += 1
      end
    rescue
      p a[i]
    end
    if (a.last)[0] > t_end
      t_end = (a.last)[0]
    end
  end

  fimg = base+'/para_cmd.'+fmt

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    #begin f = $stdout
    f.print "
set terminal #{fmt}
set output '#{fimg}'
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'
"
    f.print "plot "
    f.puts para.map{|cmd,re| "'-' w l title #{cmd.inspect}"}.join(",")
    para.each do |cmd,dat|
      dat.each do |x|
        f.puts x
      end
      f.puts "e"
    end
  end
  end

  #puts "Parallelism plot: #{fimg}"
  fimg
end

.push_time_by_key(h, row, key, start_time) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/pwrake/report/parallelism.rb', line 186

def push_time_by_key(h, row, key, start_time)
  command = row['command']
  if command == 'pwrake_profile_start'
    start_time[0] = Time.parse(row['start_time'])
  elsif command == 'pwrake_profile_end'
    t = Time.parse(row['start_time']) - start_time[0]
    h.each_value do |v|
      v << [t,0]
    end
  elsif start_time[0]
    a = (h[key] ||= [])
    n = begin Integer(row['ncore']) rescue 1 end
    t = Time.parse(row['start_time']) - start_time[0]
    a << [t,+n]
    t = Time.parse(row['end_time']) - start_time[0]
    a << [t,-n]
  end
end

.push_time_event(a, row, start_time) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pwrake/report/parallelism.rb', line 6

def push_time_event(a, row, start_time)
  command = row['command']
  if command == 'pwrake_profile_start'
    start_time[0] = Time.parse(row['start_time'])
  elsif command == 'pwrake_profile_end'
    t = Time.parse(row['start_time']) - start_time[0]
    a << [t,0]
  elsif start_time[0]
    n = begin Integer(row['ncore']) rescue 1 end
    t = Time.parse(row['start_time']) - start_time[0]
    a << [t,+n]
    t = Time.parse(row['end_time']) - start_time[0]
    a << [t,-n]
  end
end

.read_time_by_host_from_csv(csvtable) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/pwrake/report/parallelism.rb', line 205

def read_time_by_host_from_csv(csvtable)
  a = {}
  start_time = []

  csvtable.each do |row|
    push_time_by_key(a, row, row['host'], start_time)
  end
  a
end

.timeline_to_grid(a) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/pwrake/report/parallelism.rb', line 313

def timeline_to_grid(a)
  resolution = Rational(1,10)

  a = a.sort{|x,y| x[0]<=>y[0]}
  #t_end = (a.last)[0]
  #ngrid = (t_end/resolution).floor
  grid = [[0,0]]

  j = 0
  a.each do |x|
    i = (x[0]/resolution).floor
    while j < i
      grid[j+1] = [j*resolution,grid[j][1]]
      j += 1
    end
    grid[i][1] += x[1]
  end
  return grid
end