/usr/lib/python2.7/dist-packages/pyopencl/characterize/performance.py is in python-pyopencl 2016.1+git20161130-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 | from __future__ import division, absolute_import, print_function
__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.
"""
from six.moves import range
import pyopencl as cl
import numpy as np
# {{{ timing helpers
class Timer:
def __init__(self, queue):
self.queue = queue
def start(self):
pass
def stop(self):
pass
def add_event(self, evt):
pass
def get_elapsed(self):
pass
class WallTimer(Timer):
def start(self):
from time import time
self.queue.finish()
self.start = time()
def stop(self):
from time import time
self.queue.finish()
self.end = time()
def get_elapsed(self):
return self.end-self.start
def _get_time(queue, f, timer_factory=None, desired_duration=0.1,
warmup_rounds=3):
if timer_factory is None:
timer_factory = WallTimer
count = 1
while True:
timer = timer_factory(queue)
for i in range(warmup_rounds):
f()
warmup_rounds = 0
timer.start()
for i in range(count):
timer.add_event(f())
timer.stop()
elapsed = timer.get_elapsed()
if elapsed < desired_duration:
if elapsed == 0:
count *= 5
else:
new_count = int(desired_duration/elapsed)
new_count = max(2*count, new_count)
new_count = min(10*count, new_count)
count = new_count
else:
return elapsed/count
# }}}
# {{{ transfer measurements
class HostDeviceTransferBase(object):
def __init__(self, queue, block_size):
self.queue = queue
self.host_buf = np.empty(block_size, dtype=np.uint8)
self.dev_buf = cl.Buffer(queue.context, cl.mem_flags.READ_WRITE, block_size)
class HostToDeviceTransfer(HostDeviceTransferBase):
def do(self):
return cl.enqueue_copy(self. queue, self.dev_buf, self.host_buf)
class DeviceToHostTransfer(HostDeviceTransferBase):
def do(self):
return cl.enqueue_copy(self. queue, self.host_buf, self.dev_buf)
class DeviceToDeviceTransfer(object):
def __init__(self, queue, block_size):
self.queue = queue
mf = cl.mem_flags
self.dev_buf_1 = cl.Buffer(queue.context, mf.READ_WRITE, block_size)
self.dev_buf_2 = cl.Buffer(queue.context, mf.READ_WRITE, block_size)
def do(self):
return cl.enqueue_copy(self. queue, self.dev_buf_2, self.dev_buf_1)
def transfer_latency(queue, transfer_type, timer_factory=None):
transfer = transfer_type(queue, 1)
return _get_time(queue, transfer.do, timer_factory=timer_factory)
def transfer_bandwidth(queue, transfer_type, block_size, timer_factory=None):
"""Measures one-sided bandwidth."""
transfer = transfer_type(queue, block_size)
return block_size/_get_time(queue, transfer.do, timer_factory=timer_factory)
# }}}
def get_profiling_overhead(ctx, timer_factory=None):
no_prof_queue = cl.CommandQueue(ctx)
transfer = DeviceToDeviceTransfer(no_prof_queue, 1)
no_prof_time = _get_time(no_prof_queue, transfer.do, timer_factory=timer_factory)
prof_queue = cl.CommandQueue(ctx,
properties=cl.command_queue_properties.PROFILING_ENABLE)
transfer = DeviceToDeviceTransfer(prof_queue, 1)
prof_time = _get_time(prof_queue, transfer.do, timer_factory=timer_factory)
return prof_time - no_prof_time, prof_time
def get_empty_kernel_time(queue, timer_factory=None):
prg = cl.Program(queue.context, """
__kernel void empty()
{ }
""").build()
knl = prg.empty
def f():
knl(queue, (1,), None)
return _get_time(queue, f, timer_factory=timer_factory)
def _get_full_machine_kernel_rate(queue, src, args, name="benchmark",
timer_factory=None):
prg = cl.Program(queue.context, src).build()
knl = getattr(prg, name)
dev = queue.device
global_size = 4 * dev.max_compute_units
def f():
knl(queue, (global_size,), None, *args)
rates = []
num_dips = 0
while True:
elapsed = _get_time(queue, f, timer_factory=timer_factory)
rate = global_size/elapsed
print(global_size, rate, num_dips)
keep_trying = not rates
if rates and rate > 1.05*max(rates): # big improvement
keep_trying = True
num_dips = 0
if rates and rate < 0.9*max(rates) and num_dips < 3: # big dip
keep_trying = True
num_dips += 1
if keep_trying:
global_size *= 2
rates.append(rate)
else:
rates.append(rate)
return max(rates)
def get_add_rate(queue, type="float", timer_factory=None):
return 50*10*_get_full_machine_kernel_rate(queue, """
typedef %(op_t)s op_t;
__kernel void benchmark()
{
local op_t tgt[1024];
op_t val = get_global_id(0);
for (int i = 0; i < 10; ++i)
{
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
val += val; val += val; val += val; val += val; val += val;
}
tgt[get_local_id(0)] = val;
}
""" % dict(op_t=type), ())
# vim: foldmethod=marker:filetype=pyopencl
|