解决由于非线程安全的API函数而并行发生的分段错误 [英] Working around segmentation faults that occur in parallel due to a non-thread-safe API function

查看:123
本文介绍了解决由于非线程安全的API函数而并行发生的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用MATLAB编写MEX文件,以使用MATLAB 2012a中的parfor函数并行运行实验. MEX文件执行一些非常简单的数字任务,但依赖于IBM的CPLEX 12.4 API.

I am currently coding a MEX file in MATLAB to run experiments in parallel using the parfor function in MATLAB 2012a. The MEX file does some very straightforward numerical tasks but relies on the CPLEX 12.4 API from IBM.

尽管我的MEX文件按顺序工作,但我将不可避免地收到随机当我并行运行时出现细分错误.将分段错误的堆栈跟踪发送给MATLAB之后,他们建议该错误源自C库中的"putenv()"函数,这显然不是线程安全的.

Although my MEX file works sequentially, I will inevitably receive "random" segmentation fault when I run in in parallel. After sending a stack trace of the segmentation fault to MATLAB, they have suggested that the error originates from the "putenv()" function from the C library, which is apparently not thread-safe.

我没有在MEX代码中使用putenv()函数,但是事实证明,我绝对必须从CPLEX 12.4调用的函数之一确实在使用它.我想知道是否有什么办法可以避免由于此功能而导致的分段错误.以前有人建议锁住我的位"和使用信号量",但在涉及这些概念时,我的想法实在不多.

I do not use the putenv() function in my MEX code, but it turns out that one of functions that I absolutely have to call from the CPLEX 12.4 does use it. I'm wondering if there is anything I could do to avoid the segmentation faults that come as a result of this function. Someone previously suggested "locking my bits" and "using semaphores" but I'm really little over my head when it comes to these concepts.

任何建议或指导将不胜感激.

Any advice or direction would be very much appreciated.

推荐答案

事实证明,由于我同时在我的MATLAB代码中使用CPLEX MATLAB API和在我的MEX代码中使用CPLEX C API,因此发生了冲突.这两个API都使用putenv()函数,该函数不是线程安全的.特别是,当两个线程尝试同时使用putenv()函数(在MEX文件或MATLAB代码中)时,就会发生崩溃.

It turns out that the violation occurs since I use the CPLEX MATLAB API in my MATLAB code and the CPLEX C API in my MEX code at the same time. Both APIs use the putenv() function, which is not thread-safe. In particular, the crash occurs whenever two threads try to use the putenv() function at the same time (either in the MEX file or the MATLAB code).

解决方法是使用该软件包,并在C和MATLAB中使用putenv()的函数周围(即在MATLAB中C/Cplex()中的CPXopenCPLEX)周围添加一个mutex_lock/mutex_unlock.可以在在CPLEX论坛上的后续帖子

The fix is to use the package and add a mutex_lock / mutex_unlock around the functions that use putenv() in C and MATLAB (i.e. CPXopenCPLEX in C / Cplex() in MATLAB). More information and exact code to create the mutex_lock / mutex_unlock can be found in the following post on the CPLEX forums

这篇关于解决由于非线程安全的API函数而并行发生的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