Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Best Practices for passing numpy data pointer to C ?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Barker  
View profile  
 More options Jul 27 2012, 2:06 pm
From: Chris Barker <chris.bar...@noaa.gov>
Date: Fri, 27 Jul 2012 11:06:52 -0700
Local: Fri, Jul 27 2012 2:06 pm
Subject: Re: [cython-users] Re: Best Practices for passing numpy data pointer to C ?

On Fri, Jul 27, 2012 at 10:45 AM, Nader Al-Naji <iamnotna...@gmail.com> wrote:
> <dtype_t*>  np.PyArray_DATA(arr)

> is the right way to go. It is superior to the &a[0] solution because it
> doesn't require bounds checking, and it is superior to the .data solution
> 2) BONUS: it works even on
> arrays that haven't been statically casted to a Cython np.ndarray[...]. To
> fully understand what I mean by (2), take a look at the following example:

>     a = np.arange(10, dtype=int)

>     # This works even if a hasn't been casted yet.
>     fprintf(stderr, "%lu %lu\n", <size_t>np.PyArray_DATA(a))

what happens if a is not a numpy array? If you create it in your
Cython code, you can be confident that it is, but if it's passed in,
who knows? and if you have to check, why not cast it?

> Not having to cast the array before extracting the buffer is useful if one
> knows the size of the elements one is dealing with, but not necessarily the
> type.

you can cast it to a ndarray with unspecified type -- I suspect arr[0]
will work there, too.

> So I think Sturla's solution is what we should use in the future. It has the
> same semantics as .data, which I think a lot of people really like, and it
> won't be compromised if numpy changes.

well, as Sturla points out, Cython essentially creates the same code
under the hood if you index the zeroth element anyway. My it's just
aesthetics, but I like the more pyhtonic looking: &arr[0]

-Chris

> Anyone have any other reasons why Sturla's solution might not be a good
> idea?

> Thanks,
> Nader

--

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.