llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Runtime crash with -O0

Open ismail opened this issue 10 years ago • 19 comments

Bugzilla Link 26540
Resolution FIXED
Resolved on Jun 06, 2019 02:53
Version unspecified
OS Linux
Attachments Testcase
CC @alexey-bataev,@AndreyChurbanov,@hahnjo,@hfinkel,@jpeyton52
Fixed by commit(s) r362618

Extended Description

The attached code when compiled with -O0 will fail at runtime:

clang++ -Wno-c++11-narrowing -fopenmp -O0 smallpt.cpp ./a.out 100 Rendering (100 spp) 20.47%[1] 23947 segmentation fault (core dumped) ./a.out 100

Admittedly the code is very ugly but libgomp seems to work so might be an interesting testcase.

ismail avatar Feb 09 '16 12:02 ismail

Fixed by commit r362618.

Confirmed, thank you!

ismail avatar Jun 06 '19 09:06 ismail

Fixed by commit r362618.

AndreyChurbanov avatar Jun 05 '19 16:06 AndreyChurbanov

@​Intel is this worth implementing?

It looks harmless, and not of high priority (because of easy workaround). So, would be good to have it implemented. Might help some users to reduce number of environment variables to set.

AndreyChurbanov avatar Dec 07 '17 17:12 AndreyChurbanov

@​Intel is this worth implementing?

hahnjo avatar Dec 01 '17 15:12 hahnjo

Done

alexey-bataev avatar Feb 11 '16 13:02 alexey-bataev

Well I expected compatibility with libgomp would be nice, but in the end you are right.

It would.

Feel free to close the bug as invalid or similar.

I suggest we change this to "Priority: Enhancement", so we don't forget about it, but also don't cause it to be blocking anything. (Alexey, since you own it, maybe you should do that?)

llvmbot avatar Feb 11 '16 13:02 llvmbot

So, to resolve this bug your suggestion:

So the only time when this might make sense is if

  1. The user hasn't said anything
  2. The initial stack has a finite size

makes the most sense.

Yes, except that I don't really think it's a bug at all.

I agree that it would be friendly to adopt the behaviour you cite, and that may help some people, but, really, the user needs to set an appropriate stacksize for their parallel code just as they do for their serial codes. If they don't, then the code may crash, but that's not due to a bug in the runtime any more than a process exiting because it's used up a CPU limit is a bug in the runtime. Fundamentally, if the user doesn't ask for enough resources to run their code, then it'll fail to run.

Well I expected compatibility with libgomp would be nice, but in the end you are right. Feel free to close the bug as invalid or similar.

ismail avatar Feb 11 '16 13:02 ismail

So, to resolve this bug your suggestion:

So the only time when this might make sense is if

  1. The user hasn't said anything
  2. The initial stack has a finite size

makes the most sense.

Yes, except that I don't really think it's a bug at all.

I agree that it would be friendly to adopt the behaviour you cite, and that may help some people, but, really, the user needs to set an appropriate stacksize for their parallel code just as they do for their serial codes. If they don't, then the code may crash, but that's not due to a bug in the runtime any more than a process exiting because it's used up a CPU limit is a bug in the runtime. Fundamentally, if the user doesn't ask for enough resources to run their code, then it'll fail to run.

llvmbot avatar Feb 11 '16 13:02 llvmbot

  1. What do you do if the default stack is "unlimited"?

Then that's what the user wants.

They may want it, but it's not sanely implementable :-)

For the initial thread, the OS places the stack at a high address, and it grows downwards (at least on X86). Since most of the other things in the address space are at much lower addresses it has a lot of VA space into which to grow, and the kernel treats mappings with the stack bit set as being able to grow downwards. But for thread stacks you can't place 64 of them all at the top, because there's only one top! So, you'd have to explicitly scatter them through the address space, but the OpenMP runtime can't control where the kernel is going to place other stuff (like shared libraries, or heap space) so to guarantee any size for a stack the runtime has to explicitly allocate it, at which point you need to know what that size is, and it's not "unlimited".

So, to resolve this bug your suggestion:

So the only time when this might make sense is if

  1. The user hasn't said anything
  2. The initial stack has a finite size

makes the most sense.

ismail avatar Feb 11 '16 12:02 ismail

  1. What do you do if the default stack is "unlimited"?

Then that's what the user wants.

They may want it, but it's not sanely implementable :-)

For the initial thread, the OS places the stack at a high address, and it grows downwards (at least on X86). Since most of the other things in the address space are at much lower addresses it has a lot of VA space into which to grow, and the kernel treats mappings with the stack bit set as being able to grow downwards. But for thread stacks you can't place 64 of them all at the top, because there's only one top! So, you'd have to explicitly scatter them through the address space, but the OpenMP runtime can't control where the kernel is going to place other stuff (like shared libraries, or heap space) so to guarantee any size for a stack the runtime has to explicitly allocate it, at which point you need to know what that size is, and it's not "unlimited".

