How to manipulate run-time setting in g++ in Parallel Mode(-D_GLIBCXX_PARALLEL) in C++ Program?

StackOverflow https://stackoverflow.com/questions/23278246

  •  09-07-2023
  •  | 
  •  

Question

While reading the GCC 4.9.0 Standard C++ Library Reference Manual, I learnt about the uses of g++ compiler parallel support for STL algorithm. This is also proposed(N3960) to include into the future c++ standard.

There is number of attributes which has default values set in g++(like minimum threshold limits about elements for algorithms to decide whether to use sequential or parallel). However there is interface to read/change their values. These all have been defined in the header file parallel/settings.h. Most of the attributes are self explanatory as they are threshold values which decide whether our program would use parallel version of algorithm. Following is current gcc4.9 version implementation of struct _Settings constructor which set all attributes with reasonable values.

        algorithm_strategy(heuristic),
        sort_algorithm(MWMS),
        .........
        find_initial_block_size(256),
        find_maximum_block_size(8192),
        ...........
        workstealing_chunk_size(100),
        L1_cache_size(16 << 10),
        L2_cache_size(256 << 10),
        TLB_size(128),
        cache_line_size(64),
        qsb_steals(0),
        search_minimal_n(1000),
        find_scale_factor(0.01f)

However,I would like to understand about how/when to use following attributes in struct _Settings?. How they should be used and when we should changes these attributes to verify how it would impact c++ program?

L1_cache_size
L2_cache_size
cache_line_size
Was it helpful?

Solution

This is also proposed(N3960) to include into the future c++ standard.

The contents of that draft TS are not the same as the GNU C++ library's Parallel Mode algorithms. They are based on different designs and come from different groups of people.

How they should be used and when we should changes these attributes to verify how it would impact c++ program?

You can set the cache sizes to the correct values for your CPU, since different CPUs have different caches.

You tell the library what settings to use like this, e.g. for a CPU with 64kB L1 cache and 256kB L2 cache:

__gnu_parallel::_Settings settings;  // constructed with default values
settings.L1_cache_size = 64 << 10;
settings.L2_cache_size = 256 << 10;
__gnu_parallel::_Settings::set(settings);  // change global settings
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top