0

For example, When I multithread a "for loop" using OpenMP, making the iterations as different threads, How does it get translated to Assembly?

Also can a multithreaded code run on hardware supporting multithreading without any OS being involved?

I am trying to understand in the most basic sense, how multithreading code is compiled using the frameworks and how is it run on the hardware?

9
  • 1
    You can inspect assembly with godbolt.org. As you see in the example I linked, the compiler transforms the parallelized code into a helper function and then lets the openmp helper library libgomp call it via a function pointer. Other compilers may use other patterns but function pointers should be the norm. Commented Jun 2 at 7:34
  • 2
    Threads themselves are managed by the operating system and requested by the application through OS-specific libraries like POSIX pthreads or the Win32 runtime libraries. How that is done in detail is too broad a question to answer on SO. A user-space application does not have the relevant permissions to manipulate the CPU state enough to launch new threads without the OS's involvement. Commented Jun 2 at 7:40
  • Btw, please note that on all mainstream OS, thread scheduling is performed by the OS (with some CPU features to make it faster and safer). Thus, threads are necessarily an OS resource. There are user threads, but they cannot be used for parallelism without OS thread and are not used by OpenMP implementation either. Commented Jun 2 at 8:19
  • Please also note that omp for does not create the threads. It only distribute the work to them. Threads are created in omp parallel sections (often even only once since they can be recycled). omp parallel for is equivalent to the section omp parallel + omp for. Multiple omp for can be done in a single omp parallel section (which is frequent and a good idea). Commented Jun 2 at 8:24
  • Last but not least: there is not one unique OpenMP implementation but several ones. For example there is GOMP (for GCC), and IOMP (for ICC/ICX). See openmp.org/resources/openmp-compilers-tools for a list of compiler supporting OpenMP (most compiler have their own implementation though not all: the one of ICX and LLVM are AFAIK shared, MSVC have its own implementation but support the LLVM one). Commented Jun 2 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.