一个Windows平台下Fortran多线程程序的诡异结果

laughcry2002 laughcry2002 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
一个openmp多线程程序出现的异常结果。最小的示例代码附后。平台:Windows 7 (64 bit), Intel Fortran 编译器(版本附后)

代码 : test_omp.f90


module my_omp_mod
use omp_lib
implicit none
public

integer :: idt, num_threads

contains

subroutine init()
implicit none

num_threads = omp_get_num_threads()
idt = omp_get_thread_num()

write(*, *) "in init(): idt, num_threads = ", idt, num_threads, omp_get_thread_num()
end subroutine

end module

program main
use omp_lib
use my_omp_mod
implicit none

integer :: idt2, num_threads2

!$OMP parallel default(none) private(idt2, num_threads2) private(idt, num_threads)

call init()
write(*, *) "in main(): idt, num_threads = ", idt, num_threads, omp_get_thread_num()

num_threads2 = omp_get_num_threads()
idt2 = omp_get_thread_num()
write(*, *) "in main(): idt2, num_threads2 = ", idt2, num_threads2, omp_get_thread_num()

!$OMP end parallel
end program


编译:

引用
D:\>ifort -openmp test_omp.f90
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version 14.0.2.176 Build 20140130
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

-out:test_omp.exe
-subsystem:console
-defaultlib:libiomp5md.lib
-nodefaultlib:vcomp.lib
-nodefaultlib:vcompd.lib
test_omp.obj


运行:
引用
D:\>set OMP_NUM_THREADS=2

D:\>test_omp.exe
in init(): idt, num_threads = 0 2 0
in main(): idt, num_threads = 0 0 0
in main(): idt2, num_threads2 = 0 2 0
in init(): idt, num_threads = 1 2 1
in main(): idt, num_threads = 0 0 1
in main(): idt2, num_threads2 = 1 2 1


异常出在 module 中定义的 idt 变量取值,在 openmp 指导语句中声明为 private 了,但结果似乎表明在 main 中各线程共享了同一个值(而在 init() 内却是正常的)。

谁能帮忙解释一下?
...全文

相关推荐

CTabCtrl的诡异实例

对论坛讨论的奇怪问题进行了建模。

zkezhon

R语言环境配置:组学分析的第一道硬门槛

R语言不仅是统计编程工具,更是生物信息学中高度耦合的领域专用生态——其底层依赖C/Fortran编译器、中间层由Bioconductor提供1900+经同行评审的生信包(如DESeq2、limma),顶层支撑可重复报告与交互分析。理解R版本、Bioconductor版本与系统级依赖(如BLAS、gfortran)之间的精确匹配关系,是保障RNA-seq等组学流程可复现、可迁移、可回滚的前提。尤其在Mac M系列芯片、Windows Rtools适配、Linux源码编译等典型场景下,环境失败往往源于Fortr

weixin_33743703

IIS-7.5版本中一些诡异问题的解决方案

IIS-7.5版本中一些诡异问题的解决方案

zyl18317183087

隐藏在SQLServer 字段中的超诡异字符解决过程

文章作者:jhkdiy 发表日期:2009-10-11 程序架构:B/S 开发环境:Windows XP + IIS6 + VS2003 数 据 库:SQL Server 2000 部分功能:从 SQL Server 中导出数据到 Access。 这套系统做了大半年,这个导入导出还是问题不断,我负责的这块导入导出就是夹在网 络版和单机版系统之间,只要任何一边对数据库做了改动这个导入导出就会失败。哎,烦心 的事不止这些,最近又遇到了一个非常奇怪的问题。 由于之前的导入导出使用SQL Server 支持的OpenDataSource()函数来做,而当遇到 64位系统时就会遇到不支持Microsof

weixin_38733382

黑幕降临电脑主题 win7版.zip

一款非常好看的动漫主题,在一个黑幕降临的夜晚,一个身着黑色西服,长着一对兔耳朵的男子手里拿着一把勾魂镰刀,非常吓人的样子,感兴趣的朋友可下载安装到自己的电脑上哦! 黑幕降临电脑主题效果:

weixin_39841882
评论
收藏 已收藏
分享
返回顶部