last changes from bloomberg.

This commit is contained in:
Vahagn Khachatryan
2021-03-25 08:15:36 -04:00
parent 1a17b9fe6c
commit 721e183dc9
20 changed files with 1391 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:Ltbb
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <cstdio>
#include <tbb/flow_graph.h>
using namespace tbb::flow;
struct square {
int operator()(int v) { return v*v; }
};
struct cube {
int operator()(int v) { return v*v*v; }
};
class sum {
int &my_sum;
public:
sum( int &s ) : my_sum(s) {}
int operator()( tuple< int, int > v ) {
my_sum += get<0>(v) + get<1>(v);
return my_sum;
}
};
int main()
{try{
int result = 0;
graph g;
broadcast_node<int> input(g);
function_node<int,int> squarer( g, unlimited, square() );
function_node<int,int> cuber( g, unlimited, cube() );
join_node< tuple<int,int>, queueing > join( g );
function_node<tuple<int,int>,int>
summer( g, serial, sum(result) );
make_edge( input, squarer );
make_edge( input, cuber );
make_edge( squarer, get<0>( join.input_ports() ) );
make_edge( cuber, get<1>( join.input_ports() ) );
make_edge( join, summer );
for (int i = 1; i <= 10; ++i)
input.try_put(i);
g.wait_for_all();
printf("Final result is %d\n", result);
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,70 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:Ltbb
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <sstream>
#include <iostream>
#include <exception>
#include <memory>
#include "tbb/flow_graph.h"
using namespace tbb::flow;
typedef std::shared_ptr<std::string> msg_type;
struct body {
std::string my_name;
body( const char *name ) : my_name(name) {}
void operator()( msg_type msg ) const {
std::stringstream ss;
ss << my_name << " got " << *msg << std::endl;
std::cout << ss.str();
}
};
int main()
{try{
graph g;
broadcast_node<msg_type> start( g );
continue_node<msg_type> a( g, body("A"));
continue_node<msg_type> b( g, body("B"));
continue_node<msg_type> c( g, body("C"));
continue_node<msg_type> d( g, body("D"));
continue_node<msg_type> e( g, body("E"));
make_edge( start, a );
make_edge( start, b );
make_edge( a, c );
make_edge( b, c );
make_edge( c, d );
make_edge( a, e );
for (int i = 0; i < 3; ++i ) {
std::stringstream ss;
ss << i << std::endl;
start.try_put( std::make_shared( ss.str() ) );
g.wait_for_all();
}
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,97 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:LtbbD
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <cstdio>
#include <iostream>
#include <exception>
#include <thread>
#include <chrono>
#include <tbb/task.h>
/*
* Conclusion:
* The first task_sheduler_init is configuring number of threads in
* the thread pool. If the task scheduler initialised implicitly then
* the number of threads is the number of cores. To ensure uncomment
* the commented line below.
*/
class ChildTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "ChildTask is running.\n" );
return nullptr;
}
};
class ContTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "ContTask is running.\n" );
return nullptr;
}
};
tbb::task * t;
struct RootTask : public tbb::task
{
tbb::task * execute()
{
std::printf( "RootTask is running.\n" );
auto& c = *new( allocate_continuation()) ContTask();
t = &c;
tbb::task& ctask1 = *new( c.allocate_child() ) ChildTask();
tbb::task& ctask2 = *new( c.allocate_child() ) ChildTask();
c.set_ref_count( 3 );
spawn( ctask1 );
spawn( ctask2 );
std::printf( "RootTask is done.\n" );
return nullptr;
}
};
int main ( void )
{try{
auto& rtask = *new( tbb::task::allocate_root()) RootTask();
tbb::task::enqueue( rtask );
std::this_thread::sleep_for( std::chrono::seconds(1) );
std::printf( "After sleep.\n" );
tbb::task::spawn( *new (t->allocate_child()) tbb::empty_task() );
std::this_thread::sleep_for( std::chrono::seconds(1) );
std::printf( "After second sleep.\n" );
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}