Permalink
Browse files

calling scheme from c++ and c++ from scheme

  • Loading branch information...
rjmacready committed Dec 18, 2015
1 parent 2c1797a commit 8918cb4e119f209427b32a7199a1ed7b9014767b
Showing with 141 additions and 12 deletions.
  1. +3 −0 .gitignore
  2. +3 −3 src/Makefile
  3. +13 −2 src/evaluate.cpp
  4. +66 −0 src/guile.cpp
  5. +18 −0 src/guile.h
  6. +3 −1 src/main.cpp
  7. +3 −0 src/main.scm
  8. BIN src/stockfish
  9. +12 −4 src/thread.cpp
  10. +5 −2 src/thread.h
  11. +15 −0 src/userscripts.scm
View
@@ -0,0 +1,3 @@
*.o
*~
src/.depend
View
@@ -34,7 +34,7 @@ BINDIR = $(PREFIX)/bin
PGOBENCH = ./$(EXE) bench 16 1 1000 default time
### Object files
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
OBJS = guile.o 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
@@ -140,7 +140,7 @@ endif
### 3.1 Selecting compiler (default = gcc)
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 `pkg-config --cflags guile-2.0` $(EXTRACXXFLAGS)
DEPENDFLAGS += -std=c++11
LDFLAGS += $(EXTRALDFLAGS)
@@ -478,7 +478,7 @@ config-sanity:
@test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
$(EXE): $(OBJS)
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
$(CXX) -o $@ $(OBJS) `pkg-config --libs guile-2.0` $(LDFLAGS)
gcc-profile-prepare:
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
View
@@ -27,6 +27,7 @@
#include "evaluate.h"
#include "material.h"
#include "pawns.h"
#include "guile.h"
namespace {
@@ -765,8 +766,10 @@ Value Eval::evaluate(const Position& pos) {
// If we have a specialized evaluation function for the current material
// configuration, call it and return.
if (ei.me->specialized_eval_exists())
if (ei.me->specialized_eval_exists()) {
// printf("specialized!\n");
return ei.me->evaluate(pos);
}
// Probe the pawn hash table
ei.pi = Pawns::probe(pos);
@@ -848,7 +851,15 @@ Value Eval::evaluate(const Position& pos) {
Trace::add(TOTAL, score);
}
return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view
Value r = (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo;
//#if WITH_GUILE
r = guile_evaluate(pos, r);
// #endif
return r; // Side to move point of view
}
// Explicit template instantiations
View
@@ -0,0 +1,66 @@
#include "guile.h"
SCM display;
SCM evaluate;
SCM get_side_to_move(SCM pos)
{
Position *rpos = (Position*)scm_to_pointer(pos);
return scm_from_int(rpos->side_to_move());
}
void* init_guile(void* data)
{
scm_gc();
scm_c_primitive_load("/home/user/Stockfish/src/userscripts.scm");
display = scm_c_public_lookup("guile", "display");
scm_call_1(scm_variable_ref(display), scm_from_stringn("from C\n", 7, NULL, SCM_FAILED_CONVERSION_ERROR));
scm_c_define_gsubr("side-to-move", 1, 0, 0, (void*)&get_side_to_move);
evaluate = scm_c_public_lookup("userscripts", "evaluate");
//evaluate = scm_c_public_lookup("guile-user", "evaluate");
scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
scm_c_primitive_load("/home/user/Stockfish/src/main.scm");
// printf("thread: %d\n", std::thread::id());
printf("init for thread: %d %d\n", std::thread::id(), std::this_thread::get_id());
//scm_call_0(scm_variable_ref(evaluate));
//scm_call_0(scm_variable_ref(evaluate));
//scm_call_0(scm_variable_ref(evaluate));
return NULL;
}
Value guile_evaluate(const Position& pos, Value v)
{
assert(evaluate != NULL);
//SCM str = scm_from_stringn("from C\n", 7, NULL, SCM_FAILED_CONVERSION_ERROR);
//SCM display = scm_c_public_lookup("", "display"); // guile
//printf("thread: %d %d\n", std::thread::id(), std::this_thread::get_id());
//evaluate = scm_c_public_lookup("userscripts", "evaluate");
//scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
// printf("was %d\n", v);
SCM r = scm_call_2(scm_variable_ref(evaluate),
scm_from_pointer((void*)&pos, NULL),
scm_from_int(v));
// printf("now is %d\n", (Value)scm_to_int(r));
return (Value)scm_to_int(r);
}
View
@@ -0,0 +1,18 @@
#ifndef GUILE_H_INCLUDED
#define GUILE_H_INCLUDED
#define WITH_GUILE
#include <libguile.h>
#include <thread>
#include "types.h"
#include "position.h"
//extern "C" SCM get_side_to_move(SCM pos);
void* init_guile(void* data);
Value guile_evaluate(const Position& pos, Value v);
#endif
View
@@ -27,9 +27,11 @@
#include "tt.h"
#include "uci.h"
#include "syzygy/tbprobe.h"
#include "guile.h"
int main(int argc, char* argv[]) {
scm_with_guile(&init_guile, NULL);
std::cout << engine_info() << std::endl;
UCI::init(Options);
View
@@ -0,0 +1,3 @@
(use-modules (userscripts))
; other glue code ...
View
Binary file not shown.
View
@@ -24,6 +24,7 @@
#include "search.h"
#include "thread.h"
#include "uci.h"
#include "guile.h"
using namespace Search;
@@ -34,18 +35,27 @@ ThreadPool Threads; // Global object
Thread::Thread() {
resetCalls = exit = false;
resetCalls = exit = scm_inited = false;
maxPly = callsCnt = 0;
history.clear();
counterMoves.clear();
idx = Threads.size(); // Start from 0
std::unique_lock<Mutex> lk(mutex);
searching = true;
nativeThread = std::thread(&Thread::idle_loop, this);
nativeThread = std::thread(&Thread::init_and_idle, this);
sleepCondition.wait(lk, [&]{ return !searching; });
}
void Thread::init_and_idle()
{
if(scm_inited == false) {
scm_inited = true;
scm_with_guile(&init_guile, NULL);
}
idle_loop();
}
/// Thread destructor wait for thread termination before returning
@@ -93,7 +103,6 @@ void Thread::start_searching(bool resume) {
/// Thread::idle_loop() is where the thread is parked when it has no work to do
void Thread::idle_loop() {
while (!exit)
{
std::unique_lock<Mutex> lk(mutex);
@@ -120,7 +129,6 @@ void Thread::idle_loop() {
/// allocation of Endgames in the Thread constructor.
void ThreadPool::init() {
push_back(new MainThread);
read_uci_options();
}
View
@@ -45,12 +45,15 @@ class Thread {
std::thread nativeThread;
Mutex mutex;
ConditionVariable sleepCondition;
bool exit, searching;
bool exit, searching, scm_inited;
public:
Thread();
virtual ~Thread();
virtual void search();
void init_and_idle();
void idle_loop();
void start_searching(bool resume = false);
void wait_for_search_finished();
View
@@ -0,0 +1,15 @@
(define-module (userscripts)
#:export (evaluate))
(define (__evaluate pos v)
(+ (- (random 1000) 500) v))
(define (_evaluate pos v)
v)
(define (evaluate pos v)
(if (= (side-to-move pos) 0)
v
(* -1 v)))

0 comments on commit 8918cb4

Please sign in to comment.