File: Worker.cpp

package info (click to toggle)
leela-zero 0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 5,772 kB
  • sloc: cpp: 20,958; python: 4,894; makefile: 66
file content (107 lines) | stat: -rw-r--r-- 3,045 bytes parent folder | download | duplicates (2)
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
/*
    This file is part of Leela Zero.
    Copyright (C) 2017-2018 Marco Calignano

    Leela Zero is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Leela Zero is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Leela Zero.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Worker.h"
#include "Game.h"
#include <QTextStream>
#include <QLockFile>
#include <QUuid>
#include <chrono>


Worker::Worker(int index, const QString& gpuIndex, Management *parent) :
    m_index(index),
    m_state(),
    m_gpu(""),
    m_job(nullptr),
    m_boss(parent)
{
    if (!gpuIndex.isEmpty()) {
        m_gpu = " --gpu=" + gpuIndex + " ";
    }
}

void Worker::doStore() {
    QTextStream(stdout) << "Storing current game ..." << endl;
    m_job->store();
    m_state.store(STORING);
}

void Worker::order(Order o)
{
    if (!o.isValid()) {
        if (m_job != nullptr) {
            m_job->finish();
        }
        return;
    }
    if (m_todo.type() != o.type() || m_job == nullptr) {
        createJob(o.type());
    }
    m_todo = o;
    m_job->init(m_todo);
}


void Worker::createJob(int type) {
    if (m_job != nullptr) {
        delete m_job;
    }
    switch (type) {
    case Order::Production:
    case Order::RestoreSelfPlayed:
        m_job = new ProductionJob(m_gpu, m_boss);
        break;
    case Order::Validation:
    case Order::RestoreMatch:
        m_job = new ValidationJob(m_gpu, m_boss);
        break;
    case Order::Wait:
        m_job = new WaitJob(m_gpu, m_boss);
        break;
    }
}

void Worker::run() {
     Result res;
     do {
        auto start = std::chrono::high_resolution_clock::now();
        res = m_job->execute();
        auto end = std::chrono::high_resolution_clock::now();
        auto gameDuration =
        std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
        if (m_state != STORING) {
            emit resultReady(m_todo, res, m_index, gameDuration);
        }
    } while (m_state == RUNNING);
    if (m_state == STORING) {
        m_todo.add("moves", res.parameters()["moves"]);
        m_todo.add("sgf", res.parameters()["sgf"]);
        if (res.type() == Result::StoreMatch) {
            m_todo.type(Order::RestoreMatch);
        } else {
            m_todo.type(Order::RestoreSelfPlayed);
        }
        QString unique = QUuid::createUuid().toRfc4122().toHex();
        QLockFile fi("storefile" + unique + ".bin.lock");
        fi.lock();
        m_todo.save("storefile" + unique + ".bin");
        fi.unlock();
    }
    QTextStream(stdout) << "Program ends: quitting current worker." << endl;
}