nvidia-texture-tools/src/nvtt/TaskDispatcher.h

150 lines
4.0 KiB
C
Raw Normal View History

2010-11-22 07:34:05 +00:00
#include "nvtt.h"
// OpenMP
2010-12-10 02:14:56 +00:00
// http://en.wikipedia.org/wiki/OpenMP
2010-11-22 07:34:05 +00:00
#if defined(HAVE_OPENMP)
#include <omp.h>
#endif
2010-12-10 02:14:56 +00:00
// Gran Central Dispatch (GCD/libdispatch)
// http://developer.apple.com/mac/library/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
2010-11-26 07:20:23 +00:00
#if NV_OS_DARWIN && defined(HAVE_DISPATCH_H)
2018-02-06 02:55:07 +00:00
//#define HAVE_GCD 1
//#include <dispatch/dispatch.h>
2010-11-22 07:34:05 +00:00
#endif
2010-12-10 02:14:56 +00:00
// Parallel Patterns Library (PPL) is part of Microsoft's concurrency runtime:
// http://msdn.microsoft.com/en-us/library/dd504870.aspx
2010-12-10 10:29:04 +00:00
#if NV_OS_WIN32 && _MSC_VER >= 1600
//#define HAVE_PPL 1
2012-02-02 16:21:48 +00:00
#include <ppl.h>
#endif
2010-12-10 02:14:56 +00:00
// Intel Thread Building Blocks (TBB).
// http://www.threadingbuildingblocks.org/
2010-12-09 20:57:41 +00:00
#if defined(HAVE_TBB)
#include <tbb/parallel_for.h>
#endif
2011-09-27 17:48:46 +00:00
#include "nvthread/ParallelFor.h"
2010-11-22 07:34:05 +00:00
namespace nvtt {
struct SequentialTaskDispatcher : public TaskDispatcher
{
2010-12-10 10:29:04 +00:00
virtual void dispatch(Task * task, void * context, int count) {
for (int i = 0; i < count; i++) {
2010-11-22 07:34:05 +00:00
task(context, i);
}
}
};
2011-09-27 17:48:46 +00:00
struct ParallelTaskDispatcher : public TaskDispatcher
{
virtual void dispatch(Task * task, void * context, int count) {
nv::ParallelFor parallelFor(task, context);
parallelFor.run(count); // @@ Add support for custom grain.
}
};
2010-12-10 02:14:56 +00:00
#if defined(HAVE_OPENMP)
2010-11-22 07:34:05 +00:00
2010-12-10 02:14:56 +00:00
struct OpenMPTaskDispatcher : public TaskDispatcher
2010-11-22 07:34:05 +00:00
{
2010-12-10 10:29:04 +00:00
virtual void dispatch(Task * task, void * context, int count) {
2010-12-10 02:14:56 +00:00
#pragma omp parallel for
2010-12-10 10:29:04 +00:00
for (int i = 0; i < count; i++) {
2010-12-10 02:14:56 +00:00
task(context, i);
}
2010-11-22 07:34:05 +00:00
}
};
#endif
2018-02-06 02:55:07 +00:00
#if HAVE_GCD
2010-11-22 07:34:05 +00:00
2010-12-10 02:14:56 +00:00
// Task dispatcher using Apple's Grand Central Dispatch.
struct AppleTaskDispatcher : public TaskDispatcher
2010-11-22 07:34:05 +00:00
{
2010-12-10 10:29:04 +00:00
// @@ This is really lame, but I refuse to use size_t in the public API.
struct BlockContext {
Task * task;
void * context;
};
static void block(void * context, size_t id) {
BlockContext * ctx = (BlockContext *)context;
ctx->task(ctx->context, int(id));
}
virtual void dispatch(Task * task, void * context, int count) {
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
BlockContext blockCtx = { task, context };
dispatch_apply_f(count, q, &blockCtx, block);
2010-11-22 07:34:05 +00:00
}
};
#endif
#if defined(HAVE_PPL)
2010-12-10 10:29:04 +00:00
struct TaskFunctor {
TaskFunctor(Task * task, void * context) : task(task), context(context) {}
2012-02-02 16:21:48 +00:00
void operator()(int n) const {
2010-12-10 10:29:04 +00:00
task(context, n);
}
Task * task;
void * context;
};
2010-12-10 10:29:04 +00:00
// Task dispatcher using Microsoft's concurrency runtime.
struct MicrosoftTaskDispatcher : public TaskDispatcher
{
2010-12-10 10:29:04 +00:00
virtual void dispatch(Task * task, void * context, int count)
{
TaskFunctor func(task, context);
2012-02-02 16:21:48 +00:00
Concurrency::parallel_for(0, count, func);
}
};
2011-09-27 17:48:46 +00:00
#endif
2010-12-09 20:57:41 +00:00
#if defined(HAVE_TBB)
2010-12-10 10:29:04 +00:00
struct TaskFunctor {
TaskFunctor(Task * task, void * context) : task(task), context(context) {}
void operator()(int & n) const {
task(context, n);
}
Task * task;
void * context;
2010-12-09 20:57:41 +00:00
};
2010-12-10 10:29:04 +00:00
// Task dispatcher using Inte's Thread Building Blocks.
2010-12-09 20:57:41 +00:00
struct IntelTaskDispatcher : public TaskDispatcher
{
2010-12-10 10:29:04 +00:00
virtual void dispatch(Task * task, void * context, int count) {
parallel_for(blocked_range<int>(0, count, 1), TaskFunctor(task, context));
2010-12-09 20:57:41 +00:00
}
};
#endif
2010-12-10 10:29:04 +00:00
#if defined(HAVE_OPENMP)
typedef OpenMPTaskDispatcher ConcurrentTaskDispatcher;
#elif defined(HAVE_TBB)
typedef IntelTaskDispatcher ConcurrentTaskDispatcher;
#elif defined(HAVE_PPL)
typedef MicrosoftTaskDispatcher ConcurrentTaskDispatcher;
#elif defined(HAVE_GCD)
typedef AppleTaskDispatcher ConcurrentTaskDispatcher;
#else
2011-09-27 17:48:46 +00:00
//typedef SequentialTaskDispatcher ConcurrentTaskDispatcher;
typedef ParallelTaskDispatcher ConcurrentTaskDispatcher;
2010-12-10 10:29:04 +00:00
#endif
2010-11-22 07:34:05 +00:00
} // namespace nvtt