How does input console for UCI work ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

OneTrickPony
Posts: 157
Joined: Tue Apr 30, 2013 1:29 am

Re: How does input console for UCI work ?

Post by OneTrickPony »

I've made a mistake with capital letters when grepping for PeekNamedPipe before.
I get it now, thanks for help.
User avatar
hgm
Posts: 27796
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: How does input console for UCI work ?

Post by hgm »

Using "grep -i" can avoid that kind of trouble.
OneTrickPony
Posts: 157
Joined: Tue Apr 30, 2013 1:29 am

Re: How does input console for UCI work ?

Post by OneTrickPony »

So I went with two threads approach and everything works (btw, I have no idea why Robbolito uses those convoluted constructs for parsing with memcpy and appending nulls manually etc. instead of strtok).

Now I have a new minor problem: I would like both of my threads to be able to write to stdout without interfering with each other. While it's not necessary in UCI because UCI doesn't respond to most commands I am writing an engine for different game than chess and my protocol responds to every command (with something like "command ok!"). That response can interfere with output from searching (it's a very small chance as searching writes to the output only once every several seconds but still).
I wonder if there is a way to lock stdout without rewriting all printf's in my code to custom function with a mutex.
I am sorry if it's not exactly chess/UCI specific but I figured I have better chances to get help here than in other places as run-of-the-mill response to this problem is "wrap printf with custom function, lock access to that, replace all printf's in your code with this new function" and I would really like to avoid it.
User avatar
Bloodbane
Posts: 154
Joined: Thu Oct 03, 2013 4:17 pm

Re: How does input console for UCI work ?

Post by Bloodbane »

I'm not sure that is possible without wrapping printf, unless you consider allowing thread 1 to output only when the time (pick your favourite resolution) is odd and allowing thread 2 to output only when the time is even a solution.
Functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics.
https://github.com/mAarnos
OneTrickPony
Posts: 157
Joined: Tue Apr 30, 2013 1:29 am

Re: How does input console for UCI work ?

Post by OneTrickPony »

I'm not sure that is possible without wrapping printf, unless you consider allowing thread 1 to output only when the time (pick your favourite resolution) is odd and allowing thread 2 to output only when the time is even a solution.
That's quite clever, I haven't thought about it.
The probelm is that it's similar to mutex which means wrapping every call to a function using printf for output with two lines (take mutex, release mutex).
It's not as bad as substituting every printf out there (you only need to wrap calls to output function in one place - the one where communication protocol is implemented - without touching already existing printing/debugging functions) but still it will be a lot of lines of code.
If I could do "lock(stdout)" I would just do that in one place (where search is writing to output) and leave rest of the code untouched.
User avatar
Bloodbane
Posts: 154
Joined: Thu Oct 03, 2013 4:17 pm

Re: How does input console for UCI work ?

Post by Bloodbane »

How about:

#define printf myprintf

and then implementing whatever synchronization you want in myprintf. It's not pretty but that way you don't have to touch a single line of code.
Functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics.
https://github.com/mAarnos
OneTrickPony
Posts: 157
Joined: Tue Apr 30, 2013 1:29 am

Re: How does input console for UCI work ?

Post by OneTrickPony »

Right and I can do that that #define only in one file (the one working with uci like interface). Thanks, I think this will do.