[英]Passing 3D numpy array from cython to C++
我最近使用cython加速了一個應用程序,現在很難將一個3D numpy數組從cython傳遞到C ++函數。 我可以從python測試腳本調用我的函數,但它是segfauls。 當我自己測試我的C ++時,它沒有。 因此,我認為我正確地傳遞數組有問題。
那里出了什么問題?
>> python test_harvest.py
(100, 100) I twerk
[1] 6771 segmentation fault (core dumped) python test_harvest.py
logic.pyx
import cython
import numpy as np
cimport numpy as np
cdef extern from "fast_harvest.h":
void start_harvest(int *** data , int x, int y, int t, int n)
def harvest(np.ndarray[int, ndim=3, mode="c"] data not None,
int goal_x,
int goal_y,
int mission_time,
int number_of_robots):
m, n, o = data.shape[0], data.shape[1], data.shape[2]
assert m == mission_time
assert n == number_of_robots
assert o == 2
start_harvest (<int ***> data.data,
goal_x, goal_y,
mission_time,
number_of_robots)
fast_harvest.cpp
#include <iostream>
#include <cstdio>
#include "fast_harvest.h"
#include "Harvester.h"
using std::cout;
using std::endl;
void start_harvest(int ***data, int x, int y, int mission_time, int number_of_robots) {
Point p(x,y);
p.dump();
cout << "I twerk" << endl;
for(int n = 0; n < number_of_robots; n++) {
int xpos = data[0][n][0];
int ypos = data[0][n][1];
printf("(%d, %d)\n", xpos, ypos);
}
}
test_harvest.py
import numpy as np
import fharvest.logic as fhl
ROBO_COUNT = 2
MISSION_TIME = 20
GOAL_X = 100
GOAL_Y = 100
data = np.zeros([MISSION_TIME, ROBO_COUNT, 2], dtype=int)
data[0][0] = 0, 200
data[0][1] = 200, 0
fhl.harvest(data, GOAL_X, GOAL_Y, MISSION_TIME, ROBO_COUNT)
print(data)
我最終做的是將1D數組傳遞給C ++,並為它編寫了一個包裝類來進行從3D到1D坐標的轉換。 我的實際程序有其內部數據結構。 完成工作后,我將包裝器傳遞給主類,並將其狀態復制到包裝的1D緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.