NonLinearRange

RainerHeintzmann
Sun, 26 Jan 2025 10:52:38 +0000
General Usage

I was looking for way to way to allow multi-kernel broadcasting to act also over non-linear ranges.
E.g. consider a complex exponential exp.(1im .* k0 .* x) with k0=1.35 being a scalar and x=0:255 a linear range.
This will be fairly slow, since the exponential would need to be evaluated every complex-valued position. The same would hold implementing this as a comprehension, I assume.
Thinking sequentially, one could use an iterator which multiplies an internal state variable by the complex constant exp.(1im*k0) to advance by one in x. This only requires a complex-valued multiplication and is thus sequentially much faster. However, the broadcasting in the example above, might leverage some multi-kernel parallelism (does it?).

Ideally one would define a non-linear range. It is an iterator as well as allowing random access. The broadcast system should then be able to automatically be able to distribute the work over the kernels such that each kernel starts with a random access operation but then runs over the elements it should process using the iterator.
I imagine that one would like to define such a non-linear range object like this:

exp_range = NonLinRange((arg)->exp(1im*arg), (s)->s*exp(1im), k0*x)

with x being the UnitRange as above. The first argument specifies the random access operation, the second specifies the non-linear iteration applied on the current state s to advance to the successive element.
Ideally such a NonLinearRange would keep its type upon multiplication or addition, which one could guarantee with appropriate boilerplate code.
Furthermore, any exp applied to a StepRangeLen object could become such a NonLinearRange object instead of a Vector. But this may break compatibility in some cases, as then the result-type changed.

Does anything like this exist in core-julia or a package? If not, would this not be a way to speed up code significantly? I would image that an efficient all-Julia FFT implementation would make use of something like this.

8 posts - 3 participants