2

When I run the following example code:

#include "stdio.h"
#include <omp.h>

int main(int argc, char *argv[])
{
  #pragma omp parallel
  {
   int NCPU,tid,NPR,NTHR;
    /* get the total number of CPUs/cores available for OpenMP */
   NCPU = omp_get_num_procs();
   /* get the current thread ID in the parallel region */
   tid = omp_get_thread_num();
   /* get the total number of threads available in this parallel region */
   NPR = omp_get_num_threads();
   /* get the total number of threads requested */
   NTHR = omp_get_max_threads();
   /* only execute this on the master thread! */

   if (tid == 0) {
     printf("%i : NCPU\t= %i\n",tid,NCPU);
     printf("%i : NTHR\t= %i\n",tid,NTHR);
     printf("%i : NPR\t= %i\n",tid,NPR);
   }
   printf("%i : hello multicore user! I am thread %i out of %i\n",tid,tid,NPR);
  }
  return(0);
 }

with the command: gcc -fopenmp example.c -o example.exe then ./example I get the error: libgomp: Thread creation failed: Resource temporarily unavailable However, when I run this same code and command under sudo I get the expected output:

0 : NCPU    = 4
0 : NTHR    = 4
0 : NPR = 4
2 : hello multicore user! I am thread 2 out of 4
1 : hello multicore user! I am thread 1 out of 4
0 : hello multicore user! I am thread 0 out of 4
3 : hello multicore user! I am thread 3 out of 4

Im running Ubuntu 18.04 on x86_64 architecture with 4 cores.

Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               78
Model name:          Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz

I dont really feel comfortable running c code with Openmp as root user. My question is, could someone provide information as to why this may be happening? Thanks

conk
  • 57
  • 1
  • 1
  • 9
  • This is probably a question about Ubuntu's default resource restrictions or something. – R.. GitHub STOP HELPING ICE Nov 17 '18 at 14:38
  • 1
    @R.. Do you know where I could maybe find more information about what you are referring to? – conk Nov 17 '18 at 15:19
  • I'm not a Ubuntu user so I don't know, but I don't see any reason this program should fail as an ordinary user without weird nonstandard restrictions on the normal user. – R.. GitHub STOP HELPING ICE Nov 17 '18 at 15:25
  • Looking into ulimit further I see that my `ulimit -s` was 32388608. when I reduced this limit to 8000 the code ran fine. I am trying to figure out why setting ulimit on the command line would restrict the next executable that is run. Edit: I realize ulimit is obsolete and that `setrlimit()` should be used. – conk Nov 17 '18 at 16:46
  • I get the same error if virtual memory size limit (`ulimit -v`) is too low. – Mikko Rantalainen Apr 07 '20 at 08:12

2 Answers2

5

fix the error :

"libgomp: Thread creation failed: Resource temporarily unavailable"

specialy in systems that have only 1-cpu, only need to reduce threads :

export OMP_NUM_THREADS=1

and go on....

HatLess
  • 10,622
  • 5
  • 14
  • 32
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 10 '21 at 17:02
2

Problem solved! I was assigning the stack limit I need with ulimit -s <stack-size> as opposed to doing it with setrlimit() because I didnt believe that setrlimit() was working.

ulimit -s uses kilobytes and setrlimit() uses bytes. I was trying to assign 32388608 kilobytes rather than bytes!

Running as root allowed me to do this however a regular user Im assuming was not allowed utilize that much memory.

From the setrlimit() man page:

The hard limit acts as a ceiling for the soft limit: an unprivileged process > may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit.

A privileged process ... may make arbitrary changes to either limit value.

conk
  • 57
  • 1
  • 1
  • 9