5798

OpenMP creates only one thread

Question:

I use Ubuntu and write several lines of code.But it creates only one thread. When I run on my terminal the nproc command, the output is 2. My code is below

int nthreads, tid; #pragma omp parallel private(tid) { tid = omp_get_thread_num(); printf("Thread = %d\n", tid); /* for only main thread */ if (tid == 0) { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); } }

The output:

Thread = 0 Number of threads = 1

How can I do parallelism?

Answer1:

If you are using gcc/g++ you must make sure you enable openmp extensions with the -fopenmp <strong>compiler</strong> and <strong>linker</strong> options. Specifying it during linking will link in the appropriate library (-lgomp).

Compile with something like:

g++ -fopenmp myfile.c -o exec

or:

g++ -c myfile.c -fopenmp g++ -o exec myfile.o -fopenmp

If you leave out the -fopenmp compile option your program will compile but it will run as if openmp wasn't being used. If your program doesn't use omp_set_num_threads to set the number of threads they can be set from the command line:

OMP_NUM_THREADS=8 ./exec

I think the default is is generally the number of cores on a particular system.

Recommend

  • Matrix multiplication by vector OpenMP C [duplicate]
  • OpenMP - for loop thread assignment
  • How can I add each element of one array to another one's corresponding element using a Parallel
  • private variable outside parallel for-loop
  • Matlab multiple cores
  • Clojure Maps, order of key value creation
  • OpenMP average of an array
  • omp parallel for loop (reduction to find max) ran slower than serial codes
  • resetting a Tensorflow graph after OutOfRangeError when using Dataset
  • Laravel Validation Rules If Value Exists in Another Field Array
  • For-loop inside parallel region
  • help with openmp compilation problems
  • MSVC 2015 /Wall has lots of not useful messages
  • XMPP push notifications causing problems (delay + duplications) in messages
  • Interpret strings as packed binary data in C++
  • OpenMP for dependent variables
  • C++: Use input string as variable name
  • How to avoid multiple definition of function (Linux, GCC/G++, Code::Blocks)
  • How can I get the maximum number of OpenMP threads that may be created during the whole execution of
  • Ray-tracing triangles
  • Use of qualified name in function parameter
  • Is C++ compilable with OpenMP and boost on MacOS?
  • How to start server for Selenium grid Java Maven setup
  • How do I compile a C/C++ program through windows command prompt?
  • Number of threads being used during Parallel.ForEach
  • Why cout is producing no output on Code Blocks?
  • configure: error: no acceptable C compiler found in $PATH
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Spray.io: When (not) to use non-blocking route handling?
  • Javascript + PHP Encryption with pidCrypt
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Delete MySQLi record without showing the id in the URL
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Linker errors when using intrinsic function via function pointer
  • Getting Messege Twice Using IMvxMessenger
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?