Extract line from Sf bench command

Code, algorithms, languages, construction...
quantum
Posts: 28
Joined: Mon Dec 19, 2011 12:19 am

Extract line from Sf bench command

Post by quantum » Thu Jan 02, 2014 11:15 am

I want to redirect the last line of Stockfish's bench output to a textfile on Ubuntu_64.

When I change line 155 in benchmark.cpp

Code: Select all

cerr << "\n==========================="
       << "\nTotal time (ms) : " << elapsed
       << "\nNodes searched  : " << nodes
       << "\nNodes/second    : " << 1000 * nodes / elapsed << endl;
to

Code: Select all

cout << "\n==========================="
       << "\nTotal time (ms) : " << elapsed
       << "\nNodes searched  : " << nodes
       << "\nNodes/second    : " << 1000 * nodes / elapsed << endl;
I can use
./stockfish bench > bench.txt && tail -1 bench.txt >> speed.csv
to do the job. When I run bench three times in a row my file looks as follows:

Code: Select all

Nodes/second    : 1074200
Nodes/second    : 1078206
Nodes/second    : 1065217
I now want to do the same with other compiles I find on the internet (e.g. abrok.eu) but I fail badly. Any idea on how to get there?

Jeremy Bernstein
Site Admin
Posts: 1226
Joined: Wed Jun 09, 2010 7:49 am
Real Name: Jeremy Bernstein
Location: Berlin, Germany
Contact:

Re: Extract line from Sf bench command

Post by Jeremy Bernstein » Thu Jan 02, 2014 11:41 am

Why don't you just redirect stderr (and stdout) to the file and then filter the results?
./stockfish bench &> bench.txt && tail -1 bench.txt >> speed.csv
Should do what you want without requiring any source code changes.

jb

quantum
Posts: 28
Joined: Mon Dec 19, 2011 12:19 am

Re: Extract line from Sf bench command

Post by quantum » Thu Jan 02, 2014 11:32 pm

Hey, thanks for your quick reply. :)

atm I try to put it in a script to run 50 cycles automatically.Lets see if I get it right.

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Extract line from Sf bench command

Post by lucasart » Fri Jan 03, 2014 3:54 am

A couple of tips for you

1/ stdin, stdout, stderr

Every C programmer knows that stdin=0, stdout=1, stderr=2, right? You can use that in the shell. For example, if you want to run a program myProg and get it to read stdin from input.txt, write its stdout to output.txt and write its errors to error.txt:

Code: Select all

./myProg 0<./input.txt 1>./output.txt 2>./error.txt
2/ /dev/null

Sometimes, it can be useful to use /dev/null. Basically it's like a file from a programming point of view, but when you write into it, it's a no-operation for the operating system. For example, when you run Stockfish bench command, and you only want to see stderr output and discard stdout:

Code: Select all

./stockfish bench >/dev/null
Which is the same as

Code: Select all

./stockfish bench 1>/dev/null
"Talk is cheap. Show me the code." -- Linus Torvalds.

quantum
Posts: 28
Joined: Mon Dec 19, 2011 12:19 am

Re: Extract line from Sf bench command

Post by quantum » Fri Jan 03, 2014 12:01 pm

Thanks for your explanation Lucas :)

I finally finished my little script which starts with bash coammand. Especially the part with the redirection was not straightforward for me.

Code: Select all

#!/bin/bash

for (( i = 1; i <= 50; i++ ))
do
    echo "$i. Iteration"
    ./stockfish_14010201_x64 bench > bench.txt 2>&1 && tail -1 bench.txt >> speed.csv
done  

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Extract line from Sf bench command

Post by lucasart » Fri Jan 03, 2014 1:58 pm

OK, I see you're becoming a shell guru now. Here is a more advanced version:

Code: Select all

x=$(./stockfish bench 2>&1|grep "Nodes\/second"|sed "s/.*: \([0-9]*\)/\1/")
echo $x
Try to figure out how it works. Once you've done that, you can do a loop and use the shell to calculate the

Code: Select all

average=sum(x_i)/50
The shell is really a powerful scripting language, almost a programming language in a certain sense. I think there was a (crazy) guy who programmed a chess engine in shell.

That being said, I think the shell is best used for really simple scripts. Beyond that its cryptic syntax and archaism make it not the ideal choice. I prefer Python for non trivial scripts.
"Talk is cheap. Show me the code." -- Linus Torvalds.

quantum
Posts: 28
Joined: Mon Dec 19, 2011 12:19 am

Re: Extract line from Sf bench command

