Skip to content

Commit

Permalink
Merge pull request #970 from fspadoni/CpuTaskAndThreadType
Browse files Browse the repository at this point in the history
[TaskScheduler] Cpu task and scheduled thread support
  • Loading branch information
guparan authored Apr 3, 2019
2 parents 9cbb775 + 0499ddc commit cf2f243
Show file tree
Hide file tree
Showing 18 changed files with 1,370 additions and 1,548 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,66 @@ using sofa::simulation::Task;
namespace sofa
{


Task::MemoryAlloc FibonacciTask::run()
{
if (_N < 2)
{
*_sum = _N;
return MemoryAlloc::Stack;
}

Task::Status status;

int64_t x, y;

{
if (_N < 2)
{
*_sum = _N;
return MemoryAlloc::Stack;
}
simulation::CpuTask::Status status;
int64_t x, y;
simulation::TaskScheduler* scheduler = simulation::TaskScheduler::getInstance();

FibonacciTask task0(_N - 1, &x, &status);
FibonacciTask task1(_N - 2, &y, &status);

FibonacciTask task0(_N - 1, &x, &status);
FibonacciTask task1(_N - 2, &y, &status);
scheduler->addTask(&task0);
scheduler->addTask(&task1);
scheduler->workUntilDone(&status);
// Do the sum
*_sum = x + y;

return MemoryAlloc::Stack;
}



// Do the sum
*_sum = x + y;
return MemoryAlloc::Stack;
}
Task::MemoryAlloc IntSumTask::run()
{
const int64_t count = _last - _first;
if (count < 1)
{
*_sum = _first;
return MemoryAlloc::Stack;
}

const int64_t mid = _first + (count / 2);

Task::Status status;

int64_t x, y;

{
const int64_t count = _last - _first;
if (count < 1)
{
*_sum = _first;
return MemoryAlloc::Stack;
}
const int64_t mid = _first + (count / 2);
simulation::CpuTask::Status status;
int64_t x, y;
simulation::TaskScheduler* scheduler = simulation::TaskScheduler::getInstance();

IntSumTask task0(_first, mid, &x, &status);
IntSumTask task1(mid+1, _last, &y, &status);

IntSumTask task0(_first, mid, &x, &status);
IntSumTask task1(mid+1, _last, &y, &status);
scheduler->addTask(&task0);
scheduler->addTask(&task1);
scheduler->workUntilDone(&status);

// Do the sum
*_sum = x + y;


return MemoryAlloc::Stack;
}
// Do the sum
*_sum = x + y;
return MemoryAlloc::Stack;
}

} // namespace sofa
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,50 @@
namespace sofa
{

// compute recursively the Fibonacci number for input N O(~1.6 exp(N))
// this is implemented to test the task scheduler generating super lightweight tasks and not for performance
class FibonacciTask : public simulation::Task
{
public:
FibonacciTask(const int64_t N, int64_t* const sum, simulation::Task::Status* status)
: Task(status)
, _N(N)
, _sum(sum)
{}

~FibonacciTask() override { }

MemoryAlloc run() final;

private:

const int64_t _N;
int64_t* const _sum;
};


// compute recursively the sum of integers from first to last
// this is implemented to test the task scheduler generating super lightweight tasks and not for performance
class IntSumTask : public simulation::Task
{
public:
IntSumTask(const int64_t first, const int64_t last, int64_t* const sum, simulation::Task::Status* status)
: Task(status)
, _first(first)
, _last(last)
, _sum(sum)
{}

~IntSumTask() override {}

MemoryAlloc run() final;


private:

const int64_t _first;
const int64_t _last;
int64_t* const _sum;

};
// compute recursively the Fibonacci number for input N O(~1.6 exp(N))
// this is implemented to test the task scheduler generating super lightweight tasks and not for performance
class FibonacciTask : public simulation::CpuTask
{
public:
FibonacciTask(const int64_t N, int64_t* const sum, simulation::CpuTask::Status* status)
: CpuTask(status)
, _N(N)
, _sum(sum)
{}
~FibonacciTask() override { }
MemoryAlloc run() final;
private:
const int64_t _N;
int64_t* const _sum;
};
// compute recursively the sum of integers from first to last
// this is implemented to test the task scheduler generating super lightweight tasks and not for performance
class IntSumTask : public simulation::CpuTask
{
public:
IntSumTask(const int64_t first, const int64_t last, int64_t* const sum, simulation::CpuTask::Status* status)
: CpuTask(status)
, _first(first)
, _last(last)
, _sum(sum)
{}
~IntSumTask() override {}
MemoryAlloc run() final;
private:
const int64_t _first;
const int64_t _last;
int64_t* const _sum;
};
} // namespace sofa
106 changes: 53 additions & 53 deletions SofaKernel/framework/framework_test/simulation/TaskSchedulerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@
namespace sofa
{

// compute the Fibonacci number for input N
static int64_t Fibonacci(int64_t N, int nbThread = 0)
{
// compute the Fibonacci number for input N
static int64_t Fibonacci(int64_t N, int nbThread = 0)
{
simulation::TaskScheduler* scheduler = simulation::TaskScheduler::create(simulation::DefaultTaskScheduler::name());
scheduler->init(nbThread);

simulation::Task::Status status;
int64_t result = 0;

FibonacciTask task(N, &result, &status);
simulation::CpuTask::Status status;
int64_t result = 0;
FibonacciTask task(N, &result, &status);
scheduler->addTask(&task);
scheduler->workUntilDone(&status);

scheduler->stop();
return result;
}


// compute the sum of integers from 1 to N
static int64_t IntSum1ToN(const int64_t N, int nbThread = 0)
{
return result;
}
// compute the sum of integers from 1 to N
static int64_t IntSum1ToN(const int64_t N, int nbThread = 0)
{
simulation::TaskScheduler* scheduler = simulation::TaskScheduler::create(simulation::DefaultTaskScheduler::name());
scheduler->init(nbThread);

simulation::Task::Status status;
int64_t result = 0;

IntSumTask task(1, N, &result, &status);
simulation::CpuTask::Status status;
int64_t result = 0;
IntSumTask task(1, N, &result, &status);
scheduler->addTask(&task);
scheduler->workUntilDone(&status);

scheduler->stop();
return result;
}



// compute the Fibonacci single thread
TEST(TaskSchedulerTests, FibonacciSingle )
{
return result;
}
// compute the Fibonacci single thread
TEST(TaskSchedulerTests, FibonacciSingle )
{
// tested with
// 3 : 2
// 6 : 8
Expand All @@ -59,12 +59,12 @@ namespace sofa
// 47 : 2971215073
const int64_t res = Fibonacci(27, 1);
EXPECT_EQ(res, 196418);
return;
}

// compute the Fibonacci multi thread
TEST(TaskSchedulerTests, FibonacciMulti)
{
return;
}
// compute the Fibonacci multi thread
TEST(TaskSchedulerTests, FibonacciMulti)
{
// tested with
// 3 : 2
// 6 : 8
Expand All @@ -77,26 +77,26 @@ namespace sofa
// 47 : 2971215073
const int64_t res = Fibonacci(27);
EXPECT_EQ(res, 196418);
return;
}

// compute the sum of integers from 1 to N single thread
TEST(TaskSchedulerTests, IntSumSingle)
{
return;
}
// compute the sum of integers from 1 to N single thread
TEST(TaskSchedulerTests, IntSumSingle)
{
const int64_t N = 1 << 20;
int64_t res = IntSum1ToN(N, 1);
EXPECT_EQ(res, (N)*(N+1)/2);
return;
}

// compute the sum of integers from 1 to N multi thread
TEST(TaskSchedulerTests, IntSumMulti)
{
EXPECT_EQ(res, (N)*(N+1)/2);
return;
}
// compute the sum of integers from 1 to N multi thread
TEST(TaskSchedulerTests, IntSumMulti)
{
const int64_t N = 1 << 20;
int64_t res = IntSum1ToN(N);
EXPECT_EQ(res, (N)*(N + 1) / 2);
return;
}

EXPECT_EQ(res, (N)*(N + 1) / 2);
return;
}

} // namespace sofa
Loading

0 comments on commit cf2f243

Please sign in to comment.