Übergeben eines numpy 3D -Arrays an eine C -Funktion mit einem dreifachen Zeiger als ArgumentPython

Python-Programme
Anonymous
 Übergeben eines numpy 3D -Arrays an eine C -Funktion mit einem dreifachen Zeiger als Argument

Post by Anonymous »

Ich experimentiere mit der Verwendung des CTypes -Pakets in Python. Derzeit versucht es, ein 3D -Numpy -Array an eine C -Funktion zu übergeben, die einen dreifachen Zeiger als Argument nimmt und die Werte der Elemente des Arrays aktualisiert. Die C-Funktion ist: < /p>
void effect_array(int ***ptr, int rows, int cols, int depth)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
for(int k = 0; k < depth; k++)
{
ptr[j][k] *= 2;
}
}
}
}
< /code>
Derzeit habe ich versucht: < /p>
import ctypes as ct
import numpy as np

arr = np.array([
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
],
[
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]
]
])

arr_c = np.ascontiguousarray(arr)

lib = ct.CDLL("path/to/share_object.so")
_effect_array = lib.effect_array
_effect_array.argtypes = [ct.POINTER(ct.POINTER(ct.POINTER(ct.c_int))), ct.c_int, ct.c_int, ct.c_int]
_effect_array.restype = None

rows, cols, depth = arr.shape
t_ptr = (ct.POINTER(ct.POINTER(ct.c_int)) * rows)()
for i in range(rows):
t_ptr = (ct.POINTER(ct.c_int) * cols)()
for j in range(cols):
t_ptr[j] = arr_c[j].ctypes.data_as(ct.POINTER(ct.c_int))

print("Original array =")
print(arr_c)
print()
_effect_array(t_ptr, rows, cols, depth)
print("Array after pass to function =")
print(arr_c)
< /code>
Dies hat zu einer Ausgabe von: < /p>
geführtOriginal array =
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]

Array after pass to function =
[[[ 2 4 3 4]
[10 12 7 8]
[18 20 11 12]]

[[26 28 15 16]
[34 36 19 20]
[42 44 23 24]]]

< /code>
Was ich gerne hätte, wäre: < /p>
Original array =
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]

Array after pass to function =
[[[ 2 4 6 8]
[10 12 14 16]
[18 20 22 24]]

[[26 28 30 32]
[34 36 38 40]
[42 44 46 48]]]
< /code>
Ich bin mir nicht sicher, warum die C -Funktion nicht über die ersten beiden in Folge auf die Elemente zugreifen kann. Meinerverstand = "Lang-C PrettyPrint-Override">#include
#include

void effect_array(int ***ptr, int rows, int cols, int depth)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
for(int k = 0; k < depth; k++)
{
ptr[j][k] *= 2;
}
}
}
}

int main()
{
int arr[2][2][2] = {
{
{1,2},
{3,4}
},
{
{5,6},
{7,8}
}
};

int arr2[5];
int *p = arr2;

int ***ptr = (int ***)malloc(2 * sizeof(int **));
for(int i = 0; i < 2; i++)
{
ptr = (int **)malloc(2 * sizeof(int *));
for(int j = 0; j < 2; j++)
{
ptr[j] = &arr[j][0];
}
}

printf("Print array before:\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("%d ", arr[j][k]);
}
printf("\n");
}
printf("\n");
}

effect_array(ptr, 2, 2, 2);

printf("Print array after:\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
for(int k = 0; k < 2; k++)
{
printf("%d ", arr[j][k]);
}
printf("\n");
}
printf("\n");
}

for(int i = 0; i < 2; i++) free(ptr[i]);
free(ptr);

return 0;
}
< /code>
Dies basiert auch auf der Diskussion über eine frühere Frage, die ich für das Beispiel für das funktionierende C -Beispiel für @sr143 zugute kommt. wird sehr geschätzt.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post