implement barrier/critical section for OSX (PosixThreadSupport)

enable Demos/ThreadingDemo for OSX
add cmake build support for VectorAdd_OpenCL for OSX
This commit is contained in:
erwin.coumans
2010-06-28 23:03:14 +00:00
parent 8bf91f735c
commit 7bfa94b6a3
8 changed files with 234 additions and 14 deletions

View File

@@ -111,9 +111,6 @@ IF(WIN32)
ENDIF()
ENDIF()
OPTION(BUILD_MINICL_OPENCL_DEMOS "Build OpenCL demos for MiniCL (Generic CPU)" OFF)
OPTION(BUILD_CPU_DEMOS "Build original Bullet CPU demos" ON)
IF (INTERNAL_CREATE_MSVC_RELATIVE_PATH_PROJECTFILES)
@@ -123,6 +120,12 @@ ENDIF(INTERNAL_CREATE_MSVC_RELATIVE_PATH_PROJECTFILES)
ENDIF (WIN32)
OPTION(BUILD_MINICL_OPENCL_DEMOS "Build OpenCL demos for MiniCL (Generic CPU)" ON)
OPTION(BUILD_CPU_DEMOS "Build original Bullet CPU demos" ON)
OPTION(INTERNAL_UPDATE_SERIALIZATION_STRUCTURES "Internal update serialization structures" OFF)
IF (INTERNAL_UPDATE_SERIALIZATION_STRUCTURES)
ADD_DEFINITIONS( -DBT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES)

View File

@@ -2,7 +2,7 @@
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
${BULLET_PHYSICS_SOURCE_DIR}/Extras/OpenCL/Shared
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL
${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
)
@@ -42,15 +42,15 @@ IF (USE_GLUT)
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesSharedDefs.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesSharedTypes.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesDemo.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/shaders.h
${BULLET_PHYSICS_SOURCE_DIR}/Extras/OpenCL/Shared/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.h
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclUtils.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/btParticlesDemoDynamicsWorld.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/main.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesDemo.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/shaders.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Extras/OpenCL/Shared/btOclCommon.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/SharedOpenCL/btOclCommon.cpp
${BULLET_PHYSICS_SOURCE_DIR}/Demos/ParticlesOpenCL/ParticlesOCL.cl
)
ELSE (USE_GLUT)

View File

@@ -12,7 +12,7 @@ SET(GLUT_ROOT ${BULLET_PHYSICS_SOURCE_DIR}/Glut)
########################################################
#currently this demo has only been tested under Windows 32bit
IF (WIN32)
#IF (WIN32)
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL
@@ -42,4 +42,4 @@ IF (INTERNAL_ADD_POSTFIX_EXECUTABLE_NAMES)
SET_TARGET_PROPERTIES(AppThreadingDemo PROPERTIES RELWITHDEBINFO_POSTFIX "_RelWithDebugInfo")
ENDIF(INTERNAL_ADD_POSTFIX_EXECUTABLE_NAMES)
ENDIF(WIN32)
#ENDIF(WIN32)

View File

@@ -20,8 +20,40 @@ subject to the following restrictions:
/// June 2010
/// New: critical section/barriers and non-blocking pollingn for completion, currently Windows only
void SampleThreadFunc(void* userPtr,void* lsMemory);
void* SamplelsMemoryFunc();
#ifdef __APPLE__
#include "BulletMultiThreaded/PosixThreadSupport.h"
btThreadSupportInterface* createThreadSupport(int numThreads)
{
PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
SampleThreadFunc,
SamplelsMemoryFunc,
numThreads);
btThreadSupportInterface* threadSupport = new PosixThreadSupport(constructionInfo);
return threadSupport;
}
#elif defined( _WIN32)
#include "BulletMultiThreaded/Win32ThreadSupport.h"
btThreadSupportInterface* createThreadSupport(int numThreads)
{
Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
return threadSupport;
}
#endif
struct SampleArgs
{
btCriticalSection* m_cs;
@@ -64,19 +96,26 @@ void* SamplelsMemoryFunc()
int main(int argc,char** argv)
{
int numThreads = 4;
Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
btThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
threadSupport->startSPU();
for (int i=0;i<threadSupport->getNumTasks();i++)
{
SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);
btAssert(storage);
storage->threadId = i;
}
@@ -93,7 +132,7 @@ int main(int argc,char** argv)
threadSupport->sendRequest(1, (ppu_address_t) &args, i);
}
bool blockingWait = false;
bool blockingWait =true;
if (blockingWait)
{
for (i=0;i<numThreads;i++)
@@ -102,6 +141,7 @@ int main(int argc,char** argv)
}
} else
{
#if 0
int numActiveThreads = numThreads;
while (numActiveThreads)
{
@@ -115,6 +155,7 @@ int main(int argc,char** argv)
printf("polling\n");
}
};
#endif
}
threadSupport->stopSPU();

