Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' of git@github.com:costan/pset_writeups
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Dec 9, 2009
2 parents 531c62a + 500b362 commit 94c1cd8
Show file tree
Hide file tree
Showing 341 changed files with 7,781 additions and 4 deletions.
101 changes: 101 additions & 0 deletions src/6.823/lab1/all.tex
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,101 @@
\section{Question 1}
Figure \ref{q1:frequencies} shows the dependencies obtained as suggested in the
lab handout.

\begin{figure}[htb]
\includegraphics[width=6.8in]{6.823/lab1/figs/frequencies.png}
\caption{Instruction dependency frequencies for the 11 benchmarks. }
\label{q1:frequencies}.
\end{figure}

The graph is not too helpful, except to tell us that most instructions depend
on registers written in the past 4-5 instructions. Therefore, we use figure
\ref{q1:frequencies_zoom} to take a better look at the dependencies at most 5
instructions apart.

\begin{figure}[htb]
\includegraphics[width=6.8in]{6.823/lab1/figs/frequencies_zoom.png}
\caption{Instruction dependency frequencies for the 11 benchmarks. }
\label{q1:frequencies_zoom}.
\end{figure}

Given the dependency statistics, it seesms like architecture A would be
significantly faster than architecture B. In each benchmark, between 40\% -
80\% of the instructions depend on the previous instruction. In architecture B,
all instructions with dependencies on the previous instruction would have to be
stalled for 1 cycle, while the previous instruction writes its registers. This
means 40\%-80\% pipeline bubbles, which could be avoided by the forwarding
circuitry.

\section{Question 2}

Figure \ref{q2:reg_frequencies} shows the per-register breakdown for the
instruction dependency. For each register and instruction distance, we computed
the proportion of dependencies that are owed to that register. We averaged the
values for each benchmark. We only plotted registers which contributed a
dependency of at least 0.005\% on for at least one distance.

\begin{figure}[htb]
\includegraphics[width=6.8in]{6.823/lab1/figs/reg_frequencies.png}
\caption{Instruction dependency frequencies broken down by register. }
\label{q2:reg_frequencies}.
\end{figure}

The graph suggests that a few registers are responsible for most dependencies.
This is not very surprising, given the x86 architecture's predilection to use
the \texttt{eax} register as an accumulator, \texttt{esi} and \texttt{edi}
as pointers, and the presence of a condition flags register.

The best (but most difficult to implement) suggestion would be to give the
instruction set a makeover so it looks more like a RISC instruction set. It's
not cool to have 8 registers, and instructions with limitations on the registers
they can use. However, as Alpha found out, this strategy might not work
commercially.\footnote{One could argue that, even though the strategy failed in
the 1990s, we're living in a different landscape right now, where many servers
are running some flavor of UNIX/Linux. So, maybe the instruction set will be
redesigned one day.}

Assuming we're stuck with the ISA, it seems that (limited) forwarding is very
worth-while. The dependencies seem to taper off after 4 cycles, so it's
probably not worth forwarding for more than 4 cycles. This is important in
super-pipelined architectures, like the latest Pentiums and Core processors.

\section{Question 3}
The trade-off in the analysis is, in a nutshell, simpler code versus more
accurate results. Ironically, the best way to measure the trade-off is to
actually get the accurate results, and see the error. Table \ref{q3:mse} shows
the MSE (mean-square error) for the histograms for each test, as well as the
differences in the first 3 frequencies (for distance 1 up to distance 3).

\begin{figure}[htb]
\center
\input{6.823/lab1/figs/accurate_delta.tex}
\caption{The MSE and differences in the first 3 frequencies between accurate
register accounting and approximating x86 8-bit and 16-bit partial registers
with the full-length register. }
\label{q3:mse}
\end{figure}

The tradeoff is not acceptable to me, since I'm a perfectionist, and I'd rather
write 100 lines of code (mostly obtained by copy-pasting 5 lines) to get
``perfect'' (exact) numbers. However, from a technical perspective, the
trade-off is perfectly acceptable, because the inacurracy introduced (less than
1\%) should be significantly smaller than the inaccuracy of counting predicated
instructions (e.g. the \texttt{rep\_\textit{xx}} opcodes) once, instead of
accurately counting them.

\section{Question 4}

