openMP 小程序改错

augustus87 2010-04-12 10:14:04
写了一个计算3在数组里面出现的次数,现在的问题是不能多线程运算,而且统计出来的3的次数是始终不变的,求高手帮忙看一下

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#define N 10000


int main(int argc, char *argv[])
{ int i; int array[N];
int count,nthreads, tid,chunk;
chunk = 100;
count = 0;
double start, end;


start = omp_get_wtime();
#pragma opm parallel shared(nthreads,chunk)
{
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
printf("Starting count with %d threads\n",nthreads);
}
}
#pragma omp for schedule (static,chunk)
for( i = 0; i<N; i++)
{
array[i] = rand() % 10;
if(array[i]==3)
count +=1;
}



end =omp_get_wtime();
printf("The count cost %f sec. time.\n", end- start);
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#define N 10000


int main(int argc, char *argv[])
{ int i; int array[N];
int count,nthreads, tid,chunk;
chunk = 100;
count = 0;
double start, end;


start = omp_get_wtime();
#pragma opm parallel shared(nthreads,chunk)
{
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
printf("Starting count with %d threads\n",nthreads);
}
}
#pragma omp for schedule (static,chunk)
for( i = 0; i<N; i++)
{
array[i] = rand() % 10;
if(array[i]==3)
count +=1;
}



end =omp_get_wtime();
printf("The count cost %f sec. time.\n", end- start);

...全文
135 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
intel_www 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 augustus87 的回复:]

感谢,一个错误拼写,浪费了我一天的时间...
[/Quote]

用intel的编译器就没这种问题了,因为只要一编译马上就会报出这个错误信息。
alex44667416 2010-04-13
  • 打赏
  • 举报
回复
正在弄一个多核的程序设计!
MARK下
蝈蝈俊 2010-04-13
  • 打赏
  • 举报
回复
test
lehedele 2010-04-13
  • 打赏
  • 举报
回复
哦~~
augustus87 2010-04-12
  • 打赏
  • 举报
回复
printf( "The number of 3 appeared in the array are %d", count );
}
最后少了2行
augustus87 2010-04-12
  • 打赏
  • 举报
回复
感谢,一个错误拼写,浪费了我一天的时间...
intel_www 2010-04-12
  • 打赏
  • 举报
回复
#pragma opm parallel shared(array,nthreads,chunk) private(tid,i)

omp 写成 opm 了。
augustus87 2010-04-12
  • 打赏
  • 举报
回复
高手,RANDOM我解决了,现在只剩下程序不能多线程运行了,代码如下,帮我看看,谢谢!
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<omp.h>
#define N 10


int main(int argc, char *argv[])
{ int i; int array[N];
int count,nthreads, tid,chunk;
chunk = 100;
count = 0;
double start, end;



#pragma opm parallel shared(array,nthreads,chunk) private(tid,i)
{
start = omp_get_wtime();
srand( time(NULL) );

tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
printf("Starting count with %d threads\n",nthreads);
}
printf("\nValues of Array:\n");

#pragma omp for schedule (static,chunk)
for( i = 0; i<N; i++)
{

array[i] = rand() % 10;
printf("%d",array[i]);
printf(" ")
if(array[i]==3)
count +=1;

}


}
end =omp_get_wtime();
printf("The count cost %f sec. time.\n", end- start);
printf( "The number of 3 appeared in the array are %d", count );
}


intel_iclifort 2010-04-12
  • 打赏
  • 举报
回复
rand() 本来生成的就是伪随机数, 是不能支持多线程的 (包括OpenMP)

解决办法一: 先用串行的来初始化数据; 二是调用 Intel Math Kernel Library 里支持线程的随机数生成函数, 或者自己找相应的算法来实现随机数

另外, count变量如果不加上相应的保护机制, 会发生Data Race, 结果肯定不正确

567

社区成员

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

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