-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis.cpp
More file actions
1183 lines (1077 loc) · 46.6 KB
/
Copy pathanalysis.cpp
File metadata and controls
1183 lines (1077 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../core/global.h"
#include "../core/config_parser.h"
#include "../core/timer.h"
#include "../core/datetime.h"
#include "../core/makedir.h"
#include "../search/asyncbot.h"
#include "../search/patternbonustable.h"
#include "../program/setup.h"
#include "../program/playutils.h"
#include "../program/play.h"
#include "../command/commandline.h"
#include "../main.h"
#include "../external/nlohmann_json/json.hpp"
using namespace std;
using json = nlohmann::json;
struct AnalyzeRequest {
int64_t internalId;
string id;
int turnNumber;
int64_t priority;
Board board;
BoardHistory hist;
Player nextPla;
SearchParams params;
Player perspective;
int analysisPVLen;
bool includeOwnership;
bool includeOwnershipStdev;
bool includeMovesOwnership;
bool includeMovesOwnershipStdev;
bool includePolicy;
bool includePVVisits;
bool includeExtraScalars;
bool includeTree;
bool reportDuringSearch;
double reportDuringSearchEvery;
double firstReportDuringSearchAfter;
vector<int> avoidMoveUntilByLocBlack;
vector<int> avoidMoveUntilByLocWhite;
//Starts with STATUS_IN_QUEUE.
//Thread that grabs it from queue it changes it to STATUS_POPPED
//Once search is fully started thread sticks in its own thread index
//At any point it may change to STATUS_TERMINATED.
//If it ever gets to STATUS_POPPED or later, then the analysis thread is reponsible for writing the result, else the api thread is
static constexpr int STATUS_IN_QUEUE = -1;
static constexpr int STATUS_POPPED = -2;
static constexpr int STATUS_TERMINATED = -3;
std::atomic<int> status;
};
int MainCmds::analysis(const vector<string>& args) {
Board::initHash();
ScoreValue::initTables();
Rand seedRand;
ConfigParser cfg;
string modelFile;
bool numAnalysisThreadsCmdlineSpecified;
int numAnalysisThreadsCmdline;
bool quitWithoutWaiting;
KataGoCommandLine cmd("Run KataGo parallel JSON-based analysis engine.");
try {
cmd.addConfigFileArg("","analysis_example.cfg");
cmd.addModelFileArg();
cmd.setShortUsageArgLimit();
cmd.addOverrideConfigArg();
TCLAP::ValueArg<int> numAnalysisThreadsArg("","analysis-threads","Analyze up to this many positions in parallel. Equivalent to numAnalysisThreads in the config.",false,0,"THREADS");
TCLAP::SwitchArg quitWithoutWaitingArg("","quit-without-waiting","When stdin is closed, quit quickly without waiting for queued tasks");
cmd.add(numAnalysisThreadsArg);
cmd.add(quitWithoutWaitingArg);
cmd.parseArgs(args);
modelFile = cmd.getModelFile();
numAnalysisThreadsCmdlineSpecified = numAnalysisThreadsArg.isSet();
numAnalysisThreadsCmdline = numAnalysisThreadsArg.getValue();
quitWithoutWaiting = quitWithoutWaitingArg.getValue();
cmd.getConfig(cfg);
}
catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
return 1;
}
cfg.applyAlias("numSearchThreadsPerAnalysisThread", "numSearchThreads");
if(cfg.contains("numAnalysisThreads") && numAnalysisThreadsCmdlineSpecified)
throw StringError("When specifying numAnalysisThreads in the config (" + cfg.getFileName() + "), it is redundant and disallowed to also specify it via -analysis-threads");
const int numAnalysisThreads = numAnalysisThreadsCmdlineSpecified ? numAnalysisThreadsCmdline : cfg.getInt("numAnalysisThreads",1,16384);
if(numAnalysisThreads <= 0 || numAnalysisThreads > 16384)
throw StringError("Invalid value for numAnalysisThreads: " + Global::intToString(numAnalysisThreads));
const bool forDeterministicTesting =
cfg.contains("forDeterministicTesting") ? cfg.getBool("forDeterministicTesting") : false;
if(forDeterministicTesting)
seedRand.init("forDeterministicTesting");
const bool logToStdoutDefault = false;
const bool logToStderrDefault = true;
Logger logger(&cfg, logToStdoutDefault, logToStderrDefault);
const bool logToStderr = logger.isLoggingToStderr();
logger.write("Analysis Engine starting...");
logger.write(Version::getKataGoVersionForHelp());
if(!logToStderr) {
cerr << Version::getKataGoVersionForHelp() << endl;
}
const bool logAllRequests = cfg.contains("logAllRequests") ? cfg.getBool("logAllRequests") : false;
const bool logAllResponses = cfg.contains("logAllResponses") ? cfg.getBool("logAllResponses") : false;
const bool logErrorsAndWarnings = cfg.contains("logErrorsAndWarnings") ? cfg.getBool("logErrorsAndWarnings") : true;
const bool logSearchInfo = cfg.contains("logSearchInfo") ? cfg.getBool("logSearchInfo") : false;
auto loadParams = [](ConfigParser& config, SearchParams& params, Player& perspective, Player defaultPerspective) {
params = Setup::loadSingleParams(config,Setup::SETUP_FOR_ANALYSIS);
perspective = Setup::parseReportAnalysisWinrates(config,defaultPerspective);
//Set a default for conservativePass that differs from matches or selfplay
if(!config.contains("conservativePass") && !config.contains("conservativePass0"))
params.conservativePass = true;
};
SearchParams defaultParams;
Player defaultPerspective;
loadParams(cfg, defaultParams, defaultPerspective, C_EMPTY);
std::unique_ptr<PatternBonusTable> patternBonusTable = nullptr;
{
std::vector<std::unique_ptr<PatternBonusTable>> tables = Setup::loadAvoidSgfPatternBonusTables(cfg,logger);
assert(tables.size() == 1);
patternBonusTable = std::move(tables[0]);
}
const int analysisPVLen = cfg.contains("analysisPVLen") ? cfg.getInt("analysisPVLen",1,100) : 15;
const bool assumeMultipleStartingBlackMovesAreHandicap =
cfg.contains("assumeMultipleStartingBlackMovesAreHandicap") ? cfg.getBool("assumeMultipleStartingBlackMovesAreHandicap") : true;
const bool preventEncore = cfg.contains("preventCleanupPhase") ? cfg.getBool("preventCleanupPhase") : true;
NNEvaluator* nnEval;
{
Setup::initializeSession(cfg);
const int maxConcurrentEvals = numAnalysisThreads * defaultParams.numThreads * 2 + 16; // * 2 + 16 just to give plenty of headroom
const int expectedConcurrentEvals = numAnalysisThreads * defaultParams.numThreads;
const bool defaultRequireExactNNLen = false;
const int defaultMaxBatchSize = -1;
const bool disableFP16 = false;
const string expectedSha256 = "";
nnEval = Setup::initializeNNEvaluator(
modelFile,modelFile,expectedSha256,cfg,logger,seedRand,maxConcurrentEvals,expectedConcurrentEvals,
NNPos::MAX_BOARD_LEN,NNPos::MAX_BOARD_LEN,defaultMaxBatchSize,defaultRequireExactNNLen,disableFP16,
Setup::SETUP_FOR_ANALYSIS
);
}
#ifndef USE_EIGEN_BACKEND
{
int nnMaxBatchSizeTotal = nnEval->getNumGpus() * nnEval->getMaxBatchSize();
int numThreadsTotal = defaultParams.numThreads * numAnalysisThreads;
if(nnMaxBatchSizeTotal * 1.5 <= numThreadsTotal) {
logger.write(
Global::strprintf(
"Note: nnMaxBatchSize * number of GPUs (%d) is smaller than numSearchThreads * numAnalysisThreads (%d)",
nnMaxBatchSizeTotal, numThreadsTotal
)
);
logger.write("The number of simultaneous threads that might query the GPU could be larger than the batch size that the GPU will handle at once.");
logger.write("It may improve performance to increase nnMaxBatchSize, unless you are constrained on GPU memory.");
}
}
#endif
//Check for unused config keys
cfg.warnUnusedKeys(cerr,&logger);
logger.write("Loaded config "+ cfg.getFileName());
logger.write("Loaded model "+ modelFile);
cmd.logOverrides(logger);
ThreadSafeQueue<string*> toWriteQueue;
auto writeLoop = [&toWriteQueue,&logAllResponses,&logger]() {
while(true) {
string* message;
bool suc = toWriteQueue.waitPop(message);
if(!suc)
break;
cout << *message << endl;
if(logAllResponses)
logger.write("Response: " + *message);
delete message;
}
};
auto pushToWrite = [&toWriteQueue](string* s) {
bool suc = toWriteQueue.forcePush(s);
if(!suc)
delete s;
};
ThreadSafePriorityQueue<std::pair<int64_t,int64_t>, AnalyzeRequest*> toAnalyzeQueue;
int64_t numRequestsSoFar = 0; // Used as tie breaker for requests with same priority
int64_t internalIdCounter = 0; // Counter for internalId on requests.
//Open requests, keyed by internalId, mutexed by the mutex
std::mutex openRequestsMutex;
std::map<int64_t, AnalyzeRequest*> openRequests;
auto reportError = [&pushToWrite,&logger,&logErrorsAndWarnings](const string& s) {
json ret;
ret["error"] = s;
pushToWrite(new string(ret.dump()));
if(logErrorsAndWarnings)
logger.write("Error: " + ret.dump());
};
auto reportErrorForId = [&pushToWrite,&logger,&logErrorsAndWarnings](const string& id, const string& field, const string& s) {
json ret;
ret["id"] = id;
ret["field"] = field;
ret["error"] = s;
pushToWrite(new string(ret.dump()));
if(logErrorsAndWarnings)
logger.write("Error: " + ret.dump());
};
auto reportWarningForId = [&pushToWrite,&logger,&logErrorsAndWarnings](const string& id, const string& field, const string& s) {
json ret;
ret["id"] = id;
ret["field"] = field;
ret["warning"] = s;
pushToWrite(new string(ret.dump()));
if(logErrorsAndWarnings)
logger.write("Warning: " + ret.dump());
};
//Report analysis for which we don't actually have results. This is used when something is user-terminated before being actually
//analyzed properly. Only used outside of search too
auto reportNoAnalysis = [&pushToWrite](const AnalyzeRequest* request) {
json ret;
ret["id"] = request->id;
ret["turnNumber"] = request->turnNumber;
ret["isDuringSearch"] = false;
ret["noResults"] = true;
pushToWrite(new string(ret.dump()));
};
//Returns false if no analysis was reportable due to there being no root node or search results.
auto reportAnalysis = [&preventEncore,&pushToWrite](const AnalyzeRequest* request, const Search* search, bool isDuringSearch) {
json ret;
ret["id"] = request->id;
ret["turnNumber"] = request->turnNumber;
ret["isDuringSearch"] = isDuringSearch;
bool success = search->getAnalysisJson(
request->perspective,
request->analysisPVLen, preventEncore, request->includePolicy,
request->includeOwnership,request->includeOwnershipStdev,
request->includeMovesOwnership,request->includeMovesOwnershipStdev,
request->includePVVisits,
request->includeExtraScalars,
request->includeTree,
ret
);
if(success)
pushToWrite(new string(ret.dump()));
return success;
};
auto analysisLoop = [
&logger,&toAnalyzeQueue,&reportAnalysis,&reportNoAnalysis,&logSearchInfo,&nnEval,&openRequestsMutex,&openRequests
](AsyncBot* bot, int threadIdx) {
while(true) {
std::pair<std::pair<int64_t,int64_t>,AnalyzeRequest*> analysisItem;
bool suc = toAnalyzeQueue.waitPop(analysisItem);
if(!suc)
break;
AnalyzeRequest* request = analysisItem.second;
int expected = AnalyzeRequest::STATUS_IN_QUEUE;
//If it's already terminated, then there's nothing for us to do
if(!request->status.compare_exchange_strong(expected, AnalyzeRequest::STATUS_POPPED, std::memory_order_acq_rel)) {
assert(expected == AnalyzeRequest::STATUS_TERMINATED);
}
//Else, the request is live and we marked it as popped
else {
bot->setPosition(request->nextPla,request->board,request->hist);
bot->setAlwaysIncludeOwnerMap(request->includeOwnership || request->includeOwnershipStdev || request->includeMovesOwnership || request->includeMovesOwnershipStdev);
bot->setParams(request->params);
bot->setAvoidMoveUntilByLoc(request->avoidMoveUntilByLocBlack,request->avoidMoveUntilByLocWhite);
Player pla = request->nextPla;
double searchFactor = 1.0;
//Handle termination between the time we pop and the search starts
std::function<void()> onSearchBegun = [&request,&bot,&threadIdx]() {
//Try to record that we're handling this request and indicate that the search is started by this thread
int expected2 = AnalyzeRequest::STATUS_POPPED;
//If it was terminated, then stop our search
if(!request->status.compare_exchange_strong(expected2, threadIdx, std::memory_order_acq_rel)) {
assert(expected2 == AnalyzeRequest::STATUS_TERMINATED);
bot->stopWithoutWait();
}
};
if(request->reportDuringSearch) {
std::function<void(const Search* search)> callback = [&request,&reportAnalysis](const Search* search) {
const bool isDuringSearch = true;
reportAnalysis(request,search,isDuringSearch);
};
bot->genMoveSynchronousAnalyze(
pla, TimeControls(), searchFactor,
request->reportDuringSearchEvery, request->firstReportDuringSearchAfter,
callback, onSearchBegun
);
}
else {
bot->genMoveSynchronous(pla, TimeControls(), searchFactor, onSearchBegun);
}
if(logSearchInfo) {
ostringstream sout;
PlayUtils::printGenmoveLog(sout,bot,nnEval,Board::NULL_LOC,NAN,request->perspective);
logger.write(sout.str());
}
{
const bool isDuringSearch = false;
const Search* search = bot->getSearch();
bool analysisWritten = reportAnalysis(request,search,isDuringSearch);
//If the search didn't have any root or root neural net output, it must have been interrupted and we must be quitting imminently
if(!analysisWritten) {
//If the reason we stopped was because we noticed a terminate, then we will write out a dummy response even if we didn't have
//enough info to generate a real one, to fulfill a promise in the API docs that we always write something.
if(request->status.load(std::memory_order_acquire) == AnalyzeRequest::STATUS_TERMINATED)
reportNoAnalysis(request);
//Otherwise, this case is only possible if we're just shutting down
else
logger.write("Note: Search quitting due to no visits - this is normal and possible when shutting down but a bug under any other situation.");
}
}
}
//Free up bot resources in case it's a while before we do more search
bot->clearSearch();
//This request is no longer open
{
std::lock_guard<std::mutex> lock(openRequestsMutex);
openRequests.erase(request->internalId);
}
delete request;
}
};
auto analysisLoopProtected = [&logger,&analysisLoop](AsyncBot* bot, int threadIdx) {
Logger::logThreadUncaught("analysis loop", &logger, [&](){ analysisLoop(bot, threadIdx); });
};
vector<std::thread> threads;
std::thread write_thread = std::thread(writeLoop);
vector<AsyncBot*> bots;
for(int threadIdx = 0; threadIdx<numAnalysisThreads; threadIdx++) {
string searchRandSeed = Global::uint64ToHexString(seedRand.nextUInt64()) + Global::uint64ToHexString(seedRand.nextUInt64());
AsyncBot* bot = new AsyncBot(defaultParams, nnEval, &logger, searchRandSeed);
bot->setCopyOfExternalPatternBonusTable(patternBonusTable);
threads.push_back(std::thread(analysisLoopProtected,bot,threadIdx));
bots.push_back(bot);
}
logger.write("Analyzing up to " + Global::intToString(numAnalysisThreads) + " positions at a time in parallel");
logger.write("Started, ready to begin handling requests");
if(!logToStderr) {
cerr << "Started, ready to begin handling requests" << endl;
}
auto terminateRequest = [&bots,&reportNoAnalysis](AnalyzeRequest* request) {
//Firstly, flag the request as terminated
int prevStatus = request->status.exchange(AnalyzeRequest::STATUS_TERMINATED,std::memory_order_acq_rel);
//Already terminated? Nothing to do.
if(prevStatus == AnalyzeRequest::STATUS_TERMINATED)
{}
//No thread claimed it, so it's up to us to write the result
else if(prevStatus == AnalyzeRequest::STATUS_IN_QUEUE) {
reportNoAnalysis(request);
}
//A thread popped it. That thread will notice that it's terminated once it tries to put its thread idx in, so we need not do anything.
else if(prevStatus == AnalyzeRequest::STATUS_POPPED)
{}
//A thread started searching it and put its thread idx in
else {
assert(prevStatus >= 0);
//We've already set the above status to terminated so when the thread terminates due to our killing it below, it will see this.
//Or else the thread has already done so, in which case it's already properly written a result, also fine.
int threadIdx = prevStatus;
//Terminate it by thread index
bots[threadIdx]->stopWithoutWait();
}
};
auto requestLoop = [&]() {
string line;
json input;
while(getline(cin,line)) {
line = Global::trim(line);
if(line.length() == 0)
continue;
if(logAllRequests)
logger.write("Request: " + line);
try {
input = json::parse(line);
}
catch(nlohmann::detail::exception& e) {
reportError(e.what() + string(" - could not parse input line as json request: ") + line);
continue;
}
if(!input.is_object()) {
reportError("Request line was valid json but was not an object, ignoring: " + input.dump());
continue;
}
if(input.find("id") == input.end() || !input["id"].is_string()) {
reportError("Request must have a string \"id\" field");
continue;
}
AnalyzeRequest rbase;
rbase.id = input["id"].get<string>();
//Special actions
if(input.find("action") != input.end() && input["action"].is_string()) {
string action = input["action"].get<string>();
if(action == "query_version") {
input["version"] = Version::getKataGoVersion();
input["git_hash"] = Version::getGitRevision();
pushToWrite(new string(input.dump()));
}
else if(action == "clear_cache") {
//This should be thread-safe.
nnEval->clearCache();
pushToWrite(new string(input.dump()));
}
else if(action == "terminate") {
bool terminateIdFound = false;
string terminateId;
if(input.find("terminateId") != input.end() && input["terminateId"].is_string()) {
terminateId = input["terminateId"].get<string>();
terminateIdFound = true;
}
if(!terminateIdFound) {
reportErrorForId(rbase.id, "terminateId", "Requests for a terminate action must have a string \"terminateId\" field");
continue;
}
bool hasTurnNumbers = false;
vector<int> turnNumbers;
if(input.find("turnNumbers") != input.end()) {
try {
turnNumbers = input["turnNumbers"].get<vector<int> >();
hasTurnNumbers = true;
}
catch(nlohmann::detail::exception&) {
reportErrorForId(rbase.id, "turnNumbers", "If provided, must be an array of integers indicating turns to terminate");
continue;
}
}
{
std::lock_guard<std::mutex> lock(openRequestsMutex);
std::set<int> turnNumbersSet(turnNumbers.begin(),turnNumbers.end());
for(auto it = openRequests.begin(); it != openRequests.end(); ++it) {
AnalyzeRequest* request = it->second;
if(request->id == terminateId && (!hasTurnNumbers || (turnNumbersSet.find(request->turnNumber) != turnNumbersSet.end())))
terminateRequest(request);
}
}
pushToWrite(new string(input.dump()));
}
else if(action == "terminate_all") {
bool hasTurnNumbers = false;
vector<int> turnNumbers;
if(input.find("turnNumbers") != input.end()) {
try {
turnNumbers = input["turnNumbers"].get<vector<int> >();
hasTurnNumbers = true;
}
catch(nlohmann::detail::exception&) {
reportErrorForId(rbase.id, "turnNumbers", "If provided, must be an array of integers indicating turns to terminate");
continue;
}
}
{
std::lock_guard<std::mutex> lock(openRequestsMutex);
std::set<int> turnNumbersSet(turnNumbers.begin(),turnNumbers.end());
for(auto it = openRequests.begin(); it != openRequests.end(); ++it) {
AnalyzeRequest* request = it->second;
if(!hasTurnNumbers || (turnNumbersSet.find(request->turnNumber) != turnNumbersSet.end()))
terminateRequest(request);
}
}
pushToWrite(new string(input.dump()));
}
else {
reportError("'action' field must be 'query_version' or 'terminate' or 'terminate_all'");
}
continue;
}
//Defaults
rbase.params = defaultParams;
rbase.perspective = defaultPerspective;
rbase.analysisPVLen = analysisPVLen;
rbase.includeOwnership = false;
rbase.includeOwnershipStdev = false;
rbase.includeMovesOwnership = false;
rbase.includeMovesOwnershipStdev = false;
rbase.includePolicy = false;
rbase.includePVVisits = false;
rbase.includeExtraScalars = false;
rbase.includeTree = false;
rbase.reportDuringSearch = false;
rbase.reportDuringSearchEvery = 1e30;
rbase.firstReportDuringSearchAfter = 1e30;
rbase.priority = 0;
rbase.avoidMoveUntilByLocBlack.clear();
rbase.avoidMoveUntilByLocWhite.clear();
auto parseInteger = [&rbase,&reportErrorForId](const json& dict, const char* field, int64_t& buf, int64_t min, int64_t max, const char* errorMessage) {
try {
if(!dict[field].is_number_integer()) {
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
int64_t x = dict[field].get<int64_t>();
if(x < min || x > max) {
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
buf = x;
return true;
}
catch(nlohmann::detail::exception& e) {
(void)e;
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
};
auto parseDouble = [&rbase,&reportErrorForId](const json& dict, const char* field, double& buf, double min, double max, const char* errorMessage) {
try {
if(!dict[field].is_number()) {
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
double x = dict[field].get<double>();
if(!isfinite(x) || x < min || x > max) {
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
buf = x;
return true;
}
catch(nlohmann::detail::exception& e) {
(void)e;
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
};
auto parseBoolean = [&rbase,&reportErrorForId](const json& dict, const char* field, bool& buf, const char* errorMessage) {
try {
if(!dict[field].is_boolean()) {
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
buf = dict[field].get<bool>();
return true;
}
catch(nlohmann::detail::exception& e) {
(void)e;
reportErrorForId(rbase.id, field, errorMessage);
return false;
}
};
auto parsePlayer = [&rbase,&reportErrorForId](const json& dict, const char* field, Player& buf) {
buf = C_EMPTY;
try {
string s = dict[field].get<string>();
PlayerIO::tryParsePlayer(s,buf);
}
catch(nlohmann::detail::exception&) {}
if(buf != P_BLACK && buf != P_WHITE) {
reportErrorForId(rbase.id, field, "Must be \"b\" or \"w\"");
return false;
}
return true;
};
int boardXSize;
int boardYSize;
{
int64_t xBuf;
int64_t yBuf;
static const string boardSizeError = string("Must provide an integer from 2 to ") + Global::intToString(Board::MAX_LEN);
if(input.find("boardXSize") == input.end()) {
reportErrorForId(rbase.id, "boardXSize", boardSizeError.c_str());
continue;
}
if(input.find("boardYSize") == input.end()) {
reportErrorForId(rbase.id, "boardYSize", boardSizeError.c_str());
continue;
}
if(!parseInteger(input, "boardXSize", xBuf, 2, Board::MAX_LEN, boardSizeError.c_str())) {
continue;
}
if(!parseInteger(input, "boardYSize", yBuf, 2, Board::MAX_LEN, boardSizeError.c_str())) {
continue;
}
boardXSize = (int)xBuf;
boardYSize = (int)yBuf;
}
auto parseBoardLocs = [boardXSize,boardYSize,&rbase,&reportErrorForId](const json& dict, const char* field, vector<Loc>& buf, bool allowPass) {
buf.clear();
if(!dict[field].is_array()) {
reportErrorForId(rbase.id, field, "Must be an array of GTP board vertices");
return false;
}
for(auto& elt : dict[field]) {
string s;
try {
s = elt.get<string>();
}
catch(nlohmann::detail::exception& e) {
(void)e;
reportErrorForId(rbase.id, field, "Must be an array of GTP board vertices");
return false;
}
Loc loc;
if(!Location::tryOfString(s, boardXSize, boardYSize, loc) ||
(!allowPass && loc == Board::PASS_LOC) ||
(loc == Board::NULL_LOC)) {
reportErrorForId(rbase.id, field, "Could not parse board location: " + s);
return false;
}
buf.push_back(loc);
}
return true;
};
auto parseBoardMoves = [boardXSize,boardYSize,&rbase,&reportErrorForId](const json& dict, const char* field, vector<Move>& buf, bool allowPass) {
buf.clear();
if(!dict[field].is_array()) {
reportErrorForId(rbase.id, field, "Must be an array of pairs of the form: [\"b\" or \"w\", GTP board vertex]");
return false;
}
for(auto& elt : dict[field]) {
if(!elt.is_array() || elt.size() != 2) {
reportErrorForId(rbase.id, field, "Must be an array of pairs of the form: [\"b\" or \"w\", GTP board vertex]");
return false;
}
string s0;
string s1;
try {
s0 = elt[0].get<string>();
s1 = elt[1].get<string>();
}
catch(nlohmann::detail::exception& e) {
(void)e;
reportErrorForId(rbase.id, field, "Must be an array of pairs of the form: [\"b\" or \"w\", GTP board vertex]");
return false;
}
Player pla;
if(!PlayerIO::tryParsePlayer(s0,pla)) {
reportErrorForId(rbase.id, field, "Could not parse player: " + s0);
return false;
}
Loc loc;
if(!Location::tryOfString(s1, boardXSize, boardYSize, loc) ||
(!allowPass && loc == Board::PASS_LOC) ||
(loc == Board::NULL_LOC)) {
reportErrorForId(rbase.id, field, "Could not parse board location: " + s1);
return false;
}
buf.push_back(Move(loc,pla));
}
return true;
};
vector<Move> placements;
if(input.find("initialStones") != input.end()) {
if(!parseBoardMoves(input, "initialStones", placements, false))
continue;
}
vector<Move> moveHistory;
if(input.find("moves") != input.end()) {
if(!parseBoardMoves(input, "moves", moveHistory, true))
continue;
}
else {
reportErrorForId(rbase.id, "moves", "Must specify an array of [player,location] pairs");
continue;
}
Player initialPlayer = C_EMPTY;
if(input.find("initialPlayer") != input.end()) {
bool suc = parsePlayer(input, "initialPlayer", initialPlayer);
if(!suc)
continue;
}
vector<bool> shouldAnalyze(moveHistory.size()+1,false);
if(input.find("analyzeTurns") != input.end()) {
vector<int> analyzeTurns;
try {
analyzeTurns = input["analyzeTurns"].get<vector<int> >();
}
catch(nlohmann::detail::exception&) {
reportErrorForId(rbase.id, "analyzeTurns", "Must specify an array of integers indicating turns to analyze");
continue;
}
bool failed = false;
for(int i = 0; i<analyzeTurns.size(); i++) {
int turnNumber = analyzeTurns[i];
if(turnNumber < 0 || turnNumber >= shouldAnalyze.size()) {
reportErrorForId(rbase.id, "analyzeTurns", "Invalid turn number: " + Global::intToString(turnNumber));
failed = true;
break;
}
shouldAnalyze[turnNumber] = true;
}
if(failed)
continue;
}
else {
shouldAnalyze[shouldAnalyze.size()-1] = true;
}
std::map<int,int64_t> priorities;
if(input.find("priorities") != input.end()) {
vector<int64_t> prioritiesVec;
try {
prioritiesVec = input["priorities"].get<vector<int64_t> >();
}
catch(nlohmann::detail::exception&) {
reportErrorForId(rbase.id, "priorities", "Must specify an array of integers indicating priorities");
continue;
}
if(input.find("analyzeTurns") == input.end()) {
reportErrorForId(rbase.id, "priorities", "Can only specify when also specifying analyzeTurns");
continue;
}
vector<int> analyzeTurns = input["analyzeTurns"].get<vector<int> >();
if(prioritiesVec.size() != analyzeTurns.size()) {
reportErrorForId(rbase.id, "priorities", "Must be of matching length to analyzeTurns");
continue;
}
bool failed = false;
for(int i = 0; i<prioritiesVec.size(); i++) {
int64_t priority = prioritiesVec[i];
if(priority < -0x3FFFffffFFFFffffLL || priority > 0x3FFFffffFFFFffffLL) {
reportErrorForId(rbase.id, "priorities", "Invalid priority: " + Global::int64ToString(priority));
failed = true;
break;
}
priorities[analyzeTurns[i]] = priority;
}
if(failed) {
priorities.clear();
continue;
}
}
Rules rules;
if(input.find("rules") != input.end()) {
if(input["rules"].is_string()) {
string s = input["rules"].get<string>();
if(!Rules::tryParseRules(s,rules)) {
reportErrorForId(rbase.id, "rules", "Could not parse rules: " + s);
continue;
}
}
else if(input["rules"].is_object()) {
string s = input["rules"].dump();
if(!Rules::tryParseRules(s,rules)) {
reportErrorForId(rbase.id, "rules", "Could not parse rules: " + s);
continue;
}
}
else {
reportErrorForId(rbase.id, "rules", "Must specify rules string, such as \"chinese\" or \"tromp-taylor\", or a JSON object with detailed rules parameters.");
continue;
}
}
else {
reportErrorForId(rbase.id, "rules", "Must specify rules string, such as \"chinese\" or \"tromp-taylor\", or a JSON object with detailed rules parameters.");
continue;
}
if(input.find("komi") != input.end()) {
double komi;
static_assert(Rules::MIN_USER_KOMI == -150.0f, "");
static_assert(Rules::MAX_USER_KOMI == 150.0f, "");
const char* msg = "Must be a integer or half-integer from -150.0 to 150.0";
bool suc = parseDouble(input, "komi", komi, Rules::MIN_USER_KOMI, Rules::MAX_USER_KOMI, msg);
if(!suc)
continue;
rules.komi = (float)komi;
if(!Rules::komiIsIntOrHalfInt(rules.komi)) {
reportErrorForId(rbase.id, "rules", msg);
continue;
}
}
if(input.find("whiteHandicapBonus") != input.end()) {
if(!input["whiteHandicapBonus"].is_string()) {
reportErrorForId(rbase.id, "whiteHandicapBonus", "Must be a string");
continue;
}
string s = input["whiteHandicapBonus"].get<string>();
try {
int whiteHandicapBonusRule = Rules::parseWhiteHandicapBonusRule(s);
rules.whiteHandicapBonusRule = whiteHandicapBonusRule;
}
catch(const StringError& err) {
reportErrorForId(rbase.id, "whiteHandicapBonus", err.what());
continue;
}
}
if(input.find("overrideSettings") != input.end()) {
json settings = input["overrideSettings"];
if(!settings.is_object()) {
reportErrorForId(rbase.id, "overrideSettings", "Must be an object");
continue;
}
std::map<string,string> overrideSettings;
for(auto it = settings.begin(); it != settings.end(); ++it) {
overrideSettings[it.key()] = it.value().is_string() ? it.value().get<string>(): it.value().dump(); // always convert to string
}
// Reload settings to allow overrides
if(!overrideSettings.empty()) {
try {
ConfigParser localCfg(cfg);
//Ignore any unused keys in the ORIGINAL config
localCfg.markAllKeysUsedWithPrefix("");
localCfg.overrideKeys(overrideSettings);
loadParams(localCfg, rbase.params, rbase.perspective, defaultPerspective);
SearchParams::failIfParamsDifferOnUnchangeableParameter(defaultParams,rbase.params);
//Soft failure on unused override keys newly present in the config
vector<string> unusedKeys = localCfg.unusedKeys();
if(unusedKeys.size() > 0) {
reportWarningForId(rbase.id, "overrideSettings", string("Unknown config params: ") + Global::concat(unusedKeys,","));
}
}
catch(const StringError& exception) {
reportErrorForId(rbase.id, "overrideSettings", string("Could not set settings: ") + exception.what());
continue;
}
}
}
if(input.find("maxVisits") != input.end()) {
bool suc = parseInteger(input, "maxVisits", rbase.params.maxVisits, 1, (int64_t)1 << 50, "Must be an integer from 1 to 2^50");
if(!suc)
continue;
}
if(input.find("analysisPVLen") != input.end()) {
int64_t buf;
bool suc = parseInteger(input, "analysisPVLen", buf, 1, 1000, "Must be an integer from 1 to 1000");
if(!suc)
continue;
rbase.analysisPVLen = (int)buf;
}
if(input.find("rootFpuReductionMax") != input.end()) {
bool suc = parseDouble(input, "rootFpuReductionMax", rbase.params.rootFpuReductionMax, 0.0, 2.0, "Must be a number from 0.0 to 2.0");
if(!suc)
continue;
}
if(input.find("rootPolicyTemperature") != input.end()) {
bool suc = parseDouble(input, "rootPolicyTemperature", rbase.params.rootPolicyTemperature, 0.01, 100.0, "Must be a number from 0.01 to 100.0");
if(!suc)
continue;
rbase.params.rootPolicyTemperatureEarly = rbase.params.rootPolicyTemperature;
}
if(input.find("includeMovesOwnership") != input.end()) {
bool suc = parseBoolean(input, "includeMovesOwnership", rbase.includeMovesOwnership, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includeMovesOwnershipStdev") != input.end()) {
bool suc = parseBoolean(input, "includeMovesOwnershipStdev", rbase.includeMovesOwnershipStdev, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includeOwnership") != input.end()) {
bool suc = parseBoolean(input, "includeOwnership", rbase.includeOwnership, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includeOwnershipStdev") != input.end()) {
bool suc = parseBoolean(input, "includeOwnershipStdev", rbase.includeOwnershipStdev, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includePolicy") != input.end()) {
bool suc = parseBoolean(input, "includePolicy", rbase.includePolicy, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includePVVisits") != input.end()) {
bool suc = parseBoolean(input, "includePVVisits", rbase.includePVVisits, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includeExtraScalars") != input.end()) {
bool suc = parseBoolean(input, "includeExtraScalars", rbase.includeExtraScalars, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("includeTree") != input.end()) {
bool suc = parseBoolean(input, "includeTree", rbase.includeTree, "Must be a boolean");
if(!suc)
continue;
}
if(input.find("reportDuringSearchEvery") != input.end()) {
bool suc = parseDouble(input, "reportDuringSearchEvery", rbase.reportDuringSearchEvery, 0.001, 1000000.0, "Must be number of seconds from 0.001 to 1000000.0");
if(!suc)
continue;
rbase.reportDuringSearch = true;
rbase.firstReportDuringSearchAfter = rbase.reportDuringSearchEvery;
}
if(input.find("firstReportDuringSearchAfter") != input.end()) {
bool suc = parseDouble(input, "firstReportDuringSearchAfter", rbase.firstReportDuringSearchAfter, 0.001, 1000000.0, "Must be number of seconds from 0.001 to 1000000.0");
if(!suc)
continue;
rbase.reportDuringSearch = true;
}
if(input.find("priority") != input.end()) {
if(input.find("priorities") != input.end()) {
reportErrorForId(rbase.id, "priority", "Cannot specify both priority and priorities");
continue;
}
int64_t buf;
bool suc = parseInteger(input, "priority", buf, -0x3FFFffffFFFFffffLL,0x3FFFffffFFFFffffLL, "Must be a number between -2^62 and 2^62");
if(!suc)
continue;
rbase.priority = buf;
}
bool hasAllowMoves = input.find("allowMoves") != input.end();
bool hasAvoidMoves = input.find("avoidMoves") != input.end();
if(hasAllowMoves || hasAvoidMoves) {
if(hasAllowMoves && hasAvoidMoves) {
reportErrorForId(rbase.id, "allowMoves", string("Cannot specify both allowMoves and avoidMoves"));
continue;
}
string field = hasAllowMoves ? "allowMoves" : "avoidMoves";
json& avoidParamsList = input[field];
if(!avoidParamsList.is_array()) {
reportErrorForId(rbase.id, field, string("Must be a list of dicts with subfields 'player', 'moves', 'untilDepth'"));
continue;
}
if(hasAllowMoves && avoidParamsList.size() > 1) {
reportErrorForId(rbase.id, field, string("Currently allowMoves only allows one entry"));
continue;
}
bool failed = false;
for(size_t i = 0; i<avoidParamsList.size(); i++) {
json& avoidParams = avoidParamsList[i];
if(avoidParams.find("moves") == avoidParams.end() ||
avoidParams.find("untilDepth") == avoidParams.end() ||
avoidParams.find("player") == avoidParams.end()) {
reportErrorForId(rbase.id, field, string("Must be a list of dicts with subfields 'player', 'moves', 'untilDepth'"));
failed = true;
break;
}