Re: [f2py] F2PY issue with array arguments, results in unexpected sizes

David Ochoa <[email protected]> Fri, 10 Jan 2014 18:39:43 -0200
Newsgroups gmane.comp.python.f2py.user
Message-ID <CADFoYWon35GK-EAQhRg150YzTrjgCLnQ8_=bj-DDCsa5M+_C2g@mail.gmail.com>
--===============0907543711735164842==
Content-Type: multipart/alternative; boundary=089e013d1a94104bbe04efa3bbb7

--089e013d1a94104bbe04efa3bbb7
Content-Type: text/plain; charset=ISO-8859-1

Dear Michelle:

I've been using f2py lately and i think that you don't need to send the
size parameters to functions created with f2py (or at least that's how I'm
coding).

So it's just change the call:
v2 = test2(r)

And it should work (in my computer it does).

in test1 is different because you are not passing a array, only creating
and sending it back.

David




On Fri, Jan 10, 2014 at 2:27 PM, Michelle Gill <
michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org> wrote:

> 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)
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>

--089e013d1a94104bbe04efa3bbb7
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Dear Michelle:<div><br></div><div>I&#39;ve been using f2py=
 lately and i think that you don&#39;t need to send the size parameters to =
functions created with f2py (or at least that&#39;s how I&#39;m coding).</d=
iv>

<div><br></div><div>So it&#39;s just change the call:</div><div>v2 =3D test=
2(r)<br></div><div><br></div><div>And it should work (in my computer it doe=
s).</div><div><br></div><div>in test1 is different because you are not pass=
ing a array, only creating and sending it back.</div>

<div><br></div><div>David</div><div><br></div><div><br></div></div><div cla=
ss=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Fri, Jan 10, 2014 =
at 2:27 PM, Michelle Gill <span dir=3D"ltr">&lt;<a href=3D"mailto:michelle@=
michellelynngill.com" target=3D"_blank">michelle-X+ovDskamAd4hsYFU5B8wFaTQe2KTcn/@public.gmane.org</a>&g=
t;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div>
<p dir=3D"auto">Dear f2py users,</p>

<p dir=3D"auto">I am attempting to use f2py to speed up some python calcula=
tions 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=3D"auto">In the case of the first function, &quot;test1&quot;, the s=
ize of the returned<br>
array is 3x1001, which is as expected. In the case of the second function,<=
br>
&quot;test2&quot;, the size of the returned array is 3x1, which is not corr=
ect.</p>

<p dir=3D"auto">This issue has arisen in more complicated function that I&#=
39;m attempting<br>
to debug and I&#39;d appreciate any assistance in solving it. My fortran<br=
>
skills are a little rusty, so it&#39;s possible there is an issue with the<=
br>
way the arrays are being passed or declared. I&#39;ve tried searching this<=
br>
mailing list, StackOverflow, and general web searches with no luck.</p>

<p dir=3D"auto">I&#39;m using gfortran 4.7.3, python 2.7.5, numpy 1.8.0, al=
l built with<br>
MacPorts, on a Mac running OS X 10.9.1.</p>

<p dir=3D"auto">If I can provide further details, let me know.</p>

<p dir=3D"auto">Thanks,<br>
Michelle Gill</p>

<h3>Contents of file test1.f90</h3>

<p dir=3D"auto">! -<em>- f90 -</em>-<br>
subroutine test1(simdim, v)</p>

<p dir=3D"auto">integer, intent(in) :: simdim<br>
!    f2py integer, intent(in) :: simdim</p>

<p dir=3D"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=3D"auto">v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p>

<p dir=3D"auto">print *, &quot;v shape &quot;, size(v)</p>

<p dir=3D"auto">return<br>
end</p>

<h3>Contents of file test2.f90</h3>

<p dir=3D"auto">! -<em>- f90 -</em>-<br>
subroutine test2(simdim, r, v)</p>

<p dir=3D"auto">integer, intent(in) :: simdim<br>
!    f2py integer, intent(in) :: simdim</p>

<p dir=3D"auto">real, dimension(simdim), intent(in) :: r(simdim)<br>
!    f2py real, dimension(simdim), intent(in) :: r(simdim)</p>

<p dir=3D"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=3D"auto">v =3D spread( (/ 45.2, 31.2, 41.2 /), 2, simdim+1)</p>

<p dir=3D"auto">print *, &quot;r shape &quot;, size(r)<br>
print *, &quot;v shape &quot;, size(v)</p>

<p dir=3D"auto">return<br>
end</p>

<h3>Compile each with f2py</h3>

<p dir=3D"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=3D"auto">In [1]: from test1 import test1</p>

<p dir=3D"auto">In [2]: from test2  import test2</p>

<p dir=3D"auto">In [3]: import numpy as np</p>

<p dir=3D"auto">In [4]: simdim =3D 1000</p>

<p dir=3D"auto">In [5]: r =3D np.random.random(simdim)</p>

<p dir=3D"auto">In [6]: print r.shape<br>
(1000,)</p>

<p dir=3D"auto">In [7]: v1 =3D test1(simdim)<br>
 v shape         3003</p>

<p dir=3D"auto">In [8]: v2 =3D test2(simdim, r)<br>
 r shape            0<br>
 v shape            3</p>

<p dir=3D"auto">In [9]: print v1.shape, v2.shape<br>
(3, 1001) (3, 1)</p>

</div><br>_______________________________________________<br>
f2py-users mailing list<br>
<a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users" target=3D"_blank=
">http://cens.ioc.ee/mailman/listinfo/f2py-users</a><br>
<br></blockquote></div><br></div>

--089e013d1a94104bbe04efa3bbb7--


--===============0907543711735164842==
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

--===============0907543711735164842==--