| 1 |
/* checkmateRecorder.cc |
| 2 |
*/ |
| 3 |
#include "checkmateRecorder.h" |
| 4 |
#include "checkHashRecord.h" |
| 5 |
#include "checkMoveList.h" |
| 6 |
#include "checkStack.h" |
| 7 |
#include "osl/state/simpleState.h" |
| 8 |
#include "osl/record/csa.h" |
| 9 |
#include <string> |
| 10 |
#include <iostream> |
| 11 |
#include <fstream> |
| 12 |
#include <signal.h> |
| 13 |
|
| 14 |
#ifndef MINIMAL |
| 15 |
|
| 16 |
/** この値は必要に応じて編集する */ |
| 17 |
int osl::checkmate::CheckmateRecorder::DepthTracer::maxVerboseLogDepth=1; |
| 18 |
int osl::checkmate::CheckmateRecorder::DepthTracer::maxLogDepth=1; |
| 19 |
/** depth の初期値は 0 に固定 */ |
| 20 |
int osl::checkmate::CheckmateRecorder::DepthTracer::depth=0; |
| 21 |
osl::checkmate::CheckMove *osl::checkmate::CheckmateRecorder::nextMove; |
| 22 |
const char * osl::checkmate::CheckmateRecorder::leaveReason=0; |
| 23 |
const osl::SimpleState* osl::checkmate::CheckmateRecorder::currentState=0; |
| 24 |
|
| 25 |
std::ostream& osl::checkmate::CheckmateRecorder::DepthTracer::stream() |
| 26 |
{ |
| 27 |
static std::ofstream nullStream("/dev/null"); |
| 28 |
if (DepthTracer::maxVerboseLogDepth <= depth) |
| 29 |
return nullStream; |
| 30 |
return CheckmateRecorder::stream(); |
| 31 |
} |
| 32 |
|
| 33 |
namespace osl |
| 34 |
{ |
| 35 |
namespace checkmate |
| 36 |
{ |
| 37 |
static CheckStack check_stack; |
| 38 |
#ifdef CHECKMATE_DEBUG |
| 39 |
void print_check_stack(int) |
| 40 |
{ |
| 41 |
printf("oops\n"); |
| 42 |
printf("stack size %ld\n", (long int)check_stack.size()); |
| 43 |
CheckmateRecorder::dumpStack(); |
| 44 |
} |
| 45 |
class CheckSignalInitializer |
| 46 |
{ |
| 47 |
public: |
| 48 |
CheckSignalInitializer() |
| 49 |
{ |
| 50 |
signal(SIGINT, print_check_stack); |
| 51 |
} |
| 52 |
} initializer; |
| 53 |
#endif |
| 54 |
} // namespace checkmate |
| 55 |
} // namespace osl |
| 56 |
|
| 57 |
namespace |
| 58 |
{ |
| 59 |
std::ostream& makeOutlineStream() |
| 60 |
{ |
| 61 |
static std::ofstream os("checkmate.log"); |
| 62 |
os << ";;; -*-mode:outline-*-\n" |
| 63 |
<< "* root\n"; |
| 64 |
return os; |
| 65 |
} |
| 66 |
std::string makeHeader(int depth) |
| 67 |
{ |
| 68 |
assert(depth >= 0); |
| 69 |
std::string header(depth+1, '*'); |
| 70 |
header += ' '; |
| 71 |
return header; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
std::ostream& osl::checkmate::CheckmateRecorder::stream() |
| 76 |
{ |
| 77 |
static std::ostream& os = makeOutlineStream(); |
| 78 |
return os; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
void osl::checkmate::CheckmateRecorder::dumpStack() |
| 83 |
{ |
| 84 |
if (currentState) |
| 85 |
std::cerr << *currentState << "\n"; |
| 86 |
std::cerr << check_stack; |
| 87 |
if (! check_stack.empty()) |
| 88 |
check_stack[check_stack.size()-1].record->dump(std::cerr, 3); |
| 89 |
} |
| 90 |
|
| 91 |
void osl::checkmate::CheckmateRecorder:: |
| 92 |
dumpRecord(const SimpleState& state, |
| 93 |
const CheckHashRecord *record, |
| 94 |
unsigned int proofLimit, |
| 95 |
unsigned int disProofLimit, |
| 96 |
unsigned int currentProofNumber, |
| 97 |
unsigned int currentDisProofNumber) |
| 98 |
{ |
| 99 |
stream() << "CheckmateRecorder::dumpRecord\n"; |
| 100 |
stream() << "proofLimit " << proofLimit << "\n" |
| 101 |
<< "disProofLimit " << disProofLimit << "\n" |
| 102 |
<< "cur proofNumber " << currentProofNumber << "\n" |
| 103 |
<< "cur disProofNumber " << currentDisProofNumber << "\n"; |
| 104 |
stream() << state; |
| 105 |
stream() << record << "\n"; |
| 106 |
if (record) |
| 107 |
{ |
| 108 |
record->dump(stream(), 1); |
| 109 |
} |
| 110 |
stream() << std::flush; |
| 111 |
} |
| 112 |
|
| 113 |
void osl::checkmate::CheckmateRecorder::writeRootLog( |
| 114 |
#ifdef CHECKMATE_DEBUG |
| 115 |
const char *msg, unsigned int tableSize, unsigned int continuousNoExpandLoop |
| 116 |
#else |
| 117 |
const char *, unsigned int, unsigned int |
| 118 |
#endif |
| 119 |
) |
| 120 |
{ |
| 121 |
#ifdef CHECKMATE_DEBUG |
| 122 |
if (DepthTracer::maxVerboseLogDepth <= 1) |
| 123 |
return; |
| 124 |
stream() << msg << " table " << tableSize |
| 125 |
<< " continuousNoExpandLoop "<< continuousNoExpandLoop << "\n" |
| 126 |
<< std::flush; |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
void osl::checkmate::CheckmateRecorder::stat(const char *msg, Player P, |
| 131 |
unsigned int tableSize, |
| 132 |
unsigned int totalNodeCount, |
| 133 |
unsigned int totalNodeLimit) |
| 134 |
{ |
| 135 |
std::cerr << msg << " (" << P << ") " |
| 136 |
<< tableSize << "/" |
| 137 |
<< totalNodeCount << "/" << totalNodeLimit << std::endl; |
| 138 |
} |
| 139 |
|
| 140 |
void osl::checkmate:: |
| 141 |
CheckmateRecorder::enter(int depth, const char *name, |
| 142 |
CheckHashRecord *record, |
| 143 |
const HashKey& key, const PathEncoding& path, |
| 144 |
unsigned int proofLimit, unsigned int disProofLimit) |
| 145 |
{ |
| 146 |
#ifndef OSL_SMP |
| 147 |
check_assert(depth >= 0); |
| 148 |
check_stack.push_back(CheckStackEntry(nextMove, name, record, key, path, |
| 149 |
proofLimit, disProofLimit)); |
| 150 |
|
| 151 |
if (DepthTracer::maxVerboseLogDepth <= depth) |
| 152 |
return; |
| 153 |
|
| 154 |
stream() << makeHeader(depth) << "==> " << name << " "; |
| 155 |
if (nextMove) |
| 156 |
{ |
| 157 |
csaShow(stream(), nextMove->move); |
| 158 |
if (nextMove->record != record) |
| 159 |
{ |
| 160 |
std::cerr << "nextMove->record " << nextMove->record |
| 161 |
<< " != record " << record << " " << nextMove->move << "\n"; |
| 162 |
stream() << "nextMove->record " << nextMove->record |
| 163 |
<< " != record " << record << " " << nextMove->move << "\n"; |
| 164 |
} |
| 165 |
} |
| 166 |
else |
| 167 |
stream() << "root"; |
| 168 |
stream() << " limit(" << proofLimit << "," << disProofLimit << ")"; |
| 169 |
if (record) |
| 170 |
{ |
| 171 |
stream() << " depth " << record->distance |
| 172 |
<< " " << record->proofDisproof() |
| 173 |
<< " #moves " << record->moves.size(); |
| 174 |
#if 0 |
| 175 |
// TODO: 内訳の表示 |
| 176 |
if (! record->interposeMoves.empty()) |
| 177 |
stream() << " i " << record->interposeMoves.size(); |
| 178 |
if (! record->noPromoteMoves.empty()) |
| 179 |
stream() << " np " << record->noPromoteMoves.size(); |
| 180 |
if (! record->upwardMoves.empty()) |
| 181 |
stream() << " up " << record->upwardMoves.size(); |
| 182 |
#endif |
| 183 |
if (record->isConfluence) |
| 184 |
stream() << " (c)"; |
| 185 |
if (record->useMaxInsteadOfSum) |
| 186 |
stream() << " (max)"; |
| 187 |
} |
| 188 |
stream() << ' ' << record << ' ' << path << "\n" << std::flush; |
| 189 |
#ifdef CHECKMATE_DEBUG |
| 190 |
# if 0 |
| 191 |
if (record) |
| 192 |
record->dump(stream(), 1); |
| 193 |
# endif |
| 194 |
#endif |
| 195 |
|
| 196 |
#endif /* OSL_SMP */ |
| 197 |
} |
| 198 |
|
| 199 |
void osl::checkmate::CheckmateRecorder::leave(int depth, const char *name, |
| 200 |
const CheckHashRecord *record) |
| 201 |
{ |
| 202 |
#ifndef OSL_SMP |
| 203 |
check_assert(depth >= 0); |
| 204 |
check_stack.pop_back(); |
| 205 |
|
| 206 |
if (DepthTracer::maxVerboseLogDepth <= depth) |
| 207 |
return; |
| 208 |
|
| 209 |
stream() << makeHeader(depth) << "<== " << name << " " |
| 210 |
<< "(" << record->distance << ") " |
| 211 |
<< record->proofDisproof(); |
| 212 |
if (leaveReason) |
| 213 |
{ |
| 214 |
stream() << " " << leaveReason; |
| 215 |
leaveReason = 0; |
| 216 |
} |
| 217 |
stream() << " " << record << "\n" << std::flush; |
| 218 |
#endif |
| 219 |
} |
| 220 |
|
| 221 |
void osl::checkmate:: |
| 222 |
checkAbort(const char *func, const char *file, int line, const char *exp) |
| 223 |
{ |
| 224 |
std::cerr << "check_assert failed: (" << exp << "), function " << func |
| 225 |
<< ", file " << file << ", line " << line << ".\n"; |
| 226 |
CheckmateRecorder::dumpStack(); |
| 227 |
} |
| 228 |
|
| 229 |
#endif |
| 230 |
/* ------------------------------------------------------------------------- */ |
| 231 |
// ;;; Local Variables: |
| 232 |
// ;;; mode:c++ |
| 233 |
// ;;; c-basic-offset:2 |
| 234 |
// ;;; End: |