View File

@@ -0,0 +1,25 @@
# AppVectorAdd is a very basic test OpenCL/MiniCL.
IF (APPLE)
FIND_LIBRARY(OPENCL_LIBRARY OpenCL DOC "OpenCL lib for OSX")
FIND_PATH(OPENCL_INCLUDE_DIR OpenCL/cl.h DOC "Include for OpenCL on OSX")
ENDIF (APPLE)
INCLUDE_DIRECTORIES(
${BULLET_PHYSICS_SOURCE_DIR}/src
)
LINK_LIBRARIES(
LinearMath ${OPENCL_LIBRARY}
)
ADD_EXECUTABLE(AppVectorAdd_Apple
../MiniCL_VectorAdd.cpp
../VectorAddKernels.cl
)
IF (UNIX)
TARGET_LINK_LIBRARIES(AppVectorAdd_Apple pthread)
ENDIF(UNIX)

View File

@@ -245,5 +245,143 @@ void PosixThreadSupport::stopSPU()
m_activeSpuStatus.clear();
}
class PosixCriticalSection : public btCriticalSection
{
pthread_mutex_t m_mutex;
public:
PosixCriticalSection()
{
pthread_mutex_init(&m_mutex, NULL);
}
virtual ~PosixCriticalSection()
{
pthread_mutex_destroy(&m_mutex);
}
ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
virtual unsigned int getSharedParam(int i)
{
return mCommonBuff[i];
}
virtual void setSharedParam(int i,unsigned int p)
{
mCommonBuff[i] = p;
}
virtual void lock()
{
pthread_mutex_lock(&m_mutex);
}
virtual void unlock()
{
pthread_mutex_unlock(&m_mutex);
}
};
#if defined(_POSIX_BARRIERS) && (_POSIX_BARRIERS - 20012L) >= 0
/* OK to use barriers on this platform */
class PosixBarrier : public btBarrier
{
pthread_barrier_t m_barr;
int m_numThreads;
public:
PosixBarrier()
:m_numThreads(0) { }
virtual ~PosixBarrier() {
pthread_barrier_destroy(&m_barr);
}
virtual void sync()
{
int rc = pthread_barrier_wait(&m_barr);
if(rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
{
printf("Could not wait on barrier\n");
exit(-1);
}
}
virtual void setMaxCount(int numThreads)
{
int result = pthread_barrier_init(&m_barr, NULL, numThreads);
m_numThreads = numThreads;
btAssert(result==0);
}
virtual int getMaxCount()
{
return m_numThreads;
}
};
#else
/* Not OK to use barriers on this platform - insert alternate code here */
class PosixBarrier : public btBarrier
{
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
int m_numThreads;
int m_called;
public:
PosixBarrier()
:m_numThreads(0)
{
}
virtual ~PosixBarrier()
{
if (m_numThreads>0)
{
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
}
virtual void sync()
{
pthread_mutex_lock(&m_mutex);
m_called++;
if (m_called == m_numThreads) {
m_called = 0;
pthread_cond_broadcast(&m_cond);
} else {
pthread_cond_wait(&m_cond,&m_mutex);
}
pthread_mutex_unlock(&m_mutex);
}
virtual void setMaxCount(int numThreads)
{
if (m_numThreads>0)
{
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
m_called = 0;
pthread_mutex_init(&m_mutex,NULL);
pthread_cond_init(&m_cond,NULL);
m_numThreads = numThreads;
}
virtual int getMaxCount()
{
return m_numThreads;
}
};
#endif//_POSIX_BARRIERS
btBarrier* PosixThreadSupport::createBarrier()
{
return new PosixBarrier();
}
btCriticalSection* PosixThreadSupport::createCriticalSection()
{
return new PosixCriticalSection();
}
#endif // USE_PTHREADS

View File

@@ -18,6 +18,7 @@ subject to the following restrictions:
#include "PlatformDefinitions.h"
#ifdef USE_PTHREADS //platform specific defines are defined in PlatformDefinitions.h
#define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
#include <pthread.h>
#include <semaphore.h>
@@ -117,6 +118,16 @@ public:
{
return m_activeSpuStatus.size();
}
virtual btBarrier* createBarrier();
virtual btCriticalSection* createCriticalSection();
virtual void* getThreadLocalMemory(int taskId)
{
return m_activeSpuStatus[taskId].m_lsMemory;
}
};
#endif // POSIX_THREAD_SUPPORT_H

View File

@@ -77,6 +77,8 @@ public:
virtual btCriticalSection* createCriticalSection() = 0;
virtual void* getThreadLocalMemory(int taskId) { return 0; }
};
#endif //THREAD_SUPPORT_INTERFACE_H