Post by quantum » Fri Jan 03, 2014 9:45 pm

Here is my little script. This is some pretty cool stuff with sed. It is quite rewarding if you get your things working.
I wanted to save each run in an array and calculate the standard deviation but I mess up with the array stuff. Will take a look at this at a later time. I should rather study more serious than toying around with the bash :D

Do you think it is possible to change uci parameters in the bash and then run bench command? Maybe I will end up with something similar to Houdini's autotune command.

Code: Select all

#!/bin/bash
sum=0
average=0
for (( i = 1; i <= 50; i++ ))
do
    echo "$i. Iteration"
    x=$(./stockfish bench 2>&1|grep "Nodes\/second"|sed "s/.*: \([0-9]*\)/\1/")
let sum=$sum+$x
echo $x
done  

let average=$sum/50 ; echo average nps $average

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Extract line from Sf bench command

Post by lucasart » Sat Jan 04, 2014 3:11 am

Yes, regular expressions are pretty awesome. I use grep and sed all the time, and I can't imagine the world without them. Imagine going back to Windows and its pathetic DOS prompt :lol:

You can feed in stuff to the engine using the shell. The idea is that an UCI engine reads its UCI commands (from the GUI normally) from stdin, and outputs stuff (for the GUI to read) in its stdout.

Here's a sample session with an UCI engine, to search the starting position at depth 12:

Code: Select all

uci
isready
ucinewgame
position startpos
go depth 12
quit
If you fire up any UCI engine in command line and type these things, you should get the expected result.

What you can do in a script is put the commands you want to send the engine in a file, and frun:

Code: Select all

./engine <./commands_file >./output
and then parse the output, using grep for example.

The problem is that you can only do things in a static manner. You have to decide the commands you will send first, let the engine process them all and analyse the output after. With Python you can do an interactive script, that sends and receives them one by one, so it's a better choice than the shell for that kind of thing.
"Talk is cheap. Show me the code." -- Linus Torvalds.

ChessDrone
Posts: 78
Joined: Mon Jan 28, 2013 10:56 am

Re: Extract line from Sf bench command

Post by ChessDrone » Sat Mar 01, 2014 2:57 pm

quantum wrote:Here is my little script. This is some pretty cool stuff with sed. It is quite rewarding if you get your things working.
I wanted to save each run in an array and calculate the standard deviation but I mess up with the array stuff. Will take a look at this at a later time. I should rather study more serious than toying around with the bash :D

Do you think it is possible to change uci parameters in the bash and then run bench command? Maybe I will end up with something similar to Houdini's autotune command.

Code: Select all

#!/bin/bash
sum=0
average=0
for (( i = 1; i <= 50; i++ ))
do
    echo "$i. Iteration"
    x=$(./stockfish bench 2>&1|grep "Nodes\/second"|sed "s/.*: \([0-9]*\)/\1/")
let sum=$sum+$x
echo $x
done  

let average=$sum/50 ; echo average nps $average
Interesting script. Thank you
There are ways to adapt this script for several Stockfish?

Because I want to disable Threads Sleep before bench, may be this script can be mixed with this one?

Code: Select all

#!/bin/bash
cat << EOF | stockfish.exe
setoption name Idle Threads Sleep value false
bench

EOF
or this

Code: Select all

#!/bin/bash
cat < OPTIONS.INI << EOF | stockfish.exe
bench
EOF

ChessDrone
Posts: 78
Joined: Mon Jan 28, 2013 10:56 am

Re: Extract line from Sf bench command

Post by ChessDrone » Sun Mar 02, 2014 11:26 am

modified by wmahan (fics)

Code: Select all

#!/bin/bash

# http://www.open-chess.org/viewtopic.php?f=5&t=2557#p20321 modified by wmahan

for engine in profile-build/stockfish* build/stockfish*; do
    echo -e "$engine\t">>results.txt
    sum=0
    average=0
    for (( i = 1; i <= 50; i++ ))
    do
        #echo "$i. Iteration"
        out=`cat <<END | $engine 2>&1 | tail
setoption name Idle Threads Sleep value false
isready
bench
END`
        nps=`echo "$out" | grep "Nodes\/second"|sed "s/.*: \([0-9]*\)/\1/"`
        nodes=`echo "$out" | grep "Nodes searched"| sed "s/.*: \([0-9]*\)/\1/"`
        let sum=$sum+$nps
    done
    echo -e "$nodes\t">>results.txt

    let average=$sum/50 ; echo -e "$average\r">>results.txt
done
Image

Post Reply