some changes towards libspe2 support

This commit is contained in:
ejcoumans
2007-08-13 04:27:36 +00:00
parent 89272639e9
commit 620b67b752
2 changed files with 64 additions and 43 deletions

View File

@@ -23,9 +23,11 @@ subject to the following restrictions:
///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
///Setup and initialize SPU/CELL/Libspe2
SpuLibspe2Support::SpuLibspe2Support(SpuLibspe2ElfId_t elfId,int numThreads)
SpuLibspe2Support::SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads)
{
program = speprog
startSPUs(numThreads);
N = numThreads;
}
///cleanup/shutdown Libspe2
@@ -38,6 +40,7 @@ SpuLibspe2Support::~SpuLibspe2Support()
#include <stdio.h>
#ifdef WIN32
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{
@@ -66,7 +69,7 @@ DWORD WINAPI Thread_no_1( LPVOID lpParam )
return 0;
}
#endif
///send messages to SPUs
void SpuLibspe2Support::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1)
{
@@ -168,43 +171,24 @@ void SpuLibspe2Support::waitForResponse(unsigned int *puiArgument0, unsigned int
}
///start the spus group (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
void SpuLibspe2Support::startSPUs(int numThreads)
{
#ifdef WIN32
m_activeSpuStatus.resize(numThreads);
#endif
for (int i=0;i<numThreads;i++)
{
printf("starting thread %d\n",i);
btSpuStatus& spuStatus = m_activeSpuStatus[i];
LPSECURITY_ATTRIBUTES lpThreadAttributes=NULL;
SIZE_T dwStackSize=65535;
LPTHREAD_START_ROUTINE lpStartAddress=&Thread_no_1;
LPVOID lpParameter=&spuStatus;
DWORD dwCreationFlags=0;
LPDWORD lpThreadId=0;
spuStatus.m_taskDesc = 0;
sprintf(spuStatus.m_eventStartHandleName,"eventStart%d",i);
spuStatus.m_eventStartHandle = CreateEvent(0,false,false,spuStatus.m_eventStartHandleName);
sprintf(spuStatus.m_eventCompletetHandleName,"eventComplete%d",i);
spuStatus.m_eventCompletetHandle = CreateEvent(0,false,false,spuStatus.m_eventCompletetHandleName);
HANDLE handle = CreateThread(lpThreadAttributes,dwStackSize,lpStartAddress,lpParameter, dwCreationFlags,lpThreadId);
SetThreadPriority(handle,THREAD_PRIORITY_TIME_CRITICAL);
spuStatus.m_taskId = i;
spuStatus.m_commandId = 0;
spuStatus.m_status = 0;
spuStatus.m_threadHandle = handle;
spuStatus.m_lsMemory = createLocalStoreMemory();
data[i].context = spe_context_create(0, NULL);
spe_program_load(data[i].context, program);
data[i].entry = SPE_DEFAULT_ENTRY;
data[i].flags = 0;
data[i].argp = NULL;
data[i].envp = NULL;
pthread_create(&data[i].pthread, NULL, &ppu_pthread_function, &data[i]);
printf("started thread %d with threadHandle %d\n",i,handle);
}
@@ -214,9 +198,17 @@ void SpuLibspe2Support::startSPUs(int numThreads)
///tell the task scheduler we are done with the SPU tasks
void SpuLibspe2Support::stopSPUs()
{
// m_activeSpuStatus.pop_back();
// WaitForSingleObject(spuStatus.bla, INFINITE);
// CloseHandle(spuStatus.m_threadHandle);
// wait for all threads to finish
for ( i=0; i<N; i++ ) {
pthread_join (data[i].pthread, NULL);
}
// close SPE program
spe_image_close(program);
// destroy SPE contexts
for ( i=0; i<N; i++ ) {
spe_context_destroy (data[i].context);
}
}

View File

@@ -21,6 +21,8 @@ subject to the following restrictions:
#define SPU_LIBSPE2_SUPPORT_H
#include "LinearMath/btAlignedObjectArray.h"
#include <libspe2.h>
#include <pthread.h>
/**
@@ -31,6 +33,7 @@ subject to the following restrictions:
* Mixing up these values will cause the wrong code to execute, for instance, the
* solver may be asked to do a collision detection job.
*/
#ifdef WIN32 // original enum, but for libspe2 will pass the SPE program handle ptr directly
typedef enum {
SPU_ELF_COLLISION_DETECTION=0,
SPU_ELF_SAMPLE,
@@ -38,7 +41,7 @@ typedef enum {
//SPU_ELF_SOLVER,
SPU_ELF_LAST,
} SpuLibspe2ElfId_t;
#endif
#ifdef WIN32
#include <malloc.h>
#define memalign(alignment, size) malloc(size);
@@ -46,6 +49,27 @@ typedef enum {
#include <stdlib.h>
#endif // WIN32
#define MAXSPUS 16
typedef struct ppu_pthread_data {
spe_context_ptr_t context;
pthread_t pthread;
unsigned int entry;
unsigned int flags;
void *argp;
void *envp;
spe_stop_info_t stopinfo;
} ppu_pthread_data_t;
void *ppu_pthread_function(void *arg)
{
ppu_pthread_data_t datap = *(ppu_pthread_data_t *)arg;
int rc;
do {
rc = spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp, datap->envp, &datap->stopinfo);
} while (rc > 0); // loop until exit or error, and while any stop & signal
pthread_exit(NULL);
}
#include <LinearMath/btScalar.h> //for uint32_t etc.
@@ -78,8 +102,13 @@ class SpuLibspe2Support {
public:
///Setup and initialize SPU/CELL/Libspe2
SpuLibspe2Support(SpuLibspe2ElfId_t elfId,int numThreads);
SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads);
// SPE program handle ptr.
spe_program_handle_t *program;
// SPE program data
ppu_pthread_data_t data[MAX_SPUS];
// num SPE Threads
unsigned int N;
///cleanup/shutdown Libspe2
~SpuLibspe2Support();