The graphs suggest that architecture B has less general-purpose registers than
architecture A. Assuming the compiler is decent (gcc3.4 fits the bill) and its
optimizations are turned on (which would be expected when running benchmarks),
I would expect that the compiler will try to minimize the dependencies between
adjacent instructions (it seems like a good idea, no matter what the pipeline
length is).

In fact, the huge proportion of distance-1 dependencies in architecture B (80\%)
makes me guess that the architecture either has very few registers, or some
opcodes force the use of a certain register as an accumulator (for example,
early Intel x86 processors forced one of the sources and the destination of {\tt
mul} and {\tt div} to be {\tt \%eax}, the ``accumulator'' register), or the
opcodes use the 2-register format ($r_t = r_d$).
40 changes: 40 additions & 0 deletions src/6.823/lab1/code/accurate_delta.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env ruby
#
# Author:: Victor Costan
# Copyright:: none
# License:: Public Domain

require 'rubygems'
require 'gnuplot'
require '6.823/lab1/code/lab1common.rb'

bench_cases_fix_names 'original'
bench_cases_fix_names 'accurate'
cases = bench_cases
originals = bench_values(bench_cases)
accurates = bench_values(bench_cases, 'accurate')

def mse(v1, v2)
raise "The vectors don't have the same length" unless v1.length == v2.length
Math.sqrt((0...v1.length).map { |i| (v1[i] - v2[i]) ** 2 }.
inject(0) { |acc, n| acc + n } / v1.length)
end

max_distance = 3
File.open('6.823/lab1/figs/accurate_delta.tex', 'w') do |f|
f.write "\\begin{tabular}{lrrrr}\n\\hline\n"
f.write "Test & MSE"
1.upto(max_distance) { |d| f.write " & Distance #{d}" }
f.write " \\\\\n\\hline\n"
originals.keys.sort.each do |name|
v1, v2 = originals[name], accurates[name]
f.write "#{name} & #{'%.3f' % mse(v1, v2)}\\%"
0.upto(max_distance - 1) do |d|
f.write ' & '
f.write('%+.3f' % (v1[d - 1] - v2[d - 1]))
f.write "\\%"
end
f.write " \\\\\n\\hline\n"
end
f.write "\\end{tabular}\n"
end
61 changes: 61 additions & 0 deletions src/6.823/lab1/code/lab1common.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env ruby
#
# Author:: Victor Costan
# Copyright:: none
# License:: Public Domain


def bench_cases_fix_names(dir_name = 'original')
files = Dir.glob("6.823/lab1/data/#{dir_name}/*")

files.each_with_index do |file, i|
['.out', '.o'].each do |suffix|
len = suffix.length
next unless file[-len, len] == suffix
File.rename file, file[0...-len]
files[i] = file[0...-len]
end
end
end

def bench_cases
files = Dir.glob('6.823/lab1/data/original/*')

names = files.map { |file| File.basename(file) }.
map { |file| file[0...file.index('_base')] }
Hash[*names.zip(files).flatten]
end

def bench_values(cases, base_dir = 'original')
{}.tap do |values|
cases.each do |name, file|
numbers = File.read(file.gsub('/original/', "/#{base_dir}/")).split(',').
select { |token| !token.empty? }.map { |token| token.to_i }
sum = numbers.inject(0) { |acc, n| acc + n }.to_f
percentages = numbers.map { |n| n / sum }
values[name] = percentages
end
end
end

def bench_detailed_values(cases, base_dir = 'detailed')
{}.tap do |values|
cases.each do |name, file|
reg_stats, numbers = nil, nil
File.open(file.gsub('/original/', "/#{base_dir}/"), 'r') do |f|
nregs = f.gets.to_i
reg_stats = (0...nregs).map do
f.gets.strip.split(',').select { |token| !token.empty? }.
map { |token| token.to_i }
end
numbers = f.gets.split(',').select { |token| !token.empty? }.
map { |token| token.to_i }
end
reg_stats.each do |stat|
stat.each_index { |i| stat[i] /= numbers[i].to_f }
end

values[name] = {:numbers => numbers, :reg_stats => reg_stats}
end
end
end
54 changes: 54 additions & 0 deletions src/6.823/lab1/code/original_plot.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby
#
# Author:: Victor Costan
# Copyright:: none
# License:: Public Domain

# This program needs the gnuplot gem to run. Install with the following command:
# gem install gnuplot

require 'rubygems'
require 'gnuplot'
require '6.823/lab1/code/lab1common.rb'

bench_cases_fix_names 'original'
bench_cases_fix_names 'accurate'
cases = bench_cases
originals = bench_values(bench_cases)


Gnuplot.open do |gp|
Gnuplot::Plot.new gp do |plot|
plot.terminal 'png small size 1024,768'
plot.output '6.823/lab1/figs/frequencies.png'
plot.ylabel '% Instructions'
plot.xlabel 'Distance'

originals.keys.sort.each do |name|
data = originals[name]
plot.data << Gnuplot::DataSet.new([(1..data.length).to_a, data]) do |ds|
ds.title = name
ds.with = 'lines'
ds.linewidth = 1
end
end
end

maxpoints = 4
Gnuplot::Plot.new gp do |plot|
plot.terminal 'png small size 1024,768'
plot.output '6.823/lab1/figs/frequencies_zoom.png'
plot.ylabel '% Instructions'
plot.xlabel 'Distance'

originals.keys.sort.each do |name|
data = originals[name]
plot.data << Gnuplot::DataSet.new([(1..maxpoints).to_a,
data[0, maxpoints]]) do |ds|
ds.title = name
ds.with = 'lines'
ds.linewidth = 1
end
end
end
end
43 changes: 43 additions & 0 deletions src/6.823/lab1/code/per_register_plot.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env ruby
#
# Author:: Victor Costan
# Copyright:: none
# License:: Public Domain

# This program needs the gnuplot gem to run. Install with the following command:
# gem install gnuplot

require 'rubygems'
require 'gnuplot'
require '6.823/lab1/code/lab1common.rb'

bench_cases_fix_names 'detailed'
cases = bench_cases
details = bench_detailed_values bench_cases, 'detailed'

reg_count = details.values.first[:reg_stats].length
stat_count = details.values.first[:numbers].length
register_stats = (0...reg_count).map do |reg|
(0...stat_count).map do |i|
details.map { |name, detail| detail[:reg_stats][reg][i] }.
inject(0) { |acc, n| acc + n} / details.length
end
end

Gnuplot.open do |gp|
Gnuplot::Plot.new gp do |plot|
plot.terminal 'png small size 1024,768'
plot.output '6.823/lab1/figs/reg_frequencies.png'
plot.ylabel '% Instructions'
plot.xlabel 'Distance'

