关于OpenMP小程序测试结果的疑问

dimire 2008-05-28 10:52:12
今天装上了intel C++ compiler 9.0编译器,并且编了个小的测试程序
#include <stdio.h>
#include "omp.h"

int main(int argc, char* argv[])
{
int i;
#pragma omp parallel for
for (i = 0; i < 10; i++ )
{
printf("i = %d\n", i);
}
return 0;
}

这个程序理想的打印结果应该是乱序的0到9数字,可我每次运行的时候,能输出正常的结果,但有时候又会出现重复输出两个数字的结果,比如
i = 0
i = 1
i = 2
i = 3
i = 3
i = 5
i = 4
i = 6
i = 7
i = 8
i = 9
Press any key to continue

i = 0
i = 0
i = 5
i = 6
i = 7
i = 1
i = 8
i = 2
i = 3
i = 9
i = 4
Press any key to continue
这是什么原因呢?
...全文
313 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
intel_www 2008-06-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dimire 的回复:]
对于上面的那个程序,我把变量i的定义放在了for语句中用icc编译就有错,而用VC编译器就能没问题
int main(int argc, char* argv[])
{
//int i;
#pragma omp parallel for
for (int i = 0; i < 10; i++ )
{
printf("i = %d\n", i);
}
return 0;
}

显示错误信息如下:
icl openmp.c
icl: warning: problem with Microsoft compilation of 'D:\openmp\openmp.c'
D:\openmp\openmp.c(126): error: expe…
[/Quote]

这涉及C和C++语言标准的问题。你把文件后缀名改成CPP就行了。
dimire 2008-05-30
  • 打赏
  • 举报
回复
对于上面的那个程序,我把变量i的定义放在了for语句中用icc编译就有错,而用VC编译器就能没问题
int main(int argc, char* argv[])
{
//int i;
#pragma omp parallel for
for (int i = 0; i < 10; i++ )
{
printf("i = %d\n", i);
}
return 0;
}

显示错误信息如下:
icl openmp.c
icl: warning: problem with Microsoft compilation of 'D:\openmp\openmp.c'
D:\openmp\openmp.c(126): error: expected an expression
for (int i = 0; i < 10; i++ )
^
D:\openmp\openmp.c(126): error: identifier "i" is undefined
for (int i = 0; i < 10; i++ )
^
D:\openmp\openmp.c(126): error: OpenMP for-init does not conform
for (int i = 0; i < 10; i++ )
^
D:\openmp\openmp.c(126): error: OpenMP for-test does not conform
for (int i = 0; i < 10; i++ )
^
D:\openmp\openmp.c(126): error: OpenMP for-incr does not conform
for (int i = 0; i < 10; i++ )

是不是intel编译器不支持这样的变量定义?我看别人写的Openmp测试程序,有在for语句中定义并初始化变量的。请问大家有没遇到这种情况?
intel_www 2008-05-29
  • 打赏
  • 举报
回复
检查使用的C runtime library是不是多线程版本。
dimire 2008-05-29
  • 打赏
  • 举报
回复
问题解决了,谢谢大家指点:)
intel_jeff 2008-05-29
  • 打赏
  • 举报
回复
楼上有笔误,应该是
你使用了 /ML 或 /MLd (即单线程版本库)
应该使用多线程版本库,如 /MD, /MDd, /MT, /MTd
intel_jeff 2008-05-29
  • 打赏
  • 举报
回复
你选择的链接库类型不对,应该选择多线程版本,即/ML或/MLd

看我在4核机器上的结果:

============================
链接多线程版本的库,屏幕输出正确
============================
C:\>icl /Qopenmp /MD hello.c
Intel(R) C++ Compiler for applications running on IA-32, Version 10.1 Build 20080212 Package ID: w_cc_p_10.1.020
Copyright (C) 1985-2008 Intel Corporation. All rights reserved.

hello.c
hello.c(7): (col. 1) remark: OpenMP DEFINED LOOP WAS PARALLELIZED.
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

-out:hello.exe
-nodefaultlib:libguide_stats.lib
-nodefaultlib:libguide40_stats.lib
-defaultlib:libguide40.lib
hello.obj

C:\>hello
i = 0
i = 6
i = 7
i = 1
i = 2
i = 3
i = 4
i = 5
i = 8
i = 9

============================
链接单线程版本的库,屏幕输入有重复,这是由于单线程库中的printf是不可重入的,但是某些时刻却被多个线程同时调用,因此出现重复打印
============================
C:\>icl /Qopenmp /ML hello.c
Intel(R) C++ Compiler for applications running on IA-32, Version 10.1 Build 20080212 Package ID: w_cc_p_10.1.020
Copyright (C) 1985-2008 Intel Corporation. All rights reserved.

hello.c
hello.c(7): (col. 1) remark: OpenMP DEFINED LOOP WAS PARALLELIZED.
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

-out:hello.exe
-nodefaultlib:libguide_stats.lib
-nodefaultlib:libguide40_stats.lib
-defaultlib:libguide.lib
hello.obj

C:\>hello
i = 0
i = 3
i = 6
i = 6
i = 8
i = 1
i = 4
i = 7
i = 9
i = 2
i = 5

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