Giuseppe Levi
Giuseppe Levi

Reputation:

segmentation fault using OMP

all! Typing from Italy This little piece of code works if the matrix size is less then 800 and fails with a segmentation fault for higher sizes.... I have tried it with gcc 4.3.2 compiler in linux and macosx and VisualStudio compiler in windows. Seemsthe problem is in the stack size..... how can I increase it ? How can I solve the problem for bigger matrix sizes ? The code works fine in serial put fails in parallel execution. Thanks.

#include <omp.h>
#include <stdio.h>
#define Nu 4000
int main() {
float A[Nu][Nu],B[Nu][Nu],C[Nu][Nu];
int i,j;
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
#pragma omp parallel for private(j,i) shared(A,B,C) schedule(static)
for(j=0;j<Nu;j++){
for(i=0;i<Nu;i++){
//printf("Hello from thread %d, i,j %d %d\n", omp_get_thread_num(),i,j );
A[i][j]=0;
B[i][j]=0;
C[i][j]=0;

}}

}

Upvotes: 2

Views: 4170

Answers (4)

darklon
darklon

Reputation: 478

OpenMP threads are created with a stack size defined by the OMP_STACKSIZE environment variable (this is standard from OpenMP 3.0 on). If the environment variable is not present, the default stack size is implementation specific. You should probably rather be using the heap for such large allocations, but there can be legit reasons for wanting to change OpenMP threads' stack size.

Upvotes: 0

eyberg
eyberg

Reputation: 3260

you can adjust the stack size through the shell with

'ulimit -s newstacksize'

-- try 1000000

Upvotes: 0

CesarB
CesarB

Reputation: 45595

Do you really have to allocate the matrix on the stack?

You could use the heap instead. For large amounts of memory, it can even be more efficient (the allocator implementation can use things such as anonymous mmap which allow the memory to be released back to the operating system when you deallocate it).

Upvotes: 2

schnaader
schnaader

Reputation: 49731

Default stack size in OpenMP is 4-8 MB. There's an environment variable called STACKSIZE you can change (for example to 16384, which is 16 MB). See chapter 5-5 of this PDF.

Upvotes: 2

Related Questions