Wednesday, February 2, 2022

[SOLVED] Can't run C++17 parallel algorithms with GCC on Linux

Issue

I want to run std algorithms using execution policies from C++17 using GCC9.3.

I've downloaded href="https://github.com/oneapi-src/oneTBB/releases/download/v2021.1.1/oneapi-tbb-2021.1.1-lin.tgz" rel="nofollow noreferrer">https://github.com/oneapi-src/oneTBB/releases/download/v2021.1.1/oneapi-tbb-2021.1.1-lin.tgz

But I'm getting an error that "task" class is not defined Even on "hello world" example from https://www.ibm.com/developerworks/aix/library/au-intelthreadbuilding/index.html

#include "tbb/tbb.h"
#include <iostream>
using namespace tbb;
using namespace std;
 
class first_task : public task { 
    public: 
    task* execute( ) { 
       cout << "Hello World!\n";
       return NULL;
    }
};
 
int main( )
{ 
    task_scheduler_init init(task_scheduler_init::automatic);
    first_task& f1 = *new(tbb::task::allocate_root()) first_task( );
    tbb::task::spawn_root_and_wait(f1);
}

I'm getting the following errors:

test.cpp:55:32: error: expected class-name before '{' token
   55 | class first_task : public task {
      |                                ^
test.cpp:57:5: error: 'task' does not name a type
   57 |     task* execute( ) {
      |     ^~~~
test.cpp: In function 'int main()':
test.cpp:65:5: error: 'task_scheduler_init' was not declared in this scope
   65 |     task_scheduler_init init(task_scheduler_init::automatic);
      |     ^~~~~~~~~~~~~~~~~~~
test.cpp:66:38: error: 'allocate_root' is not a member of 'tbb::v1::task'
   66 |     first_task& f1 = *new(tbb::task::allocate_root()) first_task( );
      |                                      ^~~~~~~~~~~~~
test.cpp:67:16: error: 'spawn_root_and_wait' is not a member of 'tbb::v1::task'
   67 |     tbb::task::spawn_root_and_wait(f1);
      |                ^~~~~~~~~~~~~~~~~~~

I'm compiling: using the following command:

g++ -I<path_to_tbb>/oneapi-tbb-2021.1.1/include/oneapi -I<path_to_tbb>/oneapi-tbb-2021.1.1/include/oneapi/tbb -I<path_to_tbb>/oneapi-tbb-2021.1.1/include -L<path_to_tbb>/oneapi-tbb-2021.1.1/lib/intel64/gcc4.8/ test.cpp -o test

running source env/var.h from <path_to_tbb> didn't help Am I doing something wrong?

Thank you


Solution

From the error message I conclude that either the gcc or TBB implementation you use is sort of experimental. As you can see, gcc does find the TBB (no error on #include tbb/tbb.h), that's good! Then, the errors show that some functions you use are hidden inside the tbb::v1 namespace. So you can either use using namespace tbb::v1 or add tbb::v1:: in front of all "unknown" names.

Then, in https://community.intel.com/t5/Intel-oneAPI-Threading-Building/Unable-to-compile-TBB-program/td-p/1226663
they sey that tbb::task is depreciated. Please use a bit more modern TBB tutorial :-)

See also: https://github.com/oneapi-src/oneTBB/issues/243



Answered By - zkoza
Answer Checked By - David Marino (WPSolving Volunteer)