#!/usr/bin/env ruby # frozen_string_literal: true # Extract the data files and fitted constants used by paper/kgrid_experiments.tex. # # paper_data.rb [OUTDIR] (reads out/ and results/; writes OUTDIR, default paper/data/) # # Inputs : out/range_1e7.csv, out/stats_1e7.csv, results/scaling.csv, # results/spread_*.csv, out/query_1e16.log, out/poly_1e{8..16}.txt # Outputs: paper/data/*.dat (whitespace separated, for pgfplots) and # paper/data/macros.tex (fitted exponents and summary numbers). ROOT = File.expand_path('..', __dir__) OUT = ARGV[0] ? File.expand_path(ARGV[0]) : File.join(ROOT, 'paper', 'data') def path(*p) = File.join(ROOT, *p) # least squares fit of log y = log a + b log x; returns [a, b] def loglog_fit(pts) xs = pts.map { |x, _| Math.log(x) } ys = pts.map { |_, y| Math.log(y) } n = xs.size.to_f mx = xs.sum / n my = ys.sum / n b = xs.zip(ys).sum { |x, y| (x - mx) * (y - my) } / xs.sum { |x| (x - mx)**2 } [Math.exp(my - b * mx), b] end def write_dat(name, header, rows) File.open(File.join(OUT, name), 'w') do |f| f.puts header rows.each { |r| f.puts r.join(' ') } end end def gcd(a, b) = a.abs.gcd(b.abs) # polygon statistics from a vertex file def poly_stats(file) verts = File.readlines(file).grep_v(/^#/).map { |l| l.split.map(&:to_i) } bd = 0 longest = 0.0 verts.each_with_index do |p, i| q = verts[(i + 1) % verts.size] dx = q[0] - p[0] dy = q[1] - p[1] bd += gcd(dx, dy) longest = [longest, Math.hypot(dx, dy)].max end [verts.size, bd, longest] end # --- all counts up to 10^7: log-spaced sample, Delta* and feasibility stats def scan_range wanted = {} (0..280).each { |t| wanted[(10**(t / 40.0)).round] = true } sample = [] ds_hist = Hash.new(0) nf_hist = Hash.new(0) ds_max = 0.0 ds_sum = 0.0 count = 0 File.foreach(path('out', 'range_1e7.csv')) do |line| next if line.start_with?('k,') f = line.split(',') k = f[0].to_i ds = f[7].to_f count += 1 ds_sum += ds ds_max = ds if ds > ds_max ds_hist[ds.floor] += 1 nf_hist[f[8].to_i] += 1 next unless wanted[k] && k >= 3 per = f[1].to_f sample << [k, f[4].to_i, f[10].to_i, f[11].to_f, 2 * Math.sqrt(Math::PI * k) - per, per] end [sample, ds_hist, nf_hist, ds_max, ds_sum / count, count] end # --- large single queries from polygon files def big_queries (8..16).filter_map do |e| file = path('out', "poly_1e#{e}.txt") next unless File.exist?(file) per = File.foreach(file).find { |l| l.start_with?('# perimeter') }.split(':').last.to_f k = 10**e nv, bd, le = poly_stats(file) [k, nv, bd, le, 2 * Math.sqrt(Math::PI * k) - per, per] end end def time_points pts = [] File.foreach(path('results', 'scaling.csv')) do |l| f = l.split(',') next unless f[0] =~ /^\d+$/ pts << [f[1].to_i, f[6].to_f, f[7][/[\d.]+/].to_f, 'pow'] end Dir[path('results', 'spread_*.csv')].each do |file| File.foreach(file) do |l| f = l.split(',') next unless f[0] =~ /^\d+$/ pts << [f[0].to_i, f[5].to_f, f[6][/[\d.]+/].to_f, 'spread'] end end log16 = path('out', 'query_1e16.log') if File.exist?(log16) t = File.foreach(log16).find { |l| l.start_with?('time') } pts << [10**16, t[/[\d.]+ s/].to_f, t[/RSS ([\d.]+)/, 1].to_f, 'pow'] if t end pts.select { |p| p[0] >= 10**4 }.sort_by(&:first) end def radius_stats rows = [] File.foreach(path('out', 'stats_1e7.csv')) do |l| f = l.split(',') next unless f[0] =~ /^\d+$/ # m, ks, dp_nodes, items, table_len, groups, work, delta*, secs rows << [f[0].to_i, f[1].to_i, f[5].to_i, f[6].to_i, f[7].to_i, f[8].to_i, f[9].to_i, f[11].to_f, f[12].to_f] end rows end Dir.mkdir(OUT) unless Dir.exist?(OUT) sample, ds_hist, nf_hist, ds_max, ds_mean, count = scan_range big = big_queries write_dat('structure.dat', 'k vertices boundary longest deficit perimeter', sample) write_dat('structure_big.dat', 'k vertices boundary longest deficit perimeter', big) tp = time_points write_dat('time_pow.dat', 'k secs rssMB', tp.select { |p| p[3] == 'pow' }.map { |p| p[0, 3] }) write_dat('time_spread.dat', 'k secs rssMB', tp.select { |p| p[3] == 'spread' }.map { |p| p[0, 3] }) rs = radius_stats write_dat('radius.dat', 'm ks dpnodes items tablelen groups work deltastar secs', rs.select { |r| (r[0] % 7).zero? || r[0] < 50 }) macros = {} fitpts = ->(col) { (sample.select { |r| r[0] >= 10**4 } + big).map { |r| [r[0], r[col]] } } %w[Vert Bound Long].each_with_index do |name, i| a, b = loglog_fit(fitpts.call(i + 1)) macros["fit#{name}A"] = format('%.3f', a) macros["fit#{name}B"] = format('%.3f', b) end a, b = loglog_fit(fitpts.call(4)) macros["fitDefA"] = format("%.3f", a) macros["fitDefB"] = format("%.3f", b) a, b = loglog_fit(tp.select { |p| p[0] >= 10**10 }.map { |p| [p[0], p[1]] }) macros['fitTimeA'] = format('%.2e', a).sub(/e(-?)0*(\d+)/, '\\cdot 10^{\1\2}') macros['fitTimeB'] = format('%.3f', b) macros['fitTimeAnum'] = format('%.4e', a) a, b = loglog_fit(rs.select { |r| r[0] >= 200 }.map { |r| [r[0], r[6].to_f / r[1]] }) macros['fitWorkPerKB'] = format('%.3f', b) a, b = loglog_fit(rs.select { |r| r[0] >= 200 }.map { |r| [r[0], r[6]] }) macros['fitWorkA'] = format('%.2f', a) macros['fitWorkB'] = format('%.3f', b) a, b = loglog_fit(rs.select { |r| r[0] >= 200 }.map { |r| [r[0], r[4]] }) macros['fitTableB'] = format('%.3f', b) a, b = loglog_fit(rs.select { |r| r[0] >= 200 }.map { |r| [r[0], r[2]] }) macros['fitNodesB'] = format('%.3f', b) macros['dsMax'] = format('%.2f', ds_max) macros['dsMean'] = format('%.2f', ds_mean) macros['nCounts'] = count.to_s tot = ds_hist.values.sum.to_f macros['dsBelowOne'] = format('%.1f', 100 * ds_hist[0] / tot) macros['dsBelowTwo'] = format('%.2f', 100 * (ds_hist[0] + ds_hist[1]) / tot) ntot = nf_hist.values.sum.to_f macros['nfOne'] = format('%.1f', 100 * nf_hist[1] / ntot) macros['nfTwo'] = format('%.1f', 100 * nf_hist[2] / ntot) macros['nfMax'] = nf_hist.keys.max.to_s # table of single queries at powers of ten def tex_int(n) = n.to_s.gsub(/(\d)(?=(\d{3})+$)/, '\1{,}') def tex_time(s) return format('%.3f', s) if s < 1 return format('%.1f', s) if s < 100 return format('%.0f', s) if s < 600 format('%.0f\\,min', s / 60) end def tex_mem(mb) = mb < 1000 ? format('%.0f\\,MB', mb) : format('%.1f\\,GB', mb / 1000) stats_by_k = (sample + big).to_h { |r| [r[0], r] } times_by_k = tp.select { |p| p[3] == 'pow' }.to_h { |p| [p[0], p] } File.open(File.join(OUT, 'table_single.tex'), 'w') do |f| (4..16).each do |e| k = 10**e s = stats_by_k[k] t = times_by_k[k] next unless s && t f.puts format('$10^{%d}$ & %.6f & %s & %s & %.1f & %s & %s \\\\', e, s[5], tex_int(s[1]), tex_int(s[2]), s[3], tex_time(t[1]), tex_mem(t[2])) end end File.open(File.join(OUT, 'macros.tex'), 'w') do |f| f.puts '% generated by scripts/paper_data.rb' macros.each { |k, v| f.puts "\\newcommand{\\#{k}}{#{v}}" } end puts macros.map { |k, v| "#{k}=#{v}" }.join(' ') puts "nf_hist=#{nf_hist.sort.to_h} ds_hist=#{ds_hist.sort.to_h}" puts "big=#{big.map { |r| r.map { |x| x.is_a?(Float) ? x.round(4) : x } }}" # table of the per-decade spread runs (results/spread_*.csv); a count is put in # decade e = floor(log10(1.01 k)), so the last count of the radius just below # 10^e belongs to decade e. spread = Hash.new { |h, e| h[e] = [] } Dir[path('results', 'spread_*.csv')].each do |file| File.foreach(file) do |l| f = l.split(',') next unless f[0] =~ /^\d+$/ spread[Math.log10(1.01 * f[0].to_i).floor] << [f[5].to_f, f[6][/[\d.]+/].to_f, f[3].to_f] end end File.open(File.join(OUT, 'table_spread.tex'), 'w') do |f| spread.sort.each do |e, rows| t = rows.map(&:first).sort f.puts format('$[10^{%d},10^{%d})$ & %d & %s\\,s & %s\\,s & %s & %.2f \\\\', e, e + 1, rows.size, tex_time(t.last), tex_time(t[t.size / 2]), tex_mem(rows.map { |r| r[1] }.max), rows.map(&:last).max) end end puts "spread runs=#{spread.values.sum(&:size)}" # group-update comparison (results/rowmax_compare.csv from scripts/rowmax_compare.rb) rm_file = path('results', 'rowmax_compare.csv') if File.exist?(rm_file) rm = Hash.new { |h, k| h[k] = {} } File.foreach(rm_file) do |l| f = l.split(',') next unless f[0] =~ /^\d+$/ rm[f[0].to_i][f[1]] = { work: f[2].to_i, naive: f[3].to_i, dp: f[4].to_f } end sci = ->(x) { e = Math.log10(x).floor; format('$%.1f\\cdot10^{%d}$', x / 10.0**e, e) } File.open(File.join(OUT, 'table_rowmax.tex'), 'w') do |f| rm.sort.each do |k, r| f.puts format('$10^{%d}$ & %s & %s & %.3f & %.3f & %.3f \\\\', Math.log10(k).round, sci.call(r['smawk'][:work]), sci.call(r['smawk'][:naive]), r['plain'][:dp], r['dc'][:dp], r['smawk'][:dp]) end end end