简体   繁体   中英

Passing **float array from C++ Dll to python

I am currently working with C/C++ functions imported into python thanks to a Dll and ctypes. In the DLL, I have a function that takes a float **variable as argument. This argument is filled in the function and I get it back on python using ctypes.

I have a solution which works using 2 for loops, but this is very slow when the 2D array has a lot of points.

Do you know a way to do without these for loops?

Here is my code:

Short version of the exported function (DLL)

unsigned short ClassExample::GetData(float **Samples)
{
    for(long i = 0; i < rows; i++)
    {
        try
        {
            if(ch < NB_ROWS_MAX) 
            {
                Samples[ch]][i] = this->Data[ch][i];
            }
            else
            {
                ...
            }
        } ...

Python code

def GetSamples(self, ROWS, COLUMN):

    Data = (ctypes.POINTER(ctypes.c_float) * ROWS)()
    for i in range(COLUMN):
        Data[i] = (ctypes.c_float * COLUMN)()

    try:
        succeed = lib._PyGetData(ClassExampleObj, ctypes.byref(Data))
        if succeed != 0:
            print("Error number :", succeed)
    except:
        print("Unknown Error")

    Data_np = numpy.zeros((ROWS, COLUMN), dtype=float)
    for i in range(ROWS):
        for j in range(COLUMN):
            Data_np[i, j] = float(Data[i][j])
    return Data_np

Thank you !

If your problem is efficiency then you probably are working with very very large datasets.

The easiest way to make a 2-d matrix iteration much faster is to make it 1-d. If you have X columns and Y rows, then instead of accessing matrix[I][j] you can access matrix[i*X+j] , which saves you a dereference, and possibly many cache-misses. If possible, you could even go further and treat the matrix as a 1-d array.

Another time-saving solution is to avoid conditions as much as possible. If you only have conditions inside the for-loop, the compiler will be able to greatly optimize it.

Without seeing more of your code it is hard to tell whether you have an inefficient code or simply handling too much data for your computer, but the guidelines I gave seem to fit from the example you gave.

The last thing I want to mention is that float is quite imprecise and you better use double if possible.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM