Skip to content

How to declare a local array #5

Closed

Description

@certik

The requirement is that it works with CPython and NumPy, but also compiles using LPython. One option is:

def main():
    a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
    b: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
    c: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
    scalar: f32
    i: i32
    nsize: i32
    scalar = 10
    nsize = size(a)
    for i in range(nsize): # type: parallel
        a[i] = 5
        b[i] = 5
    triad(a, b, scalar, c)
    print("End Stream Triad")

So the syntax is:

    a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)

But this is redundant, as the size 10000 is repeated twice and the type f32 and numpy.float32 is also repeated.

Activity

  1. certik commented on Jan 5, 2022

    @certik
    ContributorAuthor

    One way that would definitely work is to make LPython infer the type and array size from the RHS from a code like this:

        a = numpy.empty(10000, dtype=numpy.float32)
    

    The alternative, that is, to declare arrays as:

    a: f32[10000]
    

    and somehow make it work with CPython is unclear, as somehow the type annotation would need to "execute" and call NumPy.

  2. certik commented on Jan 6, 2022

    @certik
    ContributorAuthor

    After discussing with @namannimmo10, I think the best way is to use:

        a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
    

    or an equivalent:

        a: f32[10000]
        a = numpy.empty(10000, dtype=numpy.float32)
    

    Both of which work in CPython/NumPy. This follows LPython's initial design that requires everything to be explicitly typed. In both cases, a is "declared" as a local array and allocated (on stack or heap, that is ASR backend dependent).

    Later on, as we implement type inference, we can omit the type annotation, and just do:

        a = numpy.empty(10000, dtype=numpy.float32)
    

    and the compiler will infer the type f32[10000] for a from the RHS.

    If the array is only typed using a: f32[10000] but never assigned to using a = numpy.empty(10000, dtype=numpy.float32) or similar, then LPython should given an error message.

  3. certik commented on Jan 7, 2022

    @certik
    ContributorAuthor

    Here is another example. This should give a compiler error, that b is not allocated:

        a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
        b: f32[10000]
        b[:] = a[:]
    

    The way to write it is as:

        a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
        b: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
        b[:] = a[:]
    

    or like this:

        a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
        b: f32[10000]
        b = numpy.empty(10000, dtype=numpy.float32)
        b[:] = a[:]
    

    Another example: this should work, and b is a pointer to a.

        a: f32[10000] = numpy.empty(10000, dtype=numpy.float32)
        b: f32[10000]
        b = a
    
  4. added
    numpyBugs and feature requests regarding NumPy support
    on Jan 24, 2023
  5. certik commented on Mar 21, 2023

    @certik
    ContributorAuthor

    This is mostly fixed, I opened up an issue with the remaining bug: #1599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    numpyBugs and feature requests regarding NumPy support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions