MiniCLTaskScheduler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. //#define __CELLOS_LV2__ 1
  14. #define __BT_SKIP_UINT64_H 1
  15. #define USE_SAMPLE_PROCESS 1
  16. #ifdef USE_SAMPLE_PROCESS
  17. #include "MiniCLTaskScheduler.h"
  18. #include <stdio.h>
  19. #ifdef __SPU__
  20. void SampleThreadFunc(void* userPtr,void* lsMemory)
  21. {
  22. //do nothing
  23. printf("hello world\n");
  24. }
  25. void* SamplelsMemoryFunc()
  26. {
  27. //don't create local store memory, just return 0
  28. return 0;
  29. }
  30. #else
  31. #include "bullet/BulletMultiThreaded/btThreadSupportInterface.h"
  32. //# include "SPUAssert.h"
  33. #include <string.h>
  34. #include "bullet/MiniCL/cl_platform.h"
  35. extern "C" {
  36. extern char SPU_SAMPLE_ELF_SYMBOL[];
  37. }
  38. MiniCLTaskScheduler::MiniCLTaskScheduler(btThreadSupportInterface* threadInterface, int maxNumOutstandingTasks)
  39. :m_threadInterface(threadInterface),
  40. m_maxNumOutstandingTasks(maxNumOutstandingTasks)
  41. {
  42. m_taskBusy.resize(m_maxNumOutstandingTasks);
  43. m_spuSampleTaskDesc.resize(m_maxNumOutstandingTasks);
  44. m_kernels.resize(0);
  45. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  46. {
  47. m_taskBusy[i] = false;
  48. }
  49. m_numBusyTasks = 0;
  50. m_currentTask = 0;
  51. m_initialized = false;
  52. m_threadInterface->startSPU();
  53. }
  54. MiniCLTaskScheduler::~MiniCLTaskScheduler()
  55. {
  56. m_threadInterface->stopSPU();
  57. }
  58. void MiniCLTaskScheduler::initialize()
  59. {
  60. #ifdef DEBUG_SPU_TASK_SCHEDULING
  61. printf("MiniCLTaskScheduler::initialize()\n");
  62. #endif //DEBUG_SPU_TASK_SCHEDULING
  63. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  64. {
  65. m_taskBusy[i] = false;
  66. }
  67. m_numBusyTasks = 0;
  68. m_currentTask = 0;
  69. m_initialized = true;
  70. }
  71. void MiniCLTaskScheduler::issueTask(int firstWorkUnit, int lastWorkUnit, MiniCLKernel* kernel)
  72. {
  73. #ifdef DEBUG_SPU_TASK_SCHEDULING
  74. printf("MiniCLTaskScheduler::issueTask (m_currentTask= %d\)n", m_currentTask);
  75. #endif //DEBUG_SPU_TASK_SCHEDULING
  76. m_taskBusy[m_currentTask] = true;
  77. m_numBusyTasks++;
  78. MiniCLTaskDesc& taskDesc = m_spuSampleTaskDesc[m_currentTask];
  79. {
  80. // send task description in event message
  81. taskDesc.m_firstWorkUnit = firstWorkUnit;
  82. taskDesc.m_lastWorkUnit = lastWorkUnit;
  83. taskDesc.m_kernel = kernel;
  84. //some bookkeeping to recognize finished tasks
  85. taskDesc.m_taskId = m_currentTask;
  86. // for (int i=0;i<MINI_CL_MAX_ARG;i++)
  87. for (unsigned int i=0; i < kernel->m_numArgs; i++)
  88. {
  89. taskDesc.m_argSizes[i] = kernel->m_argSizes[i];
  90. if (taskDesc.m_argSizes[i])
  91. {
  92. taskDesc.m_argData[i] = kernel->m_argData[i];
  93. // memcpy(&taskDesc.m_argData[i],&argData[MINICL_MAX_ARGLENGTH*i],taskDesc.m_argSizes[i]);
  94. }
  95. }
  96. }
  97. m_threadInterface->sendRequest(1, (ppu_address_t) &taskDesc, m_currentTask);
  98. // if all tasks busy, wait for spu event to clear the task.
  99. if (m_numBusyTasks >= m_maxNumOutstandingTasks)
  100. {
  101. unsigned int taskId;
  102. unsigned int outputSize;
  103. for (int i=0;i<m_maxNumOutstandingTasks;i++)
  104. {
  105. if (m_taskBusy[i])
  106. {
  107. taskId = i;
  108. break;
  109. }
  110. }
  111. m_threadInterface->waitForResponse(&taskId, &outputSize);
  112. //printf("PPU: after issue, received event: %u %d\n", taskId, outputSize);
  113. postProcess(taskId, outputSize);
  114. m_taskBusy[taskId] = false;
  115. m_numBusyTasks--;
  116. }
  117. // find new task buffer
  118. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  119. {
  120. if (!m_taskBusy[i])
  121. {
  122. m_currentTask = i;
  123. break;
  124. }
  125. }
  126. }
  127. ///Optional PPU-size post processing for each task
  128. void MiniCLTaskScheduler::postProcess(int taskId, int outputSize)
  129. {
  130. }
  131. void MiniCLTaskScheduler::flush()
  132. {
  133. #ifdef DEBUG_SPU_TASK_SCHEDULING
  134. printf("\nSpuCollisionTaskProcess::flush()\n");
  135. #endif //DEBUG_SPU_TASK_SCHEDULING
  136. // all tasks are issued, wait for all tasks to be complete
  137. while(m_numBusyTasks > 0)
  138. {
  139. // Consolidating SPU code
  140. unsigned int taskId;
  141. unsigned int outputSize;
  142. for (int i=0;i<m_maxNumOutstandingTasks;i++)
  143. {
  144. if (m_taskBusy[i])
  145. {
  146. taskId = i;
  147. break;
  148. }
  149. }
  150. {
  151. m_threadInterface->waitForResponse(&taskId, &outputSize);
  152. }
  153. //printf("PPU: flushing, received event: %u %d\n", taskId, outputSize);
  154. postProcess(taskId, outputSize);
  155. m_taskBusy[taskId] = false;
  156. m_numBusyTasks--;
  157. }
  158. }
  159. typedef void (*MiniCLKernelLauncher0)(int);
  160. typedef void (*MiniCLKernelLauncher1)(void*, int);
  161. typedef void (*MiniCLKernelLauncher2)(void*, void*, int);
  162. typedef void (*MiniCLKernelLauncher3)(void*, void*, void*, int);
  163. typedef void (*MiniCLKernelLauncher4)(void*, void*, void*, void*, int);
  164. typedef void (*MiniCLKernelLauncher5)(void*, void*, void*, void*, void*, int);
  165. typedef void (*MiniCLKernelLauncher6)(void*, void*, void*, void*, void*, void*, int);
  166. typedef void (*MiniCLKernelLauncher7)(void*, void*, void*, void*, void*, void*, void*, int);
  167. typedef void (*MiniCLKernelLauncher8)(void*, void*, void*, void*, void*, void*, void*, void*, int);
  168. typedef void (*MiniCLKernelLauncher9)(void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  169. typedef void (*MiniCLKernelLauncher10)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  170. typedef void (*MiniCLKernelLauncher11)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  171. typedef void (*MiniCLKernelLauncher12)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  172. typedef void (*MiniCLKernelLauncher13)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  173. typedef void (*MiniCLKernelLauncher14)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  174. typedef void (*MiniCLKernelLauncher15)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  175. typedef void (*MiniCLKernelLauncher16)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, int);
  176. static void kernelLauncher0(MiniCLTaskDesc* taskDesc, int guid)
  177. {
  178. ((MiniCLKernelLauncher0)(taskDesc->m_kernel->m_launcher))(guid);
  179. }
  180. static void kernelLauncher1(MiniCLTaskDesc* taskDesc, int guid)
  181. {
  182. ((MiniCLKernelLauncher1)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  183. guid);
  184. }
  185. static void kernelLauncher2(MiniCLTaskDesc* taskDesc, int guid)
  186. {
  187. ((MiniCLKernelLauncher2)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  188. taskDesc->m_argData[1],
  189. guid);
  190. }
  191. static void kernelLauncher3(MiniCLTaskDesc* taskDesc, int guid)
  192. {
  193. ((MiniCLKernelLauncher3)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  194. taskDesc->m_argData[1],
  195. taskDesc->m_argData[2],
  196. guid);
  197. }
  198. static void kernelLauncher4(MiniCLTaskDesc* taskDesc, int guid)
  199. {
  200. ((MiniCLKernelLauncher4)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  201. taskDesc->m_argData[1],
  202. taskDesc->m_argData[2],
  203. taskDesc->m_argData[3],
  204. guid);
  205. }
  206. static void kernelLauncher5(MiniCLTaskDesc* taskDesc, int guid)
  207. {
  208. ((MiniCLKernelLauncher5)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  209. taskDesc->m_argData[1],
  210. taskDesc->m_argData[2],
  211. taskDesc->m_argData[3],
  212. taskDesc->m_argData[4],
  213. guid);
  214. }
  215. static void kernelLauncher6(MiniCLTaskDesc* taskDesc, int guid)
  216. {
  217. ((MiniCLKernelLauncher6)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  218. taskDesc->m_argData[1],
  219. taskDesc->m_argData[2],
  220. taskDesc->m_argData[3],
  221. taskDesc->m_argData[4],
  222. taskDesc->m_argData[5],
  223. guid);
  224. }
  225. static void kernelLauncher7(MiniCLTaskDesc* taskDesc, int guid)
  226. {
  227. ((MiniCLKernelLauncher7)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  228. taskDesc->m_argData[1],
  229. taskDesc->m_argData[2],
  230. taskDesc->m_argData[3],
  231. taskDesc->m_argData[4],
  232. taskDesc->m_argData[5],
  233. taskDesc->m_argData[6],
  234. guid);
  235. }
  236. static void kernelLauncher8(MiniCLTaskDesc* taskDesc, int guid)
  237. {
  238. ((MiniCLKernelLauncher8)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  239. taskDesc->m_argData[1],
  240. taskDesc->m_argData[2],
  241. taskDesc->m_argData[3],
  242. taskDesc->m_argData[4],
  243. taskDesc->m_argData[5],
  244. taskDesc->m_argData[6],
  245. taskDesc->m_argData[7],
  246. guid);
  247. }
  248. static void kernelLauncher9(MiniCLTaskDesc* taskDesc, int guid)
  249. {
  250. ((MiniCLKernelLauncher9)(taskDesc->m_kernel->m_pCode))( taskDesc->m_argData[0],
  251. taskDesc->m_argData[1],
  252. taskDesc->m_argData[2],
  253. taskDesc->m_argData[3],
  254. taskDesc->m_argData[4],
  255. taskDesc->m_argData[5],
  256. taskDesc->m_argData[6],
  257. taskDesc->m_argData[7],
  258. taskDesc->m_argData[8],
  259. guid);
  260. }
  261. static void kernelLauncher10(MiniCLTaskDesc* taskDesc, int guid)
  262. {
  263. ((MiniCLKernelLauncher10)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  264. taskDesc->m_argData[1],
  265. taskDesc->m_argData[2],
  266. taskDesc->m_argData[3],
  267. taskDesc->m_argData[4],
  268. taskDesc->m_argData[5],
  269. taskDesc->m_argData[6],
  270. taskDesc->m_argData[7],
  271. taskDesc->m_argData[8],
  272. taskDesc->m_argData[9],
  273. guid);
  274. }
  275. static void kernelLauncher11(MiniCLTaskDesc* taskDesc, int guid)
  276. {
  277. ((MiniCLKernelLauncher11)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  278. taskDesc->m_argData[1],
  279. taskDesc->m_argData[2],
  280. taskDesc->m_argData[3],
  281. taskDesc->m_argData[4],
  282. taskDesc->m_argData[5],
  283. taskDesc->m_argData[6],
  284. taskDesc->m_argData[7],
  285. taskDesc->m_argData[8],
  286. taskDesc->m_argData[9],
  287. taskDesc->m_argData[10],
  288. guid);
  289. }
  290. static void kernelLauncher12(MiniCLTaskDesc* taskDesc, int guid)
  291. {
  292. ((MiniCLKernelLauncher12)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  293. taskDesc->m_argData[1],
  294. taskDesc->m_argData[2],
  295. taskDesc->m_argData[3],
  296. taskDesc->m_argData[4],
  297. taskDesc->m_argData[5],
  298. taskDesc->m_argData[6],
  299. taskDesc->m_argData[7],
  300. taskDesc->m_argData[8],
  301. taskDesc->m_argData[9],
  302. taskDesc->m_argData[10],
  303. taskDesc->m_argData[11],
  304. guid);
  305. }
  306. static void kernelLauncher13(MiniCLTaskDesc* taskDesc, int guid)
  307. {
  308. ((MiniCLKernelLauncher13)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  309. taskDesc->m_argData[1],
  310. taskDesc->m_argData[2],
  311. taskDesc->m_argData[3],
  312. taskDesc->m_argData[4],
  313. taskDesc->m_argData[5],
  314. taskDesc->m_argData[6],
  315. taskDesc->m_argData[7],
  316. taskDesc->m_argData[8],
  317. taskDesc->m_argData[9],
  318. taskDesc->m_argData[10],
  319. taskDesc->m_argData[11],
  320. taskDesc->m_argData[12],
  321. guid);
  322. }
  323. static void kernelLauncher14(MiniCLTaskDesc* taskDesc, int guid)
  324. {
  325. ((MiniCLKernelLauncher14)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  326. taskDesc->m_argData[1],
  327. taskDesc->m_argData[2],
  328. taskDesc->m_argData[3],
  329. taskDesc->m_argData[4],
  330. taskDesc->m_argData[5],
  331. taskDesc->m_argData[6],
  332. taskDesc->m_argData[7],
  333. taskDesc->m_argData[8],
  334. taskDesc->m_argData[9],
  335. taskDesc->m_argData[10],
  336. taskDesc->m_argData[11],
  337. taskDesc->m_argData[12],
  338. taskDesc->m_argData[13],
  339. guid);
  340. }
  341. static void kernelLauncher15(MiniCLTaskDesc* taskDesc, int guid)
  342. {
  343. ((MiniCLKernelLauncher15)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  344. taskDesc->m_argData[1],
  345. taskDesc->m_argData[2],
  346. taskDesc->m_argData[3],
  347. taskDesc->m_argData[4],
  348. taskDesc->m_argData[5],
  349. taskDesc->m_argData[6],
  350. taskDesc->m_argData[7],
  351. taskDesc->m_argData[8],
  352. taskDesc->m_argData[9],
  353. taskDesc->m_argData[10],
  354. taskDesc->m_argData[11],
  355. taskDesc->m_argData[12],
  356. taskDesc->m_argData[13],
  357. taskDesc->m_argData[14],
  358. guid);
  359. }
  360. static void kernelLauncher16(MiniCLTaskDesc* taskDesc, int guid)
  361. {
  362. ((MiniCLKernelLauncher16)(taskDesc->m_kernel->m_pCode))(taskDesc->m_argData[0],
  363. taskDesc->m_argData[1],
  364. taskDesc->m_argData[2],
  365. taskDesc->m_argData[3],
  366. taskDesc->m_argData[4],
  367. taskDesc->m_argData[5],
  368. taskDesc->m_argData[6],
  369. taskDesc->m_argData[7],
  370. taskDesc->m_argData[8],
  371. taskDesc->m_argData[9],
  372. taskDesc->m_argData[10],
  373. taskDesc->m_argData[11],
  374. taskDesc->m_argData[12],
  375. taskDesc->m_argData[13],
  376. taskDesc->m_argData[14],
  377. taskDesc->m_argData[15],
  378. guid);
  379. }
  380. static kernelLauncherCB spLauncherList[MINI_CL_MAX_ARG+1] =
  381. {
  382. kernelLauncher0,
  383. kernelLauncher1,
  384. kernelLauncher2,
  385. kernelLauncher3,
  386. kernelLauncher4,
  387. kernelLauncher5,
  388. kernelLauncher6,
  389. kernelLauncher7,
  390. kernelLauncher8,
  391. kernelLauncher9,
  392. kernelLauncher10,
  393. kernelLauncher11,
  394. kernelLauncher12,
  395. kernelLauncher13,
  396. kernelLauncher14,
  397. kernelLauncher15,
  398. kernelLauncher16
  399. };
  400. void MiniCLKernel::updateLauncher()
  401. {
  402. m_launcher = spLauncherList[m_numArgs];
  403. }
  404. struct MiniCLKernelDescEntry
  405. {
  406. void* pCode;
  407. const char* pName;
  408. };
  409. static MiniCLKernelDescEntry spKernelDesc[256];
  410. static int sNumKernelDesc = 0;
  411. MiniCLKernelDesc::MiniCLKernelDesc(void* pCode, const char* pName)
  412. {
  413. for(int i = 0; i < sNumKernelDesc; i++)
  414. {
  415. if(!strcmp(pName, spKernelDesc[i].pName))
  416. { // already registered
  417. btAssert(spKernelDesc[i].pCode == pCode);
  418. return;
  419. }
  420. }
  421. spKernelDesc[sNumKernelDesc].pCode = pCode;
  422. spKernelDesc[sNumKernelDesc].pName = pName;
  423. sNumKernelDesc++;
  424. }
  425. MiniCLKernel* MiniCLKernel::registerSelf()
  426. {
  427. m_scheduler->registerKernel(this);
  428. for(int i = 0; i < sNumKernelDesc; i++)
  429. {
  430. if(!strcmp(m_name, spKernelDesc[i].pName))
  431. {
  432. m_pCode = spKernelDesc[i].pCode;
  433. return this;
  434. }
  435. }
  436. return NULL;
  437. }
  438. #endif
  439. #endif //USE_SAMPLE_PROCESS