Parallel task schedulers, libbulletc must be compiled with BULLET2_USE_THREAD_LOCKS and BULLET2_USE_X_MULTITHREADING

This commit is contained in:
AndresTraks 2017-06-16 02:04:42 +03:00
Родитель e1b3aa1115
Коммит 131af9e224
8 изменённых файлов: 197 добавлений и 4 удалений

Просмотреть файл

@ -200,6 +200,7 @@
<Compile Include="LinearMath\PolarDecomposition.cs" />
<Compile Include="LinearMath\PoolAllocator.cs" />
<Compile Include="LinearMath\Serializer.cs" />
<Compile Include="LinearMath\Threads.cs" />
<Compile Include="LinearMath\TransformUtil.cs" />
<Compile Include="MathUtil.cs">
<SubType>Code</SubType>

Просмотреть файл

@ -0,0 +1,114 @@
using System;
namespace BulletSharp
{
public abstract class TaskScheduler
{
internal readonly IntPtr Native;
internal TaskScheduler(IntPtr native)
{
Native = native;
}
}
public sealed class TaskSchedulerSequential : TaskScheduler
{
internal TaskSchedulerSequential(IntPtr native)
: base(native)
{
}
}
public sealed class TaskSchedulerOpenMP : TaskScheduler
{
internal TaskSchedulerOpenMP(IntPtr native)
: base(native)
{
}
}
public sealed class TaskSchedulerTbb : TaskScheduler
{
internal TaskSchedulerTbb(IntPtr native)
: base(native)
{
}
}
public sealed class TaskSchedulerPpl : TaskScheduler
{
internal TaskSchedulerPpl(IntPtr native)
: base(native)
{
}
}
public class Threads
{
private static TaskSchedulerOpenMP _taskSchedulerOpenMP;
private static TaskSchedulerPpl _taskSchedulerPpl;
private static TaskSchedulerSequential _taskSchedulerSequential;
private static TaskSchedulerTbb _taskSchedulerTbb;
private static TaskScheduler _taskScheduler;
public static TaskScheduler TaskScheduler
{
get { return _taskScheduler; }
set
{
_taskScheduler = value;
UnsafeNativeMethods.btThreads_btSetTaskScheduler(value != null ? value.Native : IntPtr.Zero);
}
}
public static TaskSchedulerOpenMP GetOpenMPTaskScheduler()
{
if (_taskSchedulerOpenMP == null)
{
IntPtr native = UnsafeNativeMethods.btThreads_btGetOpenMPTaskScheduler();
if (native != IntPtr.Zero)
{
_taskSchedulerOpenMP = new TaskSchedulerOpenMP(native);
}
}
return _taskSchedulerOpenMP;
}
public static TaskSchedulerPpl GetPplTaskScheduler()
{
if (_taskSchedulerPpl == null)
{
IntPtr native = UnsafeNativeMethods.btThreads_btGetPPLTaskScheduler();
if (native != IntPtr.Zero)
{
_taskSchedulerPpl = new TaskSchedulerPpl(native);
}
}
return _taskSchedulerPpl;
}
public static TaskSchedulerSequential GetSequentialTaskScheduler()
{
if (_taskSchedulerSequential == null)
{
_taskSchedulerSequential = new TaskSchedulerSequential(UnsafeNativeMethods.btThreads_btGetSequentialTaskScheduler());
}
return _taskSchedulerSequential;
}
public static TaskSchedulerTbb GetTbbTaskScheduler()
{
if (_taskSchedulerTbb == null)
{
IntPtr native = UnsafeNativeMethods.btThreads_btGetTBBTaskScheduler();
if (native != IntPtr.Zero)
{
_taskSchedulerTbb = new TaskSchedulerTbb(native);
}
}
return _taskSchedulerTbb;
}
}
}

Просмотреть файл

