openMP is not creating threads in visual studio

后端 未结 4 1908
旧时难觅i
旧时难觅i 2020-12-06 16:26

My openMP version did not give any speed boost. I have a dual core machine and the CPU usage is always 50%. So I tried the sample program given in Wiki. Looks like the openM

相关标签:
4条回答
  • 2020-12-06 17:06

    There's nothing wrong with the program - so presumably there's some issue with how it's being compiled or run. Is this VS2008 Pro? A quick google around suggests OpenMP is not enabled in Standard. Is OpenMP enabled in Properties -> C/C++ -> Language -> OpenMP? (Eg, are you compiling with /openmp)? Is the environment variable OMP_NUM_THREADS being set to 1 somewhere when you run this?

    0 讨论(0)
  • 2020-12-06 17:11

    Why would you need more than one thread for that program? It's clearly the case that OpenMP realizes that it doesn't need to create an extra thread to run a program with no loops, no code that could run in parallel whatsoever.

    Try running some parallel stuff with OpenMP. Something like this:

    #include <omp.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define CHUNKSIZE   10
    #define N       100
    
    int main (int argc, char *argv[]) 
    {
    int nthreads, tid, i, chunk;
    float a[N], b[N], c[N];
    
    /* Some initializations */
    for (i=0; i < N; i++)
      a[i] = b[i] = i * 1.0;
    chunk = CHUNKSIZE;
    
    #pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)
      {
      tid = omp_get_thread_num();
      if (tid == 0)
        {
        nthreads = omp_get_num_threads();
        printf("Number of threads = %d\n", nthreads);
        }
      printf("Thread %d starting...\n",tid);
    
      #pragma omp for schedule(dynamic,chunk)
      for (i=0; i<N; i++)
        {
        c[i] = a[i] + b[i];
        printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
        }
    
      }  /* end of parallel section */
    
    }
    

    If you want some hard core stuff, try running one of these.

    0 讨论(0)
  • 2020-12-06 17:17

    If you want to test out your program with more than one thread, there are several constructs for specifying the number of threads in an OpenMP parallel region. They are, in order of precedence:

    • Evaluation of the if clause
    • Setting of the num_threads clause
    • Use of the omp_set_num_threads() library function
    • Setting of the OMP_NUM_THREADS environment variable
    • Implementation default

    It sounds like your implementation is defaulting to one thread (assuming you don't have OMP_NUM_THREADS=1 set in your environment).

    To test with 4 threads, for instance, you could add num_threads(4) to your #pragma omp parallel directive.

    As the other answer noted, you won't really see any "speedup" because you aren't exploiting any parallelism. But it is reasonable to want to run a "hello world" program with several threads to test it out.

    0 讨论(0)
  • 2020-12-06 17:19

    As mentioned here, http://docs.oracle.com/cd/E19422-01/819-3694/5_compiling.html I got it working by setting the environment variable OMP_DYNAMIC to FALSE

    0 讨论(0)
提交回复
热议问题