Turi Create  4.0
Threading Library

This is a rather complete threading library, providing cross platform implementations of many threading primitives. More...

Classes

class  turi::deferred_rwlock
 
class  turi::lockfree_push_back< Container, T >
 
class  turi::mutex
 
class  turi::recursive_mutex
 
class  turi::simple_spinlock
 
class  turi::padded_simple_spinlock
 
class  turi::conditional
 
class  turi::cancellable_barrier
 
class  turi::thread
 
class  turi::thread_group
 
class  turi::queued_rw_lock
 

Functions

template<typename T >
bool turi::atomic_compare_and_swap (T &a, T oldval, T newval)
 
template<typename T >
bool turi::atomic_compare_and_swap (volatile T &a, T oldval, T newval)
 
template<typename T >
turi::atomic_compare_and_swap_val (T &a, T oldval, T newval)
 
template<typename T >
turi::atomic_compare_and_swap_val (volatile T &a, T oldval, T newval)
 
template<>
bool turi::atomic_compare_and_swap (volatile double &a, double oldval, double newval)
 
template<>
bool turi::atomic_compare_and_swap (volatile float &a, float oldval, float newval)
 
template<typename T >
void turi::atomic_exchange (T &a, T &b)
 Atomically exchanges the values of a and b. More...
 
template<typename T >
void turi::atomic_exchange (volatile T &a, T &b)
 Atomically exchanges the values of a and b. More...
 
template<typename T >
turi::fetch_and_store (T &a, const T &newval)
 Atomically sets a to the newval, returning the old value.
 
std::exception_ptr turi::execute_task_in_native_thread (const std::function< void(void)> &fn)
 
void turi::in_parallel (const std::function< void(size_t thread_id, size_t num_threads)> &fn)
 
thread_poolturi::get_parfor_thread_pool ()
 
template<typename FunctionType >
void turi::parallel_for (size_t begin, size_t end, const FunctionType &fn)
 
template<typename FunctionType , typename ReduceType >
ReduceType turi::fold_reduce (size_t begin, size_t end, const FunctionType &fn, ReduceType base=ReduceType())
 
template<typename RandomAccessIterator , typename FunctionType >
void turi::parallel_for (RandomAccessIterator iter_begin, RandomAccessIterator iter_end, const FunctionType &fn, std::random_access_iterator_tag=typename std::iterator_traits< RandomAccessIterator >::iterator_category())
 

Detailed Description

This is a rather complete threading library, providing cross platform implementations of many threading primitives.

Function Documentation

◆ atomic_compare_and_swap() [1/4]

template<typename T >
bool turi::atomic_compare_and_swap ( T &  a,
oldval,
newval 
)

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return true;
}
else {
return false;
}

Definition at line 27 of file atomic_ops.hpp.

◆ atomic_compare_and_swap() [2/4]

template<typename T >
bool turi::atomic_compare_and_swap ( volatile T &  a,
oldval,
newval 
)

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return true;
}
else {
return false;
}

Definition at line 45 of file atomic_ops.hpp.

◆ atomic_compare_and_swap() [3/4]

template<>
bool turi::atomic_compare_and_swap ( volatile double &  a,
double  oldval,
double  newval 
)
inline

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return true;
}
else {
return false;
}

Definition at line 103 of file atomic_ops.hpp.

◆ atomic_compare_and_swap() [4/4]

template<>
bool turi::atomic_compare_and_swap ( volatile float &  a,
float  oldval,
float  newval 
)
inline

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return true;
}
else {
return false;
}

Definition at line 126 of file atomic_ops.hpp.

◆ atomic_compare_and_swap_val() [1/2]

template<typename T >
T turi::atomic_compare_and_swap_val ( T &  a,
oldval,
newval 
)

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return oldval;
}
else {
return a;
}

Definition at line 65 of file atomic_ops.hpp.

◆ atomic_compare_and_swap_val() [2/2]

template<typename T >
T turi::atomic_compare_and_swap_val ( volatile T &  a,
oldval,
newval 
)

atomic instruction that is equivalent to the following:

if (a==oldval) {
a = newval;
return oldval;
}
else {
return a;
}

Definition at line 83 of file atomic_ops.hpp.

◆ atomic_exchange() [1/2]

template<typename T >
void turi::atomic_exchange ( T &  a,
T &  b 
)