@ -469,6 +469,17 @@ namespace BulletSharp
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern void btQuantizedBvhTree_delete(IntPtr obj);
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern IntPtr btThreads_btGetOpenMPTaskScheduler();
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern IntPtr btThreads_btGetPPLTaskScheduler();
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern IntPtr btThreads_btGetSequentialTaskScheduler();
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern IntPtr btThreads_btGetTBBTaskScheduler();
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern void btThreads_btSetTaskScheduler(IntPtr taskScheduler);
[DllImport(Native.Dll, CallingConvention = Native.Conv)]
public static extern IntPtr GIM_BVH_DATA_new();
[DllImport(Native.Dll, CallingConvention = Native.Conv)]

Просмотреть файл

@ -20,8 +20,9 @@ namespace BasicDemo
{
demo.FreeLook.Eye = new Vector3(30, 20, 15);
demo.FreeLook.Target = new Vector3(0, 3, 0);
demo.Graphics.WindowTitle = "BulletSharp - Multi-threaded Demo";
return new MultiThreadedDemoSimulation();
var simulation = new MultiThreadedDemoSimulation();
demo.Graphics.WindowTitle = $"BulletSharp - Multi-threaded Demo ({Threads.TaskScheduler.GetType().Name})";
return simulation;
}
}
@ -34,12 +35,13 @@ namespace BasicDemo
public MultiThreadedDemoSimulation()
{
SetTaskScheduler();
using (var collisionConfigurationInfo = new DefaultCollisionConstructionInfo
{
DefaultMaxPersistentManifoldPoolSize = 80000,
DefaultMaxCollisionAlgorithmPoolSize = 80000
}
)
})
{
CollisionConfiguration = new DefaultCollisionConfiguration(collisionConfigurationInfo);
};
@ -63,6 +65,24 @@ namespace BasicDemo
this.StandardCleanup();
}
private void SetTaskScheduler()
{
TaskScheduler scheduler = Threads.GetOpenMPTaskScheduler();
if (scheduler == null)
{
scheduler = Threads.GetTbbTaskScheduler();
}
if (scheduler == null)
{
scheduler = Threads.GetPplTaskScheduler();
}
if (scheduler == null)
{
scheduler = Threads.GetSequentialTaskScheduler();
}
Threads.TaskScheduler = scheduler;
}
private void CreateGround()
{
var groundShape = new BoxShape(50, 1, 50);

Просмотреть файл

@ -353,6 +353,8 @@ ADD_LIBRARY(${BULLETC_LIB} SHARED
src/btStridingMeshInterface_wrap.h
src/btTetrahedronShape_wrap.cpp
src/btTetrahedronShape_wrap.h
src/btThreads_wrap.cpp
src/btThreads_wrap.h
src/btTransformUtil_wrap.cpp
src/btTransformUtil_wrap.h
src/btTriangleBuffer_wrap.cpp

Просмотреть файл

@ -0,0 +1,28 @@
#include <LinearMath/btThreads.h>
#include "btThreads_wrap.h"
btITaskScheduler* btThreads_btGetSequentialTaskScheduler()
{
return btGetSequentialTaskScheduler();
}
btITaskScheduler* btThreads_btGetOpenMPTaskScheduler()
{
return btGetOpenMPTaskScheduler();
}
btITaskScheduler* btThreads_btGetPPLTaskScheduler()
{
return btGetPPLTaskScheduler();
}
btITaskScheduler* btThreads_btGetTBBTaskScheduler()
{
return btGetTBBTaskScheduler();
}
void btThreads_btSetTaskScheduler(btITaskScheduler* ts)
{
btSetTaskScheduler(ts);
}

Просмотреть файл

@ -0,0 +1,13 @@
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
EXPORT btITaskScheduler* btThreads_btGetSequentialTaskScheduler();
EXPORT btITaskScheduler* btThreads_btGetOpenMPTaskScheduler();
EXPORT btITaskScheduler* btThreads_btGetPPLTaskScheduler();
EXPORT btITaskScheduler* btThreads_btGetTBBTaskScheduler();
EXPORT void btThreads_btSetTaskScheduler(btITaskScheduler* ts);
#ifdef __cplusplus
}
#endif

Просмотреть файл

@ -964,6 +964,10 @@
#define btThreadSupportInterface void
#endif
#ifndef BT_THREADS_H
#define btITaskScheduler void
#endif
#ifndef BT_TRANSFORM_H
#define btTransform void
#endif