jakeret / hope

HOPE: A Python Just-In-Time compiler for astrophysical computations
GNU General Public License v3.0
382 stars 27 forks source link

Exception when iterating over a numpy array. What am I not getting? #54

Closed rickvipond closed 7 years ago

rickvipond commented 7 years ago

Hi I'm trying to get a function that iterates over xrange and wants to extract a row from a numpy array (then do an expression).

But I can't seem to get it to access the row of the array?

i get this exception:

raise Exception("Extends of variable and subscript do not match")
Exception: Extends of variable and subscript do not match
in line c = t[i]
in body c = t[i]

These are what the types are:

ds = 1227, <type 'numpy.int32'> dt 400 <type 'int'> as 69 <type 'numpy.int32'> mt 2147483647 <type 'int'> ns 2680 <type 'int'> 248256 <type 'int'>

[[ 21 22 1 7 48650 0] [ 26 27 1 3 84823 1] [ 64 65 1 11 77351 2] ..., [ 68 69 2948 2970 53287 248253] [ 67 68 2952 2964 53498 248254] [ 68 312 2980 3034 53498 248255]]

<type 'numpy.ndarray'> shape (248256, 6) dtype int32

And the code:

@jit
    def hope_loop(self, ds, dt, as, mt, ns, nr, t):
        earliest_arrival = np.zeros(ns, dtype=np.int32)
        earliest_arrival[:] = mt
        earliest_arrival[ds] = dt

        # Set up array of all stations to track back
        in_connection = np.zeros(ns, dtype=np.int32)
        in_connection[:] = -1
        for i in xrange(nr):
            c = t[i]
           # if c[2] >= earliest_arrival[c[0]] and c[3] < earliest_arrival[c[1]]:
           # continue
        return earliest_arrival
jakeret commented 7 years ago

If t has a dimension > 1 then try c = t[i, :]

rickvipond commented 7 years ago

Right that solved that. Now I get an error:

`running build_ext building 'hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0' extension C compiler: clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall

compile options: '-I/Users/rickvipond/.virtualenvs/coachfairer/lib/python2.7/site-packages/numpy/core/include -I/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c' extra options: '-Wall -Wno-unused-variable -march=native -stdlib=libc++ -std=c++11' Warning: Can't read registry to find the necessary compiler setting Make sure that Python modules _winreg, win32api or win32con are installed. clang: /var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8/hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0.cpp /var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8/hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0.cpp:67:17: error: subscripted value is not an array, pointer, or vector cin_connection[(int)(i1)] = (npy_int64)-1;


1 error generated.
error: Command "clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -I/Users/rickvipond/.virtualenvs/coachfairer/lib/python2.7/site-packages/numpy/core/include -I/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c /var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8/hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0.cpp -o /var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8/hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0.o -Wall -Wno-unused-variable -march=native -stdlib=libc++ -std=c++11" failed with exit status 1/var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8/hope_loop_06a925dfe9caa1db638b4ae2138c20a93f2fe6052a4668df47630b84_0.cpp:67:17: error: subscripted value is not an array, pointer, or vector
                cin_connection[(int)(i1)] = (npy_int64)-1;
                ~~~~~~~~~~~~~~^~~~~~~~~~
1 error generated.
None

Traceback (most recent call last):
  File "al_csa_numpy.py", line 305, in <module>
    main()
  File "al_csa_numpy.py", line 300, in main
    print csa.compute(depstation, arrstation, deptime)
  File "al_csa_numpy.py", line 269, in compute
    print self.hope_loop(departure_station, departure_time, arrival_station, MAX_INT, self.stations.shape[0], self.timetable.shape[0], self.timetable)
  File "/Users/rickvipond/.virtualenvs/coachfairer/lib/python2.7/site-packages/hope/_wrapper.py", line 71, in _hope_callback
    return self(*args) if self.cache is None else self.cache(*args)
  File "/Users/rickvipond/.virtualenvs/coachfairer/lib/python2.7/site-packages/hope/_wrapper.py", line 104, in __call__
    so_filename = _compile(tempfolder, localfilename, self.fkt.__name__)
  File "/Users/rickvipond/.virtualenvs/coachfairer/lib/python2.7/site-packages/hope/_wrapper.py", line 267, in _compile
    raise Exception("Error compiling function {0} (compiled to {1})".format(fkt_name, target))
Exception: Error compiling function hope_loop (compiled to /var/folders/f2/1yqjznrj5hv0w019qf8ll2yc0000gn/T/hopeckCbS8)`
jakeret commented 7 years ago

Not sure whats going on. Just guessing... You could try to do de assignment a bit differently:

 in_connection = np.ones(ns, dtype=np.int32)
 in_connection[:] *= -1
rickvipond commented 7 years ago

Same error. Anyway, I've removed the two arrays i create out of the function, passing them in instead and it's gone away.