8
Every day, every minute, ... every microsecond, many decisions are made by your computer. In high-level languages, these typically take the form of statements like if
, while
and for
, but at the most basic level there exist machine language instructions called branch/jump instructions. Modern processors queue instructions up in a pipeline, and this means that the processor needs to decide whether to fill the pipeline with instructions immediately following a branch (i.e. it is not taken) or the instructions at the branch destination.
If the processor does not guess correctly, the instructions which have been wrongly entered into the pipeline need to be ignored and the correct instructions must be fetched, causing a delay. The job of the branch predictor is to try and guess whether the branch will be taken to avoid the costly action of refilling the pipeline.
You must write a predictor that will, given a sequence of past decisions, guess the next decision correctly. Your program can be written in any language, provided you specify the link to its interpreter if it's an obscure/golfing language. It must take the past actual history as its first command-line argument, but it will not be provided for the first guess of the sequence. You must then return your guess by printing it to stdout. A decision is of the form "y" or "n". Each test case is a sequence of 72 decisions, so you can assume that the specified history argument will never be longer than 71 characters. For example, the "Alternating 1" test:
ynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynyn
You will be disqualified if your program:
- does not return a result within one second
- does not return either a
y
orn
(new lines don't matter and are ignored) - attempts to modify any code or file associated with this challenge, including other contender's code
- contains anything otherwise malicious
You may use a file for persistence if you wish, but it must be uniquely named and conform to the above.
This is a code-challenge, not code-golf. Victory will be granted by the branch predictor predictor to the contender whose solution successfully predicts the most branches in the whole test suite. Tests:
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,All yes
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn,All no
ynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynynyn,Alternating 1
nnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyynnyy,Alternating 2
yyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnnyyynnn,Alternating 3
nnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyy,Alternating 4
yyyyyynnnnnnyyyyyynnnnnnyyyyyynnnnnnyyyyyynnnnnnyyyyyynnnnnnyyyyyynnnnnn,Alternating 5
nnnnnnnnnnnnyyyyyyyyyyyynnnnnnnnnnnnyyyyyyyyyyyynnnnnnnnnnnnyyyyyyyyyyyy,Alternating 6
yyyyyyyyyyyyyyyyyynnnnnnnnnnnnnnnnnnyyyyyyyyyyyyyyyyyynnnnnnnnnnnnnnnnnn,Alternating 7
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyynnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn,Alternating 8
yynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyynyyn,2-1
ynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnnynnn,1-3
nyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyy,5-1
nnnnnnnnnnnynnnnnnnnnnnynnnnnnnnnnnynnnnnnnnnnnynnnnnnnnnnnynnnnnnnnnnny,1-11
nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyynyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,35-1
yynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnnyynnnn,2-4
ynnyyynyynnnynnyyynyynnnynnyyynyynnnynnyyynyynnnynnyyynyynnnynnyyynyynnn,1-2-3
ynynynynynynynynynynynynynynynynynynyyyyyyyyyyyyyyyyyynnnnnnnnnnnnnnnnnn,A1/A7
yyyyyynnnnnnyyyyyynnnnnnyyyyyynnnnnnnnyynnyynnyynnyynnyynnyynnyynnyynnyy,A5/A2
nnnnnnnnnnnnyyyyyyyyyyyynnnnnnnnnnnnyyyynnnnyyyynnnnyyyynnnnyyyynnnnyyyy,A6/A4
nyyyyynyyyyynyyyyynyyyyynyyyyynyyyyynyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,5-1/35-1
yyynnnyyynnnyyynnnnyyyyyyyyyyyyyyyyyyyyyyynyyyyynyyyyyyynnnnyynnnnyynnnn,A3/Y/5-1/2-4
yynnnnyynnnnyynnnnnyynnnynnyyynyynnnnnnynnnynnnynnnynnyynyynyynyynyynyyn,2-4/1-2-3/1-3/2-1
The full set of tests and runner program are situated at this GitHub repository. Simply add your predictor to src/predictors.txt
in the form <name>,<command to run>
and run main.py
. I have provided two very basic predictors already for simple testing. I would recommend a column width of at least 92 when running the runner for nice output. If you happen to spot any bugs with it, please let me know. :)
3I downvoted this challenge because in my opinion it does not capture the essence of a branch predictor. Right now your challenge is more about general predicion AI rather than a branch predictor. At the very least a branch predictor challenge must have very stringent memory requirements, incremental history, and every decision must be a function of a memory address. Also, your challenge is not self-contained. – orlp – 2016-01-20T21:25:29.187
4I like this challenge. I think that a purely 50/50 random test case is a bad idea. However, an 80/20 random would be fine, as well as other test cases that include parts that are random (as some of your test cases currently do). I'd recommend including all of your test cases on your post. – Nathan Merrill – 2016-01-20T21:29:29.660
@NathanMerrill Thanks, I've removed all randomness from tests. – Doddy – 2016-01-20T21:35:33.690
@orlp I'm not sure how to incorporate every aspect of a branch predictor into a challenge that's relatively easy and (hopefully) fun. With regards to it being a general AI predictor: isn't that what branch predictors are, at a high-level, when you take away the more complex elements? This challenge is also about educating those who are in this area, i.e. computing, and provoke some thought about performance (not that BP is usually the biggest performance issue, but I think you get me). – Doddy – 2016-01-20T21:48:05.640
No, the interesting part of a branch predictor is how to manage a limited amount of shared space in order to predict branches at arbitrary positions in the executable code. The challenge as-is is more of a coin flipping game than a branch predictor. – orlp – 2016-01-20T21:52:16.383
@orlp How is this challenge in any way like coin flipping? The test cases have been refined to contain situations that a branch predictor would likely encounter. – Doddy – 2016-01-20T21:59:28.503
1
@Doddy Sorry, I meant coin guessing game. One player keeps generating zeros and ones, and the other tries to guess then.
– orlp – 2016-01-20T22:19:03.653In high school a friend of mine write a TI85 BASIC game where you tried to stump a pattern guesser while entering 1/0 bits. I wish I had his code to submit for this. – Sparr – 2016-01-21T02:07:30.510
2are we supposed to avoid optimizing for these test cases? someone is going to just submit a "perfect" algorithm that compares the history to the known test corpus and scores 95%+ – Sparr – 2016-01-21T02:10:40.853
@Sparr I'm not sure, but I believe you're generally not supposed to do that anyway. Unless, of course, the specs explicitly say otherwise. – SuperJedi224 – 2016-01-22T19:53:36.550
this would make an interesting kolmogorov-complexity code-golf challenge, if everyone was trying to make a perfect predictor for this data set as short as possible. – Sparr – 2016-01-22T20:28:36.380
I keep getting disqualified for no apparent reason. I think I print my result within one second (other types of testing showed me that), and I print a "y" or a "n"... – TheCoffeeCup – 2016-01-23T00:46:18.507
Ohhh... I see... From the editing of your program just for the sake of testing, I see that there is a but in your program. It thinks that a "y" is "121", as the ASCII value of "y" is 121... – TheCoffeeCup – 2016-01-23T00:49:12.360
I'm pretty sure some of the tests are wrong.
yyyyyynnnnnn,Alternating 5
noticed that this is sixy
's in a row, etc. All the Alternating 5-8 test cases are wrong. – mbomb007 – 2017-03-23T19:02:34.920