Message from discussion
Best Practices for passing numpy data pointer to C ?
Received: by 10.236.88.111 with SMTP id z75mr6156910yhe.6.1344287522580;
Mon, 06 Aug 2012 14:12:02 -0700 (PDT)
X-BeenThere: cython-users@googlegroups.com
Received: by 10.100.202.10 with SMTP id z10ls2071987anf.3.gmail; Mon, 06 Aug
2012 14:12:01 -0700 (PDT)
Received: by 10.236.191.70 with SMTP id f46mr6614385yhn.44.1344287521422;
Mon, 06 Aug 2012 14:12:01 -0700 (PDT)
Received: by 10.236.191.70 with SMTP id f46mr6614384yhn.44.1344287521409;
Mon, 06 Aug 2012 14:12:01 -0700 (PDT)
Return-Path: <markflorisso...@gmail.com>
Received: from mail-yw0-f41.google.com (mail-yw0-f41.google.com [209.85.213.41])
by gmr-mx.google.com with ESMTPS id r20si1654185ano.1.2012.08.06.14.12.01
(version=TLSv1/SSLv3 cipher=OTHER);
Mon, 06 Aug 2012 14:12:01 -0700 (PDT)
Received-SPF: pass (google.com: domain of markflorisso...@gmail.com designates 209.85.213.41 as permitted sender) client-ip=209.85.213.41;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of markflorisso...@gmail.com designates 209.85.213.41 as permitted sender) smtp.mail=markflorisso...@gmail.com; dkim=pass header...@gmail.com
Received: by yhr47 with SMTP id 47so3616565yhr.14
for <cython-users@googlegroups.com>; Mon, 06 Aug 2012 14:12:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=fw846g5TQWScZeNqI4MgYASmZHOH6JLWoG60J5zomsU=;
b=xfZbIUxX95kUn5rYYr4xfT7R7h0vh5KQhn+pyMSeHag2CnQs9WHj5cHpmqWSlBIc+b
JemYJVonjeSNxxhCg6iSPU96qEbNZW09E3NZVlgXU3SVN99nZ4SKi7y0woFHMqZdIpty
h8jKUqAWhDPe0rhtQiQnQdXaGPKv+yeZo9lLdcW0bTMs2SZTNLVJADQzcuY6b4dP0xW7
48yZ3Opgs/GVndAlwRvqCBOKY60mB/hf/YBlEXCar+cXdmD1NfVXYUzrvfUy25n34jRe
M7YS0aUq1fqyb8h/0YB1QM/B/+Dz4v8EBGuLxSBSy0pk9uVoDiDhsxj+glec9FJOd2nE
lFaA==
MIME-Version: 1.0
Received: by 10.60.21.103 with SMTP id u7mr21067230oee.49.1344287521246; Mon,
06 Aug 2012 14:12:01 -0700 (PDT)
Received: by 10.76.68.105 with HTTP; Mon, 6 Aug 2012 14:12:01 -0700 (PDT)
In-Reply-To: <CALGmxEKjBzOAWwk_ORjvC6h0dbBoVd8CKRrWmtTcF39k7do...@mail.gmail.com>
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>
<CALGmxEKjBzOAWwk_ORjvC6h0dbBoVd8CKRrWmtTcF39k7do...@mail.gmail.com>
Date: Mon, 6 Aug 2012 22:12:01 +0100
Message-ID: <CANg26EX4QosXAwS3W+pH8O3hy3wN4UaKK_9KMBy0o2pmOHx...@mail.gmail.com>
Subject: Re: [cython-users] Re: Best Practices for passing numpy data pointer
to C ?
From: mark florisson <markflorisso...@gmail.com>
To: cython-users@googlegroups.com
Content-Type: text/plain; charset=UTF-8
On 6 August 2012 18:28, Chris Barker <chris.bar...@noaa.gov> wrote:
> 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??)
Not entirely. A cython.view.array is an array itself, that has memory
itself. In other words, it's a (lousy) replacement for numpy. It's
useful for users when they have memory allocated from somewhere and
want to turn it into a numpy array or memoryview. The array is more of
an intermediate thing, usually created implicitly by casting a
pointer: <double[:10:1, :10]> p creates a Fortran contiguous cython
array view on the memory of shape (10, 10).
> 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?
Yes, buffers are always validated when they are assigned to by objects
at runtime. Assigning memoryviews to each other with wrong types is an
error, e.g.
cdef double[:, ::1] array = numpy_array # runtime check
cdef double[:, ::1] array2 = array # compile-time check
> -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