我最近使用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)
答案 0 :(得分:1)
我最终做的是将1D数组传递给C ++,并为它编写了一个包装类来实现从3D到1D坐标的转换。我的实际程序有其内部数据结构。完成工作后,我将包装器传递给主类,并将其状态复制到包装的1D缓冲区。