Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

785 rindas
22 KiB

  1. /*
  2. Copyright (C) 2010 Sony Computer Entertainment Inc.
  3. All rights reserved.
  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. #include "MiniCL/cl.h"
  14. #define __PHYSICS_COMMON_H__ 1
  15. #ifdef _WIN32
  16. #include "BulletMultiThreaded/Win32ThreadSupport.h"
  17. #endif
  18. #include "BulletMultiThreaded/PlatformDefinitions.h"
  19. #ifdef USE_PTHREADS
  20. #include "BulletMultiThreaded/PosixThreadSupport.h"
  21. #endif
  22. #include "BulletMultiThreaded/SequentialThreadSupport.h"
  23. #include "MiniCLTaskScheduler.h"
  24. #include "MiniCLTask/MiniCLTask.h"
  25. #include "LinearMath/btMinMax.h"
  26. #include <stdio.h>
  27. //#define DEBUG_MINICL_KERNELS 1
  28. static const char* spPlatformID = "MiniCL, SCEA";
  29. static const char* spDriverVersion= "1.0";
  30. CL_API_ENTRY cl_int CL_API_CALL clGetPlatformIDs(
  31. cl_uint num_entries,
  32. cl_platform_id * platforms,
  33. cl_uint * num_platforms ) CL_API_SUFFIX__VERSION_1_0
  34. {
  35. if(platforms != NULL)
  36. {
  37. if(num_entries <= 0)
  38. {
  39. return CL_INVALID_VALUE;
  40. }
  41. *((const char**)platforms) = spPlatformID;
  42. }
  43. if(num_platforms != NULL)
  44. {
  45. *num_platforms = 1;
  46. }
  47. return CL_SUCCESS;
  48. }
  49. CL_API_ENTRY cl_int CL_API_CALL clGetPlatformInfo(
  50. cl_platform_id platform,
  51. cl_platform_info param_name,
  52. size_t param_value_size,
  53. void * param_value,
  54. size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0
  55. {
  56. char* pId = (char*)platform;
  57. if(strcmp(pId, spPlatformID))
  58. {
  59. return CL_INVALID_PLATFORM;
  60. }
  61. switch(param_name)
  62. {
  63. case CL_PLATFORM_VERSION:
  64. {
  65. if(param_value_size < (strlen(spDriverVersion) + 1))
  66. {
  67. return CL_INVALID_VALUE;
  68. }
  69. strcpy((char*)param_value, spDriverVersion);
  70. if(param_value_size_ret != NULL)
  71. {
  72. *param_value_size_ret = strlen(spDriverVersion) + 1;
  73. }
  74. break;
  75. }
  76. case CL_PLATFORM_NAME:
  77. case CL_PLATFORM_VENDOR :
  78. if(param_value_size < (strlen(spPlatformID) + 1))
  79. {
  80. return CL_INVALID_VALUE;
  81. }
  82. strcpy((char*)param_value, spPlatformID);
  83. if(param_value_size_ret != NULL)
  84. {
  85. *param_value_size_ret = strlen(spPlatformID) + 1;
  86. }
  87. break;
  88. default :
  89. return CL_INVALID_VALUE;
  90. }
  91. return CL_SUCCESS;
  92. }
  93. CL_API_ENTRY cl_int CL_API_CALL clGetDeviceInfo(
  94. cl_device_id device ,
  95. cl_device_info param_name ,
  96. size_t param_value_size ,
  97. void * param_value ,
  98. size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0
  99. {
  100. switch (param_name)
  101. {
  102. case CL_DEVICE_NAME:
  103. {
  104. char deviceName[] = "MiniCL CPU";
  105. unsigned int nameLen = (unsigned int)strlen(deviceName)+1;
  106. btAssert(param_value_size>strlen(deviceName));
  107. if (nameLen < param_value_size)
  108. {
  109. const char* cpuName = "MiniCL CPU";
  110. sprintf((char*)param_value,"%s",cpuName);
  111. } else
  112. {
  113. printf("error: param_value_size should be at least %d, but it is %d\n",nameLen,param_value_size);
  114. return CL_INVALID_VALUE;
  115. }
  116. break;
  117. }
  118. case CL_DEVICE_TYPE:
  119. {
  120. if (param_value_size>=sizeof(cl_device_type))
  121. {
  122. cl_device_type* deviceType = (cl_device_type*)param_value;
  123. *deviceType = CL_DEVICE_TYPE_CPU;
  124. } else
  125. {
  126. printf("error: param_value_size should be at least %d\n",sizeof(cl_device_type));
  127. return CL_INVALID_VALUE;
  128. }
  129. break;
  130. }
  131. case CL_DEVICE_MAX_COMPUTE_UNITS:
  132. {
  133. if (param_value_size>=sizeof(cl_uint))
  134. {
  135. cl_uint* numUnits = (cl_uint*)param_value;
  136. *numUnits= 4;
  137. } else
  138. {
  139. printf("error: param_value_size should be at least %d\n",sizeof(cl_uint));
  140. return CL_INVALID_VALUE;
  141. }
  142. break;
  143. }
  144. case CL_DEVICE_MAX_WORK_ITEM_SIZES:
  145. {
  146. size_t workitem_size[3];
  147. if (param_value_size>=sizeof(workitem_size))
  148. {
  149. size_t* workItemSize = (size_t*)param_value;
  150. workItemSize[0] = 64;
  151. workItemSize[1] = 24;
  152. workItemSize[2] = 16;
  153. } else
  154. {
  155. printf("error: param_value_size should be at least %d\n",sizeof(cl_uint));
  156. return CL_INVALID_VALUE;
  157. }
  158. break;
  159. }
  160. case CL_DEVICE_MAX_CLOCK_FREQUENCY:
  161. {
  162. cl_uint* clock_frequency = (cl_uint*)param_value;
  163. *clock_frequency = 3*1024;
  164. break;
  165. }
  166. case CL_DEVICE_VENDOR :
  167. {
  168. if(param_value_size < (strlen(spPlatformID) + 1))
  169. {
  170. return CL_INVALID_VALUE;
  171. }
  172. strcpy((char*)param_value, spPlatformID);
  173. if(param_value_size_ret != NULL)
  174. {
  175. *param_value_size_ret = strlen(spPlatformID) + 1;
  176. }
  177. break;
  178. }
  179. case CL_DRIVER_VERSION:
  180. {
  181. if(param_value_size < (strlen(spDriverVersion) + 1))
  182. {
  183. return CL_INVALID_VALUE;
  184. }
  185. strcpy((char*)param_value, spDriverVersion);
  186. if(param_value_size_ret != NULL)
  187. {
  188. *param_value_size_ret = strlen(spDriverVersion) + 1;
  189. }
  190. break;
  191. }
  192. case CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS:
  193. {
  194. cl_uint* maxDimensions = (cl_uint*)param_value;
  195. *maxDimensions = 1;
  196. break;
  197. }
  198. case CL_DEVICE_MAX_WORK_GROUP_SIZE:
  199. {
  200. cl_uint* maxWorkGroupSize = (cl_uint*)param_value;
  201. *maxWorkGroupSize = 128;//1;
  202. break;
  203. }
  204. case CL_DEVICE_ADDRESS_BITS:
  205. {
  206. cl_uint* addressBits = (cl_uint*)param_value;
  207. *addressBits= 32; //@todo: should this be 64 for 64bit builds?
  208. break;
  209. }
  210. case CL_DEVICE_MAX_MEM_ALLOC_SIZE:
  211. {
  212. cl_ulong* maxMemAlloc = (cl_ulong*)param_value;
  213. *maxMemAlloc= 512*1024*1024; //this "should be enough for everyone" ?
  214. break;
  215. }
  216. case CL_DEVICE_GLOBAL_MEM_SIZE:
  217. {
  218. cl_ulong* maxMemAlloc = (cl_ulong*)param_value;
  219. *maxMemAlloc= 1024*1024*1024; //this "should be enough for everyone" ?
  220. break;
  221. }
  222. case CL_DEVICE_ERROR_CORRECTION_SUPPORT:
  223. {
  224. cl_bool* error_correction_support = (cl_bool*)param_value;
  225. *error_correction_support = CL_FALSE;
  226. break;
  227. }
  228. case CL_DEVICE_LOCAL_MEM_TYPE:
  229. {
  230. cl_device_local_mem_type* local_mem_type = (cl_device_local_mem_type*)param_value;
  231. *local_mem_type = CL_GLOBAL;
  232. break;
  233. }
  234. case CL_DEVICE_LOCAL_MEM_SIZE:
  235. {
  236. cl_ulong* localmem = (cl_ulong*) param_value;
  237. *localmem = 32*1024;
  238. break;
  239. }
  240. case CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE:
  241. {
  242. cl_ulong* localmem = (cl_ulong*) param_value;
  243. *localmem = 64*1024;
  244. break;
  245. }
  246. case CL_DEVICE_QUEUE_PROPERTIES:
  247. {
  248. cl_command_queue_properties* queueProp = (cl_command_queue_properties*) param_value;
  249. memset(queueProp,0,param_value_size);
  250. break;
  251. }
  252. case CL_DEVICE_IMAGE_SUPPORT:
  253. {
  254. cl_bool* imageSupport = (cl_bool*) param_value;
  255. *imageSupport = CL_FALSE;
  256. break;
  257. }
  258. case CL_DEVICE_MAX_WRITE_IMAGE_ARGS:
  259. case CL_DEVICE_MAX_READ_IMAGE_ARGS:
  260. {
  261. cl_uint* imageArgs = (cl_uint*) param_value;
  262. *imageArgs = 0;
  263. break;
  264. }
  265. case CL_DEVICE_IMAGE3D_MAX_DEPTH:
  266. case CL_DEVICE_IMAGE3D_MAX_HEIGHT:
  267. case CL_DEVICE_IMAGE2D_MAX_HEIGHT:
  268. case CL_DEVICE_IMAGE3D_MAX_WIDTH:
  269. case CL_DEVICE_IMAGE2D_MAX_WIDTH:
  270. {
  271. size_t* maxSize = (size_t*) param_value;
  272. *maxSize = 0;
  273. break;
  274. }
  275. case CL_DEVICE_EXTENSIONS:
  276. {
  277. char* extensions = (char*) param_value;
  278. *extensions = 0;
  279. break;
  280. }
  281. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE:
  282. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT:
  283. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG:
  284. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT:
  285. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT:
  286. case CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR:
  287. {
  288. cl_uint* width = (cl_uint*) param_value;
  289. *width = 1;
  290. break;
  291. }
  292. default:
  293. {
  294. printf("error: unsupported param_name:%d\n",param_name);
  295. }
  296. }
  297. return 0;
  298. }
  299. CL_API_ENTRY cl_int CL_API_CALL clReleaseMemObject(cl_mem /* memobj */) CL_API_SUFFIX__VERSION_1_0
  300. {
  301. return 0;
  302. }
  303. CL_API_ENTRY cl_int CL_API_CALL clReleaseCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0
  304. {
  305. return 0;
  306. }
  307. CL_API_ENTRY cl_int CL_API_CALL clReleaseProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0
  308. {
  309. return 0;
  310. }
  311. CL_API_ENTRY cl_int CL_API_CALL clReleaseKernel(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0
  312. {
  313. return 0;
  314. }
  315. // Enqueued Commands APIs
  316. CL_API_ENTRY cl_int CL_API_CALL clEnqueueReadBuffer(cl_command_queue command_queue ,
  317. cl_mem buffer ,
  318. cl_bool /* blocking_read */,
  319. size_t offset ,
  320. size_t cb ,
  321. void * ptr ,
  322. cl_uint /* num_events_in_wait_list */,
  323. const cl_event * /* event_wait_list */,
  324. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0
  325. {
  326. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) command_queue;
  327. ///wait for all work items to be completed
  328. scheduler->flush();
  329. memcpy(ptr,(char*)buffer + offset,cb);
  330. return 0;
  331. }
  332. CL_API_ENTRY cl_int clGetProgramBuildInfo(cl_program /* program */,
  333. cl_device_id /* device */,
  334. cl_program_build_info /* param_name */,
  335. size_t /* param_value_size */,
  336. void * /* param_value */,
  337. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0
  338. {
  339. return 0;
  340. }
  341. // Program Object APIs
  342. CL_API_ENTRY cl_program
  343. clCreateProgramWithSource(cl_context context ,
  344. cl_uint /* count */,
  345. const char ** /* strings */,
  346. const size_t * /* lengths */,
  347. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  348. {
  349. *errcode_ret = CL_SUCCESS;
  350. return (cl_program)context;
  351. }
  352. CL_API_ENTRY cl_int CL_API_CALL clEnqueueWriteBuffer(cl_command_queue command_queue ,
  353. cl_mem buffer ,
  354. cl_bool /* blocking_read */,
  355. size_t offset,
  356. size_t cb ,
  357. const void * ptr ,
  358. cl_uint /* num_events_in_wait_list */,
  359. const cl_event * /* event_wait_list */,
  360. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0
  361. {
  362. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) command_queue;
  363. ///wait for all work items to be completed
  364. scheduler->flush();
  365. memcpy((char*)buffer + offset, ptr,cb);
  366. return 0;
  367. }
  368. CL_API_ENTRY cl_int CL_API_CALL clFlush(cl_command_queue command_queue)
  369. {
  370. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) command_queue;
  371. ///wait for all work items to be completed
  372. scheduler->flush();
  373. return 0;
  374. }
  375. CL_API_ENTRY cl_int CL_API_CALL clEnqueueNDRangeKernel(cl_command_queue /* command_queue */,
  376. cl_kernel clKernel ,
  377. cl_uint work_dim ,
  378. const size_t * /* global_work_offset */,
  379. const size_t * global_work_size ,
  380. const size_t * /* local_work_size */,
  381. cl_uint /* num_events_in_wait_list */,
  382. const cl_event * /* event_wait_list */,
  383. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0
  384. {
  385. MiniCLKernel* kernel = (MiniCLKernel*) clKernel;
  386. for (unsigned int ii=0;ii<work_dim;ii++)
  387. {
  388. int maxTask = kernel->m_scheduler->getMaxNumOutstandingTasks();
  389. int numWorkItems = global_work_size[ii];
  390. // //at minimum 64 work items per task
  391. // int numWorkItemsPerTask = btMax(64,numWorkItems / maxTask);
  392. int numWorkItemsPerTask = numWorkItems / maxTask;
  393. if (!numWorkItemsPerTask) numWorkItemsPerTask = 1;
  394. for (int t=0;t<numWorkItems;)
  395. {
  396. //Performance Hint: tweak this number during benchmarking
  397. int endIndex = (t+numWorkItemsPerTask) < numWorkItems ? t+numWorkItemsPerTask : numWorkItems;
  398. kernel->m_scheduler->issueTask(t, endIndex, kernel);
  399. t = endIndex;
  400. }
  401. }
  402. /*
  403. void* bla = 0;
  404. scheduler->issueTask(bla,2,3);
  405. scheduler->flush();
  406. */
  407. return 0;
  408. }
  409. #define LOCAL_BUF_SIZE 32768
  410. static int sLocalMemBuf[LOCAL_BUF_SIZE * 4 + 16];
  411. static int* spLocalBufCurr = NULL;
  412. static int sLocalBufUsed = LOCAL_BUF_SIZE; // so it will be reset at the first call
  413. static void* localBufMalloc(int size)
  414. {
  415. int size16 = (size + 15) >> 4; // in 16-byte units
  416. if((sLocalBufUsed + size16) > LOCAL_BUF_SIZE)
  417. { // reset
  418. spLocalBufCurr = sLocalMemBuf;
  419. while((unsigned long)spLocalBufCurr & 0x0F) spLocalBufCurr++; // align to 16 bytes
  420. sLocalBufUsed = 0;
  421. }
  422. void* ret = spLocalBufCurr;
  423. spLocalBufCurr += size16 * 4;
  424. sLocalBufUsed += size;
  425. return ret;
  426. }
  427. CL_API_ENTRY cl_int CL_API_CALL clSetKernelArg(cl_kernel clKernel ,
  428. cl_uint arg_index ,
  429. size_t arg_size ,
  430. const void * arg_value ) CL_API_SUFFIX__VERSION_1_0
  431. {
  432. MiniCLKernel* kernel = (MiniCLKernel* ) clKernel;
  433. btAssert(arg_size <= MINICL_MAX_ARGLENGTH);
  434. if (arg_index>MINI_CL_MAX_ARG)
  435. {
  436. printf("error: clSetKernelArg arg_index (%u) exceeds %u\n",arg_index,MINI_CL_MAX_ARG);
  437. } else
  438. {
  439. if (arg_size>MINICL_MAX_ARGLENGTH)
  440. //if (arg_size != MINICL_MAX_ARGLENGTH)
  441. {
  442. printf("error: clSetKernelArg argdata too large: %zu (maximum is %zu)\n",arg_size,MINICL_MAX_ARGLENGTH);
  443. }
  444. else
  445. {
  446. if(arg_value == NULL)
  447. { // this is only for __local memory qualifier
  448. void* ptr = localBufMalloc(arg_size);
  449. kernel->m_argData[arg_index] = ptr;
  450. }
  451. else
  452. {
  453. memcpy(&(kernel->m_argData[arg_index]), arg_value, arg_size);
  454. }
  455. kernel->m_argSizes[arg_index] = arg_size;
  456. if(arg_index >= kernel->m_numArgs)
  457. {
  458. kernel->m_numArgs = arg_index + 1;
  459. kernel->updateLauncher();
  460. }
  461. }
  462. }
  463. return 0;
  464. }
  465. // Kernel Object APIs
  466. CL_API_ENTRY cl_kernel CL_API_CALL clCreateKernel(cl_program program ,
  467. const char * kernel_name ,
  468. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  469. {
  470. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) program;
  471. MiniCLKernel* kernel = new MiniCLKernel();
  472. int nameLen = strlen(kernel_name);
  473. if(nameLen >= MINI_CL_MAX_KERNEL_NAME)
  474. {
  475. *errcode_ret = CL_INVALID_KERNEL_NAME;
  476. return NULL;
  477. }
  478. strcpy(kernel->m_name, kernel_name);
  479. kernel->m_numArgs = 0;
  480. //kernel->m_kernelProgramCommandId = scheduler->findProgramCommandIdByName(kernel_name);
  481. //if (kernel->m_kernelProgramCommandId>=0)
  482. //{
  483. // *errcode_ret = CL_SUCCESS;
  484. //} else
  485. //{
  486. // *errcode_ret = CL_INVALID_KERNEL_NAME;
  487. //}
  488. kernel->m_scheduler = scheduler;
  489. if(kernel->registerSelf() == NULL)
  490. {
  491. *errcode_ret = CL_INVALID_KERNEL_NAME;
  492. return NULL;
  493. }
  494. else
  495. {
  496. *errcode_ret = CL_SUCCESS;
  497. }
  498. return (cl_kernel)kernel;
  499. }
  500. CL_API_ENTRY cl_int CL_API_CALL clBuildProgram(cl_program /* program */,
  501. cl_uint /* num_devices */,
  502. const cl_device_id * /* device_list */,
  503. const char * /* options */,
  504. void (*pfn_notify)(cl_program /* program */, void * /* user_data */),
  505. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0
  506. {
  507. return CL_SUCCESS;
  508. }
  509. CL_API_ENTRY cl_program CL_API_CALL clCreateProgramWithBinary(cl_context context ,
  510. cl_uint /* num_devices */,
  511. const cl_device_id * /* device_list */,
  512. const size_t * /* lengths */,
  513. const unsigned char ** /* binaries */,
  514. cl_int * /* binary_status */,
  515. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0
  516. {
  517. return (cl_program)context;
  518. }
  519. // Memory Object APIs
  520. CL_API_ENTRY cl_mem CL_API_CALL clCreateBuffer(cl_context /* context */,
  521. cl_mem_flags flags ,
  522. size_t size,
  523. void * host_ptr ,
  524. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  525. {
  526. cl_mem buf = (cl_mem)malloc(size);
  527. if ((flags&CL_MEM_COPY_HOST_PTR) && host_ptr)
  528. {
  529. memcpy(buf,host_ptr,size);
  530. }
  531. *errcode_ret = 0;
  532. return buf;
  533. }
  534. // Command Queue APIs
  535. CL_API_ENTRY cl_command_queue CL_API_CALL clCreateCommandQueue(cl_context context ,
  536. cl_device_id /* device */,
  537. cl_command_queue_properties /* properties */,
  538. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  539. {
  540. *errcode_ret = 0;
  541. return (cl_command_queue) context;
  542. }
  543. extern CL_API_ENTRY cl_int CL_API_CALL clGetContextInfo(cl_context /* context */,
  544. cl_context_info param_name ,
  545. size_t param_value_size ,
  546. void * param_value,
  547. size_t * param_value_size_ret ) CL_API_SUFFIX__VERSION_1_0
  548. {
  549. switch (param_name)
  550. {
  551. case CL_CONTEXT_DEVICES:
  552. {
  553. if (!param_value_size)
  554. {
  555. *param_value_size_ret = 13;
  556. } else
  557. {
  558. const char* testName = "MiniCL_Test.";
  559. sprintf((char*)param_value,"%s",testName);
  560. }
  561. break;
  562. };
  563. default:
  564. {
  565. printf("unsupported\n");
  566. }
  567. }
  568. return 0;
  569. }
  570. CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromType(const cl_context_properties * /* properties */,
  571. cl_device_type device_type ,
  572. void (*pfn_notify)(const char *, const void *, size_t, void *) /* pfn_notify */,
  573. void * /* user_data */,
  574. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  575. {
  576. int maxNumOutstandingTasks = 4;
  577. // int maxNumOutstandingTasks = 2;
  578. // int maxNumOutstandingTasks = 1;
  579. gMiniCLNumOutstandingTasks = maxNumOutstandingTasks;
  580. const int maxNumOfThreadSupports = 8;
  581. static int sUniqueThreadSupportIndex = 0;
  582. static const char* sUniqueThreadSupportName[maxNumOfThreadSupports] =
  583. {
  584. "MiniCL_0", "MiniCL_1", "MiniCL_2", "MiniCL_3", "MiniCL_4", "MiniCL_5", "MiniCL_6", "MiniCL_7"
  585. };
  586. btThreadSupportInterface* threadSupport = 0;
  587. if (device_type==CL_DEVICE_TYPE_DEBUG)
  588. {
  589. SequentialThreadSupport::SequentialThreadConstructionInfo stc("MiniCL",processMiniCLTask,createMiniCLLocalStoreMemory);
  590. threadSupport = new SequentialThreadSupport(stc);
  591. } else
  592. {
  593. #if _WIN32
  594. btAssert(sUniqueThreadSupportIndex < maxNumOfThreadSupports);
  595. const char* bla = "MiniCL";
  596. threadSupport = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
  597. // bla,
  598. sUniqueThreadSupportName[sUniqueThreadSupportIndex++],
  599. processMiniCLTask, //processCollisionTask,
  600. createMiniCLLocalStoreMemory,//createCollisionLocalStoreMemory,
  601. maxNumOutstandingTasks));
  602. #else
  603. #ifdef USE_PTHREADS
  604. PosixThreadSupport::ThreadConstructionInfo constructionInfo("PosixThreads",
  605. processMiniCLTask,
  606. createMiniCLLocalStoreMemory,
  607. maxNumOutstandingTasks);
  608. threadSupport = new PosixThreadSupport(constructionInfo);
  609. #else
  610. ///todo: add posix thread support for other platforms
  611. SequentialThreadSupport::SequentialThreadConstructionInfo stc("MiniCL",processMiniCLTask,createMiniCLLocalStoreMemory);
  612. threadSupport = new SequentialThreadSupport(stc);
  613. #endif //USE_PTHREADS
  614. #endif
  615. }
  616. MiniCLTaskScheduler* scheduler = new MiniCLTaskScheduler(threadSupport,maxNumOutstandingTasks);
  617. *errcode_ret = 0;
  618. return (cl_context)scheduler;
  619. }
  620. CL_API_ENTRY cl_int CL_API_CALL
  621. clGetDeviceIDs(cl_platform_id /* platform */,
  622. cl_device_type /* device_type */,
  623. cl_uint /* num_entries */,
  624. cl_device_id * /* devices */,
  625. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0
  626. {
  627. return 0;
  628. }
  629. CL_API_ENTRY cl_context CL_API_CALL
  630. clCreateContext(const cl_context_properties * properties ,
  631. cl_uint num_devices ,
  632. const cl_device_id * devices ,
  633. void (*pfn_notify)(const char *, const void *, size_t, void *),
  634. void * user_data ,
  635. cl_int * errcode_ret ) CL_API_SUFFIX__VERSION_1_0
  636. {
  637. return clCreateContextFromType(properties,CL_DEVICE_TYPE_ALL,pfn_notify,user_data,errcode_ret);
  638. }
  639. CL_API_ENTRY cl_int CL_API_CALL clReleaseContext(cl_context context ) CL_API_SUFFIX__VERSION_1_0
  640. {
  641. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) context;
  642. btThreadSupportInterface* threadSupport = scheduler->getThreadSupportInterface();
  643. delete scheduler;
  644. delete threadSupport;
  645. return 0;
  646. }
  647. extern CL_API_ENTRY cl_int CL_API_CALL
  648. clFinish(cl_command_queue command_queue ) CL_API_SUFFIX__VERSION_1_0
  649. {
  650. MiniCLTaskScheduler* scheduler = (MiniCLTaskScheduler*) command_queue;
  651. ///wait for all work items to be completed
  652. scheduler->flush();
  653. return CL_SUCCESS;
  654. }
  655. extern CL_API_ENTRY cl_int CL_API_CALL
  656. clGetProgramInfo(cl_program /* program */,
  657. cl_program_info /* param_name */,
  658. size_t /* param_value_size */,
  659. void * /* param_value */,
  660. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0
  661. {
  662. return 0;
  663. }
  664. extern CL_API_ENTRY cl_int CL_API_CALL
  665. clGetKernelWorkGroupInfo(cl_kernel kernel ,
  666. cl_device_id /* device */,
  667. cl_kernel_work_group_info wgi/* param_name */,
  668. size_t sz /* param_value_size */,
  669. void * ptr /* param_value */,
  670. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0
  671. {
  672. if((wgi == CL_KERNEL_WORK_GROUP_SIZE)
  673. &&(sz == sizeof(size_t))
  674. &&(ptr != NULL))
  675. {
  676. MiniCLKernel* miniCLKernel = (MiniCLKernel*)kernel;
  677. MiniCLTaskScheduler* scheduler = miniCLKernel->m_scheduler;
  678. *((size_t*)ptr) = scheduler->getMaxNumOutstandingTasks();
  679. return CL_SUCCESS;
  680. }
  681. else
  682. {
  683. return CL_INVALID_VALUE;
  684. }
  685. }