Message from discussion
Best Practices for passing numpy data pointer to C ?
Received: by 10.66.85.231 with SMTP id k7mr1461718paz.38.1344274135616;
Mon, 06 Aug 2012 10:28:55 -0700 (PDT)
X-BeenThere: cython-users@googlegroups.com
Received: by 10.68.226.132 with SMTP id rs4ls5984813pbc.6.gmail; Mon, 06 Aug
2012 10:28:54 -0700 (PDT)
Received: by 10.66.85.3 with SMTP id d3mr1331037paz.18.1344274134772;
Mon, 06 Aug 2012 10:28:54 -0700 (PDT)
Received: by 10.66.85.3 with SMTP id d3mr1331036paz.18.1344274134737;
Mon, 06 Aug 2012 10:28:54 -0700 (PDT)
Return-Path: <chris.bar...@noaa.gov>
Received: from na3sys009aog115.obsmtp.com (na3sys009aog115.obsmtp.com [74.125.149.238])
by gmr-mx.google.com with SMTP id vv9si577555pbc.2.2012.08.06.10.28.54
(version=TLSv1/SSLv3 cipher=OTHER);
Mon, 06 Aug 2012 10:28:54 -0700 (PDT)
Received-SPF: neutral (google.com: 74.125.149.238 is neither permitted nor denied by best guess record for domain of chris.bar...@noaa.gov) client-ip=74.125.149.238;
Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 74.125.149.238 is neither permitted nor denied by best guess record for domain of chris.bar...@noaa.gov) smtp.mail=chris.bar...@noaa.gov
Received: from mail-qa0-f42.google.com ([209.85.216.42]) (using TLSv1) by na3sys009aob115.postini.com ([74.125.148.12]) with SMTP
ID DSNKUB/+1RZWixVSP38GmPr4+vmhaV8f4...@postini.com; Mon, 06 Aug 2012 10:28:54 PDT
Received: by qaeb19 with SMTP id b19so591458qae.15
for <cython-users@googlegroups.com>; Mon, 06 Aug 2012 10:28:50 -0700 (PDT)
d=google.com; s=20120113;
h=mime-version:in-reply-to:references:from:date:message-id:subject:to
:content-type:x-gm-message-state;
bh=A8JTxmBE1AiI0DMI1VhkTgG+HfhtKccfu5jJ/jfMNpg=;
b=irO70sdsVz4ETHNiwmyRrHih/MOP8yr/Ug5D76IIKsjlfRrx+SeFW12l+uCJWvEgya
FOPXRr1eS5xH9kRq3G7E7Stf69IDNhZOjDZh6jxyCs826xgS/AmXtcipUSba0YwZfA1y
JobySgacpH+nFK1/gd9+Kl8en+H5yjECqXfe7JUxWlBivCk2hcI5eTzyvD7AGAUiI8Le
kiQp/3IYC4a+cHa4g6ilItNB7ttza37Cyu4Oq5WinD8JnDYttZHKNnjpW3jlXGLL++Sl
VgkKnGD3NKv30rJMxTBV7PWvIhsCq/VmE5zyXeMToYvbX2MZi5SKaoSfEysOIp6Uvns2
oWpw==
Received: by 10.60.7.133 with SMTP id j5mr20282003oea.8.1344274130411; Mon, 06
Aug 2012 10:28:50 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.182.237.199 with HTTP; Mon, 6 Aug 2012 10:28:10 -0700 (PDT)
In-Reply-To: <5016F078.8070...@yahoo.no>
References: <CALGmxEJOLLx8VtNtK3jBt-AJ=kNU5i1kbEqgyTv9OErBgOG...@mail.gmail.com>
<4666d921-974c-4236-83b3-5dd6ed98d09c@googlegroups.com> <CALGmxE+oftOv4sN6PGE=sp+sVLnmarLTEopEF85QCof9_GS...@mail.gmail.com>
<5012E12A.7040...@yahoo.no> <18844327-e8c6-4bd1-b9d4-64202f6f1a53@googlegroups.com>
<f1d083e5-7466-4c55-8fd7-367838fa2069@googlegroups.com> <5016C15A.6030...@yahoo.no>
<5016F078.8070...@yahoo.no>
From: Chris Barker <chris.bar...@noaa.gov>
Date: Mon, 6 Aug 2012 10:28:10 -0700
Message-ID: <CALGmxEKjBzOAWwk_ORjvC6h0dbBoVd8CKRrWmtTcF39k7do...@mail.gmail.com>
Subject: Re: [cython-users] Re: Best Practices for passing numpy data pointer
to C ?
To: cython-users@googlegroups.com
Content-Type: text/plain; charset=UTF-8
X-Gm-Message-State: ALoCoQmYOCtniorQXeNwu6bxkuljIhq8n/c2nALIbF+HO2RkWKbJhasIRCgIZ3ZwSzSHzVo7hB1u
On Mon, Jul 30, 2012 at 1:37 PM, Sturla Molden <sturlamol...@yahoo.no> wrote:
> So I suggest one of these idioms for calling C from Cython:
> cdef double[:,::1] array = numpy_array
> cdef Py_ssize_t m, n
> m = array.shape[0]
> n = array.shape[1]
> foobar(m, n, &array[0,0])
>
> cdef view.array array = <double[:,::1]> numpy_array
> cdef Py_ssize_t m, n
> m = array.shape[0]
> n = array.shape[1]
> foobar(m, n, <double*> array.data)
The only difference is that first line: will they generate the same C
code? -- why would one choose on or the other?
(there should be one obvious way to do it...)
I'll probably go update the wiki page, but to make sure I'm clear:
Using a view.array will build a Cython "memoryview", using the
extended buffer interface -- so you can pass in any Python object that
supports PEP 3118 buffers (does anything other than numpy suport that
now??)
This is a lightweight way to both get a pointer to the data, and to
have a way, in Cython, to slice and dice the data.
Will this check for C-contiguous and raise an error if not?
-Chris
>
>
> Sturla
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Bar...@noaa.gov