How to parallelize (OpenMP) a method call?

0

So, I am developing a work to do the comparison of a sequential and parallel execution of a Pattern Matching method. My input is a .txt of dozens of lines, which I go through and store inside a buffer:

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

Once this is done, I run it 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);

Execution ends.

My idea was to do sequential execution the same way. For each line, a thread would be allocated to perform the check, but I guess it does not work just like that.

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 question: Do I have to declare in a different way? Something's missing? If anyone can give me a light, I would be very grateful!

Still, if anyone knows: why does a single thread work? I guess it does not run sequentially, even with a single thread, because the execution time is less than that of the sequential ..

    
asked by anonymous 15.10.2018 / 22:12

1 answer

0

For those who are interested or if someday someone has the same doubt, I was able to solve my problem.

The problem was that, as I was doing, I did not keep any indexes pointing to the strings that I would like to parallelize. And so (I think) that my program "crashed" because the threads had no idea 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 the parallelization, I added the "reduction" parameter, which, from what I understand, prevents threads from overwriting each other'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++;
            }
        }
  }
    
16.10.2018 / 17:19