openmp - while loop for text file reading and using a pipeline

后端 未结 3 1203
遥遥无期
遥遥无期 2020-12-15 15:02

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.

I have this bit of cod

3条回答
  •  青春惊慌失措
    2020-12-15 15:39

    It's too bad people are so quick to select the best answer. Here is my answer.
    First, you should read the file into a buffer with something like fread. This is very quick. An example of how to do this can be found here http://www.cplusplus.com/reference/cstdio/fread/

    Then you can operate on the buffer in parallel with OpenMP. I have implemented most of this for you. Below is the code. You did not provide the segment_read function so I created a dummy one. I used a few functions from C++ such as std::vector and std::sort but with a little more work you could do this in pure C as well.

    Edit: I edited this code and was able to remove the sorting and critical section.

    I compiled with g++ foo.cpp -o foo -fopenmp -O3

    #include 
    #include 
    #include 
    
    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 *posa;
        int nthreads;
    
        #pragma omp parallel 
        {
            nthreads = omp_get_num_threads();
            const int ithread = omp_get_thread_num();
            #pragma omp single 
            {
                posa = new vector[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

提交回复
热议问题