Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Parallel STL: Controlling the number of threads?

barry_m_1
Beginner
1,642 Views

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
Vladimir_P_1234567890
1,642 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

0 Kudos
2 Replies
Vladimir_P_1234567890
1,643 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

 

 

0 Kudos
barry_m_1
Beginner
1,642 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.

0 Kudos
Reply