Community
cancel
Showing results for 
Search instead for 
Did you mean: 
barry_m_1
Beginner
367 Views

Parallel STL: Controlling the number of threads?

Jump to solution

Hello All,

So, I wanted to try out some of the new C++17 parallel algorithms and luckily I have access to Parallel Studio 2018. Compiled the following code:

#include <vector>
#include <iostream>
#include "pstl/execution"
#include "pstl/algorithm"
#include "pstl/numeric"
#include "pstl/memory"

int main() {
  
  // 1,000,000 double vector with 1/2
  std::vector<double> v(1'000'000);
  std::fill(std::execution::par_unseq, v.begin(), v.end(), 0.5);

  // Reduction
  auto result = std::reduce(std::execution::par, v.begin(), v.end(), 0.0);
  std::cout << "Result: " << result << "\n";
  return 0;
}

If I run it through vtune, I see it uses the 12 threads on my machine. I did a little googling, but I don't see how I restrict the number of threads this code uses. I saw some ancient suggestions of TBB_NUM_THREADS but that doesn't appear to change anything. How do I control the number of threads if I wanted to run PSTL code in my HPC scheduler?

Thanks.

Barry

 

0 Kudos
1 Solution
367 Views

Hello,

you can use either tbb::task_scheduler_init object 

https://software.intel.com/en-us/node/506296

or task_arena class

https://software.intel.com/sites/default/files/managed/6a/78/parallel_mag_issue18.pdf, pages 24-25

Vladimir

 

 

View solution in original post

2 Replies
368 Views

Hello,

you can use either tbb::task_scheduler_init object 

https://software.intel.com/en-us/node/506296

or task_arena class

https://software.intel.com/sites/default/files/managed/6a/78/parallel_mag_issue18.pdf, pages 24-25

Vladimir

 

 

View solution in original post

barry_m_1
Beginner
367 Views

Vladimir,

Entire code with your suggestion:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <sstream>
#include "pstl/execution"
#include "pstl/algorithm"
#include "pstl/numeric"
#include "pstl/memory"
#include "tbb/task_scheduler_init.h"

int main() {
  // Init TBB Threads
  int pstl_num_threads;
  if (auto pstl_num_threads_ca = std::getenv("PSTL_NUM_THREADS")) {
    std::istringstream iss(pstl_num_threads_ca);
    iss >> pstl_num_threads;
  }
  else {
    pstl_num_threads = tbb::task_scheduler_init::default_num_threads();
  }
  std::cout << "PSTL_NUM_THREADS: " << pstl_num_threads << '\n';
  tbb::task_scheduler_init init(pstl_num_threads);
  
  // 10,000 double vector with 1/2
  std::vector<double> v(1'000'000);
  std::fill(std::execution::par_unseq, v.begin(), v.end(), 0.5);

  // Reduction
  auto result = std::reduce(std::execution::par, v.begin(), v.end(), 0.0);
  std::cout << "Result: " << result << "\n";
  return 0;
}

Hope it helps someone else. Thanks.

Reply