Working through file using single/tasks

General OpenMP discussion

Working through file using single/tasks

Postby lars » Wed Apr 09, 2014 9:02 am

I am getting some weird results from the following code using OpenMP 3.0 on Linux with gcc.

This code reads in a line from a file and searches it for keywords stored in a data structure (paca). When a match is found the callback function match_handler is called, that prints the line, plus som other identifying data. When I run this code serially, it works fine, but not when these pragmas are recognized by the compiler.

As an example, lets say I'm searching for the word "line". This is what the output looks like:

Load search line: This is line 1 of the file
Load search line: This is line 2 of the file
Load search line: This is line 3 of the file
Load search line: This is line 4 of the file
5235235, This is line 4 of the file
Load search line: This is line 5 of the file
This is line 5 of the file
Load search line: This is line 6 of the file
65875763, This is line 6 of the file
Load search line: This is line 7 of the file
90356834, This is line 7 of the file


As you can see the program starts printing the lines as it should, without searching for words in them, then all of a sudden it begins searching and finding matches. This is by forcing one thread. If I add more threads, the results are too chaotic to post here. I thought I'd correct this issue before complicating it with more threads, as I've seen is a common debugging method with OpenMP.

Here is a code excerpt:

Code: Select all
// Search filename line by line for search terms in automata pointed to by paca
int search_file_by_line(const char *filename, AC_AUTOMATA_t *paca)
{
    FILE *fp; // Input file descriptor
    static AC_TEXT_t intext; // input text

    // Read search text from file
    char *stline = NULL;
    size_t ln = 0;
    ssize_t linelen = 0;

    // Open input file
    if(!(fp = fopen(filename, "r")))
    {
        printf("Could not open search text file\n");
        exit(1);
    }

    // begin parallel processing
    #pragma omp parallel num_threads(1)
    {
        #pragma omp single
        {
            // loop to load and search the input file, line by line
            linelen = getline(&stline, &ln, fp);
            while(linelen != -1)
            {
                // Clean up newline at end
                if(stline[linelen-1] == '\n')
                {
                    stline[linelen-1] = '\0';
                }
                intext.astring = stline;
                intext.length = linelen;

                printf ("Load search line: %s\n", intext.astring);

                #pragma omp task firstprivate(intext, paca, fp)
                ac_automata_search(paca, &intext, 0, match_handler, 0);

                linelen = getline(&stline, &ln, fp);
            }
        } // end single
    } // end parallel

    fclose(fp);
    return 0;
}
lars
 
Posts: 1
Joined: Wed Apr 09, 2014 8:03 am

Re: Working through file using single/tasks

Postby MarkB » Fri Apr 11, 2014 9:24 am

Do you get sensible output if you enclose all the printf's in critical constructs?
MarkB
 
Posts: 432
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 8 guests