spcl / dace

DaCe - Data Centric Parallel Programming
http://dace.is/fast
BSD 3-Clause "New" or "Revised" License
487 stars 121 forks source link

Codegen: Scalar Return Values are Impossible #1610

Open philip-paul-mueller opened 2 months ago

philip-paul-mueller commented 2 months ago

It is impossible to return a scalar from an SDFG.

This is a code generator issue, as the final signature of the C function of __return is a scalar and not a pointer (okay calling it on the Python side must also be different then). The ability to return scalars is an intended behaviour, see CompiledSDFG._convert_return_values() that handles this case, but it does not work.

PR#1609 adds unit tests for properly testing this case (previously only array returns where tested). Furthermore, it patches the validation to detect such cases and rejects such SDFG, however, it is not a solution. A true solution would allow that any (global) scalar can be used to pass information to the outside, as it is possible with arrays.

Code to reproduce:

import dace
import numpy
@dace.program(auto_optimize=False, recreate_sdfg=True)
def testee(
    A: dace.float64[20],
) -> dace.float64:
    return dace.float64(A[3])

A = np.random.rand(20)
sdfg = testee.to_sdfg()
res = sdfg(A=A)
assert isinstance(res, np.float64)
assert A[3] == res

By dropping the return value annotation in the above code, the frontend would actually promote the return value to an array and it would work, but no scalar is returned then.