openmp - 用于文本文件读取和使用管道的 while 循环 [英] openmp - while loop for text file reading and using a pipeline

查看:44
本文介绍了openmp - 用于文本文件读取和使用管道的 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 openmp 不支持 while 循环(或者至少不太喜欢它们).而且也不喜欢!="操作符.

I discovered that openmp doesn't support while loops( or at least doesn't like them too much). And also doesn't like the ' != ' operator.

我有这段代码.

int count = 1;
#pragma omp parallel for
    while ( fgets(buff, BUFF_SIZE, f) != NULL )
    {
        len = strlen(buff);
        int sequence_counter = segment_read(buff,len,count);
        if (sequence_counter == 1)
        {
            count_of_reads++;
            printf("
 Total No. of reads: %d 
",count_of_reads);
        }
    count++;
    }

有关如何管理此问题的任何线索?我在某处(包括关于 stackoverflow 的另一篇文章)读到我可以使用管道.那是什么 ?以及如何实施?

Any clues as to how to manage this ? I read somewhere ( another post on stackoverflow included) that I can use a pipeline. What is that ? and how to implement it ?

推荐答案

在 OpenMP 中实现parallel while"的一种方法是使用创建任务的 while 循环.这是一个总体草图:

One way to implement "parallel while" in OpenMP is to use a while loop that create tasks. Here is a general sketch:

void foo() {
    while( Foo* f = get_next_thing() ) {
#pragma omp task firstprivate(f)
        bar(f);
    }
#pragma omp taskwait
}

对于循环 fgets 的特定情况,请注意 fgets 具有固有的顺序语义(它获取下"行),因此需要在启动任务之前调用它.每个任务对 fgets 返回的数据的自己的副本进行操作也很重要,这样对 fgets 的调用就不会覆盖前一个任务正在操作的缓冲区.

For the specific case of looping over fgets, note that fgets has inherently sequential semantics (it gets the "next" line), so it would need to be called before launching the task. It would also be important for each task to operate on its own copy of the data returned by fgets, so that a call to fgets does not overwrite the buffer being operated on by a previous task.

这篇关于openmp - 用于文本文件读取和使用管道的 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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