[f2py] F2PY issue with array arguments, results in unexpected sizes
"Michelle Gill" <michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org> Fri, 10 Jan 2014 11:27:44 -0500
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
--===============5009650662440656013== Content-Type: multipart/alternative; boundary="=_MailMate_5481346D-BCEE-45F0-8B55-4CB5DECAE027_=" --=_MailMate_5481346D-BCEE-45F0-8B55-4CB5DECAE027_= Content-Type: text/plain; markup=markdown Dear f2py users, I am attempting to use f2py to speed up some python calculations and am getting some unexpected results. I made two sample fortran functions to illustrate the issue. These two functions compile fine with f2py. When accessed within python, the respective returned rank-2 arrays are different sized even though their sizes should be the same. In the case of the first function, "test1", the size of the returned array is 3x1001, which is as expected. In the case of the second function, "test2", the size of the returned array is 3x1, which is not correct. This issue has arisen in more complicated function that I'm attempting to debug and I'd appreciate any assistance in solving it. My fortran skills are a little rusty, so it's possible there is an issue with the way the arrays are being passed or declared. I've tried searching this mailing list, StackOverflow, and general web searches with no luck. I'm using gfortran 4.7.3, python 2.7.5, numpy 1.8.0, all built with MacPorts, on a Mac running OS X 10.9.1. If I can provide further details, let me know. Thanks, Michelle Gill ### Contents of file test1.f90 ### ! -*- f90 -*- subroutine test1(simdim, v) integer, intent(in) :: simdim ! f2py integer, intent(in) :: simdim real, dimension(3,simdim+1), intent(out):: v(3,simdim+1) ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1) v = spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1) print *, "v shape ", size(v) return end ### Contents of file test2.f90 ### ! -*- f90 -*- subroutine test2(simdim, r, v) integer, intent(in) :: simdim ! f2py integer, intent(in) :: simdim real, dimension(simdim), intent(in) :: r(simdim) ! f2py real, dimension(simdim), intent(in) :: r(simdim) real, dimension(3,simdim+1), intent(out):: v(3,simdim+1) ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1) v = spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1) print *, "r shape ", size(r) print *, "v shape ", size(v) return end ### Compile each with f2py ### f2py --verbose -m test1 -c test1.f90 f2py --verbose -m test2 -c test2.f90 ### Compare the functions in IPython ### In [1]: from test1 import test1 In [2]: from test2 import test2 In [3]: import numpy as np In [4]: simdim = 1000 In [5]: r = np.random.random(simdim) In [6]: print r.shape (1000,) In [7]: v1 = test1(simdim) v shape 3003 In [8]: v2 = test2(simdim, r) r shape 0 v shape 3 In [9]: print v1.shape, v2.shape (3, 1001) (3, 1) --=_MailMate_5481346D-BCEE-45F0-8B55-4CB5DECAE027_= Content-Type: text/html <div class="markdown"> <p dir="auto">Dear f2py users,</p> <p dir="auto">I am attempting to use f2py to speed up some python calculations and am<br> getting some unexpected results. I made two sample fortran functions to<br> illustrate the issue. These two functions compile fine with f2py. When<br> accessed within python, the respective returned rank-2 arrays are<br> different sized even though their sizes should be the same.</p> <p dir="auto">In the case of the first function, "test1", the size of the returned<br> array is 3x1001, which is as expected. In the case of the second function,<br> "test2", the size of the returned array is 3x1, which is not correct.</p> <p dir="auto">This issue has arisen in more complicated function that I'm attempting<br> to debug and I'd appreciate any assistance in solving it. My fortran<br> skills are a little rusty, so it's possible there is an issue with the<br> way the arrays are being passed or declared. I've tried searching this<br> mailing list, StackOverflow, and general web searches with no luck.</p> <p dir="auto">I'm using gfortran 4.7.3, python 2.7.5, numpy 1.8.0, all built with<br> MacPorts, on a Mac running OS X 10.9.1.</p> <p dir="auto">If I can provide further details, let me know.</p> <p dir="auto">Thanks,<br> Michelle Gill</p> <h3>Contents of file test1.f90</h3> <p dir="auto">! -<em>- f90 -</em>-<br> subroutine test1(simdim, v)</p> <p dir="auto">integer, intent(in) :: simdim<br> ! f2py integer, intent(in) :: simdim</p> <p dir="auto">real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)<br> ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)</p> <p dir="auto">v = spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p> <p dir="auto">print *, "v shape ", size(v)</p> <p dir="auto">return<br> end</p> <h3>Contents of file test2.f90</h3> <p dir="auto">! -<em>- f90 -</em>-<br> subroutine test2(simdim, r, v)</p> <p dir="auto">integer, intent(in) :: simdim<br> ! f2py integer, intent(in) :: simdim</p> <p dir="auto">real, dimension(simdim), intent(in) :: r(simdim)<br> ! f2py real, dimension(simdim), intent(in) :: r(simdim)</p> <p dir="auto">real, dimension(3,simdim+1), intent(out):: v(3,simdim+1)<br> ! f2py real, dimension(3,simdim+1), intent(out) :: v(3,simdim+1)</p> <p dir="auto">v = spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p> <p dir="auto">print *, "r shape ", size(r)<br> print *, "v shape ", size(v)</p> <p dir="auto">return<br> end</p> <h3>Compile each with f2py</h3> <p dir="auto">f2py --verbose -m test1 -c test1.f90<br> f2py --verbose -m test2 -c test2.f90</p> <h3>Compare the functions in IPython</h3> <p dir="auto">In [1]: from test1 import test1</p> <p dir="auto">In [2]: from test2 import test2</p> <p dir="auto">In [3]: import numpy as np</p> <p dir="auto">In [4]: simdim = 1000</p> <p dir="auto">In [5]: r = np.random.random(simdim)</p> <p dir="auto">In [6]: print r.shape<br> (1000,)</p> <p dir="auto">In [7]: v1 = test1(simdim)<br> v shape 3003</p> <p dir="auto">In [8]: v2 = test2(simdim, r)<br> r shape 0<br> v shape 3</p> <p dir="auto">In [9]: print v1.shape, v2.shape<br> (3, 1001) (3, 1)</p> </div> --=_MailMate_5481346D-BCEE-45F0-8B55-4CB5DECAE027_=-- --===============5009650662440656013== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users --===============5009650662440656013==--