clutils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /****************************************************************************\
  2. * Copyright (c) 2011, Advanced Micro Devices, Inc. *
  3. * All rights reserved. *
  4. * *
  5. * Redistribution and use in source and binary forms, with or without *
  6. * modification, are permitted provided that the following conditions *
  7. * are met: *
  8. * *
  9. * Redistributions of source code must retain the above copyright notice, *
  10. * this list of conditions and the following disclaimer. *
  11. * *
  12. * Redistributions in binary form must reproduce the above copyright notice, *
  13. * this list of conditions and the following disclaimer in the documentation *
  14. * and/or other materials provided with the distribution. *
  15. * *
  16. * Neither the name of the copyright holder nor the names of its contributors *
  17. * may be used to endorse or promote products derived from this software *
  18. * without specific prior written permission. *
  19. * *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR *
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR *
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  31. * *
  32. * If you use the software (in whole or in part), you shall adhere to all *
  33. * applicable U.S., European, and other export laws, including but not *
  34. * limited to the U.S. Export Administration Regulations (“EAR”), (15 C.F.R. *
  35. * Sections 730 through 774), and E.U. Council Regulation (EC) No 1334/2000 *
  36. * of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, you *
  37. * hereby certify that, except pursuant to a license granted by the United *
  38. * States Department of Commerce Bureau of Industry and Security or as *
  39. * otherwise permitted pursuant to a License Exception under the U.S. Export *
  40. * Administration Regulations ("EAR"), you will not (1) export, re-export or *
  41. * release to a national of a country in Country Groups D:1, E:1 or E:2 any *
  42. * restricted technology, software, or source code you receive hereunder, *
  43. * or (2) export to Country Groups D:1, E:1 or E:2 the direct product of such *
  44. * technology or software, if such foreign produced direct product is subject *
  45. * to national security controls as identified on the Commerce Control List *
  46. *(currently found in Supplement 1 to Part 774 of EAR). For the most current *
  47. * Country Group listings, or for additional information about the EAR or *
  48. * your obligations under those regulations, please refer to the U.S. Bureau *
  49. * of Industry and Security’s website at http://www.bis.doc.gov/. *
  50. \****************************************************************************/
  51. #ifndef __CL_UTILS_H__
  52. #define __CL_UTILS_H__
  53. #include <CL/cl.h>
  54. // The cl_time type is OS specific
  55. #ifdef _WIN32
  56. #include <tchar.h>
  57. #include <Windows.h>
  58. typedef __int64 cl_time;
  59. #else
  60. #include <sys/time.h>
  61. typedef double cl_time;
  62. #endif
  63. //-------------------------------------------------------
  64. // Initialization and Cleanup
  65. //-------------------------------------------------------
  66. // Detects platforms and devices, creates context and command queue
  67. cl_context cl_init(char devicePreference='\0');
  68. // Creates a context given a platform and a device
  69. cl_context cl_init_context(int platform,int dev, int quiet=0, int use_gpu= 0);
  70. // Releases resources used by clutils
  71. void cl_cleanup();
  72. // Releases a kernel object
  73. void cl_freeKernel(cl_kernel kernel);
  74. // Releases a memory object
  75. void cl_freeMem(cl_mem mem);
  76. // Releases a program object
  77. void cl_freeProgram(cl_program program);
  78. // Returns the global command queue
  79. cl_command_queue cl_getCommandQueue();
  80. //-------------------------------------------------------
  81. // Synchronization functions
  82. //-------------------------------------------------------
  83. // Performs a clFinish on the command queue
  84. void cl_sync();
  85. //-------------------------------------------------------
  86. // Memory allocation
  87. //-------------------------------------------------------
  88. // Allocates a regular buffer on the device
  89. cl_mem cl_allocBuffer(size_t mem_size,
  90. cl_mem_flags flags = CL_MEM_READ_WRITE);
  91. // XXX I don't think this does exactly what we want it to do
  92. // Allocates a read-only buffer and transfers the data
  93. cl_mem cl_allocBufferConst(size_t mem_size, void* host_ptr);
  94. // Allocates pinned memory on the host
  95. cl_mem cl_allocBufferPinned(size_t mem_size);
  96. // Allocates an image on the device
  97. cl_mem cl_allocImage(size_t height, size_t width, char type,
  98. cl_mem_flags flags = CL_MEM_READ_WRITE);
  99. //-------------------------------------------------------
  100. // Data transfers
  101. //-------------------------------------------------------
  102. // Copies a buffer from the device to pinned memory on the host and
  103. // maps it so it can be read
  104. void* cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size);
  105. // Copies from one buffer to another
  106. void cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size);
  107. // Copies data to a buffer on the device
  108. void cl_copyBufferToDevice(cl_mem dst, void *src, size_t mem_size,
  109. cl_bool blocking = CL_TRUE);
  110. // Copies data to an image on the device
  111. void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width);
  112. // Copies an image from the device to the host
  113. void cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width);
  114. // Copies data from a device buffer to the host
  115. void cl_copyBufferToHost(void *dst, cl_mem src, size_t mem_size,
  116. cl_bool blocking = CL_TRUE);
  117. // Copies data from a buffer on the device to an image on the device
  118. void cl_copyBufferToImage(cl_mem src, cl_mem dst, int height, int width);
  119. // Maps a buffer
  120. void* cl_mapBuffer(cl_mem mem, size_t mem_size, cl_mem_flags flags);
  121. // Unmaps a buffer
  122. void cl_unmapBuffer(cl_mem mem, void *ptr);
  123. // Writes data to a zero-copy buffer on the device
  124. void cl_writeToZCBuffer(cl_mem mem, void* data, size_t size);
  125. //-------------------------------------------------------
  126. // Program and kernels
  127. //-------------------------------------------------------
  128. // Compiles a program
  129. cl_program cl_compileProgram(char* kernelPath, char* compileoptions,
  130. bool verboseoptions = 0);
  131. // Creates a kernel
  132. cl_kernel cl_createKernel(cl_program program, const char* kernelName);
  133. // Sets a kernel argument
  134. void cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
  135. void* data);
  136. //-------------------------------------------------------
  137. // Profiling/events
  138. //-------------------------------------------------------
  139. // Computes the execution time (start to end) for an event
  140. double cl_computeExecTime(cl_event);
  141. // Compute the elapsed time between two CPU timer values
  142. double cl_computeTime(cl_time start, cl_time end);
  143. // Creates an event from CPU timers
  144. void cl_createUserEvent(cl_time start, cl_time end, char* desc);
  145. // Disable logging of events
  146. void cl_disableEvents();
  147. // Enable logging of events
  148. void cl_enableEvents();
  149. // Query the current system time
  150. void cl_getTime(cl_time* time);
  151. // Calls a function which prints events to the terminal
  152. void cl_printEvents();
  153. // Calls a function which writes the events to a file
  154. void cl_writeEventsToFile(char* path);
  155. //-------------------------------------------------------
  156. // Error handling
  157. //-------------------------------------------------------
  158. // Compare a status value to CL_SUCCESS and optionally exit on error
  159. int cl_errChk(const cl_int status, const char *msg, bool exitOnErr);
  160. // Queries the supported image formats for the device and prints
  161. // them to the screen
  162. void printSupportedImageFormats();
  163. //-------------------------------------------------------
  164. // Platform and device information
  165. //-------------------------------------------------------
  166. bool cl_deviceIsAMD(cl_device_id dev=NULL);
  167. bool cl_deviceIsNVIDIA(cl_device_id dev=NULL);
  168. bool cl_platformIsNVIDIA(cl_platform_id plat=NULL);
  169. char* cl_getDeviceDriverVersion(cl_device_id dev=NULL);
  170. char* cl_getDeviceName(cl_device_id dev=NULL);
  171. char* cl_getDeviceVendor(cl_device_id dev=NULL);
  172. char* cl_getDeviceVersion(cl_device_id dev=NULL);
  173. char* cl_getPlatformName(cl_platform_id platform);
  174. char* cl_getPlatformVendor(cl_platform_id platform);
  175. //-------------------------------------------------------
  176. // Utility functions
  177. //-------------------------------------------------------
  178. char* catStringWithInt(const char* str, int integer);
  179. char* itoa_portable(int value, char* result, int base);
  180. //-------------------------------------------------------
  181. // Data types
  182. //-------------------------------------------------------
  183. typedef struct{
  184. int x;
  185. int y;
  186. } int2;
  187. typedef struct{
  188. float x;
  189. float y;
  190. }float2;
  191. typedef struct{
  192. float x;
  193. float y;
  194. float z;
  195. float w;
  196. }float4;
  197. //-------------------------------------------------------
  198. // Defines
  199. //-------------------------------------------------------
  200. #define MAX_ERR_VAL 64
  201. #define NUM_PROGRAMS 7
  202. #define NUM_KERNELS 13
  203. #define KERNEL_INIT_DET 0
  204. #define KERNEL_BUILD_DET 1
  205. #define KERNEL_SURF_DESC 2
  206. #define KERNEL_NORM_DESC 3
  207. #define KERNEL_NON_MAX_SUP 4
  208. #define KERNEL_GET_ORIENT1 5
  209. #define KERNEL_GET_ORIENT2 6
  210. #define KERNEL_NN 7
  211. #define KERNEL_SCAN 8
  212. #define KERNEL_SCAN4 9
  213. #define KERNEL_TRANSPOSE 10
  214. #define KERNEL_SCANIMAGE 11
  215. #define KERNEL_TRANSPOSEIMAGE 12
  216. #endif