/usr/lib/python2.7/dist-packages/pyopencl/characterize/__init__.py is in python-pyopencl 2017.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | from __future__ import division, absolute_import
__copyright__ = "Copyright (C) 2009 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import pyopencl as cl
from pytools import memoize
import six
from six.moves import range, zip
class CLCharacterizationWarning(UserWarning):
pass
@memoize
def has_double_support(dev):
for ext in dev.extensions.split(" "):
if ext == "cl_khr_fp64":
return True
return False
def has_amd_double_support(dev):
""""Fix to allow incomplete amd double support in low end boards"""
for ext in dev.extensions.split(" "):
if ext == "cl_amd_fp64":
return True
return False
def reasonable_work_group_size_multiple(dev, ctx=None):
try:
return dev.warp_size_nv
except Exception:
pass
if ctx is None:
ctx = cl.Context([dev])
prg = cl.Program(ctx, """
__kernel void knl(__global float *a)
{
a[get_global_id(0)] = 0;
}
""")
prg.build()
return prg.knl.get_work_group_info(
cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
dev)
def nv_compute_capability(dev):
"""If *dev* is an Nvidia GPU :class:`pyopencl.Device`, return a tuple
*(major, minor)* indicating the device's compute capability.
"""
try:
return (dev.compute_capability_major_nv,
dev.compute_capability_minor_nv)
except Exception:
return None
def usable_local_mem_size(dev, nargs=None):
"""Return an estimate of the usable local memory size.
:arg nargs: Number of 32-bit arguments passed.
"""
usable_local_mem_size = dev.local_mem_size
nv_compute_cap = nv_compute_capability(dev)
if (nv_compute_cap is not None
and nv_compute_cap < (2, 0)):
# pre-Fermi use local mem for parameter passing
if nargs is None:
# assume maximum
usable_local_mem_size -= 256
else:
usable_local_mem_size -= 4*nargs
return usable_local_mem_size
def simultaneous_work_items_on_local_access(dev):
"""Return the number of work items that access local
memory simultaneously and thereby may conflict with
each other.
"""
nv_compute_cap = nv_compute_capability(dev)
if nv_compute_cap is not None:
if nv_compute_cap < (2, 0):
return 16
else:
if nv_compute_cap >= (3, 0):
from warnings import warn
warn("wildly guessing conflicting local access size on '%s'"
% dev,
CLCharacterizationWarning)
return 32
if dev.type & cl.device_type.GPU:
from warnings import warn
warn("wildly guessing conflicting local access size on '%s'"
% dev,
CLCharacterizationWarning)
return 16
elif dev.type & cl.device_type.CPU:
return 1
else:
from warnings import warn
warn("wildly guessing conflicting local access size on '%s'"
% dev,
CLCharacterizationWarning)
return 16
def local_memory_access_granularity(dev):
"""Return the number of bytes per bank in local memory."""
return 4
def local_memory_bank_count(dev):
"""Return the number of banks present in local memory.
"""
nv_compute_cap = nv_compute_capability(dev)
if nv_compute_cap is not None:
if nv_compute_cap < (2, 0):
return 16
else:
if nv_compute_cap >= (3, 0):
from warnings import warn
warn("wildly guessing local memory bank count on '%s'"
% dev,
CLCharacterizationWarning)
return 32
if dev.type & cl.device_type.GPU:
from warnings import warn
warn("wildly guessing local memory bank count on '%s'"
% dev,
CLCharacterizationWarning)
return 16
elif dev.type & cl.device_type.CPU:
if dev.local_mem_type == cl.device_local_mem_type.GLOBAL:
raise RuntimeError("asking for a bank count is "
"meaningless for cache-based lmem")
from warnings import warn
warn("wildly guessing conflicting local access size on '%s'"
% dev,
CLCharacterizationWarning)
return 16
def why_not_local_access_conflict_free(dev, itemsize,
array_shape, array_stored_shape=None):
"""
:param itemsize: size of accessed data in bytes
:param array_shape: array dimensions, fastest-moving last
(C order)
:returns: a tuple (multiplicity, explanation), where *multiplicity*
is the number of work items that will conflict on a bank when accessing
local memory. *explanation* is a string detailing the found conflict.
"""
# FIXME: Treat 64-bit access on NV CC 2.x + correctly
if array_stored_shape is None:
array_stored_shape = array_shape
rank = len(array_shape)
array_shape = array_shape[::-1]
array_stored_shape = array_stored_shape[::-1]
gran = local_memory_access_granularity(dev)
if itemsize != gran:
from warnings import warn
warn("local conflict info might be inaccurate "
"for itemsize != %d" % gran,
CLCharacterizationWarning)
sim_wi = simultaneous_work_items_on_local_access(dev)
bank_count = local_memory_bank_count(dev)
conflicts = []
for work_item_axis in range(rank):
bank_accesses = {}
for work_item_id in range(sim_wi):
addr = 0
addr_mult = itemsize
idx = []
left_over_idx = work_item_id
for axis, (ax_size, ax_stor_size) in enumerate(
zip(array_shape, array_stored_shape)):
if axis >= work_item_axis:
left_over_idx, ax_idx = divmod(left_over_idx, ax_size)
addr += addr_mult*ax_idx
idx.append(ax_idx)
else:
idx.append(0)
addr_mult *= ax_stor_size
if left_over_idx:
# out-of-bounds, assume not taking place
continue
bank = (addr // gran) % bank_count
bank_accesses.setdefault(bank, []).append(
"w.item %s -> %s" % (work_item_id, idx[::-1]))
conflict_multiplicity = max(
len(acc) for acc in six.itervalues(bank_accesses))
if conflict_multiplicity > 1:
for bank, acc in six.iteritems(bank_accesses):
if len(acc) == conflict_multiplicity:
conflicts.append(
(conflict_multiplicity,
"%dx conflict on axis %d (from right, 0-based): "
"%s access bank %d" % (
conflict_multiplicity,
work_item_axis,
", ".join(acc), bank)))
if conflicts:
return max(conflicts)
else:
return 1, None
def get_fast_inaccurate_build_options(dev):
"""Return a list of flags valid on device *dev* that enable fast, but
potentially inaccurate floating point math.
"""
result = ["-cl-mad-enable", "-cl-fast-relaxed-math",
"-cl-no-signed-zeros", ]
if dev.vendor.startswith("Advanced Micro") or dev.vendor.startswith("NVIDIA"):
result.append("-cl-strict-aliasing")
return result
def get_simd_group_size(dev, type_size):
"""Return an estimate of how many work items will be executed across SIMD
lanes. This returns the size of what Nvidia calls a warp and what AMD calls
a wavefront.
Only refers to implicit SIMD.
:arg type_size: number of bytes in vector entry type.
"""
try:
return dev.warp_size_nv
except Exception:
pass
lc_vendor = dev.platform.vendor.lower()
if "nvidia" in lc_vendor:
return 32
if ("advanced micro" in lc_vendor or "ati" in lc_vendor):
if dev.type & cl.device_type.GPU:
# Tomasz Rybak says, in response to reduction mishbehaving on the AMD
# 'Loveland' APU:
#
# Like in CUDA reduction bug (related to Fermi) it again seems
# to be related to too eager concurrency when reducing results.
# According to http://oscarbg.blogspot.com/2009/10/news-from-web.html
# "Actually the wavefront size is only 64 for the highend cards(48XX,
# 58XX, 57XX), but 32 for the middleend cards and 16 for the lowend
# cards."
# IMO we should use PREFERRED_WORK_GROUP_SIZE_MULTIPLE to get
# non_sync_size. At the same size we lose SIMD CPU optimisation,
# but I do not know for now how to fix those two at the same time.
# Attached patch fixes problem on Loveland, not breaking anything on
# NVIDIA ION.
# This is therefore our best guess as to the SIMD group size.
return reasonable_work_group_size_multiple(dev)
elif dev.type & cl.device_type.CPU:
return 1
else:
raise RuntimeError("unexpected AMD device type")
if dev.type & cl.device_type.CPU:
# implicit assumption: Impl. will vectorize
return 1
return None
def get_pocl_version(platform, fallback_value=None):
if platform.name != "Portable Computing Language":
return None
import re
ver_match = re.match(
r"^OpenCL [0-9.]+ pocl ([0-9]+)\.([0-9]+)", platform.version)
if ver_match is None:
msg = ("pocl version number did not have expected format: '%s'"
% platform.version)
if fallback_value is not None:
from warnings import warn
warn(msg)
return fallback_value
else:
raise ValueError(msg)
else:
return (int(ver_match.group(1)), int(ver_match.group(2)))
_CHECK_FOR_POCL_ARG_COUNT_BUG_CACHE = {}
def _check_for_pocl_arg_count_bug(dev, ctx=None):
try:
return _CHECK_FOR_POCL_ARG_COUNT_BUG_CACHE[dev]
except KeyError:
pass
if ctx is None:
build_ctx = cl.Context([dev])
else:
build_ctx = ctx
prg = cl.Program(build_ctx, """
struct two_things
{
long a;
long b;
};
__kernel void test_knl(struct two_things x)
{
}
""").build()
result = prg.test_knl.num_args == 2
_CHECK_FOR_POCL_ARG_COUNT_BUG_CACHE[dev] = result
return result
def has_struct_arg_count_bug(dev, ctx=None):
"""Checks whether the device is expected to have the
`argument counting bug <https://github.com/pocl/pocl/issues/197>`_.
"""
if dev.platform.name == "Apple" and dev.type & cl.device_type.CPU:
return "apple"
if dev.platform.name == "Portable Computing Language":
pocl_version = get_pocl_version(dev.platform, fallback_value=(0.14))
if pocl_version <= (0, 13):
return "pocl"
elif pocl_version <= (0, 14) and _check_for_pocl_arg_count_bug(dev, ctx):
return "pocl"
return False
|