1

I need some help understanding the critical sections of the code described below, in particular whether how I use std::map is thread safe.

I am loading a file line-by-line, building a string out of the contents, and every now and then reach a separator. On this separator, I create a std::mapkey/value pair, and create a task to operate on this value (see below). This value is a pair, the first being a housekeeping int and the second being a vector of uint64_t.

#pragma omp parallel num_threads(4)
#pragma omp single
{
    while (getline(fin, line) && ...)
    {
        if (line[0] == '>')
        {
            #pragma omp task
            do_work_on_value(tmp, map[key])

            tmp.clear();

            // insert map entry (new key!)
            map.insert(...);
        }
        else
            tmp += line;
    }
}

...

static void do_work_on_value(string vals, pair<int, vector<uint64_t>> &val)
{
    ...
}

So, the key operations being :

  • Adding new key/value pairs whilst operating on a different value
  • Working on the vector of a value (push_back and some arithmetic/bitwise on contents)

I learnt about some of the OpenMP directives using this answer, and is it sufficient to use a #pragma omp critical over the map.insert? Each task will be operating on a unique key's value, so I don't believe I need to do much more.

Cheers in advance!


@Gilad thanks for your link to the YouTube Intel OpenMP tutorials,

These are specific relevant videos -

Introduction to tasks and examples

Discussion of tasks in linked list example

Community
  • 1
  • 1
PidgeyBAWK
  • 319
  • 1
  • 4
  • 13
  • 1
    please check this great playlist by Intel https://www.youtube.com/playlist?list=PLLX-Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG – Gilad Jan 03 '15 at 10:39

1 Answers1

0

Using the Intel OpenMP tutorials linked in comments by @Gilad, it was clear there was no issue dispatching the tasks assuming no key duplicates/critical regions even in the tasks' parent scope. The final code used for the task was along the lines of :

#pragma omp task firstprivate(tmp), if (large tmp), untied
do_work_on_value(tmp, map[key])

Performance is poor unfortunately, I'll keep experimenting.

PidgeyBAWK
  • 319
  • 1
  • 4
  • 13