thneed_common.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "selfdrive/modeld/thneed/thneed.h"
  2. #include <cassert>
  3. #include <cstring>
  4. #include <map>
  5. #include "common/clutil.h"
  6. #include "common/timing.h"
  7. map<pair<cl_kernel, int>, string> g_args;
  8. map<pair<cl_kernel, int>, int> g_args_size;
  9. map<cl_program, string> g_program_source;
  10. void Thneed::stop() {
  11. //printf("Thneed::stop: recorded %lu commands\n", cmds.size());
  12. record = false;
  13. }
  14. void Thneed::clinit() {
  15. device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
  16. if (context == NULL) context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));
  17. //cl_command_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
  18. cl_command_queue_properties props[3] = {CL_QUEUE_PROPERTIES, 0, 0};
  19. command_queue = CL_CHECK_ERR(clCreateCommandQueueWithProperties(context, device_id, props, &err));
  20. printf("Thneed::clinit done\n");
  21. }
  22. cl_int Thneed::clexec() {
  23. if (debug >= 1) printf("Thneed::clexec: running %lu queued kernels\n", kq.size());
  24. for (auto &k : kq) {
  25. if (record) ckq.push_back(k);
  26. cl_int ret = k->exec();
  27. assert(ret == CL_SUCCESS);
  28. }
  29. return clFinish(command_queue);
  30. }
  31. void Thneed::copy_inputs(float **finputs, bool internal) {
  32. for (int idx = 0; idx < inputs.size(); ++idx) {
  33. if (debug >= 1) printf("copying %lu -- %p -> %p (cl %p)\n", input_sizes[idx], finputs[idx], inputs[idx], input_clmem[idx]);
  34. if (internal) {
  35. // if it's internal, using memcpy is fine since the buffer sync is cached in the ioctl layer
  36. if (finputs[idx] != NULL) memcpy(inputs[idx], finputs[idx], input_sizes[idx]);
  37. } else {
  38. if (finputs[idx] != NULL) CL_CHECK(clEnqueueWriteBuffer(command_queue, input_clmem[idx], CL_TRUE, 0, input_sizes[idx], finputs[idx], 0, NULL, NULL));
  39. }
  40. }
  41. }
  42. void Thneed::copy_output(float *foutput) {
  43. if (output != NULL) {
  44. size_t sz;
  45. clGetMemObjectInfo(output, CL_MEM_SIZE, sizeof(sz), &sz, NULL);
  46. if (debug >= 1) printf("copying %lu for output %p -> %p\n", sz, output, foutput);
  47. CL_CHECK(clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0, sz, foutput, 0, NULL, NULL));
  48. } else {
  49. printf("CAUTION: model output is NULL, does it have no outputs?\n");
  50. }
  51. }
  52. // *********** CLQueuedKernel ***********
  53. CLQueuedKernel::CLQueuedKernel(Thneed *lthneed,
  54. cl_kernel _kernel,
  55. cl_uint _work_dim,
  56. const size_t *_global_work_size,
  57. const size_t *_local_work_size) {
  58. thneed = lthneed;
  59. kernel = _kernel;
  60. work_dim = _work_dim;
  61. assert(work_dim <= 3);
  62. for (int i = 0; i < work_dim; i++) {
  63. global_work_size[i] = _global_work_size[i];
  64. local_work_size[i] = _local_work_size[i];
  65. }
  66. char _name[0x100];
  67. clGetKernelInfo(kernel, CL_KERNEL_FUNCTION_NAME, sizeof(_name), _name, NULL);
  68. name = string(_name);
  69. clGetKernelInfo(kernel, CL_KERNEL_NUM_ARGS, sizeof(num_args), &num_args, NULL);
  70. // get args
  71. for (int i = 0; i < num_args; i++) {
  72. char arg_name[0x100] = {0};
  73. clGetKernelArgInfo(kernel, i, CL_KERNEL_ARG_NAME, sizeof(arg_name), arg_name, NULL);
  74. arg_names.push_back(string(arg_name));
  75. clGetKernelArgInfo(kernel, i, CL_KERNEL_ARG_TYPE_NAME, sizeof(arg_name), arg_name, NULL);
  76. arg_types.push_back(string(arg_name));
  77. args.push_back(g_args[make_pair(kernel, i)]);
  78. args_size.push_back(g_args_size[make_pair(kernel, i)]);
  79. }
  80. // get program
  81. clGetKernelInfo(kernel, CL_KERNEL_PROGRAM, sizeof(program), &program, NULL);
  82. }
  83. int CLQueuedKernel::get_arg_num(const char *search_arg_name) {
  84. for (int i = 0; i < num_args; i++) {
  85. if (arg_names[i] == search_arg_name) return i;
  86. }
  87. printf("failed to find %s in %s\n", search_arg_name, name.c_str());
  88. assert(false);
  89. }
  90. cl_int CLQueuedKernel::exec() {
  91. if (kernel == NULL) {
  92. kernel = clCreateKernel(program, name.c_str(), NULL);
  93. arg_names.clear();
  94. arg_types.clear();
  95. for (int j = 0; j < num_args; j++) {
  96. char arg_name[0x100] = {0};
  97. clGetKernelArgInfo(kernel, j, CL_KERNEL_ARG_NAME, sizeof(arg_name), arg_name, NULL);
  98. arg_names.push_back(string(arg_name));
  99. clGetKernelArgInfo(kernel, j, CL_KERNEL_ARG_TYPE_NAME, sizeof(arg_name), arg_name, NULL);
  100. arg_types.push_back(string(arg_name));
  101. cl_int ret;
  102. if (args[j].size() != 0) {
  103. assert(args[j].size() == args_size[j]);
  104. ret = thneed_clSetKernelArg(kernel, j, args[j].size(), args[j].data());
  105. } else {
  106. ret = thneed_clSetKernelArg(kernel, j, args_size[j], NULL);
  107. }
  108. assert(ret == CL_SUCCESS);
  109. }
  110. }
  111. if (thneed->debug >= 1) {
  112. debug_print(thneed->debug >= 2);
  113. }
  114. return clEnqueueNDRangeKernel(thneed->command_queue,
  115. kernel, work_dim, NULL, global_work_size, local_work_size, 0, NULL, NULL);
  116. }
  117. void CLQueuedKernel::debug_print(bool verbose) {
  118. printf("%p %56s -- ", kernel, name.c_str());
  119. for (int i = 0; i < work_dim; i++) {
  120. printf("%4zu ", global_work_size[i]);
  121. }
  122. printf(" -- ");
  123. for (int i = 0; i < work_dim; i++) {
  124. printf("%4zu ", local_work_size[i]);
  125. }
  126. printf("\n");
  127. if (verbose) {
  128. for (int i = 0; i < num_args; i++) {
  129. string arg = args[i];
  130. printf(" %s %s", arg_types[i].c_str(), arg_names[i].c_str());
  131. void *arg_value = (void*)arg.data();
  132. int arg_size = arg.size();
  133. if (arg_size == 0) {
  134. printf(" (size) %d", args_size[i]);
  135. } else if (arg_size == 1) {
  136. printf(" = %d", *((char*)arg_value));
  137. } else if (arg_size == 2) {
  138. printf(" = %d", *((short*)arg_value));
  139. } else if (arg_size == 4) {
  140. if (arg_types[i] == "float") {
  141. printf(" = %f", *((float*)arg_value));
  142. } else {
  143. printf(" = %d", *((int*)arg_value));
  144. }
  145. } else if (arg_size == 8) {
  146. cl_mem val = (cl_mem)(*((uintptr_t*)arg_value));
  147. printf(" = %p", val);
  148. if (val != NULL) {
  149. cl_mem_object_type obj_type;
  150. clGetMemObjectInfo(val, CL_MEM_TYPE, sizeof(obj_type), &obj_type, NULL);
  151. if (arg_types[i] == "image2d_t" || arg_types[i] == "image1d_t" || obj_type == CL_MEM_OBJECT_IMAGE2D) {
  152. cl_image_format format;
  153. size_t width, height, depth, array_size, row_pitch, slice_pitch;
  154. cl_mem buf;
  155. clGetImageInfo(val, CL_IMAGE_FORMAT, sizeof(format), &format, NULL);
  156. assert(format.image_channel_order == CL_RGBA);
  157. assert(format.image_channel_data_type == CL_HALF_FLOAT || format.image_channel_data_type == CL_FLOAT);
  158. clGetImageInfo(val, CL_IMAGE_WIDTH, sizeof(width), &width, NULL);
  159. clGetImageInfo(val, CL_IMAGE_HEIGHT, sizeof(height), &height, NULL);
  160. clGetImageInfo(val, CL_IMAGE_ROW_PITCH, sizeof(row_pitch), &row_pitch, NULL);
  161. clGetImageInfo(val, CL_IMAGE_DEPTH, sizeof(depth), &depth, NULL);
  162. clGetImageInfo(val, CL_IMAGE_ARRAY_SIZE, sizeof(array_size), &array_size, NULL);
  163. clGetImageInfo(val, CL_IMAGE_SLICE_PITCH, sizeof(slice_pitch), &slice_pitch, NULL);
  164. assert(depth == 0);
  165. assert(array_size == 0);
  166. assert(slice_pitch == 0);
  167. clGetImageInfo(val, CL_IMAGE_BUFFER, sizeof(buf), &buf, NULL);
  168. size_t sz = 0;
  169. if (buf != NULL) clGetMemObjectInfo(buf, CL_MEM_SIZE, sizeof(sz), &sz, NULL);
  170. printf(" image %zu x %zu rp %zu @ %p buffer %zu", width, height, row_pitch, buf, sz);
  171. } else {
  172. size_t sz;
  173. clGetMemObjectInfo(val, CL_MEM_SIZE, sizeof(sz), &sz, NULL);
  174. printf(" buffer %zu", sz);
  175. }
  176. }
  177. }
  178. printf("\n");
  179. }
  180. }
  181. }
  182. cl_int thneed_clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value) {
  183. g_args_size[make_pair(kernel, arg_index)] = arg_size;
  184. if (arg_value != NULL) {
  185. g_args[make_pair(kernel, arg_index)] = string((char*)arg_value, arg_size);
  186. } else {
  187. g_args[make_pair(kernel, arg_index)] = string("");
  188. }
  189. cl_int ret = clSetKernelArg(kernel, arg_index, arg_size, arg_value);
  190. return ret;
  191. }