Atomically exchanges the values of a and b.

Warning
This is not a full atomic exchange. Read of a, and the write of b into a is atomic. But the write into b is not.

Definition at line 142 of file atomic_ops.hpp.

◆ atomic_exchange() [2/2]

template<typename T >
void turi::atomic_exchange ( volatile T &  a,
T &  b 
)

Atomically exchanges the values of a and b.

Warning
This is not a full atomic exchange. Read of a, and the write of b into a is atomic. But the write into b is not.

Definition at line 153 of file atomic_ops.hpp.

◆ execute_task_in_native_thread()

std::exception_ptr turi::execute_task_in_native_thread ( const std::function< void(void)> &  fn)

Takes a function and executes it in a native stack space. Used to get by some libjvm oddities when using coroutines.

Returns an exception if an exception was thrown while executing the inner task.

◆ fold_reduce()

template<typename FunctionType , typename ReduceType >
ReduceType turi::fold_reduce ( size_t  begin,
size_t  end,
const FunctionType &  fn,
ReduceType  base = ReduceType() 
)

Runs a map reduce operation for ranging from the integers 'begin' to 'end'.

When run single threaded, is equivalent to

T acc;
for(size_t i = begin; i < end; ++i) {
acc = n(i, acc);
}
return acc;

Example:

// performs an inner product of 'a' and 'b'
double vec_proc(const std::vector<double>& a,
const std::vector<double>& b) {
double result = fold_reduce(0, a.size(), [&](size_t i, double& acc) {
acc += a[i] * b[i];
return acc;
}, 0.0);
return result;
}
Parameters
beginThe beginning integer of the for loop
endThe ending integer of the for loop
fnThe function to run. The function must take a single size_t argument which is a current index.

Definition at line 158 of file lambda_omp.hpp.

◆ get_parfor_thread_pool()

thread_pool& turi::get_parfor_thread_pool ( )

Returns the thread pool dedicated for running parallel for jobs.

◆ in_parallel()

void turi::in_parallel ( const std::function< void(size_t thread_id, size_t num_threads)> &  fn)
inline

Runs a provided function in parallel, passing the function the thread ID and the number of threads. The thread ID is always between 0 and #threads - 1.

in_parallel([&](size_t thrid, size_t num_threads) {
std::cout << "Thread " << thrid << " out of "
<< num_threads << "\n";
});
Parameters
fnThe function to run. The function must take two size_t arguments: the thread ID and the number of threads.

Definition at line 35 of file lambda_omp.hpp.

◆ parallel_for() [1/2]

template<typename FunctionType >
void turi::parallel_for ( size_t  begin,
size_t  end,
const FunctionType &  fn 
)

Runs a parallel for ranging from the integers 'begin' to 'end'.

When run single threaded, is equivalent to

for(size_t i = begin; i < end; ++i) {
fn(i);
}

Example:

// performs an element wise multiplication of 'a' and 'b'
std::vector<double> vec_multiple(const std::vector<double>& a,
const std::vector<double>& b) {
std::vector<double> ret(a.size(), 0); // allocate the return object
parallel_for(0, a.size(), [&](size_t i) {
ret[i] = a[i] * b[i];
});
return ret;
}
Parameters
beginThe beginning integer of the for loop
endThe ending integer of the for loop
fnThe function to run. The function must take a single size_t argument which is a current index.

Definition at line 93 of file lambda_omp.hpp.

◆ parallel_for() [2/2]

template<typename RandomAccessIterator , typename FunctionType >
void turi::parallel_for ( RandomAccessIterator  iter_begin,
RandomAccessIterator  iter_end,
const FunctionType &  fn,
std::random_access_iterator_tag  = typename std::iterator_traits<RandomAccessIterator>::iterator_category() 
)
inline

Runs a parallel for over a random access iterator range.

When run single threaded, is equivalent to

RandomAccessIterator iter = begin;
while (iter != end) {
fn(*iiter);
++end;
}

Example:

// squares each element of the vector in place
void square(std::vector<double>& v) {
parallel_for(v.begin(), v.end(), [&](double& d) {
d = d * d;
});
}
Parameters
beginThe beginning integer of the for loop
endThe ending integer of the for loop
fnThe function to run. The function must take a single size_t argument which is a current index.

Definition at line 229 of file lambda_omp.hpp.