You are correct, managing temporary arrays is one of the main advantages of bundling kernel/computation calls in a bigger computation (although it is possible to do that manually). Admittedly, it is not always straightforward. What are the problems you're experiencing with the example? Since it's not a doctest, it may have got out of date with the current API (although at a glance it looks ok). Have you tried looking at the full examples, e.g.
https://github.com/fjarri/reikna/blob/develop/examples/demo_specgram.py ?
In your case it will look something like
class MyComputation(Computation):
def __init__(self, input_shape, input_dtype):
Computation.__init__(
self,
[
Parameter('output', Annotation(Type(input_dtype, input_shape), 'o')),
Parameter('input', Annotation(Type(input_dtype, input_shape), 'i')),
])
def _build_plan(self, plan_factory, device_params, output, input):
plan = plan_factory()
output_1 = plan.temp_array_like(input)
plan.kernel_call(kernel1_source, [output_1, input], global_size=..., local_size=...)
output_2 = plan.temp_array_like(input)
plan.kernel_call(kernel2_source, [output_2, output1], global_size=..., local_size=...)
...
plan.kernel_call(kernelN_source, [output, outputN-1], global_size=..., local_size=...)
comp = MyComp(input_shape, input_dtype).compile(thr)
for i in range(0,NumbersOfDatasets):
input_array = load(data_i)
input = thr.to_device(input_array)
comp(output, input)
result[i] = output.get()
Note that:
- you need to supply the kernel source during _build_plan(), since at that moment the Thread is not available yet.
- you need to be able to derive your computation's signature (input/output paramteres, their shapes and dtypes) at construction time. Pass whatever arguments you need to the constructor, you can create class attributes too to use in _build_plan() later.
Refer to the API reference for the full list of parameters for various calls used here.
Of course, you don't have to do that. If temporary memory is the only problem, you can manage it manually with just ZeroOffsetManager linked above. It won't be much easier, but may be more flexible if your dataflow cannot be fit into Computation's limitations. You will need to create your temporary arrays with
http://reikna.publicfields.net/en/latest/api/cluda.html#reikna.cluda.tempalloc.TemporaryManager.array and manually specify the dependencies. Then you can use them as you would use regular arrays.
Hope that helps.