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
How to copy data from a C array to a numpy array efficiently?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
JBT  
View profile  
 More options Oct 7 2012, 2:43 am
From: JBT <jianbao....@gmail.com>
Date: Sat, 6 Oct 2012 23:43:40 -0700 (PDT)
Local: Sun, Oct 7 2012 2:43 am
Subject: How to copy data from a C array to a numpy array efficiently?

Hi,

I am developing a Python wrapper of the NASA CDF C library in Cython. I
have got a working version of it now, but it is slower than the counterpart
in IDL. For loading the same file, mine takes about 400 ms, whereas the IDL
version takes about 290 ms.

The main overhead in my code is caused by a for-loop of element-by-element
copying. Here is the relevant code in cython:
#-------------------------------- code
-----------------------------------------------------
    #-- double
        realData = numpy.zeros(lenData, np_dtype)

        dblEntry = <double *>malloc(lenData * sizeof(double))
        status = CDFlib(
                       SELECT_, zVAR_RECCOUNT_, numRecs,
                       NULL_)
        status = CDFlib(
                       GET_, zVAR_HYPERDATA_, dblEntry,
                       NULL_)
        for ii in range(lenData):
            realData[ii] = dblEntry[ii]
        realData.shape = np_shape
        free(dblEntry)
#--------------------------------- end of code
-------------------------------------------
The time-consuming for-loop is highlighted in red. If I change
range(lenData) to range(lenData/2), the cpu time will be down from 400 ms
to 230 ms for the case I mentioned above. Because the element-by-element
copying for-loop seems pretty naive to me, I am wondering if there is a
better way to copy data from the C array, dblEntry, to the numpy array,
realData.

I tried the numpy C API PyArray_NewFromDescr with flag NPY_ENSURECOPY, but
didn't get any luck. On the one hand, the flag above didn't seem to work as
I expected, because I got memory deallocation failure error messages when I
quitted ipython, where I tested my code, which I don't get if I use the
naive for-loop. On the other hand, I can't figure out how to use
PyArray_NewFromDescr correctly because the loaded data I got were not
correct. Anyway, here is how I used PyArray_NewFromDescr:
#----------------------------------------- code
------------------------------------------
        cdef np.npy_intp dims[1]
        dims[0] = lenData
        realData = PyArray_NewFromDescr(numpy.ndarray,
numpy.dtype(np_dtype),
                                        1, dims, NULL, <void *>dblEntry,
                                        NPY_CARRAY|NPY_ENSURECOPY, None)
        free(dblEntry)
#-------------------------------------- end of code
--------------------------------------
BTW,  it can be compiled successfully by cython, in case you are wondering
if the code had all the necessary pieces,

Thank you very much for reading. :-)

Cheers,
Jianbao


 
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.
Bradley Froehle  
View profile  
 More options Oct 7 2012, 2:03 pm
From: Bradley Froehle <brad.froe...@gmail.com>
Date: Sun, 7 Oct 2012 11:03:18 -0700 (PDT)
Local: Sun, Oct 7 2012 2:03 pm
Subject: Re: How to copy data from a C array to a numpy array efficiently?

Can you not just pass in your realData array to CDFlib, to avoid the copy
in the first place?  Something like:

cimport numpy as np
import numpy as np

        cdef np.ndarray realData = np.zeros(np_shape, dtype='d')
        status = CDFlib(
                       SELECT_, zVAR_RECCOUNT_, numRecs,
                       NULL_)
        status = CDFlib(
                       GET_, zVAR_HYPERDATA_, <double*> realData.data,
                       NULL_)


 
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.
JBT  
View profile  
 More options Oct 7 2012, 2:55 pm
From: JBT <jianbao....@gmail.com>
Date: Sun, 7 Oct 2012 11:55:36 -0700 (PDT)
Local: Sun, Oct 7 2012 2:55 pm
Subject: Re: How to copy data from a C array to a numpy array efficiently?

Thank you so much, Bradley!!!!!!!!!!!!!!!

It works like a charm!!!

Now the CPU time is down from 400 ms to less than 100 ms. WOW~~

Cheers,
Jianbao


 
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.
Chris Barker  
View profile  
 More options Oct 8 2012, 3:08 pm
From: Chris Barker <chris.bar...@noaa.gov>
Date: Mon, 8 Oct 2012 12:08:01 -0700
Local: Mon, Oct 8 2012 3:08 pm
Subject: Re: [cython-users] Re: How to copy data from a C array to a numpy array efficiently?

>         status = CDFlib(
>                        GET_, zVAR_HYPERDATA_, <double*> realData.data,
>                        NULL_)

> For the record, using .data may or may not be supported in future numpy

releases.

You can use realData[0] instead. And it saves you a typecast.

Also, if your lib does not support passing in a pointer, you can use
memcpy() similarly.

Chris.


 
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.
Jianbao Tao  
View profile  
 More options Oct 8 2012, 3:31 pm
From: Jianbao Tao <jianbao....@gmail.com>
Date: Mon, 8 Oct 2012 12:31:40 -0700
Local: Mon, Oct 8 2012 3:31 pm
Subject: Re: [cython-users] Re: How to copy data from a C array to a numpy array efficiently?

Roger that. Thanks, Chris.


 
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.
End of messages
« Back to Discussions « Newer topic     Older topic »