omp_get_num_threads() 和 omp_get_thread_num() 返回废话

omp_get_num_threads() and omp_get_thread_num() returning nonsense

我刚开始使用英特尔 Fortran 编译器和 Visual Studio 2015 在 Fortran 中使用 OpenMP。在项目属性中,我将 "Fortran -> Language -> Process OpenMP Directives" 设置为 "Generate Parallel Code (/Qopenmp)"

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

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

   print *,"there are",  threads, "threads"

这会产生

there are -2147483648 threads

当然没有。设置线程数似乎没问题,因为:

   !$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

产出

hello from thread -2147483648 out of -2147483648

hello from thread -2147483648 out of -2147483648

hello from thread -2147483648 out of -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

产出

this is thread -2147483648 of -2147483648

this is thread -2147483648 of -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

产出

this is thread NaN of NaN

this is thread NaN of NaN

我的配置 and/or 代码有什么问题?

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

这样做之后你会发现这些函数没有被声明并且假定为 real。废话实际值然后转换为 integer 值并存储在您打印的变量中。

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