int main(int argc, char *argv[]) { camera::Loc.moveTo(0, 200, 0); NewWorld.loadWorld(NULL); //³õʼ»¯äÖȾÏß³Ì if (!initThread(argc, argv)) { return -1; } //¼ÓÔØBlock initBlock(); renderGroup Test; renderGroup VAO; location TTT(0, 0, 0); unsigned int i = -1; while (IsRenderThreadStart) { //äÖȾ NewWorld.refreshVAO(); Sleep(1); } #ifdef _DEBUG _CrtDumpMemoryLeaks(); #endif }
void *threadStart(void *arg) { Thread *thread = (Thread *)arg; //Object *jThread = thread->ee->thread; Object *jThread = thread->thread; /* Parent thread created thread with suspension disabled. This is inherited so we need to enable */ enableSuspend(thread); /* Complete initialisation of the thread structure, create the thread stack and add the thread to the thread list */ initThread(thread, INST_DATA(jThread)[daemon_offset], &thread); /* Add thread to thread ID map hash table. */ addThreadToHash(thread); /* Execute the thread's run method */ DummyFrame dummy; executeMethod(&dummy, jThread, CLASS_CB(jThread->classobj)->method_table[run_mtbl_idx]); /* Run has completed. Detach the thread from the VM and exit */ detachThread(thread); TRACE("Thread 0x%x id: %d exited\n", thread, thread->id); return NULL; }
Thread *attachThread(char *name, char is_daemon, void *stack_base, Thread *thread, Object *group) { Object *java_thread; /* Create the ExecEnv for the thread */ // ExecEnv *ee = (ExecEnv*)sysMalloc(sizeof(ExecEnv)); // memset(ee, 0, sizeof(ExecEnv)); thread->tid = pthread_self(); //thread->ee = ee; /* Complete initialisation of the thread structure, create the thread stack and add the thread to the thread list */ initThread(thread, is_daemon, stack_base); /* Initialise the Java-level thread objects representing this thread */ if((java_thread = initJavaThread(thread, is_daemon, name)) == NULL) return NULL; /* Initialiser doesn't handle the thread group */ INST_DATA(java_thread)[group_offset] = (uintptr_t)group; DummyFrame dummy; executeMethod(&dummy, group, addThread_mb, java_thread); /* We're now attached to the VM...*/ TRACE("Thread 0x%x id: %d attached\n", thread, thread->id); return thread; }
// Returns the actual usable size of a chunk. Since this requires loading the // header, we will return it in the second parameter, as it can be required // by the caller to perform additional processing. uptr getUsableSize(const void *Ptr, UnpackedHeader *Header) { if (UNLIKELY(!ThreadInited)) initThread(); if (!Ptr) return 0; uptr ChunkBeg = reinterpret_cast<uptr>(Ptr); ScudoChunk *Chunk = reinterpret_cast<ScudoChunk *>(ChunkBeg - ChunkHeaderSize); Chunk->loadHeader(Header); // Getting the usable size of a chunk only makes sense if it's allocated. if (Header->State != ChunkAllocated) { dieWithMessage("ERROR: attempted to size a non-allocated chunk at " "address %p\n", Chunk); } uptr Size = BackendAllocator.GetActuallyAllocatedSize(Chunk->AllocBeg(Header)); // UsableSize works as malloc_usable_size, which is also what (AFAIU) // tcmalloc's MallocExtension::GetAllocatedSize aims at providing. This // means we will return the size of the chunk from the user beginning to // the end of the 'user' allocation, hence us subtracting the header size // and the offset from the size. if (Size == 0) return Size; return Size - ChunkHeaderSize - (Header->Offset << MinAlignmentLog); }
time_t registerTimer(const time_t seconds, int *id, void *cb) { time_t now, then; time_t next; int i, idx; if (0 == thread_id) { initThread(); } if (seconds <= 0) return -1 ; // get the current time time(&now); for (idx = 0; idx < TIMEOUTS; ++idx) if (!((timeout_list[idx].timeout_state & TIMEOUT_USED) & TIMEOUT_USED)) break; if (TIMEOUTS == idx) // reach to end of timer list return -1; // idx th timeout will be used. // Reset and set state of the timer timeout_list[idx].timeout_state = 0; timeout_list[idx].timeout_state |= TIMEOUT_USED; // calculate when the timeout should fire then = now; timespec_add(&then, seconds); timeout_list[idx].timeout_time = then; timeout_list[idx].timeout_seconds = seconds; // printf( "\nbefore timeout_list[idx].cb = %X\n", timeout_list[idx].cb); timeout_list[idx].cb = cb; // printf( " after timeout_list[idx].cb = %X\n", timeout_list[idx].cb); // How long till the next timeout? next = seconds; for (i = 0; i < TIMEOUTS; i++) { if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED)) == TIMEOUT_USED) { const time_t secs = timespec_diff(timeout_list[i].timeout_time, now); if (secs >= 0 && secs < next) next = secs; } } *id = idx; /* Return the timeout number. */ return timeout_list[idx].timeout_time; }
// Allocates a chunk. void *allocate(uptr Size, uptr Alignment, AllocType Type) { if (UNLIKELY(!ThreadInited)) initThread(); if (!IsPowerOfTwo(Alignment)) { dieWithMessage("ERROR: malloc alignment is not a power of 2\n"); } if (Alignment > MaxAlignment) return BackendAllocator.ReturnNullOrDie(); if (Alignment < MinAlignment) Alignment = MinAlignment; if (Size == 0) Size = 1; if (Size >= MaxAllowedMallocSize) return BackendAllocator.ReturnNullOrDie(); uptr RoundedSize = RoundUpTo(Size, MinAlignment); uptr ExtraBytes = ChunkHeaderSize; if (Alignment > MinAlignment) ExtraBytes += Alignment; uptr NeededSize = RoundedSize + ExtraBytes; if (NeededSize >= MaxAllowedMallocSize) return BackendAllocator.ReturnNullOrDie(); void *Ptr; if (LIKELY(!ThreadTornDown)) { Ptr = BackendAllocator.Allocate(&Cache, NeededSize, MinAlignment); } else { SpinMutexLock l(&FallbackMutex); Ptr = BackendAllocator.Allocate(&FallbackAllocatorCache, NeededSize, MinAlignment); } if (!Ptr) return BackendAllocator.ReturnNullOrDie(); // If requested, we will zero out the entire contents of the returned chunk. if (ZeroContents && BackendAllocator.FromPrimary(Ptr)) memset(Ptr, 0, BackendAllocator.GetActuallyAllocatedSize(Ptr)); uptr AllocBeg = reinterpret_cast<uptr>(Ptr); uptr ChunkBeg = AllocBeg + ChunkHeaderSize; if (!IsAligned(ChunkBeg, Alignment)) ChunkBeg = RoundUpTo(ChunkBeg, Alignment); CHECK_LE(ChunkBeg + Size, AllocBeg + NeededSize); ScudoChunk *Chunk = reinterpret_cast<ScudoChunk *>(ChunkBeg - ChunkHeaderSize); UnpackedHeader Header = {}; Header.State = ChunkAllocated; Header.Offset = (ChunkBeg - ChunkHeaderSize - AllocBeg) >> MinAlignmentLog; Header.AllocType = Type; Header.RequestedSize = Size; Header.Salt = static_cast<u16>(Prng.Next()); Chunk->storeHeader(&Header); void *UserPtr = reinterpret_cast<void *>(ChunkBeg); // TODO(kostyak): hooks sound like a terrible idea security wise but might // be needed for things to work properly? // if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(UserPtr, Size); return UserPtr; }
void PoolMemoryAllocator::init() { #ifndef OGDF_MEMORY_POOL_NTS #ifdef OGDF_NO_COMPILER_TLS pthread_key_create(&s_tpKey,NULL); #endif s_criticalSection = new CriticalSection(500); #endif initThread(); }
SerialPortUi::SerialPortUi(QWidget *parent) : QDialog(parent), ui(new Ui::SerialPortUi) { ui->setupUi(this); initData(); initUI(); initThread(); initConnect(); }
IMUReader::IMUReader() { if ((file = open(I2CDEV, O_RDWR)) < 0) { COMM_EXCEPTION(InternalError, "Can't open I2C device"); } currentAngles = { 0, 0, 0 }; initThread(); }
CANUi::CANUi(QWidget *parent) : QFrame(parent), ui(new Ui::CANUi) ,m_masterBoard(new s_BOARD{}) ,m_canThread(CanThread::getInstance()) { ui->setupUi(this); initData(); initUI(); initThread(); initConnect(); }
void *calloc(uptr NMemB, uptr Size) { if (UNLIKELY(!ThreadInited)) initThread(); uptr Total = NMemB * Size; if (Size != 0 && Total / Size != NMemB) // Overflow check return BackendAllocator.ReturnNullOrDie(); void *Ptr = allocate(Total, MinAlignment, FromMalloc); // If ZeroContents, the content of the chunk has already been zero'd out. if (!ZeroContents && Ptr && BackendAllocator.FromPrimary(Ptr)) memset(Ptr, 0, getUsableSize(Ptr)); return Ptr; }
// Deallocates a Chunk, which means adding it to the delayed free list (or // Quarantine). void deallocate(void *UserPtr, uptr DeleteSize, AllocType Type) { if (UNLIKELY(!ThreadInited)) initThread(); // TODO(kostyak): see hook comment above // if (&__sanitizer_free_hook) __sanitizer_free_hook(UserPtr); if (!UserPtr) return; uptr ChunkBeg = reinterpret_cast<uptr>(UserPtr); if (!IsAligned(ChunkBeg, MinAlignment)) { dieWithMessage("ERROR: attempted to deallocate a chunk not properly " "aligned at address %p\n", UserPtr); } ScudoChunk *Chunk = reinterpret_cast<ScudoChunk *>(ChunkBeg - ChunkHeaderSize); UnpackedHeader OldHeader; Chunk->loadHeader(&OldHeader); if (OldHeader.State != ChunkAllocated) { dieWithMessage("ERROR: invalid chunk state when deallocating address " "%p\n", Chunk); } UnpackedHeader NewHeader = OldHeader; NewHeader.State = ChunkQuarantine; Chunk->compareExchangeHeader(&NewHeader, &OldHeader); if (DeallocationTypeMismatch) { // The deallocation type has to match the allocation one. if (NewHeader.AllocType != Type) { // With the exception of memalign'd Chunks, that can be still be free'd. if (NewHeader.AllocType != FromMemalign || Type != FromMalloc) { dieWithMessage("ERROR: allocation type mismatch on address %p\n", Chunk); } } } uptr Size = NewHeader.RequestedSize; if (DeleteSizeMismatch) { if (DeleteSize && DeleteSize != Size) { dieWithMessage("ERROR: invalid sized delete on chunk at address %p\n", Chunk); } } if (LIKELY(!ThreadTornDown)) { AllocatorQuarantine.Put(&ThreadQuarantineCache, QuarantineCallback(&Cache), Chunk, Size); } else { SpinMutexLock l(&FallbackMutex); AllocatorQuarantine.Put(&FallbackQuarantineCache, QuarantineCallback(&FallbackAllocatorCache), Chunk, Size); } }
VoltageReader::VoltageReader() { auto instance = ConfigManager::getInstance(); if (!instance) { COMM_EXCEPTION(NullPointerException, "Configuration manager " "instance is null."); } auto adcInfo = instance->getAdcInfo(); voltageChannel = adcInfo.voltageChannel; currentChannel = adcInfo.currentChannel; sensorInfo = instance->getVoltageSensorInfo(); initThread(); }
int main() { int choix = 0; char schoix[10]; int nbthread = 0; char snbthread[10]; /*introduction du programme*/ printf("----- Programme SuperPremier -----\n"); printf("Voici les nombres des premiers nombres premiers que vous pouvez chercher:\n"); printf("\t0 : exit\t5 : 2M\n"); printf("\t1 : 128K\t6 : 4M\n"); printf("\t2 : 256K\t7 : 8M\n"); printf("\t3 : 512K\t8 : 16M\n"); printf("\t4 : 1M \t9 : 32M\n"); /*selection du nb de nombre premier*/ printf("Faites votre choix: "); lecture(schoix); choix = atoi(schoix); #ifdef DEBUG debug("choix = %d",choix); #endif if(choix != 0) { /*selection du nb de thread*/ printf("Choississez le nombre de threads (de 1 à 8) : "); lecture(snbthread); nbthread = atoi(snbthread); #ifdef DEBUG debug("snbthread = %s",snbthread); debug("nbthread = %d",nbthread); debug("initThread(nbthread = %d, choix = %d)",nbthread,choix); #endif initThread(nbthread, choix); } printf("\n----- Fin du programme -----\n"); return 0; }
static void* startThreadEntryPoint(void* _args) { ThreadEntryPointArgs* args = (ThreadEntryPointArgs*) _args; Env* env = args->env; Thread* thread = args->thread; JavaThread* threadObj = args->threadObj; rvmLockThreadsList(); jboolean failure = TRUE; setThreadEnv(env); if (!rvmExceptionOccurred(env)) { if (initThread(env, thread, threadObj)) { if (rvmSetupSignals(env)) { failure = FALSE; thread->stackAddr = getStackAddress(); } } } thread->status = THREAD_STARTING; pthread_cond_broadcast(&threadStartCond); while (thread->status != THREAD_VMWAIT) { pthread_cond_wait(&threadStartCond, &threadsLock); } rvmUnlockThreadsList(); if (!failure) { rvmChangeThreadStatus(env, thread, THREAD_RUNNING); rvmChangeThreadPriority(env, thread, thread->threadObj->priority); Method* run = rvmGetInstanceMethod2(env, java_lang_Thread, "run", "()V"); if (run) { jvalue emptyArgs[0]; rvmCallVoidInstanceMethodA(env, (Object*) threadObj, run, emptyArgs); } } detachThread(env, TRUE); return NULL; }
// Reallocates a chunk. We can save on a new allocation if the new requested // size still fits in the chunk. void *reallocate(void *OldPtr, uptr NewSize) { if (UNLIKELY(!ThreadInited)) initThread(); UnpackedHeader OldHeader; uptr Size = getUsableSize(OldPtr, &OldHeader); uptr ChunkBeg = reinterpret_cast<uptr>(OldPtr); ScudoChunk *Chunk = reinterpret_cast<ScudoChunk *>(ChunkBeg - ChunkHeaderSize); if (OldHeader.AllocType != FromMalloc) { dieWithMessage("ERROR: invalid chunk type when reallocating address %p\n", Chunk); } UnpackedHeader NewHeader = OldHeader; // The new size still fits in the current chunk. if (NewSize <= Size) { NewHeader.RequestedSize = NewSize; Chunk->compareExchangeHeader(&NewHeader, &OldHeader); return OldPtr; } // Otherwise, we have to allocate a new chunk and copy the contents of the // old one. void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); if (NewPtr) { uptr OldSize = OldHeader.RequestedSize; memcpy(NewPtr, OldPtr, Min(NewSize, OldSize)); NewHeader.State = ChunkQuarantine; Chunk->compareExchangeHeader(&NewHeader, &OldHeader); if (LIKELY(!ThreadTornDown)) { AllocatorQuarantine.Put(&ThreadQuarantineCache, QuarantineCallback(&Cache), Chunk, OldSize); } else { SpinMutexLock l(&FallbackMutex); AllocatorQuarantine.Put(&FallbackQuarantineCache, QuarantineCallback(&FallbackAllocatorCache), Chunk, OldSize); } } return NewPtr; }
int main (int argc, char * argv[]) { srand(time(NULL)); srand((unsigned int)time((time_t *)NULL)); #ifdef READ_THREADS traj_reader.read_threads_from_file(); all_threads = traj_reader.get_all_threads(); #endif /* int me, numprocs; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); std::cout << "num processors: " << numprocs << std::endl; MPI_Comm_rank(MPI_COMM_WORLD, &me);*/ printf("Instructions:\n" "Hold down the left mouse button to rotate image: \n" "\n" "Hold 'm' while holding down the right mouse to move the end\n" "Hold 't' while holding down the right mouse to rotate the tangent \n" "\n" "Press 'q' to quit\n" ); /* initialize glut */ glutInit (&argc, argv); //can i do that? glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(900,900); glutCreateWindow ("Thread"); glutDisplayFunc (DrawStuff); glutMotionFunc (MouseMotion); glutMouseFunc (processMouse); glutKeyboardFunc(processNormalKeys); glutKeyboardUpFunc(processKeyUp); /* create popup menu */ glutCreateMenu (JoinStyle); glutAddMenuEntry ("Exit", 99); glutAttachMenu (GLUT_MIDDLE_BUTTON); /* initialize GL */ glClearDepth (1.0); glEnable (GL_DEPTH_TEST); glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMatrixMode (GL_PROJECTION); /* roughly, measured in centimeters */ glFrustum (-30.0, 30.0, -30.0, 30.0, 50.0, 500.0); glMatrixMode(GL_MODELVIEW); /* initialize lighting */ glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition); glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor); glEnable (GL_LIGHT0); glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition); glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor); glEnable (GL_LIGHT1); glLightfv (GL_LIGHT2, GL_POSITION, lightThreePosition); glLightfv (GL_LIGHT2, GL_DIFFUSE, lightThreeColor); glEnable (GL_LIGHT2); glLightfv (GL_LIGHT3, GL_POSITION, lightFourPosition); glLightfv (GL_LIGHT3, GL_DIFFUSE, lightFourColor); glEnable (GL_LIGHT3); glEnable (GL_LIGHTING); glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable (GL_COLOR_MATERIAL); InitStuff (); #ifdef READ_THREADS thread = &all_threads[thread_ind]; #else initThread(); thread->minimize_energy(); #endif updateThreadPoints(); updateIms(_start_pt); thread_vision.set_max_length(98); thread_vision.setInitPt(_start_pt); thread_vision.optimizeThread(); for (int i=0; i < NUM_PTS; i++) { radii[i]=THREAD_RADII; } glutMainLoop (); // return 0; /* ANSI C requires main to return int. */ }
static jint attachThread(VM* vm, Env** envPtr, char* name, Object* group, jboolean daemon) { Env* env = *envPtr; // env is NULL if rvmAttachCurrentThread() was called. If non NULL rvmInitThreads() was called. if (!env) { // If the thread was already attached there's an Env* associated with the thread. env = (Env*) pthread_getspecific(tlsEnvKey); if (env) { env->attachCount++; *envPtr = env; return JNI_OK; } } if (!env) { env = rvmCreateEnv(vm); if (!env) goto error; } setThreadEnv(env); if (rvmExceptionOccurred(env)) goto error; Thread* thread = allocThread(env); if (!thread) goto error; thread->stackAddr = getStackAddress(); thread->pThread = pthread_self(); env->currentThread = thread; rvmChangeThreadStatus(env, thread, THREAD_RUNNING); JavaThread* threadObj = (JavaThread*) rvmAllocateObject(env, java_lang_Thread); if (!threadObj) goto error; rvmLockThreadsList(); if (!initThread(env, thread, threadObj)) { rvmUnlockThreadsList(); goto error; } if (!rvmSetupSignals(env)) { rvmUnlockThreadsList(); goto error; } DL_PREPEND(threads, thread); pthread_cond_broadcast(&threadsChangedCond); rvmUnlockThreadsList(); Object* threadName = NULL; if (name) { threadName = rvmNewStringUTF(env, name, -1); if (!threadName) goto error_remove; } Method* threadConstructor = rvmGetInstanceMethod2(env, java_lang_Thread, "<init>", "(JLjava/lang/String;Ljava/lang/ThreadGroup;Z)V"); if (!threadConstructor) goto error_remove; rvmCallNonvirtualVoidInstanceMethod(env, (Object*) threadObj, threadConstructor, PTR_TO_LONG(thread), threadName, group, daemon); if (rvmExceptionOccurred(env)) goto error_remove; *envPtr = env; return JNI_OK; error_remove: rvmLockThreadsList(); DL_DELETE(threads, thread); pthread_cond_broadcast(&threadsChangedCond); rvmTearDownSignals(env); rvmUnlockThreadsList(); error: if (env) env->currentThread = NULL; clearThreadEnv(); return JNI_ERR; }
void Client::initThreadIfNotAlready(const char* desc) { if (currentClient.getMake()->get()) return; initThread(desc); }
void Client::initThread(StringData desc, transport::SessionHandle session) { initThread(desc, getGlobalServiceContext(), std::move(session)); }
int main(int argc, char **argv) { InitializeFast(); Backend backend; ANNEvaluator evaluator; ANNMoveEvaluator mevaluator(evaluator); // if eval.net exists, use the ANN evaluator // if both eval.net and meval.net exist, use the ANN move evaluator if (FileReadable(EvalNetFilename)) { backend.SetEvaluator(&evaluator); std::cout << "# Using ANN evaluator" << std::endl; if (FileReadable(MoveEvalNetFilename)) { std::cout << "# Using ANN move evaluator" << std::endl; backend.SetMoveEvaluator(&mevaluator); } else { std::cout << "# Using static move evaluator" << std::endl; backend.SetMoveEvaluator(&gStaticMoveEvaluator); } } else { std::cout << "# Using static evaluator" << std::endl; std::cout << "# Using static move evaluator" << std::endl; backend.SetEvaluator(&Eval::gStaticEvaluator); backend.SetMoveEvaluator(&gStaticMoveEvaluator); } // first we handle special operation modes if (argc >= 2 && std::string(argv[1]) == "tdl") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 3) { std::cout << "Usage: " << argv[0] << " tdl positions" << std::endl; return 0; } //try //{ Learn::TDL(argv[2]); //} //catch(...){} return 0; } else if (argc >= 2 && std::string(argv[1]) == "conv") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 3) { std::cout << "Usage: " << argv[0] << " conv FEN" << std::endl; return 0; } std::stringstream ss; for (int i = 2; i < argc; ++i) { ss << argv[i] << ' '; } Board b(ss.str()); std::vector<FeaturesConv::FeatureDescription> ret; FeaturesConv::ConvertBoardToNN(b, ret); return 0; } else if(argc >= 2 && std::string(argv[1]) == "train_eval") { // ./giraffe train_eval <list filename> <epd_data_path> <epd_label_path> <epochs> InitializeSlowBlocking(evaluator, mevaluator); std::ifstream epd_file_list(argv[2]); std::string epd_data_path = argv[3]; std::string epd_label_path = argv[4]; std::string epd_filename; std::vector<std::string> filenames; while(std::getline(epd_file_list, epd_filename)){ filenames.push_back(epd_filename); } int epochs = std::stoi(argv[5]); for(int i = 0; i < epochs*filenames.size(); i++){ std::string epd_path_full = epd_data_path + "/" + filenames[i % filenames.size()]; std::string label_path_full = epd_label_path + "/" + filenames[i % filenames.size()] + ".xie"; std::cout << label_path_full << std::endl; std::ifstream epd_file(epd_path_full); std::ifstream label_file(label_path_full); std::string fen; std::vector<std::string> fens; //std::cout << "Reading FENS" << std::endl; while(std::getline(epd_file, fen)) { fens.push_back(fen); } //std::cout << "Reading labels" << std::endl; std::string label; NNMatrixRM mat_labels = NNMatrixRM(fens.size(), 1); //std::vector<int> labels; int idx = 0; while(std::getline(label_file, label)){ //labels.push_back(stoi(label)); mat_labels(idx, 0) = std::stoi(label); idx++; } //std::cout << "Getting feature descriptions" << std::endl; Board dummy; std::vector<FeaturesConv::FeatureDescription> ret; FeaturesConv::ConvertBoardToNN(dummy, ret); //std::cout << "Starting Training" << std::endl; std::ofstream outNet(argv[6]); //evaluator.Serialize(outNet); evaluator.TrainLoop(fens, mat_labels, 10, ret); evaluator.Serialize(outNet); } /* std::ifstream epd_file(argv[2]); std::ifstream label_file(argv[3]); std::string fen; std::vector<std::string> fens; std::cout << "Reading FENS" << std::endl; while(std::getline(epd_file, fen)) { fens.push_back(fen); } std::cout << "Reading labels" << std::endl; std::string label; NNMatrixRM mat_labels = NNMatrixRM(fens.size(), 1); //std::vector<int> labels; int idx = 0; while(std::getline(label_file, label)){ //labels.push_back(stoi(label)); mat_labels(idx, 0) = std::stoi(label); idx++; } std::cout << "Getting feature descriptions" << std::endl; int epochs = std::stoi(argv[4]); Board dummy; std::vector<FeaturesConv::FeatureDescription> ret; FeaturesConv::ConvertBoardToNN(dummy, ret); std::cout << "Starting Training" << std::endl; std::ofstream outNet(argv[5]); evaluator.Serialize(outNet); evaluator.TrainLoop(fens, mat_labels, epochs, ret); evaluator.Serialize(outNet); */ return 0; } else if (argc >= 2 && std::string(argv[1]) == "conv_file") { InitializeSlowBlocking(evaluator, mevaluator); std::ifstream inFile(argv[2]); std::ofstream outFile(argv[3]); std::string fen; std::vector<std::string> fens; while(std::getline(inFile, fen)) { fens.push_back(fen); std::cout << fen << std::endl; /* Board b(fen); std::vector<FeaturesConv::FeatureDescription> ret; FeaturesConv::ConvertBoardToNN(b, ret); std::stringstream ss; for (int i = 0; i < ret.size()-1; i++) { ss << ret[i].XieToString() << " "; } ss << ret[ret.size() - 1].XieToString(); outFile << ss.str() << std::endl; std::cout << ss.str() << std::endl; std::cout << "****" << std::endl; */ } std::vector<FeaturesConv::FeatureDescription> dummy(363); NNMatrixRM ret = evaluator.BoardsToFeatureRepresentation_(fens, dummy); for(int64_t row = 0; row < ret.rows(); ++row) { for(int64_t col = 0; col < ret.cols(); ++ col) { outFile << ret(row,col) << ' '; } outFile << '\n'; } inFile.close(); outFile.close(); return 0; } else if (argc >= 2 && std::string(argv[1]) == "mconv") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 3) { std::cout << "Usage: " << argv[0] << " mconv FEN" << std::endl; return 0; } std::stringstream ss; for (int i = 2; i < argc; ++i) { ss << argv[i] << ' '; } Board b(ss.str()); MoveList moves; b.GenerateAllLegalMoves<Board::ALL>(moves); NNMatrixRM ret; FeaturesConv::ConvertMovesInfo convInfo; FeaturesConv::ConvertMovesToNN(b, convInfo, moves, ret); for (int64_t row = 0; row < ret.rows(); ++row) { for (int64_t col = 0; col < ret.cols(); ++col) { std::cout << ret(row, col) << ' '; } std::cout << std::endl; } return 0; } else if (argc >= 2 && std::string(argv[1]) == "bench") { InitializeSlowBlocking(evaluator, mevaluator); double startTime = CurrentTime(); static const NodeBudget BenchNodeBudget = 64*1024*1024; Search::SyncSearchNodeLimited(Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); Search::SyncSearchNodeLimited(Board("2r2rk1/pp3pp1/b2Pp3/P1Q4p/RPqN2n1/8/2P2PPP/2B1R1K1 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); Search::SyncSearchNodeLimited(Board("8/1nr3pk/p3p1r1/4p3/P3P1q1/4PR1N/3Q2PK/5R2 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); Search::SyncSearchNodeLimited(Board("5R2/8/7r/7P/5RPK/1k6/4r3/8 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); Search::SyncSearchNodeLimited(Board("r5k1/2p2pp1/1nppr2p/8/p2PPp2/PPP2P1P/3N2P1/R3RK2 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); Search::SyncSearchNodeLimited(Board("8/R7/8/1k6/1p1Bq3/8/4NK2/8 w - - 0 1"), BenchNodeBudget, backend.GetEvaluator(), backend.GetMoveEvaluator()); std::cout << "Time: " << (CurrentTime() - startTime) << "s" << std::endl; return 0; } else if (argc >= 2 && std::string(argv[1]) == "check_bounds") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 3) { std::cout << "Usage: " << argv[0] << " check_bounds <EPD/FEN file>" << std::endl; return 0; } std::ifstream infile(argv[2]); if (!infile) { std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl; return 1; } uint64_t passes = 0; uint64_t total = 0; float windowSizeTotal = 0.0f; std::string fen; std::vector<std::string> fens; while (std::getline(infile, fen)) { fens.push_back(fen); } #pragma omp parallel { auto evaluatorCopy = evaluator; #pragma omp for for (size_t i = 0; i < fens.size(); ++i) { Board b(fens[i]); float windowSize = 0.0f; bool res = evaluatorCopy.CheckBounds(b, windowSize); #pragma omp critical(boundCheckAccum) { if (res) { ++passes; } ++total; windowSizeTotal += windowSize; } } } std::cout << passes << "/" << total << std::endl; std::cout << "Average window size: " << (windowSizeTotal / total) << std::endl; return 0; } else if (argc >= 2 && std::string(argv[1]) == "train_bounds") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 4) { std::cout << "Usage: " << argv[0] << " train_bounds <EPD/FEN file> <output net file>" << std::endl; return 0; } std::ifstream infile(argv[2]); if (!infile) { std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl; return 1; } std::vector<FeaturesConv::FeatureDescription> featureDescriptions; Board dummyBoard; FeaturesConv::ConvertBoardToNN(dummyBoard, featureDescriptions); std::string line; std::vector<std::string> fens; while (std::getline(infile, line)) { fens.push_back(line); } const size_t BlockSize = 256; const size_t PrintInterval = BlockSize * 100; for (size_t i = 0; i < (fens.size() - BlockSize); i += BlockSize) { if (i % PrintInterval == 0) { std::cout << i << "/" << fens.size() << std::endl; } std::vector<std::string> positions; for (size_t j = 0; j < BlockSize; ++j) { positions.push_back(fens[i + j]); } evaluator.TrainBounds(positions, featureDescriptions, 1.0f); } std::ofstream outfile(argv[3]); if (!outfile) { std::cerr << "Failed to open " << argv[3] << " for writing" << std::endl; return 1; } evaluator.Serialize(outfile); return 0; } else if (argc >= 2 && std::string(argv[1]) == "sample_internal") { // MUST UNCOMMENT "#define SAMPLING" in static move evaluator InitializeSlowBlocking(evaluator, mevaluator); if (argc < 4) { std::cout << "Usage: " << argv[0] << " sample_internal <EPD/FEN file> <output file>" << std::endl; return 0; } std::ifstream infile(argv[2]); std::ofstream outfile(argv[3]); if (!infile) { std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl; return 1; } std::string fen; std::vector<std::string> fens; static const uint64_t maxPositions = 5000000; uint64_t numPositions = 0; while (std::getline(infile, fen) && numPositions < maxPositions) { fens.push_back(fen); ++numPositions; } #pragma omp parallel { auto evaluatorCopy = evaluator; #pragma omp for for (size_t i = 0; i < fens.size(); ++i) { std::cout << i << std::endl; Board b(fens[i]); Search::SyncSearchNodeLimited(b, 1000, &evaluatorCopy, &gStaticMoveEvaluator, nullptr, nullptr); } } for (const auto &pos : gStaticMoveEvaluator.samples) { outfile << pos << std::endl; } return 0; } else if (argc >= 2 && std::string(argv[1]) == "label_bm") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 4) { std::cout << "Usage: " << argv[0] << " label_bm <EPD/FEN file> <output file>" << std::endl; return 0; } std::ifstream infile(argv[2]); std::ofstream outfile(argv[3]); if (!infile) { std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl; return 1; } std::string fen; std::vector<std::string> fens; static const uint64_t maxPositions = 5000000; uint64_t numPositions = 0; while (std::getline(infile, fen) && numPositions < maxPositions) { Board b(fen); if (b.GetGameStatus() != Board::ONGOING) { continue; } fens.push_back(fen); ++numPositions; } std::vector<std::string> bm(fens.size()); uint64_t numPositionsDone = 0; double lastPrintTime = CurrentTime(); size_t lastDoneCount = 0; #pragma omp parallel { auto evaluatorCopy = evaluator; #pragma omp for schedule(dynamic) for (size_t i = 0; i < fens.size(); ++i) { Board b(fens[i]); Search::SearchResult result = Search::SyncSearchNodeLimited(b, 100000, &evaluatorCopy, &gStaticMoveEvaluator, nullptr, nullptr); bm[i] = b.MoveToAlg(result.pv[0]); #pragma omp critical(numPositionsAndOutputFileUpdate) { ++numPositionsDone; outfile << fens[i] << '\n'; outfile << bm[i] << '\n'; if (omp_get_thread_num() == 0) { double currentTime = CurrentTime(); double timeDiff = currentTime - lastPrintTime; if (timeDiff > 1.0) { std::cout << numPositionsDone << '/' << fens.size() << std::endl; std::cout << "Positions per second: " << static_cast<double>(numPositionsDone - lastDoneCount) / timeDiff << std::endl; lastPrintTime = currentTime; lastDoneCount = numPositionsDone; } } } } } return 0; } else if (argc >= 2 && std::string(argv[1]) == "train_move_eval") { InitializeSlowBlocking(evaluator, mevaluator); if (argc < 4) { std::cout << "Usage: " << argv[0] << " train_move_eval <EPD/FEN file> <output file>" << std::endl; return 0; } std::ifstream infile(argv[2]); if (!infile) { std::cerr << "Failed to open " << argv[2] << " for reading" << std::endl; return 1; } std::cout << "Reading positions from " << argv[2] << std::endl; std::string fen; std::string bestMove; std::vector<std::string> fens; std::vector<std::string> bestMoves; static const uint64_t MaxPositions = 5000000; uint64_t numPositions = 0; while (std::getline(infile, fen) && std::getline(infile, bestMove) && numPositions < MaxPositions) { Board b(fen); if (b.GetGameStatus() != Board::ONGOING) { continue; } fens.push_back(fen); bestMoves.push_back(bestMove); ++numPositions; } assert(bestMoves.size() == fens.size()); // now we split a part of it out into a withheld test set size_t numTrainExamples = fens.size() * 0.9f; std::vector<std::string> fensTest(fens.begin() + numTrainExamples, fens.end()); std::vector<std::string> bestMovesTest(bestMoves.begin() + numTrainExamples, bestMoves.end()); static const uint64_t MaxTestingPositions = 10000; if (fensTest.size() > MaxTestingPositions) { fensTest.resize(MaxTestingPositions); bestMovesTest.resize(MaxTestingPositions); } fens.resize(numTrainExamples); bestMoves.resize(numTrainExamples); std::cout << "Num training examples: " << numTrainExamples << std::endl; std::cout << "Num testing examples: " << fensTest.size() << std::endl; std::cout << "Starting training" << std::endl; ANNMoveEvaluator meval(evaluator); meval.Train(fens, bestMoves); meval.Test(fensTest, bestMovesTest); std::ofstream outfile(argv[3]); meval.Serialize(outfile); return 0; } // we need a mutex here because InitializeSlow needs to print, and it may decide to // print at the same time as the main command loop (if the command loop isn't waiting) std::mutex coutMtx; coutMtx.lock(); // do all the heavy initialization in a thread so we can reply to "protover 2" in time std::thread initThread(InitializeSlow, std::ref(evaluator), std::ref(mevaluator), std::ref(coutMtx)); auto waitForSlowInitFunc = [&initThread, &coutMtx]() { coutMtx.unlock(); initThread.join(); coutMtx.lock(); }; while (true) { std::string lineStr; coutMtx.unlock(); std::getline(std::cin, lineStr); coutMtx.lock(); std::stringstream line(lineStr); // we set usermove=1, so all commands from xboard start with a unique word std::string cmd; line >> cmd; // this is the list of commands we can process before initialization finished if ( cmd != "xboard" && cmd != "protover" && cmd != "hard" && cmd != "easy" && cmd != "cores" && cmd != "memory" && cmd != "accepted" && cmd != "rejected" && initThread.joinable()) { // wait for initialization to be done waitForSlowInitFunc(); } if (cmd == "xboard") {} // ignore since we only support xboard mode anyways else if (cmd == "protover") { int32_t ver; line >> ver; if (ver >= 2) { std::string name = "Giraffe"; if (gVersion != "") { name += " "; name += gVersion; } std::cout << "feature ping=1 setboard=1 playother=0 san=0 usermove=1 time=1 draw=0 sigint=0 sigterm=0 " "reuse=1 analyze=1 myname=\"" << name << "\" variants=normal colors=0 ics=0 name=0 pause=0 nps=0 " "debug=1 memory=0 smp=0 done=0" << std::endl; std::cout << "feature option=\"GaviotaTbPath -path .\"" << std::endl; std::cout << "feature done=1" << std::endl; } } else if (cmd == "accepted") {}
ServerThreadManager::ServerThreadManager() { initThread(0, newJobId()); }