Ad
Passing Objects By Reference In Python With Ctypes
I am trying to use ctypes to interface with c code. I am trying to pass obj by reference to c and I am using ndpointer function from numpy to pass to the function.
My C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void sum(int *a, int *b, int *c, size_t m) {
int ix;
for (ix=0; ix<m; ix++) {
printf("%d: (%d,%d)\n",ix, a[ix], b[ix]);
c[ix] = a[ix] + b[ix];
}
}
Compiled it to a shared library on ubuntu 20.04 using
gcc -Wall -fPIC -shared -pedantic -shared test.c -o test.so
And my python wrapper is:
import ctypes
from numpy.ctypeslib import ndpointer
import numpy as np
add = ctypes.CDLL('testing_ctypes/test.so').sum
add.argtypes = [ndpointer(dtype=np.int64, ndim=1, flags="aligned, c_contiguous"), ndpointer(dtype=np.int64, ndim=1, flags="aligned, c_contiguous"), ndpointer(dtype=np.int64, ndim=1,flags="aligned, c_contiguous"), ctypes.c_size_t]
add.restype = None
def test_add(a, b):
c = np.array([0,0,0], dtype=np.int64)
add(a,b,c,5)
return c
op = test_add(np.array([1, 10, 5], np.int64), np.array([5, 4, 6], np.int64))
When I run this code I get the output:
0: (1,5)
1: (0,0)
2: (10,4)
3: (0,0)
4: (5,6)
Why are the values not contiguous? I tried different flags given in numpy reference but none of them have any effect on output. I am super confused why this would be the output. Any help is appreciated!
Ad
Answer
Your C function takes int *
arguments but your np.array
is using 64-bit integers. In C, int
is 4 bytes (32-bit). Try np.int32
.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad