SpuLibspe2Support.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifdef USE_LIBSPE2
  14. #include "SpuLibspe2Support.h"
  15. //SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  16. ///Setup and initialize SPU/CELL/Libspe2
  17. SpuLibspe2Support::SpuLibspe2Support(spe_program_handle_t *speprog, int numThreads)
  18. {
  19. this->program = speprog;
  20. this->numThreads = ((numThreads <= spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1)) ? numThreads : spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1));
  21. }
  22. ///cleanup/shutdown Libspe2
  23. SpuLibspe2Support::~SpuLibspe2Support()
  24. {
  25. stopSPU();
  26. }
  27. ///send messages to SPUs
  28. void SpuLibspe2Support::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1)
  29. {
  30. spe_context_ptr_t context;
  31. switch (uiCommand)
  32. {
  33. case CMD_SAMPLE_TASK_COMMAND:
  34. {
  35. //get taskdescription
  36. SpuSampleTaskDesc* taskDesc = (SpuSampleTaskDesc*) uiArgument0;
  37. btAssert(taskDesc->m_taskId<m_activeSpuStatus.size());
  38. //get status of SPU on which task should run
  39. btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->m_taskId];
  40. //set data for spuStatus
  41. spuStatus.m_commandId = uiCommand;
  42. spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
  43. spuStatus.m_taskDesc.p = taskDesc;
  44. //get context
  45. context = data[taskDesc->m_taskId].context;
  46. taskDesc->m_mainMemoryPtr = reinterpret_cast<uint64_t> (spuStatus.m_lsMemory.p);
  47. break;
  48. }
  49. case CMD_GATHER_AND_PROCESS_PAIRLIST:
  50. {
  51. //get taskdescription
  52. SpuGatherAndProcessPairsTaskDesc* taskDesc = (SpuGatherAndProcessPairsTaskDesc*) uiArgument0;
  53. btAssert(taskDesc->taskId<m_activeSpuStatus.size());
  54. //get status of SPU on which task should run
  55. btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->taskId];
  56. //set data for spuStatus
  57. spuStatus.m_commandId = uiCommand;
  58. spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
  59. spuStatus.m_taskDesc.p = taskDesc;
  60. //get context
  61. context = data[taskDesc->taskId].context;
  62. taskDesc->m_lsMemory = (CollisionTask_LocalStoreMemory*)spuStatus.m_lsMemory.p;
  63. break;
  64. }
  65. default:
  66. {
  67. ///not implemented
  68. btAssert(0);
  69. }
  70. };
  71. //write taskdescription in mailbox
  72. unsigned int event = Spu_Mailbox_Event_Task;
  73. spe_in_mbox_write(context, &event, 1, SPE_MBOX_ANY_NONBLOCKING);
  74. }
  75. ///check for messages from SPUs
  76. void SpuLibspe2Support::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
  77. {
  78. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  79. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  80. btAssert(m_activeSpuStatus.size());
  81. int last = -1;
  82. //find an active spu/thread
  83. while(last < 0)
  84. {
  85. for (int i=0;i<m_activeSpuStatus.size();i++)
  86. {
  87. if ( m_activeSpuStatus[i].m_status == Spu_Status_Free)
  88. {
  89. last = i;
  90. break;
  91. }
  92. }
  93. if(last < 0)
  94. sched_yield();
  95. }
  96. btSpuStatus& spuStatus = m_activeSpuStatus[last];
  97. ///need to find an active spu
  98. btAssert(last>=0);
  99. *puiArgument0 = spuStatus.m_taskId;
  100. *puiArgument1 = spuStatus.m_status;
  101. }
  102. void SpuLibspe2Support::startSPU()
  103. {
  104. this->internal_startSPU();
  105. }
  106. ///start the spus group (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  107. void SpuLibspe2Support::internal_startSPU()
  108. {
  109. m_activeSpuStatus.resize(numThreads);
  110. for (int i=0; i < numThreads; i++)
  111. {
  112. if(data[i].context == NULL)
  113. {
  114. /* Create context */
  115. if ((data[i].context = spe_context_create(0, NULL)) == NULL)
  116. {
  117. perror ("Failed creating context");
  118. exit(1);
  119. }
  120. /* Load program into context */
  121. if(spe_program_load(data[i].context, this->program))
  122. {
  123. perror ("Failed loading program");
  124. exit(1);
  125. }
  126. m_activeSpuStatus[i].m_status = Spu_Status_Startup;
  127. m_activeSpuStatus[i].m_taskId = i;
  128. m_activeSpuStatus[i].m_commandId = 0;
  129. m_activeSpuStatus[i].m_lsMemory.p = NULL;
  130. data[i].entry = SPE_DEFAULT_ENTRY;
  131. data[i].flags = 0;
  132. data[i].argp.p = &m_activeSpuStatus[i];
  133. data[i].envp.p = NULL;
  134. /* Create thread for each SPE context */
  135. if (pthread_create(&data[i].pthread, NULL, &ppu_pthread_function, &(data[i]) ))
  136. {
  137. perror ("Failed creating thread");
  138. exit(1);
  139. }
  140. /*
  141. else
  142. {
  143. printf("started thread %d\n",i);
  144. }*/
  145. }
  146. }
  147. for (int i=0; i < numThreads; i++)
  148. {
  149. if(data[i].context != NULL)
  150. {
  151. while( m_activeSpuStatus[i].m_status == Spu_Status_Startup)
  152. {
  153. // wait for spu to set up
  154. sched_yield();
  155. }
  156. printf("Spu %d is ready\n", i);
  157. }
  158. }
  159. }
  160. ///tell the task scheduler we are done with the SPU tasks
  161. void SpuLibspe2Support::stopSPU()
  162. {
  163. // wait for all threads to finish
  164. int i;
  165. for ( i = 0; i < this->numThreads; i++ )
  166. {
  167. unsigned int event = Spu_Mailbox_Event_Shutdown;
  168. spe_context_ptr_t context = data[i].context;
  169. spe_in_mbox_write(context, &event, 1, SPE_MBOX_ALL_BLOCKING);
  170. pthread_join (data[i].pthread, NULL);
  171. }
  172. // close SPE program
  173. spe_image_close(program);
  174. // destroy SPE contexts
  175. for ( i = 0; i < this->numThreads; i++ )
  176. {
  177. if(data[i].context != NULL)
  178. {
  179. spe_context_destroy (data[i].context);
  180. }
  181. }
  182. m_activeSpuStatus.clear();
  183. }
  184. #endif //USE_LIBSPE2