Skip to content

Commit 32dbeb6

Browse files
committed
Refs #221. Set stack limit to 16MB to prevent a SEGFAULT bug on Mac OS X with DYNAMIC_ARCH=1 & NUM_THREADS=256.
1 parent 5794453 commit 32dbeb6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

driver/others/memory.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105105

106106
#if defined(OS_FREEBSD) || defined(OS_DARWIN)
107107
#include <sys/sysctl.h>
108+
#include <sys/resource.h>
108109
#endif
109110

110111
#if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
@@ -216,6 +217,24 @@ int get_num_procs(void) {
216217
}
217218
return nums;
218219
}
220+
221+
void set_stack_limit(int limitMB){
222+
int result=0;
223+
struct rlimit rl;
224+
rlim_t StackSize;
225+
226+
StackSize=limitMB*1024*1024;
227+
result=getrlimit(RLIMIT_STACK, &rl);
228+
if(result==0){
229+
if(rl.rlim_cur < StackSize){
230+
rl.rlim_cur=StackSize;
231+
result=setrlimit(RLIMIT_STACK, &rl);
232+
if(result !=0){
233+
fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
234+
}
235+
}
236+
}
237+
}
219238
#endif
220239

221240
/*
@@ -1248,11 +1267,18 @@ void CONSTRUCTOR gotoblas_init(void) {
12481267

12491268
if (gotoblas_initialized) return;
12501269

1270+
12511271
#ifdef PROFILE
12521272
moncontrol (0);
12531273
#endif
12541274

12551275
#ifdef DYNAMIC_ARCH
1276+
#if defined(SMP) && defined(OS_DARWIN) && MAX_CPU_NUMBER > 128
1277+
//Set stack limit to 16MB on Mac OS X
1278+
//when NUM_THREADS>128 and DYNAMIC_ARCH=1.
1279+
//Prevent the SEGFAULT bug.
1280+
set_stack_limit(16);
1281+
#endif
12561282
gotoblas_dynamic_init();
12571283
#endif
12581284

0 commit comments

Comments
 (0)