1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief CPU warm-up utility, used to counteract CPU throttling.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuCPUWarmup.hpp"
34 namespace warmupCPUInternal
37 volatile Dummy g_dummy;
41 template <typename T, int Size>
42 static inline float floatMedian (const T (&v)[Size])
45 for (int i = 0; i < Size; i++)
48 std::sort(DE_ARRAY_BEGIN(temp), DE_ARRAY_END(temp));
51 ? 0.5f * ((float)temp[Size/2-1] + (float)temp[Size/2])
52 : (float)temp[Size/2];
55 template <typename T, int Size>
56 static inline float floatRelativeMedianAbsoluteDeviation (const T (&v)[Size])
58 const float median = floatMedian(v);
59 float absoluteDeviations[Size];
61 for (int i = 0; i < Size; i++)
62 absoluteDeviations[i] = deFloatAbs((float)v[i] - median);
64 return floatMedian(absoluteDeviations) / median;
67 static inline float dummyComputation (float initial, int numIterations)
72 for (int i = 0; i < numIterations; i++)
74 // Arbitrary computations.
75 for (int j = 0; j < 4; j++)
77 a = deFloatCos(a + (float)b);
78 b = (b + 63) % 107 + de::abs((int)(a*10.0f));
87 float dummy = *warmupCPUInternal::g_dummy.m_v;
88 int computationSize = 1;
90 // Do a rough calibration for computationSize to get dummyComputation's running time above a certain threshold.
91 while (computationSize < 1<<30) // \note This condition is unlikely to be met. The "real" loop exit is the break below.
93 const float singleMeasurementThreshold = 10000.0f;
94 const int numMeasurements = 3;
95 deInt64 times[numMeasurements];
97 for (int i = 0; i < numMeasurements; i++)
99 const deUint64 startTime = deGetMicroseconds();
100 dummy = dummyComputation(dummy, computationSize);
101 times[i] = (deInt64)(deGetMicroseconds() - startTime);
104 if (floatMedian(times) >= singleMeasurementThreshold)
107 computationSize *= 2;
110 // Do dummyComputations until running time seems stable enough.
112 const int maxNumMeasurements = 50;
113 const int numConsecutiveMeasurementsRequired = 5;
114 const float relativeMedianAbsoluteDeviationThreshold = 0.05f;
115 deInt64 latestTimes[numConsecutiveMeasurementsRequired];
117 for (int measurementNdx = 0;
119 measurementNdx < maxNumMeasurements &&
120 (measurementNdx < numConsecutiveMeasurementsRequired ||
121 floatRelativeMedianAbsoluteDeviation(latestTimes) > relativeMedianAbsoluteDeviationThreshold);
125 const deUint64 startTime = deGetMicroseconds();
126 dummy = dummyComputation(dummy, computationSize);
127 latestTimes[measurementNdx % numConsecutiveMeasurementsRequired] = (deInt64)(deGetMicroseconds() - startTime);
131 *warmupCPUInternal::g_dummy.m_v = dummy;