1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief CPU warm-up utility, used to counteract CPU throttling.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuCPUWarmup.hpp"
25 #include "deDefs.hpp"
26 #include "deMath.h"
27 #include "deClock.h"
28
29 #include <algorithm>
30
31 namespace tcu
32 {
33
34 namespace warmupCPUInternal
35 {
36
37 volatile Unused g_unused;
38
39 }
40
41 template <typename T, int Size>
floatMedian(const T (& v)[Size])42 static inline float floatMedian(const T (&v)[Size])
43 {
44 T temp[Size];
45 for (int i = 0; i < Size; i++)
46 temp[i] = v[i];
47
48 std::sort(DE_ARRAY_BEGIN(temp), DE_ARRAY_END(temp));
49
50 return Size % 2 == 0 ? 0.5f * ((float)temp[Size / 2 - 1] + (float)temp[Size / 2]) : (float)temp[Size / 2];
51 }
52
53 template <typename T, int Size>
floatRelativeMedianAbsoluteDeviation(const T (& v)[Size])54 static inline float floatRelativeMedianAbsoluteDeviation(const T (&v)[Size])
55 {
56 const float median = floatMedian(v);
57 float absoluteDeviations[Size];
58
59 for (int i = 0; i < Size; i++)
60 absoluteDeviations[i] = deFloatAbs((float)v[i] - median);
61
62 return floatMedian(absoluteDeviations) / median;
63 }
64
unusedComputation(float initial,int numIterations)65 static inline float unusedComputation(float initial, int numIterations)
66 {
67 float a = initial;
68 int b = 123;
69
70 for (int i = 0; i < numIterations; i++)
71 {
72 // Arbitrary computations.
73 for (int j = 0; j < 4; j++)
74 {
75 a = deFloatCos(a + (float)b);
76 b = (b + 63) % 107 + de::abs((int)(a * 10.0f));
77 }
78 }
79
80 return a + (float)b;
81 }
82
warmupCPU(void)83 void warmupCPU(void)
84 {
85 float unused = *warmupCPUInternal::g_unused.m_v;
86 int computationSize = 1;
87
88 // Do a rough calibration for computationSize to get unusedComputation's running time above a certain threshold.
89 while (computationSize <
90 1 << 30) // \note This condition is unlikely to be met. The "real" loop exit is the break below.
91 {
92 const float singleMeasurementThreshold = 10000.0f;
93 const int numMeasurements = 3;
94 int64_t times[numMeasurements];
95
96 for (int i = 0; i < numMeasurements; i++)
97 {
98 const uint64_t startTime = deGetMicroseconds();
99 unused = unusedComputation(unused, computationSize);
100 times[i] = (int64_t)(deGetMicroseconds() - startTime);
101 }
102
103 if (floatMedian(times) >= singleMeasurementThreshold)
104 break;
105
106 computationSize *= 2;
107 }
108
109 // Do unusedComputations until running time seems stable enough.
110 {
111 const int maxNumMeasurements = 50;
112 const int numConsecutiveMeasurementsRequired = 5;
113 const float relativeMedianAbsoluteDeviationThreshold = 0.05f;
114 int64_t latestTimes[numConsecutiveMeasurementsRequired];
115
116 for (int measurementNdx = 0;
117
118 measurementNdx < maxNumMeasurements &&
119 (measurementNdx < numConsecutiveMeasurementsRequired ||
120 floatRelativeMedianAbsoluteDeviation(latestTimes) > relativeMedianAbsoluteDeviationThreshold);
121
122 measurementNdx++)
123 {
124 const uint64_t startTime = deGetMicroseconds();
125 unused = unusedComputation(unused, computationSize);
126 latestTimes[measurementNdx % numConsecutiveMeasurementsRequired] =
127 (int64_t)(deGetMicroseconds() - startTime);
128 }
129 }
130
131 *warmupCPUInternal::g_unused.m_v = unused;
132 }
133
134 } // namespace tcu
135