Class OclInterleave renamed to OclSharedData and added class OclSharedState.
This commit is contained in:
parent
c908ef2489
commit
f4943b77f3
10 changed files with 154 additions and 34 deletions
130
src/backend/opencl/runners/tools/OclSharedData.cpp
Normal file
130
src/backend/opencl/runners/tools/OclSharedData.cpp
Normal file
|
@ -0,0 +1,130 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "backend/opencl/runners/tools/OclSharedData.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/tools/Chrono.h"
|
||||
|
||||
|
||||
#include <cinttypes>
|
||||
#include <thread>
|
||||
|
||||
|
||||
uint64_t xmrig::OclSharedData::adjustDelay(size_t id)
|
||||
{
|
||||
if (m_threads < 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint64_t t0 = Chrono::steadyMSecs();
|
||||
uint64_t delay = 0;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
const uint64_t dt = t0 - m_timestamp;
|
||||
m_timestamp = t0;
|
||||
|
||||
// The perfect interleaving is when N threads on the same GPU start with T/N interval between each other
|
||||
// If a thread starts earlier than 0.75*T/N ms after the previous thread, delay it to restore perfect interleaving
|
||||
if ((dt > 0) && (dt < m_threshold * (m_averageRunTime / m_threads))) {
|
||||
delay = static_cast<uint64_t>(m_averageRunTime / m_threads - dt);
|
||||
m_threshold = 0.75;
|
||||
}
|
||||
}
|
||||
|
||||
if (delay == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (delay >= 400) {
|
||||
delay = 200;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
|
||||
|
||||
# ifdef XMRIG_INTERLEAVE_DEBUG
|
||||
LOG_WARN("Thread #%zu was paused for %" PRIu64 " ms to adjust interleaving", id, delay);
|
||||
# endif
|
||||
|
||||
return delay;
|
||||
}
|
||||
|
||||
|
||||
uint64_t xmrig::OclSharedData::resumeDelay(size_t id)
|
||||
{
|
||||
if (m_threads < 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t delay = 0;
|
||||
|
||||
{
|
||||
constexpr const double firstThreadSpeedupCoeff = 1.25;
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
delay = static_cast<uint64_t>(m_resumeCounter * m_averageRunTime / m_threads / firstThreadSpeedupCoeff);
|
||||
++m_resumeCounter;
|
||||
}
|
||||
|
||||
if (delay == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (delay > 1000) {
|
||||
delay = 1000;
|
||||
}
|
||||
|
||||
# ifdef XMRIG_INTERLEAVE_DEBUG
|
||||
LOG_WARN("Thread #%zu will be paused for %" PRIu64 " ms to before resuming", id, delay);
|
||||
# endif
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
|
||||
|
||||
return delay;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::OclSharedData::setResumeCounter(uint32_t value)
|
||||
{
|
||||
if (m_threads < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_resumeCounter = value;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::OclSharedData::setRunTime(uint64_t time)
|
||||
{
|
||||
// averagingBias = 1.0 - only the last delta time is taken into account
|
||||
// averagingBias = 0.5 - the last delta time has the same weight as all the previous ones combined
|
||||
// averagingBias = 0.1 - the last delta time has 10% weight of all the previous ones combined
|
||||
constexpr double averagingBias = 0.1;
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_averageRunTime = m_averageRunTime * (1.0 - averagingBias) + time * averagingBias;
|
||||
}
|
64
src/backend/opencl/runners/tools/OclSharedData.h
Normal file
64
src/backend/opencl/runners/tools/OclSharedData.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_OCLSHAREDDATA_H
|
||||
#define XMRIG_OCLSHAREDDATA_H
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class OclSharedData
|
||||
{
|
||||
public:
|
||||
OclSharedData() = default;
|
||||
|
||||
uint64_t adjustDelay(size_t id);
|
||||
uint64_t resumeDelay(size_t id);
|
||||
void setResumeCounter(uint32_t value);
|
||||
void setRunTime(uint64_t time);
|
||||
|
||||
inline OclSharedData &operator++() { ++m_threads; return *this; }
|
||||
|
||||
private:
|
||||
double m_averageRunTime = 0.0;
|
||||
double m_threshold = 0.95;
|
||||
size_t m_threads = 0;
|
||||
std::mutex m_mutex;
|
||||
uint32_t m_resumeCounter = 0;
|
||||
uint64_t m_timestamp = 0;
|
||||
};
|
||||
|
||||
|
||||
using OclSharedDataPtr = std::shared_ptr<OclSharedData>;
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_OCLSHAREDDATA_H */
|
62
src/backend/opencl/runners/tools/OclSharedState.cpp
Normal file
62
src/backend/opencl/runners/tools/OclSharedState.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "backend/opencl/runners/tools/OclSharedState.h"
|
||||
#include "backend/opencl/runners/tools/OclSharedData.h"
|
||||
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
static std::map<uint32_t, OclSharedData> map;
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
xmrig::OclSharedData &xmrig::OclSharedState::get(uint32_t index)
|
||||
{
|
||||
return map[index];
|
||||
}
|
||||
|
||||
|
||||
void xmrig::OclSharedState::release()
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::OclSharedState::start(const std::vector<OclLaunchData> &threads)
|
||||
{
|
||||
assert(map.empty());
|
||||
|
||||
for (const auto &data : threads) {
|
||||
++map[data.device.index()];
|
||||
}
|
||||
}
|
48
src/backend/opencl/runners/tools/OclSharedState.h
Normal file
48
src/backend/opencl/runners/tools/OclSharedState.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_OCLSHAREDSTATE_H
|
||||
#define XMRIG_OCLSHAREDSTATE_H
|
||||
|
||||
|
||||
#include "backend/opencl/OclLaunchData.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class OclSharedState
|
||||
{
|
||||
public:
|
||||
static OclSharedData &get(uint32_t index);
|
||||
static void release();
|
||||
static void start(const std::vector<OclLaunchData> &threads);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_OCLSHAREDSTATE_H */
|
Loading…
Add table
Add a link
Reference in a new issue