llvmbot avatar Feb 11 '16 12:02 llvmbot

As I suspected libgomp is using the system's default stack size retrieved via pthread_attr_getstacksize function. It would make sense for libiomp to do the same at least on Linux.

It's not immediately obvious that that is the right thing to do.

  1. OpenMP specifically allows the user to set the stack size by use of the OMP_STACKSIZE environment variable. This is part of the standard, not an implementation specific tweak.

libgomp lets you override stack size via OMP_STACKSIZE so that's not a problem.

  1. What do you do if the default stack is "unlimited"?

Then that's what the user wants.

  1. In many codes the user may not need as large a stack in the worker threads as they do in the initial one. (Though I suppose this only allocates address-space, not physical memory).

So the only time when this might make sense is if

  1. The user hasn't said anything

Yes.

  1. The initial stack has a finite size

Well, yes.

I guess that may still be worth doing, but it's not a panacea for all stack size problems!

Indeed and I'll admit this is not a real life test case.

Thanks!

ismail avatar Feb 11 '16 12:02 ismail

As I suspected libgomp is using the system's default stack size retrieved via pthread_attr_getstacksize function. It would make sense for libiomp to do the same at least on Linux.

It's not immediately obvious that that is the right thing to do.

  1. OpenMP specifically allows the user to set the stack size by use of the OMP_STACKSIZE environment variable. This is part of the standard, not an implementation specific tweak.

  2. What do you do if the default stack is "unlimited"?

  3. In many codes the user may not need as large a stack in the worker threads as they do in the initial one. (Though I suppose this only allocates address-space, not physical memory).

So the only time when this might make sense is if

  1. The user hasn't said anything
  2. The initial stack has a finite size

I guess that may still be worth doing, but it's not a panacea for all stack size problems!

llvmbot avatar Feb 11 '16 12:02 llvmbot

Looks like this test case needs some stack size for OpenMP worker threads, and the default value of 4MB (on Linux 64-bit) is not enough. Try to set bigger stack size by setting environment variable KMP_STACKSIZE to 8M or bigger. The 8M works for me running with parameter 1000.

I don't know how much stack size libgomp gives its worker threads, probably more than libomp.

Indeed I wonder if libgomp uses the default stack size of the system:

λ ulimit -s 8192

which shows 8MB of default stack.

As I suspected libgomp is using the system's default stack size retrieved via pthread_attr_getstacksize function. It would make sense for libiomp to do the same at least on Linux.

ismail avatar Feb 11 '16 10:02 ismail

Looks like this test case needs some stack size for OpenMP worker threads, and the default value of 4MB (on Linux 64-bit) is not enough. Try to set bigger stack size by setting environment variable KMP_STACKSIZE to 8M or bigger. The 8M works for me running with parameter 1000.

I don't know how much stack size libgomp gives its worker threads, probably more than libomp.

Indeed I wonder if libgomp uses the default stack size of the system:

λ ulimit -s 8192

which shows 8MB of default stack.

ismail avatar Feb 10 '16 19:02 ismail

Looks like this test case needs some stack size for OpenMP worker threads, and the default value of 4MB (on Linux 64-bit) is not enough. Try to set bigger stack size by setting environment variable KMP_STACKSIZE to 8M or bigger. The 8M works for me running with parameter 1000.

I don't know how much stack size libgomp gives its worker threads, probably more than libomp.

AndreyChurbanov avatar Feb 10 '16 18:02 AndreyChurbanov

Hi, could you provide more details about system, environment etc.? Tried to reproduce on MacOS 10.11 and Linux x86_64 with no luck, the code works without any crashes

This is on Linux x86-64 with kernel v4.4.0, just make sure you run the executable with a number >= 100

such as

./a.out 100

if you still can't produce try a higher number like 1000.

Ahh, missed that. Yes, I see a crash.

alexey-bataev avatar Feb 10 '16 10:02 alexey-bataev

Hi, could you provide more details about system, environment etc.? Tried to reproduce on MacOS 10.11 and Linux x86_64 with no luck, the code works without any crashes

This is on Linux x86-64 with kernel v4.4.0, just make sure you run the executable with a number >= 100

such as

./a.out 100

if you still can't produce try a higher number like 1000.

ismail avatar Feb 10 '16 09:02 ismail

Hi, could you provide more details about system, environment etc.? Tried to reproduce on MacOS 10.11 and Linux x86_64 with no luck, the code works without any crashes

alexey-bataev avatar Feb 10 '16 08:02 alexey-bataev

assigned to @AndreyChurbanov

ismail avatar Feb 09 '16 12:02 ismail