openmp - 用于文本文件读取和使用管道的while循环

Sid*_*427 0 c parallel-processing hpc openmp

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

我有这段代码.

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("\n Total No. of reads: %d \n",count_of_reads);
        }
    count++;
    }
Run Code Online (Sandbox Code Playgroud)

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

小智 12

人们选择最好的答案是如此之快,这太糟糕了.这是我的答案.
首先,你应该将文件读入一个像fread这样的缓冲区.这很快.有关如何执行此操作的示例,请访问http://www.cplusplus.com/reference/cstdio/fread/

然后,您可以与OpenMP并行操作缓冲区.我已经为你实现了大部分内容.下面是代码.你没有提供这个segment_read功能所以我创建了一个虚拟的功能.我使用了C++中的一些函数,比如std :: vector和std :: sort,但是在纯C中你可以做更多的工作.

编辑: 我编辑了这段代码,并且能够删除排序和关键部分.

我编译了 g++ foo.cpp -o foo -fopenmp -O3

#include <stdio.h>
#include <omp.h>
#include <vector>

using namespace std;

int segment_read(char *buff, const int len, const int count) {
  return 1;  
}

void foo(char* buffer, size_t size) {
    int count_of_reads = 0;
    int count = 1;
    std::vector<int> *posa;
    int nthreads;

    #pragma omp parallel 
    {
        nthreads = omp_get_num_threads();
        const int ithread = omp_get_thread_num();
        #pragma omp single 
        {
            posa = new vector<int>[nthreads];
            posa[0].push_back(0);
        }

        //get the number of lines and end of line position
        #pragma omp for reduction(+: count)
        for(int i=0; i<size; i++) {
            if(buffer[i] == '\n') { //should add EOF as well to be safe
                count++;
                posa[ithread].push_back(i);
            }
        }

        #pragma omp for     
        for(int i=1; i<count ;i++) {    
            const int len = posa[ithread][i] - posa[ithread][i-1];
            char* buff = &buffer[posa[ithread][i-1]];
            const int sequence_counter = segment_read(buff,len,i);
            if (sequence_counter == 1) {
                #pragma omp atomic
                count_of_reads++;
                printf("\n Total No. of reads: %d \n",count_of_reads);
            }

        }
    }
    delete[] posa;
}

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.txt" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */
  foo(buffer, result);
  // terminate


  fclose (pFile);
  free (buffer);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)