interfacing with UCI

Discussion of chess software programming and technical issues.

Moderator: Ras

Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: interfacing with UCI

Post by Whiskers »

I got it working! Thank you all for your help, especially you H.G.Muller (I take some solace in the fact that I was actually on the right path with the pipes and GetPeekAtPipe command before I saw your comments, but the code itself was non-functional before.)

What really helped was getting to see the Engine log (Banksia has it but I do not know if Arena does). Debugging the errors left over (i had one with position startpos moves, for example) was quite easy after that. And then of course I had to change the format of my outputs, but that's no biggie.

It's still very simple and there's still lots of things to work on but I plan to add more stuff and then can finally release it as an executable rather than just source code :)

Image
JoAnnP38
Posts: 253
Joined: Mon Aug 26, 2019 4:34 pm
Location: Clearwater, Florida USA
Full name: JoAnn Peeler

Re: interfacing with UCI

Post by JoAnnP38 »

Roland Chastain wrote: Thu Mar 02, 2023 5:07 pm @H.G.Muller

Very useful informations. Thank you.
hgm wrote: Thu Mar 02, 2023 11:05 amAnd you don't have to deal with the complexity of multi-threading and thread synchronization.
This is something to take into account, indeed.
I just want to add one more thing for you to think about because this is what I was thinking when I decided to use a separate thread for search and keep the i/o on the main thread. If you want to eventually evolve to a multi-threaded search using Lazy-SMP, designing your engine from the get-go such that search is executed on a separate thread will make it much easier to evolve in that direction. In theory, you would just fork off your same search thread multiple times. However, if one of those threads is responsible for the i/o... well you can probably see the problem. If on the other hand your goal is to build your engine using the simplest design you don't need to have a separate thread. The beauty of simplicity is something that often evades me for sure.
User avatar
Roland Chastain
Posts: 679
Joined: Sat Jun 08, 2013 10:07 am
Location: France
Full name: Roland Chastain

Re: interfacing with UCI

Post by Roland Chastain »

@JoAnnP38

Thanks. For my part, I have a preference for simple and easy to understand programs. :wink:

@Whiskers

Congratulations!
Qui trop embrasse mal étreint.
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: interfacing with UCI

Post by Whiskers »

I thought I had it working, but something is wrong with the pipe setup; it reads input in just fine, but PeekNamedPipe always fails and returns 0, even right after initialization. Am I doing something wrong? The code above is taken from the CPW engine so I don't really understand why it's not working.

Code: Select all

    unsigned long dw;
    hstdin = GetStdHandle(STD_INPUT_HANDLE);
    pipe = !GetConsoleMode(hstdin, &dw);

    if (!pipe) {
        SetConsoleMode(hstdin,dw&~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
        FlushConsoleInputBuffer(hstdin);
    } else {
        setvbuf(stdin,NULL,_IONBF,0);
        setvbuf(stdout,NULL,_IONBF,0);
    }

    if(!PeekNamedPipe(hstdin, NULL, 0, NULL, NULL, NULL)){  //this always returns error!
    exit(1);
    }     
    
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: interfacing with UCI

Post by hgm »

Well, PeekNamedPipe() can only be used if stdin is actually a pipe. I don't know how you tested this, but in your code it seems you call it whether stdin is a pipe or not.
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: interfacing with UCI

Post by Whiskers »

hgm wrote: Wed Mar 08, 2023 9:01 am Well, PeekNamedPipe() can only be used if stdin is actually a pipe. I don't know how you tested this, but in your code it seems you call it whether stdin is a pipe or not.
I thought that the “if (!pipe)” part did that. If stdin is not a pipe what function do you recommend I call in inputpending?
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: interfacing with UCI

Post by Whiskers »

and now it works again. (When run on GUIs it is indeed a valid pipe. This would explain why a lot of chess engine executables immediately exit when you run the executable directly, because they check for pipes and stdin does not have them.) I'm not even sure what I did differently this time, it feels like black magic!
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: interfacing with UCI

Post by hgm »

Whiskers wrote: Wed Mar 08, 2023 6:00 pm
hgm wrote: Wed Mar 08, 2023 9:01 am Well, PeekNamedPipe() can only be used if stdin is actually a pipe. I don't know how you tested this, but in your code it seems you call it whether stdin is a pipe or not.
I thought that the “if (!pipe)” part did that. If stdin is not a pipe what function do you recommend I call in inputpending?
It only does that for the code inside that if-clause. But your

if(!PeekNamedPipe(hstdin, NULL, 0, NULL, NULL, NULL)){ //this always returns error!

is not. So you should expect an error return if stdin is not a pipe. Whatever you do in the if(!pipe) clause, it cannot make a console into a pipe. For testing for pending input on a console you need a different function (e.g. PeekConsoleInput).