| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtConcurrent module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qtconcurrentiteratekernel.h" |
| 41 | |
| 42 | #include <qdeadlinetimer.h> |
| 43 | #include "private/qfunctions_p.h" |
| 44 | |
| 45 | |
| 46 | #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | enum { |
| 51 | TargetRatio = 100, |
| 52 | MedianSize = 7 |
| 53 | }; |
| 54 | |
| 55 | static qint64 getticks() |
| 56 | { |
| 57 | return QDeadlineTimer::current(timerType: Qt::PreciseTimer).deadlineNSecs(); |
| 58 | } |
| 59 | |
| 60 | static double elapsed(qint64 after, qint64 before) |
| 61 | { |
| 62 | return double(after - before); |
| 63 | } |
| 64 | |
| 65 | namespace QtConcurrent { |
| 66 | |
| 67 | /*! |
| 68 | \class QtConcurrent::Median |
| 69 | \inmodule QtConcurrent |
| 70 | \internal |
| 71 | */ |
| 72 | |
| 73 | /*! |
| 74 | \class QtConcurrent::MedianDouble |
| 75 | \inmodule QtConcurrent |
| 76 | \internal |
| 77 | */ |
| 78 | |
| 79 | /*! |
| 80 | \class QtConcurrent::BlockSizeManager |
| 81 | \inmodule QtConcurrent |
| 82 | \internal |
| 83 | */ |
| 84 | |
| 85 | /*! |
| 86 | \class QtConcurrent::BlockSizeManagerV2 |
| 87 | \inmodule QtConcurrent |
| 88 | \internal |
| 89 | */ |
| 90 | |
| 91 | /*! |
| 92 | \class QtConcurrent::ResultReporter |
| 93 | \inmodule QtConcurrent |
| 94 | \internal |
| 95 | */ |
| 96 | |
| 97 | /*! \fn bool QtConcurrent::selectIteration(std::bidirectional_iterator_tag) |
| 98 | \internal |
| 99 | */ |
| 100 | |
| 101 | /*! \fn bool QtConcurrent::selectIteration(std::forward_iterator_tag) |
| 102 | \internal |
| 103 | */ |
| 104 | |
| 105 | /*! \fn bool QtConcurrent::selectIteration(std::random_access_iterator_tag) |
| 106 | \internal |
| 107 | */ |
| 108 | |
| 109 | /*! |
| 110 | \class QtConcurrent::IterateKernel |
| 111 | \inmodule QtConcurrent |
| 112 | \internal |
| 113 | */ |
| 114 | |
| 115 | /*! \internal |
| 116 | |
| 117 | */ |
| 118 | BlockSizeManager::BlockSizeManager(int iterationCount) |
| 119 | : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)), |
| 120 | beforeUser(0), afterUser(0), |
| 121 | controlPartElapsed(MedianSize), userPartElapsed(MedianSize), |
| 122 | m_blockSize(1) |
| 123 | { } |
| 124 | |
| 125 | // Records the time before user code. |
| 126 | void BlockSizeManager::timeBeforeUser() |
| 127 | { |
| 128 | if (blockSizeMaxed()) |
| 129 | return; |
| 130 | |
| 131 | beforeUser = getticks(); |
| 132 | controlPartElapsed.addValue(value: elapsed(after: beforeUser, before: afterUser)); |
| 133 | } |
| 134 | |
| 135 | // Records the time after user code and adjust the block size if we are spending |
| 136 | // to much time in the for control code compared with the user code. |
| 137 | void BlockSizeManager::timeAfterUser() |
| 138 | { |
| 139 | if (blockSizeMaxed()) |
| 140 | return; |
| 141 | |
| 142 | afterUser = getticks(); |
| 143 | userPartElapsed.addValue(value: elapsed(after: afterUser, before: beforeUser)); |
| 144 | |
| 145 | if (controlPartElapsed.isMedianValid() == false) |
| 146 | return; |
| 147 | |
| 148 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median()) |
| 149 | return; |
| 150 | |
| 151 | m_blockSize = qMin(a: m_blockSize * 2, b: maxBlockSize); |
| 152 | |
| 153 | #ifdef QTCONCURRENT_FOR_DEBUG |
| 154 | qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize; |
| 155 | #endif |
| 156 | |
| 157 | // Reset the medians after adjusting the block size so we get |
| 158 | // new measurements with the new block size. |
| 159 | controlPartElapsed.reset(); |
| 160 | userPartElapsed.reset(); |
| 161 | } |
| 162 | |
| 163 | int BlockSizeManager::blockSize() |
| 164 | { |
| 165 | return m_blockSize; |
| 166 | } |
| 167 | |
| 168 | /*! \internal |
| 169 | |
| 170 | */ |
| 171 | BlockSizeManagerV2::BlockSizeManagerV2(int iterationCount) |
| 172 | : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)), |
| 173 | beforeUser(0), afterUser(0), |
| 174 | m_blockSize(1) |
| 175 | { } |
| 176 | |
| 177 | // Records the time before user code. |
| 178 | void BlockSizeManagerV2::timeBeforeUser() |
| 179 | { |
| 180 | if (blockSizeMaxed()) |
| 181 | return; |
| 182 | |
| 183 | beforeUser = getticks(); |
| 184 | controlPartElapsed.addValue(value: elapsed(after: beforeUser, before: afterUser)); |
| 185 | } |
| 186 | |
| 187 | // Records the time after user code and adjust the block size if we are spending |
| 188 | // to much time in the for control code compared with the user code. |
| 189 | void BlockSizeManagerV2::timeAfterUser() |
| 190 | { |
| 191 | if (blockSizeMaxed()) |
| 192 | return; |
| 193 | |
| 194 | afterUser = getticks(); |
| 195 | userPartElapsed.addValue(value: elapsed(after: afterUser, before: beforeUser)); |
| 196 | |
| 197 | if (controlPartElapsed.isMedianValid() == false) |
| 198 | return; |
| 199 | |
| 200 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median()) |
| 201 | return; |
| 202 | |
| 203 | m_blockSize = qMin(a: m_blockSize * 2, b: maxBlockSize); |
| 204 | |
| 205 | #ifdef QTCONCURRENT_FOR_DEBUG |
| 206 | qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize; |
| 207 | #endif |
| 208 | |
| 209 | // Reset the medians after adjusting the block size so we get |
| 210 | // new measurements with the new block size. |
| 211 | controlPartElapsed.reset(); |
| 212 | userPartElapsed.reset(); |
| 213 | } |
| 214 | |
| 215 | int BlockSizeManagerV2::blockSize() |
| 216 | { |
| 217 | return m_blockSize; |
| 218 | } |
| 219 | |
| 220 | } // namespace QtConcurrent |
| 221 | |
| 222 | QT_END_NAMESPACE |
| 223 | |
| 224 | #endif // QT_NO_CONCURRENT |
| 225 | |