Initial commit for gRPC code.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 16 Mar 2016 23:18:37 +0000 (00:18 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 24 Nov 2018 10:17:41 +0000 (11:17 +0100)
src/Makefile
src/client.cc [new file with mode: 0644]
src/hashprobe.proto [new file with mode: 0644]
src/main.cpp
src/position.cpp

index 8b9b16e..ab317e1 100644 (file)
@@ -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 (file)
index 0000000..05fe597
--- /dev/null
@@ -0,0 +1,33 @@
+#include <iostream>
+#include <memory>
+#include <string>
+
+#include <grpc++/grpc++.h>
+
+#include "hashprobe.grpc.pb.h"
+
+using grpc::Channel;
+using grpc::ClientContext;
+using grpc::Status;
+
+int main(int argc, char** argv) {
+  std::shared_ptr<Channel> channel(grpc::CreateChannel(
+      "localhost:50051", grpc::InsecureChannelCredentials()));
+  std::unique_ptr<HashProbe::Stub> 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 (file)
index 0000000..5df9b9c
--- /dev/null
@@ -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) {}
+}
index a093b5b..6c1aa03 100644 (file)
@@ -19,6 +19,7 @@
 */
 
 #include <iostream>
+#include <thread>
 
 #include "bitboard.h"
 #include "position.h"
 #include "uci.h"
 #include "syzygy/tbprobe.h"
 
+#include <grpc/grpc.h>
+#include <grpc++/server.h>
+#include <grpc++/server_builder.h>
+#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> 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);
index c915a1f..cf17212 100644 (file)
@@ -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;
 }