From: Steinar H. Gunderson Date: Wed, 16 Mar 2016 23:18:37 +0000 (+0100) Subject: Initial commit for gRPC code. X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=5d7ad47c198c54e7a91876a6d8d8488a0c23a1ba;ds=sidebyside Initial commit for gRPC code. --- diff --git a/src/Makefile b/src/Makefile index 8b9b16e4..ab317e10 100644 --- a/src/Makefile +++ b/src/Makefile @@ -38,7 +38,9 @@ PGOBENCH = ./$(EXE) bench ### Object files OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \ material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \ - search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o + search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o \ + hashprobe.grpc.pb.o hashprobe.pb.o +CLIOBJS = client.o hashprobe.grpc.pb.o hashprobe.pb.o ### Establish the operating system name KERNEL = $(shell uname -s) @@ -456,7 +458,7 @@ default: ### Section 5. Private targets ### ========================================================================== -all: $(EXE) .depend +all: $(EXE) client .depend config-sanity: @echo "" @@ -531,6 +533,30 @@ icc-profile-use: EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \ all +### GRPC + +PROTOS_PATH = . +PROTOC = protoc +GRPC_CPP_PLUGIN = grpc_cpp_plugin +GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)` + +%.grpc.pb.h %.grpc.pb.cc: %.proto + $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $< + +# oh my +%.cpp: %.cc + cp $< $@ + +%.pb.h %.pb.cc: %.proto + $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $< + +LDFLAGS += -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc + +client: $(CLIOBJS) + $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS) + +# Other stuff + .depend: -@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null diff --git a/src/client.cc b/src/client.cc new file mode 100644 index 00000000..05fe5972 --- /dev/null +++ b/src/client.cc @@ -0,0 +1,33 @@ +#include +#include +#include + +#include + +#include "hashprobe.grpc.pb.h" + +using grpc::Channel; +using grpc::ClientContext; +using grpc::Status; + +int main(int argc, char** argv) { + std::shared_ptr channel(grpc::CreateChannel( + "localhost:50051", grpc::InsecureChannelCredentials())); + std::unique_ptr stub(HashProbe::NewStub(channel)); + + HashProbeRequest request; +// request.set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + request.set_fen(argv[1]); + + HashProbeResponse response; + ClientContext context; + Status status = stub->Probe(&context, request, &response); + + if (status.ok()) { + std::cout << response.DebugString(); + } else { + std::cout << "RPC failed"; + } + + return 0; +} diff --git a/src/hashprobe.proto b/src/hashprobe.proto new file mode 100644 index 00000000..5df9b9c5 --- /dev/null +++ b/src/hashprobe.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +message HashProbeRequest { + string fen = 1; +} +message HashProbeResponse { + bool found = 1; + int32 move = 2; + int32 value = 3; + int32 eval = 4; + int32 depth = 5; + int32 bound = 6; +} + +service HashProbe { + rpc Probe(HashProbeRequest) returns (HashProbeResponse) {} +} diff --git a/src/main.cpp b/src/main.cpp index a093b5bf..6c1aa034 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ */ #include +#include #include "bitboard.h" #include "position.h" @@ -28,6 +29,55 @@ #include "uci.h" #include "syzygy/tbprobe.h" +#include +#include +#include +#include "hashprobe.grpc.pb.h" + +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::Status; +using grpc::StatusCode; + +class HashProbeImpl final : public HashProbe::Service { +public: + Status Probe(ServerContext* context, + const HashProbeRequest* request, + HashProbeResponse *response) { + std::cout << "fen=" << request->fen() << std::endl; + Position pos(request->fen(), /*isChess960=*/false, Threads.main()); + if (!pos.pos_is_ok()) { + return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN"); + } + std::cout << "parsed=" << pos.fen() << std::endl; + bool found; + TTEntry *entry = TT.probe(pos.key(), found); + response->set_found(found); + if (found) { + response->set_move(entry->move()); + response->set_value(entry->value()); + response->set_eval(entry->eval()); + response->set_depth(entry->depth()); + response->set_bound(entry->bound()); + } + return Status::OK; + } +}; + +void rpc_thread() +{ + std::string server_address("0.0.0.0:50051"); + HashProbeImpl service; + + ServerBuilder builder; + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + std::unique_ptr server(builder.BuildAndStart()); + std::cout << "Server listening on " << server_address << std::endl; + server->Wait(); +} + namespace PSQT { void init(); } @@ -46,6 +96,8 @@ int main(int argc, char* argv[]) { Threads.set(Options["Threads"]); Search::clear(); // After threads are up + std::thread(&rpc_thread).detach(); + UCI::loop(argc, argv); Threads.set(0); diff --git a/src/position.cpp b/src/position.cpp index bd5daa6d..d3afddeb 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -318,8 +318,6 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th thisThread = th; set_state(st); - assert(pos_is_ok()); - return *this; }