OpenMP和文件I / O [英] OpenMP and File I/O

查看:113
本文介绍了OpenMP和文件I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码上做了一些时间试验,从逻辑上看,使用OpenMP进行并行化似乎非常容易,因为每个试验都独立于其他试验。现在,我的代码如下所示:

  for(int size = 30; size< 50; ++ size ){
#pragma omp parallel for
for(int trial = 0; trial< 8; ++ trial){
time_t start,end;
//初始化
时间(& start);

//执行计算

时间(& end);

输出<<大小<< \t<< difftime(结束,开始)< ENDL;
}
输出<< ENDL;





$ b我有一个偷偷的怀疑,这是一种失礼,但是,因为两个线程可能同时向输出写入值,从而导致格式化。这是一个问题,如果是的话,将围绕输出<<大小<< ... 使用代码#pragma omp critical 语句修复它?

解决方案

不要介意你的输出是否会被搞砸(很可能会)。除非你非常小心地将OpenMP线程分配给不共享内存带宽等资源的不同处理器,否则您的时间测试也不是很有意义。不同的运行会相互干扰。

解决问题的方法是将结果写入数组的指定元素,对于每一个审判,并输出结果后的事实。


I'm doing some time trials on my code, and logically it seems really easy to parallelize with OpenMP as each trial is independent of the others. As it stands, my code looks something like this:

for(int size = 30; size < 50; ++size) {
    #pragma omp parallel for
    for(int trial = 0; trial < 8; ++trial) {
        time_t start, end;
        //initializations
        time(&start);

        //perform computation

        time(&end);

        output << size << "\t" << difftime(end,start) << endl;
    }
    output << endl;
}

I have a sneaking suspicion that this is kind of a faux pas, however, as two threads may simultaneously write values to the output, thus screwing up the formatting. Is this a problem, and if so, will surrounding the output << size << ... code with a #pragma omp critical statement fix it?

解决方案

Never mind whether your output will be screwed up (it likely will). Unless you're really careful to assign your OpenMP threads to different processors that don't share resources like memory bandwidth, your time trials aren't very meaningful either. Different runs will be interfering with each other.

The solution to the problem you're asking about is to write the result times into designated elements of an array, with one slot for each trial, and ouput the results after the fact.

这篇关于OpenMP和文件I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