How to parallelize (Openmp) a method call?

Asked

Viewed 63 times

1

So I’m developing a work to perform the comparison of a sequential and parallel execution of a Pattern Matching method. My input is a. txt of dozens of lines, which I traverse and store inside a buffer:

...
fread(buffer, sizeof(char), numero_bytes, aux);

Once done, I run once sequentially:

       /* quebro a string em varias linhas*/
      char * string_sequencial = strtok(strdup(buffer), str);

      /*Enquanto existir linhas a serem verificadas: */
      while(string_sequencial != NULL) {

        if(match(padrao, string_sequencial)) // Método Pattern Matching
            {
              cont_padroes_sequencial++;
            }

        /*vai para a proxima linha*/
        string_sequencial  = strtok(NULL, str); 

        /*Conta o numero de linhas para o for da execução paralela*/
        n_linha++;           
      }

So far so good, my problem is that when I use more than one thread in:

omp_set_num_threads(1);

The execution ends.

My idea was to make it equal to sequential execution. For each line, a thread would be allocated to perform the check, but I guess it doesn’t just work that way.

The stretch of parallelization:

  char * string_paralelo = strtok(strdup(buffer), str);
  // <<< 

  #pragma omp parallel
  {
    omp_set_num_threads(1);
    #pragma omp for
    for(int i = 0; i < n_linha; i++)
        {
         if(match(padrao, string_paralelo))
            {
              cont_padroes_paralelo++;
            }
          string_paralelo  = strtok(NULL, str);
        }
  }

My doubt: Do I need to state something differently? Is something missing? If someone can give me a light, I would be very grateful!

Still, if anyone knows: why does a single thread work? I imagine it doesn’t run sequentially, even with a single thread, because the running time is shorter than the sequential one..

1 answer

1

For whoever is interested or if ever someone has the same doubt, I have solved my problem.

The problem was that, the way I was doing it, I didn’t keep any "index" pointed at the strings I’d like to parallelize. And so (I think) that my program "crashed" because the threads had no notion of the order that was to be executed. So, I decided to create an array to serialize the strings.

while(string_sequencial != NULL) {

        transforma_linha(string_sequencial); /*LETRAS MINUSCULAS*/

          // Array adicionado: 
        array[i] = string_sequencial;
        i++;
        if(match(padrao, string_sequencial))  /* CONTABILIZA PADRÃO*/
            {
              //printf("%s \n", string_sequencial);
              cont_padroes_sequencial++;
            }

        string_sequencial  = strtok(NULL, str);  /*ITERA PARA PROXIMA LINHA*/
        n_linha++;                               /*CONTA QUANTIDADE DE LINHAS*/
      }

And for parallelization, I added the "reduction" parameter, which, from what I understand, prevents threads from overwriting one another’s values.

#pragma omp parallel
  {
    #pragma omp for reduction(+:cont_padroes_paralelo) // Inclusão do REDUCTION
    for(int i = 0; i < n_linha; i++)
        {
         if(match(padrao, array[i]))  // SE ACHOU UM PADRÃO, CONTABILIZA.
            {
              cont_padroes_paralelo++;
            }
        }
  }
  • the clause reduction makes a reducing, that is, it combines the values of cont_padroes_paralelo obtained by all threads by the type of reduction specified, in which case the +: sum all values

Browser other questions tagged

You are not signed in. Login or sign up in order to post.