inducer / inducer/pyopencl

fill_normal sometimes yields inf values

Open
#318 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
1.2k
Forks
247
Avg merge
7h 2m
Merged PRs (30d)
6

Description

The `fill_normal()` method gives `inf` values sometimes, probably because the random generators sometimes yield zeros especially with 32 bit floats. The logarithm in Box-Muller then causes the infinity.

The following output shows that although zeros are much less frequent than other values such as ones, they occassionally come up in `fill_uniform()` , and so do infinities in `fill_normal()`. No difference between Threefry and Philox.

Output on my system, running pyopencl 2017.2.2:
```
3 zeros found in uniform
570 ones founds in uniform
2 infs found in normal
```

The code:
```
from pyopencl import clrandom
import pyopencl as cl
import numpy as np

context = cl.create_some_context(interactive=False)
queue = cl.CommandQueue(context)

#random_gen = clrandom.PhiloxGenerator(context)
random_gen = clrandom.ThreefryGenerator(context)

shape = (1000000,)
arr = cl.array.empty(queue, shape, dtype=np.float32)

n_zeros = 0
n_ones = 0
n_infs = 0
for i in range(20000):
random_gen.fill_uniform(arr).wait()
vals = arr.get()
n_zeros += np.count_nonzero(vals == 0.0)
n_ones += np.count_nonzero(vals == 1.0)

random_gen.fill_normal(arr).wait()
vals = arr.get()
n_infs += np.count_nonzero(np.isinf(vals))

print(n_zeros, 'zeros found in uniform')
print(n_ones, 'ones founds in uniform')
print(n_infs, 'infs found in normal')
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.