2

I want to run this program :

#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
    int numThread, myId;
    cout << "num_procs=" << omp_get_num_procs();
    omp_set_num_threads(omp_get_num_procs());
    #pragma omp parallel 
    {
        cout << "\nid=" << omp_get_thread_num();
        numThread = omp_get_num_threads();
        cout << "\nmax-thread=" << omp_get_max_threads();
    }
    getchar();
}

The result is:

num_procs=4
id=0
max-thread=4

I think this result must be repeat and print 4 times but I don't know why it prints only one time.

I run below code from this comment in this post and my result is different.

#include <iostream>
#include <omp.h>
int main(int argc, const char * argv[])
{
    int nProcessors = omp_get_max_threads();
    std::cout << nProcessors << std::endl;
    omp_set_num_threads(nProcessors);
    std::cout << omp_get_num_threads() << std::endl;

    #pragma omp parallel for 
    for (int i = 0; i<5; i++){
        int tid = omp_get_thread_num();
        std::cout << tid << "\t tid" << std::endl;
        int nThreads = omp_get_num_threads();
        std::cout << nThreads << "\t nThreads" << std::endl;
    }
    exit(0);
}

print this result :

4
1
0       tid
1       nThreads
0       tid
1       nThreads
0       tid
1       nThreads
0       tid
1       nThreads
0       tid
1       nThreads

I run this command in cmd :

set OMP_NUM_THREADS=16

and when i run :

set OMP_NUM_THREADS in cmd 

print this result: OMP_NUM_THREADS=16

but when I close cmd and reopen it and run set OMP_NUM_THREADS print this result:

Environment variable OMP_NUM_THREADS not defined !!!!!!!!Please Help me.

Community
  • 1
  • 1
rijisoft
  • 57
  • 2
  • 13

2 Answers2

0

Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.

setx OMP_NUM_THREADS 16

doqtor
  • 8,414
  • 2
  • 20
  • 36
0

thanks from doqtor for answer to me , i learn two things here and it is very good. answer for my question is 1. use setx command for persist Environment varaiable 2.enable openmp support in visual studio 2013: VIEW->Other Windows->Property Manager then then right click on the Property Manager and Configuration Properties -> C/C++ -> Language -> OpenMP Support set Yes!

Community
  • 1
  • 1
rijisoft
  • 57
  • 2
  • 13