omp_get_num_threads()和omp_get_thread_num()返回废话

Mat*_*ath 4 multithreading fortran openmp visual-studio-2015

我刚开始使用英特尔Fortran编译器和Visual Studio 2015在Fortran中使用OpenMP.在项目属性中,我将"Fortran - >语言 - >处理OpenMP指令"设置为"生成并行代码(/ Qopenmp)"

我有一个简单的程序,如下所示:

program hellothreads
   integer threads, id
   call omp_set_num_threads(3)   
   threads = omp_get_num_threads()

   print *,"there are",  threads, "threads"
Run Code Online (Sandbox Code Playgroud)

这产生了

有-2147483648个主题

当然没有.设置线程数似乎可以正常工作,因为:

   !$OMP Parallel private(id) shared(threads)
   threads = omp_get_num_threads()
   id = omp_get_thread_num()
   print *, "hello from thread", id, "out of", threads
   !$OMP end Parallel
Run Code Online (Sandbox Code Playgroud)

输出

你好,来自-2147483648的主题-2147483648

你好,来自-2147483648的主题-2147483648

你好,来自-2147483648的主题-2147483648

并继续:

   !$OMP Parallel private(id) shared(threads)
   threads = omp_get_num_threads()
   id = omp_get_thread_num()
   print *, "this is thread", id, "of", threads
   !$OMP end Parallel
Run Code Online (Sandbox Code Playgroud)

输出

这是-2147483648的主题-2147483648

这是-2147483648的主题-2147483648

最后,如果我在"print"中调用OpenMP函数,则会有不同的奇怪行为:例如:

   !$OMP Parallel private(id) shared(threads)
   print *, "this is thread", omp_get_num_threads(), "of", omp_get_thread_num()
   !$OMP end Parallel
   stop
end
Run Code Online (Sandbox Code Playgroud)

输出

这是NaN的线程NaN

这是NaN的线程NaN

我的配置和/或代码有什么问题?

Vla*_*r F 6

使用implicit none在所有的Fortran程序!

完成后,您将意识到函数未声明并假设为real.将无意义的实际值转换为integer值并存储在您打印的变量中.

正如@francescalus在评论中建议的那样,use omp_lib您使用一个包含正确的函数声明的模块,并帮助您检查是否正确使用它们.