|
1 | 1 | import operator |
2 | 2 | import numpy |
| 3 | +from mako.template import Template |
3 | 4 |
|
4 | 5 | from unittest import TestCase |
5 | 6 | from pygpu import gpuarray, ndgpuarray as elemary |
@@ -297,3 +298,37 @@ def broadcast(shapea, shapeb): |
297 | 298 | rg = ag + bg |
298 | 299 |
|
299 | 300 | check_meta_content(rg, rc) |
| 301 | + |
| 302 | + |
| 303 | +_inf_preamb_tpl = Template(''' |
| 304 | +WITHIN_KERNEL ${flt} |
| 305 | +infinity() {return INFINITY;} |
| 306 | +
|
| 307 | +WITHIN_KERNEL ${flt} |
| 308 | +neg_infinity() {return -INFINITY;} |
| 309 | +''') |
| 310 | + |
| 311 | + |
| 312 | +def test_infinity(): |
| 313 | + for dtype in ['float32', 'float64']: |
| 314 | + ac, ag = gen_gpuarray((2,), dtype, ctx=context, cls=elemary) |
| 315 | + out_g = ag._empty_like_me() |
| 316 | + flt = 'ga_float' if dtype == 'float32' else 'ga_double' |
| 317 | + out_arg = arg('out', out_g.dtype, scalar=False, read=False, write=True) |
| 318 | + preamble = _inf_preamb_tpl.render(flt=flt) |
| 319 | + |
| 320 | + # +infinity |
| 321 | + ac[:] = numpy.inf |
| 322 | + expr_inf = 'out = infinity()' |
| 323 | + kernel = GpuElemwise(context, expr_inf, [out_arg], |
| 324 | + preamble=preamble) |
| 325 | + kernel(out_g) |
| 326 | + assert numpy.array_equal(ac, numpy.asarray(out_g)) |
| 327 | + |
| 328 | + # -infinity |
| 329 | + ac[:] = -numpy.inf |
| 330 | + expr_neginf = 'out = neg_infinity()' |
| 331 | + kernel = GpuElemwise(context, expr_neginf, [out_arg], |
| 332 | + preamble=preamble) |
| 333 | + kernel(out_g) |
| 334 | + assert numpy.array_equal(ac, numpy.asarray(out_g)) |
0 commit comments