字典初始化python的seg断层

发布于 2025-01-25 23:51:06 字数 1667 浏览 4 评论 0原文

因此,我正在研究一个处理大量车辆和这些车辆之间的变速器的项目。我有一个工作代码,可以很好地适用于少量车辆,但是当我开始使用大量〜500辆车时,该程序将使故障大约一半时间。我已经使用faulthandler()回溯了SEG故障,并且字典初始化是程序当前故障的位置。这是字典初始化的代码:

for j in range(self.num_vehicles):  # for each vehicle
        if self.timeIndex == 0:
            self.vehicles[j].pseudo_random_number(0)
    self.seconds = []
    for j in range(500):
        mso = self.vehicles[j].full_mso_range()
        vehicle_transmissions = []
        for k in range(500):
            vehicle_transmissions.append(0)
        # Iterate through all of the other vehicles besides vehicle j
        for k in range(j+1, j+500):
            distance = float(np.ceil(np.sqrt(
                (self.vehicles[j].position[0] - self.vehicles[k % self.num_vehicles].position[0]) ** 2.0 +
                (self.vehicles[j].position[1] - self.vehicles[k % self.num_vehicles].position[1]) ** 2.0 +
                (self.vehicles[j].position[2] - self.vehicles[k % self.num_vehicles].position[2]) ** 2.0)))
            
            transmission = {"receiverID": int(k%500),
                            "distance": 0,
                            "receivedpower": 0,
                            "successfuldecode": 0}
            
            vehicle_transmissions[transmission["receiverID"]] = transmission  # store transmission
        self.vehicles[j].transmissions = vehicle_transmissions.copy()  # save transmission to vehicle
        self.vehicles[j].frame += 1  # increase frame
        self.seconds.append(mso)  # record MSO in a list that helps with collisions

分配传输的情况下发生SEG故障。我试图简化分配,因此请尝试隔离故障,但到目前为止我还没有运气。任何建议将不胜感激。

另外,该代码是通过C ++代码调用的,而GDB仅告诉我该功能中的SEG故障正在发生。

So I am working on a project that deals with a large number of vehicles and transmissions between those vehicles. I have a working code that works well for small numbers of vehicles, but when I start using large numbers ~500 vehicles then the program will seg fault about half the time. I have backtraced the seg fault using faulthandler() and the dictionary initialization is where the program currently faults. Here is the code for the dictionary initialization:

for j in range(self.num_vehicles):  # for each vehicle
        if self.timeIndex == 0:
            self.vehicles[j].pseudo_random_number(0)
    self.seconds = []
    for j in range(500):
        mso = self.vehicles[j].full_mso_range()
        vehicle_transmissions = []
        for k in range(500):
            vehicle_transmissions.append(0)
        # Iterate through all of the other vehicles besides vehicle j
        for k in range(j+1, j+500):
            distance = float(np.ceil(np.sqrt(
                (self.vehicles[j].position[0] - self.vehicles[k % self.num_vehicles].position[0]) ** 2.0 +
                (self.vehicles[j].position[1] - self.vehicles[k % self.num_vehicles].position[1]) ** 2.0 +
                (self.vehicles[j].position[2] - self.vehicles[k % self.num_vehicles].position[2]) ** 2.0)))
            
            transmission = {"receiverID": int(k%500),
                            "distance": 0,
                            "receivedpower": 0,
                            "successfuldecode": 0}
            
            vehicle_transmissions[transmission["receiverID"]] = transmission  # store transmission
        self.vehicles[j].transmissions = vehicle_transmissions.copy()  # save transmission to vehicle
        self.vehicles[j].frame += 1  # increase frame
        self.seconds.append(mso)  # record MSO in a list that helps with collisions

The Seg Fault occurs where the transmission is allocated. I have tried to simplify the allocation so try and isolate the fault but I have had no luck so far. Any advice would be appreciated.

Also this code is called through a c++ code and gdb only tells me that the seg fault is happening in this function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2025-02-01 23:51:06

问题最终是Python到C ++接口的错误。它只有在有大量呼叫时才发生,它似乎在内存中失去了位置,并且访问该点再次遇到错误时。我最终只是在解决问题的C ++中编写完整的代码。如果其他人读到这一点,则更容易从C ++读取,该C ++将调用Python而不是调用C ++。

The problem ended up being a error in the python to C++ interface. It only happens when there is a large number of calls, it seems to lose its place in memory and when it accesses that point again hits an error. I ended up just writing the complete code in C++ which solves the problem. If anyone else reads this its easier to go from C++ which calls Python rather than Python calling C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文