register_stats.each_with_index do |stats, i|
next if stats.max < 0.005
plot.data << Gnuplot::DataSet.new([(1..stat_count).to_a, stats]) do |ds|
ds.title = "Reg #{i}"
ds.with = 'lines'
ds.linewidth = 1
end
end
end
end
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
1047108636,252165086,122681484,60136336,111417706,29735778,20414680,48328895,2929923,22280847,969630,4502499,5518819,5597508,5373531,5289723,2992835,1842674,3303019,8141786,2340449,2011381,808271,421024,8591190,533186,338627,3426036,2242165,1212897,3141629,1185065,3019531,1071335,200345,2855576,91893,548276,17557,25717,432974,190469,270029,795646,3913931,464219,947773,526049,156737,107774,856405,589752,907977,8349,5828,19224,100985,1082205,312223,271020,219217,3088243,6846,5233,11663,65382,105916,48104,155000,100508,314612,6099,12062,465649,67238,80753,574981,104630,5783,505184,5477,666922,5403,501715,4355,173814,454992,612018,6755,3876,4026,6136,5372,2675,5255,3808,18520,65020,4099,3215,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
6758147487,801586904,687232152,15177101,63418426,66219749,354942,5256529,460726,240631,14675443,52249675,25565851,81910,29578342,33304065,11827736,14692804,5090722,359495,11801978,20131665,7888,15313762,27212565,34446255,10213221,14517311,10203861,5110857,180863,5136326,132041,28484026,81063,14149131,5100760,2570612,2541183,5082708,14439193,9853627,5269386,3748514,15598919,2570656,28487293,28951071,5110200,35154,139574,3894560,4705583,2580362,2498829,11326022,1105676,1428115,85869,4145524,17054656,30650,30725,1074376,2549948,16980422,3963858,1118346,1078170,6194652,1422539,1516707,4036781,84715,451,12479395,590,2325,2528651,76158,14690,4242381,2586056,3598,1217775,1087608,2560357,5080576,6531863,1118891,4717841,2159624,1452575,8417971,2945473,89275,1432530,84998,940,30456,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
4349320624,971428644,887447585,896575489,388201344,280640188,263465006,1283264280,206093193,202730348,322783342,52288372,29139878,29846050,44635284,18154233,67344881,17674364,46753242,40814025,41965613,25233926,13862056,25935100,26866454,14500241,21655249,10893933,18940247,23257898,7614827,15939075,10297264,5311606,2666980,4759805,895830,1121430,1061289,2531807,524890,4091322,9613951,5393736,5359605,6894338,1114355,7807430,2492408,3614488,1743780,1353617,232224,946723,333934,6209357,524414,4356901,715745,519385,2259403,1308745,3085482,4685527,4319920,6403613,2641296,2265548,2099495,1758309,105022,1040010,646687,2627837,6332018,6348205,7354904,6720568,4443859,7792785,3673066,2324632,6631777,2843766,3773060,3832242,2474094,2451063,2910366,4057450,3527456,3876840,1967323,3183026,2512868,3773890,4107077,2210772,2850921,2569145,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
1057145592,235129639,164096770,107005600,81726717,61044318,52223733,42326516,38400880,40919828,28167252,28323446,24440731,17912725,17650310,16878103,13869750,14592789,13317316,11518041,9538504,11502915,8302636,8970440,7624872,7902799,7512973,7254577,6540812,6070650,6175028,4488082,5420195,4537693,3931123,4599335,3899888,3994311,3691171,3918689,4105546,3658000,3138308,2994494,3139518,2718928,2648182,2637298,2151047,1819945,2010288,2164976,1969453,2341430,1874643,1726134,1996265,1794677,1760226,1643142,1611822,1777720,1272701,1295618,1135999,1159825,1159926,1436524,1169731,1207982,969052,1033102,1289916,1040663,937388,862202,1086969,984824,1184942,846939,1114715,827793,803580,691294,750841,806260,834905,688904,840322,738072,754941,675360,486066,548117,719253,590739,748691,592480,613482,607873,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
4214905136,953933376,584178057,267761334,252848100,108936758,115050063,145879598,79065665,84603724,58727725,89563821,65227853,61050117,60173995,42387409,74713567,60242271,91364883,71686548,73898555,51435741,53659792,41032426,35746627,34182778,35296859,26865945,29428838,33330354,31789307,32500485,29243672,24973540,31529082,33253240,36642116,29755033,33532206,34830389,22189841,25623068,25790692,27511449,25042200,26671559,27603684,33126882,17129881,30205826,23882245,30253123,26894837,34878485,23618409,23829962,26258419,20985966,24306018,19338695,26444087,20436637,15743750,17187486,20807341,22726529,16505708,22302802,21401768,11112484,17627940,15493690,14749793,13823170,13767594,13008208,12878509,11681767,9891715,15683586,12185469,14085056,12295184,15749957,11141818,13009005,12358816,14080537,12669092,13909405,7971506,10956665,13240849,11709425,11108805,11222514,13996867,11566087,11146423,11160238,
1 change: 1 addition & 0 deletions src/6.823/lab1/data/accurate/equake_base.none_______inp.in
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
1752305273,244693639,115173681,75309215,33136602,26183871,31109137,13866605,16583025,12947847,14019898,15582533,17290071,13804907,6022456,15180178,20070806,9561669,9898170,5774490,7872041,7792926,12452771,7953722,934023,10349781,2963685,2187852,5570636,2282926,1670478,3753673,4248030,3377548,2776086,3908091,5005038,3861268,3113310,2136771,3221178,4173497,488208,509106,1566890,1700722,1507096,779660,1349587,2334942,1015311,2089701,1003550,1769242,796766,7802606,1300316,471876,782860,5020711,1261734,1135758,1480060,2021668,630006,513038,713608,767307,753894,1040595,241591,1585138,727161,745703,427480,1335202,1536198,520328,664674,797791,1455298,709726,184397,685492,751603,902247,153589,429824,717140,201551,1638495,262696,1330448,672015,412492,759359,414070,959693,182039,1589405,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
637164513,142093954,87732309,71402717,45966650,44896393,47638471,24462950,21734864,21053316,13410777,12952929,9824035,9909490,8638980,8347952,5332097,5817408,6141256,4992309,8498858,4130178,5251507,5209901,3225440,4023789,3465722,3820539,2487734,2769803,2741300,2453934,2738866,2236524,1912082,2278884,1700746,947455,1989921,1507625,1331954,975472,998956,886451,945781,1142307,1026577,691791,537292,532956,440278,1150783,337645,904950,669212,1153189,351590,377606,833471,380138,448224,1163491,539992,365405,405560,320607,677383,321480,495783,515991,439267,592670,1278592,341018,292613,437299,1041190,335670,308504,407577,417713,519783,303545,318149,571188,246188,225490,224028,317268,260206,204925,159887,323551,306321,276976,140766,131628,283662,207979,192183,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
1443493370,328816052,303074799,127123686,149124309,98769574,103732742,26770252,41597523,100644908,31956310,30555925,68000293,23709146,7272735,30967080,4711309,21898880,19925152,15830717,19735923,8831257,17197836,11041789,14095040,29446469,7897247,11613014,8614914,7849031,9140884,3033245,9320359,794061,15120425,10014175,8367753,6572826,14151609,7123246,3183697,5099362,3749745,5447274,2391286,693890,918555,2322228,185147,2167291,1978936,831483,5450751,1346907,5583071,340375,4037128,4009154,92365,878680,627449,1335337,681129,120126,411226,496091,474477,173077,218115,267711,241614,444166,109925,188404,293602,122877,153809,132407,246549,186249,72282,105572,80104,147331,59626,192894,110810,156015,66797,100477,49536,77190,59856,66963,15723,68655,119067,116321,54066,38857,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
2012614036,597652299,297926314,219103920,157692810,175723969,164888012,130049991,105403434,71337011,83455200,55627694,61269417,62117427,78066849,30517364,47170727,52377426,27463922,29808877,27480920,33013745,16494964,20073674,28377202,16150778,21923627,21174489,16857483,11778939,14036220,24414049,18680352,14712382,24682916,21701770,6004408,21341776,10713823,11327217,22132897,22019930,11006497,10462516,11258883,12714318,8451667,15331039,13801464,8278684,7576997,17917688,8608361,20167659,20446239,15911445,15470182,4270049,5529064,11773818,6604988,7593217,7917858,3632136,2919961,2656999,10162673,6505206,46972,3945015,1357495,1345947,3955876,28855,3705496,8674424,35103,10852536,46721,5277218,3937399,40103,3967283,6572315,30210,6612199,3608798,14088091,9191984,8851794,7882961,7894622,12784016,3608785,10491045,14111254,3597165,8846841,3603981,3632060,
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
2701225550,669887518,421525485,478195162,267275648,175591064,266038996,132519783,105858561,89708772,59474473,46419325,49552088,51366197,31367332,23711043,22346959,26375392,36225939,26321183,13821868,10796693,13147200,17426132,21359526,14686592,17067891,10675837,5792956,3568538,2804089,4554867,2957986,3431430,4942828,4323798,4158625,6266574,5734683,3063459,5906439,6161416,5756628,2780123,4681496,3869578,3730615,3724639,2117755,2642923,2631204,850937,1219781,1259999,1078262,1026711,944134,1409931,1597784,1263946,823815,476448,774500,623919,926993,913595,1156957,1533315,518274,1188092,728976,397962,341793,300364,461988,293152,383352,214621,367123,292870,230572,192091,187601,607244,135005,467976,183096,455637,368800,274335,224291,156214,495431,225666,215143,217354,176710,216648,123633,298530,
1 change: 1 addition & 0 deletions src/6.823/lab1/data/accurate/swim_base.none_______swim.in
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
867515455,85612267,14672425,52018933,22896587,11064485,14981785,1336460,6053813,539929,8139735,3421649,285346,7130,1061233,5253861,6345,8918720,4199029,5774086,2888582,528601,2623214,6298962,3977,2623588,9316,3148808,5244805,2891379,265192,2623858,3676,8362,270282,1402,2623578,264152,1421,2622870,1192,1537,1783,2617425,2623056,2626924,729,1107,904,666,787,5181,2357358,840,837,6170,910,263929,1081,667,773,866,1097,578,4896,1298,743,1118,766,679,466,676,654,812,1012,585,364,461,358,411,360,712,319,510,666,803,366,407,679,456,265,817,539,263,258,581,478,518,630,431,
Loading

0 comments on commit 94c1cd8

Please sign in to comment.