2

I was writing a basic program (that sorts an integer vector 'list' using the parallel execution policy)-

#include <iostream>
#include <execution>
#include <algorithm>
#include <chrono>
#include <vector>

int main()
{
  std::vector<int> list {0,1,2,3,4,5};

  auto start = std::chrono::high_resolution_clock::now();
  std::sort(std::execution::par, list.begin(), list.end());
  auto end   = std::chrono::high_resolution_clock::now();

  std::cout << "time taken : " << std::chrono::duration<float>(end - start).count() << " s" << std::endl;

return 0;
}

When i build this program, an error was generated:

/home/test.cpp:12:18: error: ‘std::execution’ has not been declared
   12 |   std::sort(std::execution::par, list.begin(), list.end());
      |                  ^~~~~~~~~
The terminal process "/bin/bash '-c', '/usr/bin/g++ -g '/home/test.cpp' -o '/home/test''" terminated with exit code: 1.

I am using Visual Studio Code running C++17 with gcc 9.3.0 as my compiler. I am using Linux mint 20 as my OS. Please bear in mind that i am still new to Linux, VSCode, GCC/G++ and C++17 and so I could be missing some information.

Could anyone tell me what could be done, and/or where I am wrong?

EDIT: I had checked the definition of std::execution, and i saw this -

#ifndef __PSTL_glue_execution_defs_H
#define __PSTL_glue_execution_defs_H

#include <type_traits>

#include "execution_defs.h"

namespace std
{
// Type trait
using __pstl::execution::is_execution_policy;
#if __PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT
#if __INTEL_COMPILER
template <class T>
constexpr bool is_execution_policy_v = is_execution_policy<T>::value;
#else
using __pstl::execution::is_execution_policy_v;
#endif
#endif

namespace execution
{
// Standard C++ policy classes
using __pstl::execution::sequenced_policy;
#if __PSTL_USE_PAR_POLICIES
using __pstl::execution::parallel_policy;
using __pstl::execution::parallel_unsequenced_policy;
#endif
// Standard predefined policy instances
using __pstl::execution::seq;
#if __PSTL_USE_PAR_POLICIES
using __pstl::execution::par;
using __pstl::execution::par_unseq;
#endif
// Implementation-defined names
// Unsequenced policy is not yet standard, but for consistency
// we include it into namespace std::execution as well
using __pstl::execution::unseq;
using __pstl::execution::unsequenced_policy;
} // namespace execution
} // namespace std

#include "algorithm_impl.h"
#include "numeric_impl.h"
#include "parallel_backend.h"

#endif /* __PSTL_glue_execution_defs_H */
  • 2
    Could you be missing `-std=c++17`? – kabanus Aug 25 '20 at 06:36
  • 1
    Is this a duplicate of https://stackoverflow.com/questions/42567998/how-do-i-use-the-new-c17-execution-policies? – Ole Wolf Aug 25 '20 at 06:36
  • 2
    Reading the [status page](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017), you may also be missing `-ltbb`. – kabanus Aug 25 '20 at 06:43
  • [Kabanus], how do I use it, that `-std=c++17` ? – Madhav Madhusoodanan Aug 25 '20 at 06:44
  • There is no `-std=c++17` in the command line. You need to add this option to one of them .json files. – n. m. could be an AI Aug 25 '20 at 06:45
  • [Ole Wolf], the question that you have linked is similar to mine, but for me the 'execution' header file does not seem to be missing – Madhav Madhusoodanan Aug 25 '20 at 06:47
  • To ping someone, use @someone (I hope that's not a real username). Otherwise it's sheer luck I saw you message. Add it to your compilation line: `/bin/bash -c /usr/bin/g++ -g /home/test.cpp -o /home/test -std=c++17 -ltbb` (not sure about the `ltbb`, that's what it says in the docs). @OleWolf's target is old, I don't think it's relevant. – kabanus Aug 25 '20 at 06:49
  • I notice you are using VS. I'm sure you can find online how to add compilation flags in it. – kabanus Aug 25 '20 at 06:50
  • @kabanus, i have tried your json file edit, and now i am getting too many messages of the likes of `undefined reference to 'vtable for tbb::task'`. Thank you @kabanus for that pinging suggestion...i didnt know about it – Madhav Madhusoodanan Aug 25 '20 at 06:55
  • 1
    Seems you are missing the `tbb` libraries. You can try removing `-ltbb` and see if that works though I doubt it,. If not, you will have to figure out out to install it on your system. – kabanus Aug 25 '20 at 07:00
  • Thank you so much @kabanus , that `-ltbb` was the much needed line in the json file...now my program works like a charm! – Madhav Madhusoodanan Aug 25 '20 at 07:08

0 Answers0