| 1 |
# ######################################################################## |
| 2 |
# Copyright 2013 Advanced Micro Devices, Inc. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
# ######################################################################## |
| 16 |
|
| 17 |
import itertools |
| 18 |
import re#gex |
| 19 |
import subprocess |
| 20 |
import os |
| 21 |
import sys |
| 22 |
from datetime import datetime |
| 23 |
|
| 24 |
# Common data and functions for the performance suite |
| 25 |
|
| 26 |
tableHeader = 'lengthx,lengthy,lengthz,batch,device,inlay,outlay,place,precision,label,GFLOPS' |
| 27 |
|
| 28 |
class TestCombination: |
| 29 |
def __init__(self, |
| 30 |
lengthx, lengthy, lengthz, batchsize, |
| 31 |
device, inlayout, outlayout, placeness, precision, |
| 32 |
label): |
| 33 |
self.x = lengthx |
| 34 |
self.y = lengthy |
| 35 |
self.z = lengthz |
| 36 |
self.batchsize = batchsize |
| 37 |
self.device = device |
| 38 |
self.inlayout = inlayout |
| 39 |
self.outlayout = outlayout |
| 40 |
self.placeness = placeness |
| 41 |
self.precision = precision |
| 42 |
self.label = label |
| 43 |
|
| 44 |
def __str__(self): |
| 45 |
return self.x + 'x' + self.y + 'x' + self.z + ':' + self.batchsize + ', ' + self.device + ', ' + self.inlayout + '/' + self.outlayout + ', ' + self.placeness + ', ' + self.precision + ' -- ' + self.label |
| 46 |
|
| 47 |
class GraphPoint: |
| 48 |
def __init__(self, |
| 49 |
lengthx, lengthy, lengthz, batchsize, |
| 50 |
precision, device, label, |
| 51 |
gflops): |
| 52 |
self.x = lengthx |
| 53 |
self.y = lengthy |
| 54 |
self.z = lengthz |
| 55 |
self.batchsize = batchsize |
| 56 |
self.device = device |
| 57 |
self.label = label |
| 58 |
self.precision = precision |
| 59 |
self.gflops = gflops |
| 60 |
self.problemsize = str(int(self.x) * int(self.y) * int(self.z) * int(self.batchsize)) |
| 61 |
|
| 62 |
def __str__(self): |
| 63 |
# ALL members must be represented here (x, y, z, batch, device, label, etc) |
| 64 |
return self.x + 'x' + self.y + 'x' + self.z + ':' + self.batchsize + ', ' + self.precision + ' precision, ' + self.device + ', -- ' + self.label + '; ' + self.gflops |
| 65 |
|
| 66 |
class TableRow: |
| 67 |
# parameters = class TestCombination instantiation |
| 68 |
def __init__(self, parameters, gflops): |
| 69 |
self.parameters = parameters |
| 70 |
self.gflops = gflops |
| 71 |
|
| 72 |
def __str__(self): |
| 73 |
return self.parameters.__str__() + '; ' + self.gflops |
| 74 |
|
| 75 |
def transformDimension(x,y,z): |
| 76 |
if int(z) != 1: |
| 77 |
return 3 |
| 78 |
elif int(y) != 1: |
| 79 |
return 2 |
| 80 |
elif int(x) != 1: |
| 81 |
return 1 |
| 82 |
|
| 83 |
def executable(library): |
| 84 |
if type(library) != str: |
| 85 |
print 'ERROR: expected library name to be a string' |
| 86 |
quit() |
| 87 |
|
| 88 |
if sys.platform != 'win32' and sys.platform != 'linux2': |
| 89 |
print 'ERROR: unknown operating system' |
| 90 |
quit() |
| 91 |
|
| 92 |
if library == 'clFFT' or library == 'null': |
| 93 |
if sys.platform == 'win32': |
| 94 |
exe = 'clFFT-client.exe' |
| 95 |
elif sys.platform == 'linux2': |
| 96 |
exe = 'clFFT-client' |
| 97 |
elif library == 'cuFFT': |
| 98 |
if sys.platform == 'win32': |
| 99 |
exe = 'cuFFT-client.exe' |
| 100 |
elif sys.platform == 'linux2': |
| 101 |
exe = 'cuFFT-client' |
| 102 |
else: |
| 103 |
print 'ERROR: unknown library -- cannot determine executable name' |
| 104 |
quit() |
| 105 |
|
| 106 |
return exe |
| 107 |
|
| 108 |
def max_mem_available_in_bytes(exe, device): |
| 109 |
arguments = [exe, '-i', device] |
| 110 |
|
| 111 |
deviceInfo = subprocess.check_output(arguments, stderr=subprocess.STDOUT).split(os.linesep) |
| 112 |
deviceInfo = itertools.ifilter( lambda x: x.count('MAX_MEM_ALLOC_SIZE'), deviceInfo) |
| 113 |
deviceInfo = list(itertools.islice(deviceInfo, None)) |
| 114 |
maxMemoryAvailable = re.search('\d+', deviceInfo[0]) |
| 115 |
return int(maxMemoryAvailable.group(0)) |
| 116 |
|
| 117 |
def max_problem_size(exe, layout, precision, device): |
| 118 |
|
| 119 |
if precision == 'single': |
| 120 |
bytes_in_one_number = 4 |
| 121 |
elif precision == 'double': |
| 122 |
bytes_in_one_number = 8 |
| 123 |
else: |
| 124 |
print 'max_problem_size(): unknown precision' |
| 125 |
quit() |
| 126 |
|
| 127 |
max_problem_size = pow(2,25) |
| 128 |
if layout == '5': |
| 129 |
max_problem_size = pow(2,24) # TODO: Upper size limit for real transform |
| 130 |
return max_problem_size |
| 131 |
|
| 132 |
def maxBatchSize(lengthx, lengthy, lengthz, layout, precision, exe, device): |
| 133 |
problemSize = int(lengthx) * int(lengthy) * int(lengthz) |
| 134 |
maxBatchSize = max_problem_size(exe, layout, precision, device) / problemSize |
| 135 |
return str(maxBatchSize) |
| 136 |
|
| 137 |
def create_ini_file_if_requested(args): |
| 138 |
if args.createIniFilename: |
| 139 |
for x in vars(args): |
| 140 |
if (type(getattr(args,x)) != file) and x.count('File') == 0: |
| 141 |
args.createIniFilename.write('--' + x + os.linesep) |
| 142 |
args.createIniFilename.write(str(getattr(args,x)) + os.linesep) |
| 143 |
quit() |
| 144 |
|
| 145 |
def load_ini_file_if_requested(args, parser): |
| 146 |
if args.useIniFilename: |
| 147 |
argument_list = args.useIniFilename.readlines() |
| 148 |
argument_list = [x.strip() for x in argument_list] |
| 149 |
args = parser.parse_args(argument_list) |
| 150 |
return args |
| 151 |
|
| 152 |
def is_numeric_type(x): |
| 153 |
return type(x) == int or type(x) == long or type(x) == float |
| 154 |
|
| 155 |
def split_up_comma_delimited_lists(args): |
| 156 |
for x in vars(args): |
| 157 |
attr = getattr(args, x) |
| 158 |
if attr == None: |
| 159 |
setattr(args, x, [None]) |
| 160 |
elif is_numeric_type(attr): |
| 161 |
setattr(args, x, [attr]) |
| 162 |
elif type(attr) == str: |
| 163 |
setattr(args, x, attr.split(',')) |
| 164 |
return args |
| 165 |
|
| 166 |
class Range: |
| 167 |
def __init__(self, ranges, defaultStep='+1'): |
| 168 |
# we might be passed in a single value or a list of strings |
| 169 |
# if we receive a single value, we want to feed it right back |
| 170 |
if type(ranges) != list: |
| 171 |
self.expanded = ranges |
| 172 |
elif ranges[0] == None: |
| 173 |
self.expanded = [None] |
| 174 |
else: |
| 175 |
self.expanded = [] |
| 176 |
for thisRange in ranges: |
| 177 |
thisRange = str(thisRange) |
| 178 |
if re.search('^\+\d+$', thisRange): |
| 179 |
self.expanded = self.expanded + [thisRange] |
| 180 |
elif thisRange == 'max': |
| 181 |
self.expanded = self.expanded + ['max'] |
| 182 |
else: |
| 183 |
#elif thisRange != 'max': |
| 184 |
if thisRange.count(':'): |
| 185 |
self._stepAmount = thisRange.split(':')[1] |
| 186 |
else: |
| 187 |
self._stepAmount = defaultStep |
| 188 |
thisRange = thisRange.split(':')[0] |
| 189 |
|
| 190 |
if self._stepAmount.count('x'): |
| 191 |
self._stepper = '_mult' |
| 192 |
else: |
| 193 |
self._stepper = '_add' |
| 194 |
self._stepAmount = self._stepAmount.lstrip('+x') |
| 195 |
self._stepAmount = int(self._stepAmount) |
| 196 |
|
| 197 |
if thisRange.count('-'): |
| 198 |
self.begin = int(thisRange.split('-')[0]) |
| 199 |
self.end = int(thisRange.split('-')[1]) |
| 200 |
else: |
| 201 |
self.begin = int(thisRange.split('-')[0]) |
| 202 |
self.end = int(thisRange.split('-')[0]) |
| 203 |
self.current = self.begin |
| 204 |
|
| 205 |
if self.begin == 0 and self._stepper == '_mult': |
| 206 |
self.expanded = self.expanded + [0] |
| 207 |
else: |
| 208 |
while self.current <= self.end: |
| 209 |
self.expanded = self.expanded + [self.current] |
| 210 |
self._step() |
| 211 |
|
| 212 |
# now we want to uniquify and sort the expanded range |
| 213 |
self.expanded = list(set(self.expanded)) |
| 214 |
self.expanded.sort() |
| 215 |
|
| 216 |
# advance current value to next |
| 217 |
def _step(self): |
| 218 |
getattr(self, self._stepper)() |
| 219 |
|
| 220 |
def _mult(self): |
| 221 |
self.current = self.current * self._stepAmount |
| 222 |
|
| 223 |
def _add(self): |
| 224 |
self.current = self.current + self._stepAmount |
| 225 |
|
| 226 |
def expand_range(a_range): |
| 227 |
return Range(a_range).expanded |
| 228 |
|
| 229 |
def decode_parameter_problemsize(problemsize): |
| 230 |
if not problemsize.count(None): |
| 231 |
i = 0 |
| 232 |
while i < len(problemsize): |
| 233 |
problemsize[i] = problemsize[i].split(':') |
| 234 |
j = 0 |
| 235 |
while j < len(problemsize[i]): |
| 236 |
problemsize[i][j] = problemsize[i][j].split('x') |
| 237 |
j = j+1 |
| 238 |
i = i+1 |
| 239 |
|
| 240 |
return problemsize |
| 241 |
|
| 242 |
def gemm_table_header(): |
| 243 |
return 'm,n,k,lda,ldb,ldc,alpha,beta,order,transa,transb,function,device,library,label,GFLOPS' |
| 244 |
|
| 245 |
class GemmTestCombination: |
| 246 |
def __init__(self, |
| 247 |
sizem, sizen, sizek, lda, ldb, ldc, |
| 248 |
alpha, beta, order, transa, transb, |
| 249 |
function, device, library, label): |
| 250 |
self.sizem = str(sizem) |
| 251 |
self.sizen = str(sizen) |
| 252 |
self.sizek = str(sizek) |
| 253 |
self.lda = str(lda) |
| 254 |
self.ldb = str(ldb) |
| 255 |
self.ldc = str(ldc) |
| 256 |
self.alpha = str(alpha) |
| 257 |
self.beta = str(beta) |
| 258 |
self.order = order |
| 259 |
self.transa = transa |
| 260 |
self.transb = transb |
| 261 |
self.function = function |
| 262 |
self.device = device |
| 263 |
self.library = library |
| 264 |
self.label = label |
| 265 |
|
| 266 |
def __str__(self): |
| 267 |
return self.sizem + 'x' + self.sizen + 'x' + self.sizek + ':' + self.lda + 'x' + self.ldb + 'x' + self.ldc + ', ' + self.device + ', ' + self.function + ', ' + self.library + ', alpha(' + self.alpha + '), beta(' + self.beta + '), order(' + self.order + '), transa(' + self.transa + '), transb(' + self.transb + ') -- ' + self.label |
| 268 |
|
| 269 |
class GemmGraphPoint: |
| 270 |
def __init__(self, |
| 271 |
sizem, sizen, sizek, |
| 272 |
lda, ldb, ldc, |
| 273 |
device, order, transa, transb, |
| 274 |
function, library, label, |
| 275 |
gflops): |
| 276 |
self.sizem = sizem |
| 277 |
self.sizen = sizen |
| 278 |
self.sizek = sizek |
| 279 |
self.lda = lda |
| 280 |
self.ldb = ldb |
| 281 |
self.ldc = ldc |
| 282 |
self.device = device |
| 283 |
self.order = order |
| 284 |
self.transa = transa |
| 285 |
self.transb = transb |
| 286 |
self.function = function |
| 287 |
self.library = library |
| 288 |
self.label = label |
| 289 |
self.gflops = gflops |
| 290 |
|
| 291 |
def __str__(self): |
| 292 |
# ALL members must be represented here (x, y, z, batch, device, label, etc) |
| 293 |
return self.sizem + 'x' + self.sizen + 'x' + self.sizek + ':' + self.device + ', ' + self.function + ', ' + self.library + ', order(' + self.order + '), transa(' + self.transa + '), transb(' + self.transb + ') -- ' + self.label + '; ' + self.gflops + ' gflops' |
| 294 |
|
| 295 |
def open_file( filename ): |
| 296 |
if type(filename) == list: |
| 297 |
filename = filename[0] |
| 298 |
if filename == None: |
| 299 |
filename = 'results' + datetime.now().isoformat().replace(':','.') + '.txt' |
| 300 |
else: |
| 301 |
if os.path.isfile(filename): |
| 302 |
oldname = filename |
| 303 |
filename = filename + datetime.now().isoformat().replace(':','.') |
| 304 |
message = 'A file with the name ' + oldname + ' already exists. Changing filename to ' + filename |
| 305 |
print message |
| 306 |
|
| 307 |
return open(filename, 'w') |