2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
18 #include "SpuLibspe2Support.h"
23 //SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
24 ///Setup and initialize SPU/CELL/Libspe2
25 SpuLibspe2Support::SpuLibspe2Support(spe_program_handle_t *speprog, int numThreads)
27 this->program = speprog;
28 this->numThreads = ((numThreads <= spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1)) ? numThreads : spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1));
31 ///cleanup/shutdown Libspe2
32 SpuLibspe2Support::~SpuLibspe2Support()
40 ///send messages to SPUs
41 void SpuLibspe2Support::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1)
43 spe_context_ptr_t context;
47 case CMD_SAMPLE_TASK_COMMAND:
50 SpuSampleTaskDesc* taskDesc = (SpuSampleTaskDesc*) uiArgument0;
52 btAssert(taskDesc->m_taskId<m_activeSpuStatus.size());
54 //get status of SPU on which task should run
55 btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->m_taskId];
57 //set data for spuStatus
58 spuStatus.m_commandId = uiCommand;
59 spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
60 spuStatus.m_taskDesc.p = taskDesc;
63 context = data[taskDesc->m_taskId].context;
66 taskDesc->m_mainMemoryPtr = reinterpret_cast<uint64_t> (spuStatus.m_lsMemory.p);
71 case CMD_GATHER_AND_PROCESS_PAIRLIST:
74 SpuGatherAndProcessPairsTaskDesc* taskDesc = (SpuGatherAndProcessPairsTaskDesc*) uiArgument0;
76 btAssert(taskDesc->taskId<m_activeSpuStatus.size());
78 //get status of SPU on which task should run
79 btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->taskId];
81 //set data for spuStatus
82 spuStatus.m_commandId = uiCommand;
83 spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
84 spuStatus.m_taskDesc.p = taskDesc;
87 context = data[taskDesc->taskId].context;
90 taskDesc->m_lsMemory = (CollisionTask_LocalStoreMemory*)spuStatus.m_lsMemory.p;
103 //write taskdescription in mailbox
104 unsigned int event = Spu_Mailbox_Event_Task;
105 spe_in_mbox_write(context, &event, 1, SPE_MBOX_ANY_NONBLOCKING);
109 ///check for messages from SPUs
110 void SpuLibspe2Support::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
112 ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
114 ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
116 btAssert(m_activeSpuStatus.size());
121 //find an active spu/thread
124 for (int i=0;i<m_activeSpuStatus.size();i++)
126 if ( m_activeSpuStatus[i].m_status == Spu_Status_Free)
138 btSpuStatus& spuStatus = m_activeSpuStatus[last];
140 ///need to find an active spu
145 *puiArgument0 = spuStatus.m_taskId;
146 *puiArgument1 = spuStatus.m_status;
152 void SpuLibspe2Support::startSPU()
154 this->internal_startSPU();
159 ///start the spus group (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
160 void SpuLibspe2Support::internal_startSPU()
162 m_activeSpuStatus.resize(numThreads);
165 for (int i=0; i < numThreads; i++)
168 if(data[i].context == NULL)
172 if ((data[i].context = spe_context_create(0, NULL)) == NULL)
174 perror ("Failed creating context");
178 /* Load program into context */
179 if(spe_program_load(data[i].context, this->program))
181 perror ("Failed loading program");
185 m_activeSpuStatus[i].m_status = Spu_Status_Startup;
186 m_activeSpuStatus[i].m_taskId = i;
187 m_activeSpuStatus[i].m_commandId = 0;
188 m_activeSpuStatus[i].m_lsMemory.p = NULL;
191 data[i].entry = SPE_DEFAULT_ENTRY;
193 data[i].argp.p = &m_activeSpuStatus[i];
194 data[i].envp.p = NULL;
196 /* Create thread for each SPE context */
197 if (pthread_create(&data[i].pthread, NULL, &ppu_pthread_function, &(data[i]) ))
199 perror ("Failed creating thread");
205 printf("started thread %d\n",i);
211 for (int i=0; i < numThreads; i++)
213 if(data[i].context != NULL)
215 while( m_activeSpuStatus[i].m_status == Spu_Status_Startup)
217 // wait for spu to set up
220 printf("Spu %d is ready\n", i);
225 ///tell the task scheduler we are done with the SPU tasks
226 void SpuLibspe2Support::stopSPU()
228 // wait for all threads to finish
230 for ( i = 0; i < this->numThreads; i++ )
233 unsigned int event = Spu_Mailbox_Event_Shutdown;
234 spe_context_ptr_t context = data[i].context;
235 spe_in_mbox_write(context, &event, 1, SPE_MBOX_ALL_BLOCKING);
236 pthread_join (data[i].pthread, NULL);
240 spe_image_close(program);
241 // destroy SPE contexts
242 for ( i = 0; i < this->numThreads; i++ )
244 if(data[i].context != NULL)
246 spe_context_destroy (data[i].context);
250 m_activeSpuStatus.clear();