HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Task-Based Multithreading Library - Implemented using Fibers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementedfibersusinglibrarybasedmultithreadingtask

Problem

This is a library I wrote to enable task-based multithreading. It allows execution of task graphs with arbitrary dependencies. Dependencies are represented as atomic counters. Under the covers, the task graph is executed using fibers, which in turn, are run on a pool of worker threads (one thread per CPU core). This allows the scheduler to wait on dependencies without task chaining or context switches.

The code is inspired by the GDC presentation given by Christian Gyrling: 'Parallelizing the Naughty Dog Engine Using Fibers'

The code for the library is all on github here, however, I will give an overview of the major sections of the code below:

First an example of how the code is used:

```
#include "fiber_tasking_lib/task_scheduler.h"

struct NumberSubset {
uint64 start;
uint64 end;

uint64 total;
};

FTL_TASK_ENTRY_POINT(AddNumberSubset) {
NumberSubset *subset = reinterpret_cast(arg);

subset->total = 0;

while (subset->start != subset->end) {
subset->total += subset->start;
++subset->start;
}

subset->total += subset->end;
}

/**
* Calculates the value of a triangle number by dividing the additions up into tasks
*
* A triangle number is defined as:
* Tn = 1 + 2 + 3 + ... + n
*
* The code is checked against the numerical solution which is:
Tn = n (n + 1) / 2
*/
FTL_TASK_ENTRY_POINT(MainTask) {
// Define the constants to test
const uint64 triangleNum = 47593243ull;
const uint64 numAdditionsPerTask = 10000ull;
const uint64 numTasks = (triangleNum + numAdditionsPerTask - 1ull) / numAdditionsPerTask;

// Create the tasks
FiberTaskingLib::Task *tasks = new FiberTaskingLib::Task[numTasks];
NumberSubset *subsets = new NumberSubset[numTasks];
uint64 nextNumber = 1ull;

for (uint64 i = 0ull; i start = nextNumber;
subset->end = nextNumber + numAdditionsPerTask - 1ull;
if (subset->end > triangleNum) {
subset->end = triangleNum;

Solution

You copied code from boost.context which is licensed under BOOST license.
But because your 'fiber_tasking_lib' is licensed under Apache 2.0 the copied code from Boost is re-licensed under Apache 2.0 - but this is not permitted.

Edit: Why not simply linking against boost.context?

Context

StackExchange Code Review Q#158739, answer score: 4

Revisions (0)

No revisions yet.