see-perf.cc
Go to the documentation of this file.
00001 #include "osl/eval/see.h"
00002 #include "osl/eval/pieceEval.h"
00003 #include "osl/effect_util/pin.h"
00004 #include "osl/record/csaRecord.h"
00005 #include "osl/record/csaIOError.h"
00006 #include "osl/move_generator/legalMoves.h"
00007 #include "osl/container/moveVector.h"
00008 #include "osl/stat/average.h"
00009 #include "osl/misc/perfmon.h"
00010 
00011 #include <boost/format.hpp>
00012 #include <string>
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <cmath>
00016 #include <cstdio>
00017 using namespace osl;
00018 
00024 void usage(const char *prog)
00025 {
00026   using namespace std;
00027   cerr << "Usage: " << prog << " [-v] [-f skip] [-o] csafiles\n"
00028        << endl;
00029   exit(1);
00030 }
00031 
00032 size_t first_skip = 0;
00033 bool verbose = false;
00034 bool old = false;
00035 
00036 stat::Average moves, cycles, cycles_per_move;
00037 
00038 void test_file(const char *filename);
00039 
00040 int main(int argc, char **argv)
00041 {
00042   const char *program_name = argv[0];
00043   bool error_flag = false;
00044   extern char *optarg;
00045   extern int optind;
00046     
00047   char c;
00048   while ((c = getopt(argc, argv, "f:ovh")) != EOF)
00049   {
00050     switch(c)
00051     {
00052     case 'f':   first_skip = atoi(optarg);
00053       break;
00054     case 'v':   verbose = true;
00055       break;
00056     case 'o':   old = true;
00057       break;
00058     default:    error_flag = true;
00059     }
00060   }
00061   argc -= optind;
00062   argv += optind;
00063 
00064   if (error_flag || (argc < 1))
00065     usage(program_name);
00066 
00067   for (int i=0; i<argc; ++i)
00068   {
00069     if (i % 128 == 0)
00070       std::cerr << '.';
00071     test_file(argv[i]);
00072   }
00073 
00074   std::cout << "average cycles/position " << cycles.getAverage() << "\n"
00075             << "average cycles/position/move " << cycles_per_move.getAverage()
00076             << "\n";
00077 }
00078 
00079 /* ------------------------------------------------------------------------- */
00080 
00081 size_t num_positions = 0;
00082 void test_position(const NumEffectState& state)
00083 {
00084   MoveVector moves;
00085   LegalMoves::generate(state, moves);
00086   const PieceMask my_pin = effect_util::Pin::make(state, state.turn());
00087   const PieceMask op_pin = effect_util::Pin::make(state, alt(state.turn()));
00088   
00089   misc::PerfMon clock;
00090   for (size_t i=0; i<moves.size(); ++i) {
00091     if (old)
00092       PieceEval::computeDiffAfterMoveForRP(state, moves[i]);
00093     else
00094       See::see(state, moves[i], my_pin, op_pin);
00095   }
00096   const size_t consumed = clock.stop();
00097   cycles.add(consumed);
00098   cycles_per_move.add(consumed/moves.size());
00099   ++num_positions;
00100 }
00101 
00102 void test_file(const char *filename)
00103 {
00104   Record rec;
00105   try {
00106     rec = CsaFile(filename).getRecord();
00107   }
00108   catch (CsaIOError& e) {
00109     std::cerr << "skip " << filename <<"\n";
00110     std::cerr << e.what() << "\n";
00111     return;
00112   }
00113   catch (...) {
00114     throw;
00115   }
00116   
00117   NumEffectState state(rec.getInitialState());
00118   const osl::stl::vector<osl::Move> moves=rec.getMoves();
00119 
00120   for (size_t i=0; i<moves.size(); ++i) {
00121     const Move move = moves[i];
00122     assert(state.isValidMove(move));
00123     if (i >= first_skip) {
00124       test_position(state);
00125     }
00126     state.makeMove(move);
00127   }
00128 }
00129 
00130 /* ------------------------------------------------------------------------- */
00131 // ;;; Local Variables:
00132 // ;;; mode:c++
00133 // ;;; c-basic-offset:2
00134 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines