Home > Software design >  Why does the loop in openmp run sequentially?
Why does the loop in openmp run sequentially?

Time:10-25

I try run example for scheduling in openmp, but its work sequentially.

omp_set_num_threads(4);
#pragma omp parallel for schedule(static, 3)
    for (int i = 0; i < 20; i  )
    {
        printf("Thread %d is running number %d\n", omp_get_thread_num(), i);
    }

Result:

Thread 0 is running number 0
Thread 0 is running number 1
Thread 0 is running number 2
Thread 0 is running number 3
Thread 0 is running number 4
Thread 0 is running number 5
Thread 0 is running number 6
Thread 0 is running number 7
Thread 0 is running number 8
Thread 0 is running number 9
Thread 0 is running number 10
Thread 0 is running number 11
Thread 0 is running number 12
Thread 0 is running number 13
Thread 0 is running number 14
Thread 0 is running number 15
Thread 0 is running number 16
Thread 0 is running number 17
Thread 0 is running number 18
Thread 0 is running number 19

How can I get the code to work in parallel?

CodePudding user response:

In Microsoft Visual Studio, OpenMP support is disabled by default. You can enable it with the /openmp compiler option.

This option can be enabled in the project properties, under C/C ->Language->Open MP Support.

  • Related