1

The following minimal example illustrates the behaviour of stxxl when initializing containers in parallel (using openMP):

#include <omp.h>
#include <stdio.h>
#include <stxxl.h>

typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;

int main(int argc, char* argv[]) {
    const unsigned long NUM = 8;
    #pragma omp parallel num_threads(NUM) 
    {       
            VEC_T v;
            printf("%d\t%p\n", omp_get_thread_num(), &v);
    }
    return EXIT_SUCCESS;
}

running into either

[STXXL-ERROR] File too large 

or

[SYSTEM-ERROR]Segmentation fault

How can I allocate stxxl containers in multiple threads ?

Lars Hadidi
  • 558
  • 1
  • 5
  • 15

1 Answers1

1

The initialization of stxxl containers isn't thread-safe, therefore a mutual exclusion for a thread initializing a container is needed. Using openMP, this will read as follows:

#include <omp.h>
#include <stdio.h>
#include <stxxl.h>

typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;

int main(int argc, char* argv[]) {
    const unsigned long NUM = 8;
    #pragma omp parallel num_threads(NUM) 
    {
            VEC_T* v;
            #pragma omp critical
            {
                    v = new VEC_T();
            }
            printf("%d\t%p\n", omp_get_thread_num(), &v);
            delete v;
    }
    return EXIT_SUCCESS;
}
Lars Hadidi
  • 558
  • 1
  • 5
  • 15
  • And, of course, if the entire purpose of your loop is to construct these container, you will see basically no benefit to running this in OpenMP vs serially because of the ratio of serial to parallel work here. – NoseKnowsAll Oct 22 '15 at 20:48
  • Just a minimal example on the concept of parallel initialization – Lars Hadidi Oct 24 '15 at 11:27
  • Are you sure that the initialization of vectors is really not thread-safe? http://stxxl.sourceforge.net/tags/master/faq.html#faq_threadsafe claims that accesses to different data structures from concurrent threads are okay. My guess is that just the first initialization of the STXXL is not thread-safe. When I add a `VEC_T u;` before the parallel section, I do not get any errors anymore. – michitux Mar 21 '16 at 17:48