| 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 |
import os |
| 26 |
import copy |
| 27 |
import yaml |
| 28 |
import math |
| 29 |
|
| 30 |
class BenchmarkSplitter(object): |
| 31 |
|
| 32 |
""" |
| 33 |
Benchmark splitter class |
| 34 |
Loads in a benchmark yaml file and splits |
| 35 |
it into several smaller benchmarks limited |
| 36 |
by a number of problem sizes entries. |
| 37 |
""" |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
def __readConfigFile(benchmarkConfigFile): |
| 41 |
with open(benchmarkConfigFile) as f: |
| 42 |
data = yaml.safe_load(f) |
| 43 |
return data |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@staticmethod |
| 49 |
def __splitByProblem(data): |
| 50 |
rv = [] |
| 51 |
problemKey = "BenchmarkProblems" |
| 52 |
for i in range(len(data[problemKey])): |
| 53 |
result = {} |
| 54 |
for k in data.keys(): |
| 55 |
if k == problemKey: |
| 56 |
result[k] = [copy.deepcopy(data[k][i])] |
| 57 |
else: |
| 58 |
result[k] = copy.deepcopy(data[k]) |
| 59 |
rv.append(result) |
| 60 |
return rv |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@staticmethod |
| 66 |
def __splitByBenchmarkGroup(data): |
| 67 |
rv = [] |
| 68 |
problemKey = "BenchmarkProblems" |
| 69 |
|
| 70 |
assert len(data[problemKey]) == 1, "Config file must have one BenchmarkProblems group" |
| 71 |
|
| 72 |
benchmarkProblems = data[problemKey][0] |
| 73 |
|
| 74 |
|
| 75 |
problemIdx = -1 |
| 76 |
for i in range(len(benchmarkProblems)): |
| 77 |
if("OperationType" in benchmarkProblems[i].keys()): |
| 78 |
problemIdx = i |
| 79 |
break |
| 80 |
|
| 81 |
assert problemIdx != -1, "Could not find problem type group" |
| 82 |
|
| 83 |
|
| 84 |
for i in range(len(benchmarkProblems)): |
| 85 |
if i != problemIdx: |
| 86 |
result = {} |
| 87 |
for k in data.keys(): |
| 88 |
if k == problemKey: |
| 89 |
|
| 90 |
result[k] = [[copy.deepcopy(benchmarkProblems[problemIdx]), \ |
| 91 |
copy.deepcopy(benchmarkProblems[i])]] |
| 92 |
else: |
| 93 |
|
| 94 |
result[k] = copy.deepcopy(data[k]) |
| 95 |
rv.append(result) |
| 96 |
return rv |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@staticmethod |
| 103 |
def __splitByBenchmarkSizes(data, numSizes=1): |
| 104 |
rv = [] |
| 105 |
problemKey = "BenchmarkProblems" |
| 106 |
|
| 107 |
assert len(data[problemKey]) == 1, "Config file must have one BenchmarkProblems group" |
| 108 |
|
| 109 |
benchmarkProblems = data[problemKey][0] |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
problemIdx = -1 |
| 114 |
benchmarkIdx = -1 |
| 115 |
for i in range(len(benchmarkProblems)): |
| 116 |
groupKeys = benchmarkProblems[i].keys() |
| 117 |
if("OperationType" in groupKeys): |
| 118 |
problemIdx = i |
| 119 |
elif("BenchmarkFinalParameters" in groupKeys): |
| 120 |
benchmarkIdx = i |
| 121 |
|
| 122 |
assert len(benchmarkProblems) == 2 \ |
| 123 |
and problemIdx is not -1 \ |
| 124 |
and benchmarkIdx is not -1, \ |
| 125 |
"Config file must have one ProblemType group and one Benchmark group" |
| 126 |
|
| 127 |
|
| 128 |
benchmarkGroup = benchmarkProblems[benchmarkIdx] |
| 129 |
assert "ProblemSizes" in benchmarkGroup["BenchmarkFinalParameters"][0] \ |
| 130 |
and len(benchmarkGroup["BenchmarkFinalParameters"][0]["ProblemSizes"]), \ |
| 131 |
"Benchmark group must have non-empty ProblemSizes" |
| 132 |
|
| 133 |
problemSizesGroup = benchmarkGroup["BenchmarkFinalParameters"][0]["ProblemSizes"] |
| 134 |
problemSizesCount = len(problemSizesGroup) |
| 135 |
|
| 136 |
numFiles = math.ceil(problemSizesCount / numSizes) |
| 137 |
|
| 138 |
|
| 139 |
for i in range(numFiles): |
| 140 |
result = {} |
| 141 |
for k in data.keys(): |
| 142 |
if k == problemKey: |
| 143 |
|
| 144 |
|
| 145 |
newBenchmarkGroup = {} |
| 146 |
for bk in benchmarkGroup.keys(): |
| 147 |
if bk == "BenchmarkFinalParameters": |
| 148 |
newBenchmarkGroup[bk] = [ {"ProblemSizes": [] } ] |
| 149 |
for j in range(i*numSizes, min((i+1)*numSizes, problemSizesCount)): |
| 150 |
newBenchmarkGroup[bk][0]["ProblemSizes"].append(copy.deepcopy(problemSizesGroup[j])) |
| 151 |
else: |
| 152 |
newBenchmarkGroup[bk] = copy.deepcopy(benchmarkGroup[bk]) |
| 153 |
|
| 154 |
result[k] = [[copy.deepcopy(benchmarkProblems[problemIdx]), copy.deepcopy(newBenchmarkGroup)]] |
| 155 |
else: |
| 156 |
result[k] = copy.deepcopy(data[k]) |
| 157 |
rv.append(result) |
| 158 |
return rv |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
@staticmethod |
| 165 |
def __appendFileNameSuffix(filePath, suffix, separator="_", formatting="{:02}"): |
| 166 |
root, ext = os.path.splitext(filePath) |
| 167 |
suffixString = (separator + formatting).format(suffix) |
| 168 |
return root + suffixString + ext |
| 169 |
|
| 170 |
@staticmethod |
| 171 |
def splitBenchmarkBySizes(configFile, outputDir, numSizes=1, baseFileName="", separator="_", suffixFormat="{:02}"): |
| 172 |
|
| 173 |
|
| 174 |
if baseFileName == "": |
| 175 |
baseFileName = os.path.basename(configFile) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
data = BenchmarkSplitter.__readConfigFile(configFile) |
| 181 |
benchmarksByProblem = BenchmarkSplitter.__splitByProblem(data) |
| 182 |
benchmarksByGroup = [] |
| 183 |
benchmarksBySize = [] |
| 184 |
for problem in benchmarksByProblem: |
| 185 |
benchmarksByGroup += BenchmarkSplitter.__splitByBenchmarkGroup(problem) |
| 186 |
for group in benchmarksByGroup: |
| 187 |
benchmarksBySize += BenchmarkSplitter.__splitByBenchmarkSizes(group, numSizes) |
| 188 |
|
| 189 |
|
| 190 |
outputFileBase = os.path.join(outputDir, baseFileName) |
| 191 |
for i in range(len(benchmarksBySize)): |
| 192 |
outFileName = BenchmarkSplitter.__appendFileNameSuffix(outputFileBase, i, separator, suffixFormat) |
| 193 |
with open(outFileName, "w") as f: |
| 194 |
yaml.safe_dump(benchmarksBySize[i], f) |