/
divideNumTauBlocks.cpp
71 lines (61 loc) · 2.62 KB
/
divideNumTauBlocks.cpp
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
/*
* Copyright (C) 2016 Omer Mano
*
* 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, 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; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <tuple>
#include <vector>
#include <limits>
#include <algorithm>
#include <cmath>
#include "include/extractKernels.hpp"
tuple<vector<int>, vector<int>, int, int> divideNumTauBlocks(const int numTau, const int maxThreads, const int maxComputableTau) {
// In this version of the function we will return constant sized local tiles
// This is an area for further optimization
// We want to find numTau1Local and numTau2Local such that
// 1) max(numTau1local,numTau2local) is small relative to maxComputableTau (and definitely not greater)
// 2) Their product divided by 16 is less than or equal to maxThreads
// 3) numTau2local is divisible by 16
// 4) Tiling them into maxTau does not take too many blocks
const int numTaus = maxThreads * 16;
float minCost = numeric_limits<float>::infinity();
int minCostTau1 = 0;
int minCostTau2 = 0;
for (int testTau1 = 1; testTau1 <= min({ maxThreads, maxComputableTau, numTau }); testTau1++) {
for (int testTau2 = 16;
((testTau1*testTau2 / 16) <= maxThreads) & // 2)
(testTau2 <= maxComputableTau) & // 1)
(testTau2 < numTau + 16); // Skip unreasonable tau2s
testTau2 += 16) {
float relativeCostMaxTauTooHigh = float(maxComputableTau) / float(maxComputableTau - max(testTau1, testTau2));
float numTilesTau1 = ceil(float(numTau) / float(testTau1));
float numTilesTau2 = ceil(float(numTau) / float(testTau2));
float cost = relativeCostMaxTauTooHigh*numTilesTau1*numTilesTau2;
if (cost < minCost) {
minCost = cost;
minCostTau1 = testTau1;
minCostTau2 = testTau2;
}
}
}
vector<int> tau1Offsets, tau2Offsets, numTau1Locals, numTau2Locals;
for (int tau1Offset = 0; tau1Offset < numTau; tau1Offset += minCostTau1)
for (int tau2Offset = 0; tau2Offset < numTau; tau2Offset += minCostTau2) {
tau1Offsets.push_back(tau1Offset);
tau2Offsets.push_back(tau2Offset);
}
return tuple<vector<int>, vector<int>, int, int> {tau1Offsets, tau2Offsets, minCostTau1, minCostTau2};
}