opencl_batch.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * opencl_batch.c — Intel Gen7.5..Gen9.5 OpenCL 1.2 Zero-Copy Shared Memory Batch Offload (v0.8.1.19)
  3. *
  4. * Strictly OFF the real-time short serial path!
  5. * Dynamically loads libOpenCL.so.1 via dlopen; automatically falls back to pure
  6. * x86-64 SSE2 CPU kernels on 2nd-Gen Sandy Bridge, missing ICDs, below break-even
  7. * threshold S*, or when 15W U-series TDP headroom would throttle CPU clocks.
  8. */
  9. #define _GNU_SOURCE
  10. #include "stl_accel.h"
  11. #include <dlfcn.h>
  12. #include <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. /* Zig pure-SSE2 CPU fallbacks (zig_kernels.zig) */
  19. extern size_t zig_batch_rescan_logs_cpu(
  20. const uint8_t *buf_ptr,
  21. size_t len,
  22. uint32_t *out_offsets,
  23. uint8_t *out_tags,
  24. size_t max_lines);
  25. extern void zig_rmt_eye_reduce_cpu(
  26. const float *margins_in,
  27. size_t station_count,
  28. float *eye_grid_out,
  29. float *summary_out);
  30. /* Minimal Khronos OpenCL 1.2 C ABI definitions (no external headers required) */
  31. typedef int32_t cl_int;
  32. typedef uint32_t cl_uint;
  33. typedef uint64_t cl_ulong;
  34. typedef uint64_t cl_bitfield;
  35. typedef cl_uint cl_bool;
  36. typedef void *cl_platform_id;
  37. typedef void *cl_device_id;
  38. typedef void *cl_context;
  39. typedef void *cl_command_queue;
  40. typedef void *cl_mem;
  41. typedef void *cl_program;
  42. typedef void *cl_kernel;
  43. typedef void *cl_event;
  44. #define CL_SUCCESS 0
  45. #define CL_TRUE 1u
  46. #define CL_DEVICE_TYPE_GPU (1ULL << 2)
  47. #define CL_DEVICE_NAME 0x102Bu
  48. #define CL_DEVICE_MAX_COMPUTE_UNITS 0x1002u
  49. #define CL_DEVICE_HOST_UNIFIED_MEMORY 0x1035u
  50. #define CL_MEM_READ_WRITE (1ULL << 0)
  51. #define CL_MEM_WRITE_ONLY (1ULL << 1)
  52. #define CL_MEM_READ_ONLY (1ULL << 2)
  53. #define CL_MEM_USE_HOST_PTR (1ULL << 3)
  54. #define CL_MAP_READ (1ULL << 0)
  55. typedef cl_int (*fn_clGetPlatformIDs)(cl_uint, cl_platform_id *, cl_uint *);
  56. typedef cl_int (*fn_clGetDeviceIDs)(cl_platform_id, cl_bitfield, cl_uint, cl_device_id *, cl_uint *);
  57. typedef cl_int (*fn_clGetDeviceInfo)(cl_device_id, cl_uint, size_t, void *, size_t *);
  58. typedef cl_context (*fn_clCreateContext)(const intptr_t *, cl_uint, const cl_device_id *, void *, void *, cl_int *);
  59. typedef cl_command_queue (*fn_clCreateCommandQueue)(cl_context, cl_device_id, cl_bitfield, cl_int *);
  60. typedef cl_program (*fn_clCreateProgramWithSource)(cl_context, cl_uint, const char **, const size_t *, cl_int *);
  61. typedef cl_int (*fn_clBuildProgram)(cl_program, cl_uint, const cl_device_id *, const char *, void *, void *);
  62. typedef cl_kernel (*fn_clCreateKernel)(cl_program, const char *, cl_int *);
  63. typedef cl_mem (*fn_clCreateBuffer)(cl_context, cl_bitfield, size_t, void *, cl_int *);
  64. typedef cl_int (*fn_clSetKernelArg)(cl_kernel, cl_uint, size_t, const void *);
  65. typedef cl_int (*fn_clEnqueueNDRangeKernel)(
  66. cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *, const size_t *, cl_uint, const cl_event *, cl_event *);
  67. typedef void *(*fn_clEnqueueMapBuffer)(
  68. cl_command_queue, cl_mem, cl_bool, cl_bitfield, size_t, size_t, cl_uint, const cl_event *, cl_event *, cl_int *);
  69. typedef cl_int (*fn_clEnqueueUnmapMemObject)(
  70. cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *);
  71. typedef cl_int (*fn_clFinish)(cl_command_queue);
  72. typedef cl_int (*fn_clReleaseMemObject)(cl_mem);
  73. static const char *k_opencl_src =
  74. "__kernel void rmt_eye_surface(\n"
  75. " __global const float4 *margins,\n"
  76. " __global float *eye_out)\n"
  77. "{\n"
  78. " int ch_idx = get_global_id(0);\n"
  79. " int pix = get_global_id(1);\n"
  80. " int row = pix >> 4;\n"
  81. " int col = pix & 15;\n"
  82. " float4 m = fabs(margins[ch_idx]);\n"
  83. " float ht = fmax((m.x + m.y) * 0.5f, 0.001f);\n"
  84. " float hv = fmax((m.z + m.w) * 0.5f, 0.001f);\n"
  85. " float nx = fabs(((float)col - 7.5f) / 7.5f * 32.0f) / ht;\n"
  86. " float ny = fabs(((float)row - 7.5f) / 7.5f * 32.0f) / hv;\n"
  87. " eye_out[ch_idx * 256 + pix] = fmax(0.0f, 1.0f - (nx * nx + ny * ny));\n"
  88. "}\n"
  89. "__kernel void batch_line_tags(\n"
  90. " __global const uchar *buf,\n"
  91. " uint len,\n"
  92. " __global uchar *block_flags)\n"
  93. "{\n"
  94. " uint blk = get_global_id(0);\n"
  95. " uint base = blk << 8;\n"
  96. " uint lim = base + 256u;\n"
  97. " if (lim > len) lim = len;\n"
  98. " uchar flg = 0u;\n"
  99. " for (uint i = base; i < lim; i++) {\n"
  100. " uchar c = buf[i];\n"
  101. " if (c == 10u || c == 13u) flg |= 16u;\n"
  102. " else if (c == 91u) flg |= 1u;\n"
  103. " else if (c == 62u || c == 35u) flg |= 2u;\n"
  104. " else if (c == 69u || c == 70u || c == 87u || c == 85u) flg |= 4u;\n"
  105. " else if (c >= 48u && c <= 57u) flg |= 8u;\n"
  106. " }\n"
  107. " block_flags[blk] = flg;\n"
  108. "}\n";
  109. static pthread_once_t g_cl_once = PTHREAD_ONCE_INIT;
  110. static pthread_mutex_t g_cl_mutex = PTHREAD_MUTEX_INITIALIZER;
  111. static StlOpenClInfo g_cl_info;
  112. static void *g_cl_lib = NULL;
  113. static cl_context g_cl_ctx = NULL;
  114. static cl_command_queue g_cl_queue = NULL;
  115. static cl_kernel g_cl_eye_k = NULL;
  116. static cl_kernel g_cl_log_k = NULL;
  117. static fn_clCreateBuffer p_clCreateBuffer = NULL;
  118. static fn_clSetKernelArg p_clSetKernelArg = NULL;
  119. static fn_clEnqueueNDRangeKernel p_clEnqueueNDRangeKernel = NULL;
  120. static fn_clEnqueueMapBuffer p_clEnqueueMapBuffer = NULL;
  121. static fn_clEnqueueUnmapMemObject p_clEnqueueUnmapMemObject = NULL;
  122. static fn_clFinish p_clFinish = NULL;
  123. static fn_clReleaseMemObject p_clReleaseMemObject = NULL;
  124. static void init_opencl_once(void) {
  125. memset(&g_cl_info, 0, sizeof(g_cl_info));
  126. g_cl_info.break_even_bytes = 262144ULL; /* S* = 256 KiB */
  127. snprintf(g_cl_info.device_name, sizeof(g_cl_info.device_name), "cpu-simd-fallback");
  128. g_cl_lib = dlopen("libOpenCL.so.1", RTLD_LAZY | RTLD_LOCAL);
  129. if (!g_cl_lib) return;
  130. fn_clGetPlatformIDs p_getPlats = (fn_clGetPlatformIDs)dlsym(g_cl_lib, "clGetPlatformIDs");
  131. fn_clGetDeviceIDs p_getDevs = (fn_clGetDeviceIDs)dlsym(g_cl_lib, "clGetDeviceIDs");
  132. fn_clGetDeviceInfo p_getInfo = (fn_clGetDeviceInfo)dlsym(g_cl_lib, "clGetDeviceInfo");
  133. fn_clCreateContext p_mkCtx = (fn_clCreateContext)dlsym(g_cl_lib, "clCreateContext");
  134. fn_clCreateCommandQueue p_mkQ = (fn_clCreateCommandQueue)dlsym(g_cl_lib, "clCreateCommandQueue");
  135. fn_clCreateProgramWithSource p_mkProg =
  136. (fn_clCreateProgramWithSource)dlsym(g_cl_lib, "clCreateProgramWithSource");
  137. fn_clBuildProgram p_bldProg = (fn_clBuildProgram)dlsym(g_cl_lib, "clBuildProgram");
  138. fn_clCreateKernel p_mkKern = (fn_clCreateKernel)dlsym(g_cl_lib, "clCreateKernel");
  139. p_clCreateBuffer = (fn_clCreateBuffer)dlsym(g_cl_lib, "clCreateBuffer");
  140. p_clSetKernelArg = (fn_clSetKernelArg)dlsym(g_cl_lib, "clSetKernelArg");
  141. p_clEnqueueNDRangeKernel = (fn_clEnqueueNDRangeKernel)dlsym(g_cl_lib, "clEnqueueNDRangeKernel");
  142. p_clEnqueueMapBuffer = (fn_clEnqueueMapBuffer)dlsym(g_cl_lib, "clEnqueueMapBuffer");
  143. p_clEnqueueUnmapMemObject = (fn_clEnqueueUnmapMemObject)dlsym(g_cl_lib, "clEnqueueUnmapMemObject");
  144. p_clFinish = (fn_clFinish)dlsym(g_cl_lib, "clFinish");
  145. p_clReleaseMemObject = (fn_clReleaseMemObject)dlsym(g_cl_lib, "clReleaseMemObject");
  146. if (!p_getPlats || !p_getDevs || !p_getInfo || !p_mkCtx || !p_mkQ || !p_mkProg ||
  147. !p_bldProg || !p_mkKern || !p_clCreateBuffer || !p_clSetKernelArg ||
  148. !p_clEnqueueNDRangeKernel || !p_clEnqueueMapBuffer || !p_clEnqueueUnmapMemObject ||
  149. !p_clFinish || !p_clReleaseMemObject) {
  150. return;
  151. }
  152. cl_platform_id plats[8];
  153. cl_uint nplats = 0;
  154. if (p_getPlats(8, plats, &nplats) != CL_SUCCESS || nplats == 0) return;
  155. cl_device_id chosen_dev = NULL;
  156. for (cl_uint p = 0; p < nplats && !chosen_dev; p++) {
  157. cl_device_id devs[4];
  158. cl_uint ndevs = 0;
  159. if (p_getDevs(plats[p], CL_DEVICE_TYPE_GPU, 4, devs, &ndevs) == CL_SUCCESS && ndevs > 0) {
  160. chosen_dev = devs[0];
  161. }
  162. }
  163. if (!chosen_dev) return;
  164. char name_buf[96];
  165. memset(name_buf, 0, sizeof(name_buf));
  166. (void)p_getInfo(chosen_dev, CL_DEVICE_NAME, sizeof(name_buf) - 1, name_buf, NULL);
  167. cl_uint cus = 0;
  168. (void)p_getInfo(chosen_dev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cus), &cus, NULL);
  169. cl_bool unified = 0;
  170. (void)p_getInfo(chosen_dev, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(unified), &unified, NULL);
  171. cl_int err = 0;
  172. cl_context ctx = p_mkCtx(NULL, 1, &chosen_dev, NULL, NULL, &err);
  173. if (!ctx || err != CL_SUCCESS) return;
  174. cl_command_queue q = p_mkQ(ctx, chosen_dev, 0, &err);
  175. if (!q || err != CL_SUCCESS) return;
  176. size_t src_len = strlen(k_opencl_src);
  177. cl_program prog = p_mkProg(ctx, 1, &k_opencl_src, &src_len, &err);
  178. if (!prog || err != CL_SUCCESS) return;
  179. if (p_bldProg(prog, 1, &chosen_dev, "-cl-fast-relaxed-math", NULL, NULL) != CL_SUCCESS) return;
  180. cl_kernel kern = p_mkKern(prog, "rmt_eye_surface", &err);
  181. if (!kern || err != CL_SUCCESS) return;
  182. cl_kernel log_kern = p_mkKern(prog, "batch_line_tags", &err);
  183. g_cl_ctx = ctx;
  184. g_cl_queue = q;
  185. g_cl_eye_k = kern;
  186. g_cl_log_k = (err == CL_SUCCESS) ? log_kern : NULL;
  187. g_cl_info.available = 1;
  188. g_cl_info.zero_copy_llc = (unified == CL_TRUE) ? 1 : 0;
  189. g_cl_info.compute_units = cus;
  190. snprintf(g_cl_info.device_name, sizeof(g_cl_info.device_name), "%s", name_buf);
  191. }
  192. static int read_sysfs_u64(const char *path, uint64_t *out) {
  193. FILE *f = fopen(path, "r");
  194. if (!f) return 0;
  195. unsigned long long v = 0;
  196. int ok = (fscanf(f, "%llu", &v) == 1);
  197. fclose(f);
  198. if (ok) *out = (uint64_t)v;
  199. return ok;
  200. }
  201. /*
  202. * Guard against 15W U-series package power throttling:
  203. * If RAPL PL1 <= 16W and CPU load or frequency droop indicates package TDP pressure,
  204. * keep iGPU idle so CPU cores retain full Turbo clock for serial/protocol actors.
  205. */
  206. int stl_opencl_has_tdp_headroom(void) {
  207. uint64_t pl1_uw = 0;
  208. if (read_sysfs_u64("/sys/class/powercap/intel-rapl:0/constraint_0_power_limit_uw", &pl1_uw)) {
  209. if (pl1_uw > 0 && pl1_uw <= 16000000ULL) {
  210. /* 15W U-series SKU: check CPU loadavg and current scaling frequency */
  211. double load1 = 0.0;
  212. if (getloadavg(&load1, 1) == 1) {
  213. long ncores = sysconf(_SC_NPROCESSORS_ONLN);
  214. if (ncores > 0 && load1 > (double)ncores * 0.55) {
  215. return 0;
  216. }
  217. }
  218. }
  219. }
  220. uint64_t cur_khz = 0, max_khz = 0;
  221. if (read_sysfs_u64("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", &cur_khz) &&
  222. read_sysfs_u64("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", &max_khz)) {
  223. if (max_khz > 0 && cur_khz * 100ULL < max_khz * 50ULL) {
  224. /* Only if heavily throttled under active load */
  225. double load1 = 0.0;
  226. if (getloadavg(&load1, 1) == 1 && load1 > 1.5) return 0;
  227. }
  228. }
  229. return 1;
  230. }
  231. void stl_opencl_query_info(StlOpenClInfo *out_info) {
  232. pthread_once(&g_cl_once, init_opencl_once);
  233. if (!out_info) return;
  234. *out_info = g_cl_info;
  235. }
  236. size_t stl_batch_rescan_logs(
  237. const uint8_t *buf_4k_aligned,
  238. size_t len,
  239. uint32_t *out_line_offsets,
  240. uint8_t *out_line_tags,
  241. size_t max_lines,
  242. int *used_gpu) {
  243. pthread_once(&g_cl_once, init_opencl_once);
  244. if (used_gpu) *used_gpu = 0;
  245. if (!buf_4k_aligned || len == 0 || !out_line_offsets || !out_line_tags || max_lines == 0) {
  246. return 0;
  247. }
  248. /* Break-even & 15W TDP headroom gate: short logs or missing iGPU stay on pure SSE2 CPU */
  249. if (!g_cl_info.available || !g_cl_log_k || len < (size_t)g_cl_info.break_even_bytes ||
  250. ((uintptr_t)buf_4k_aligned & 4095u) != 0 || !stl_opencl_has_tdp_headroom()) {
  251. return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
  252. }
  253. size_t n_blocks = (len + 255u) >> 8;
  254. size_t in_bytes = (len + 4095u) & ~4095u;
  255. size_t flg_bytes = (n_blocks + 4095u) & ~4095u;
  256. void *flg_4k = NULL;
  257. if (posix_memalign(&flg_4k, 4096, flg_bytes) != 0) {
  258. return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
  259. }
  260. int gpu_ok = 0;
  261. pthread_mutex_lock(&g_cl_mutex);
  262. cl_int e1 = 0, e2 = 0;
  263. cl_mem m_in = p_clCreateBuffer(
  264. g_cl_ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, in_bytes, (void *)buf_4k_aligned, &e1);
  265. cl_mem m_flg = p_clCreateBuffer(
  266. g_cl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, flg_bytes, flg_4k, &e2);
  267. cl_uint u_len = (cl_uint)(len > 0xFFFFFFFFu ? 0xFFFFFFFFu : len);
  268. if (m_in && m_flg && e1 == CL_SUCCESS && e2 == CL_SUCCESS) {
  269. if (p_clSetKernelArg(g_cl_log_k, 0, sizeof(cl_mem), &m_in) == CL_SUCCESS &&
  270. p_clSetKernelArg(g_cl_log_k, 1, sizeof(cl_uint), &u_len) == CL_SUCCESS &&
  271. p_clSetKernelArg(g_cl_log_k, 2, sizeof(cl_mem), &m_flg) == CL_SUCCESS) {
  272. size_t gws[1] = { n_blocks };
  273. if (p_clEnqueueNDRangeKernel(g_cl_queue, g_cl_log_k, 1, NULL, gws, NULL, 0, NULL, NULL) == CL_SUCCESS) {
  274. cl_int map_err = 0;
  275. uint8_t *mapped = (uint8_t *)p_clEnqueueMapBuffer(
  276. g_cl_queue, m_flg, CL_TRUE, CL_MAP_READ, 0, n_blocks, 0, NULL, NULL, &map_err);
  277. if (mapped && map_err == CL_SUCCESS) {
  278. gpu_ok = 1;
  279. (void)p_clEnqueueUnmapMemObject(g_cl_queue, m_flg, mapped, 0, NULL, NULL);
  280. (void)p_clFinish(g_cl_queue);
  281. }
  282. }
  283. }
  284. }
  285. if (m_in) (void)p_clReleaseMemObject(m_in);
  286. if (m_flg) (void)p_clReleaseMemObject(m_flg);
  287. pthread_mutex_unlock(&g_cl_mutex);
  288. if (!gpu_ok) {
  289. free(flg_4k);
  290. return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
  291. }
  292. /* Prefix-compact 256B block flags from zero-copy iGPU pass into exact line offsets & tags */
  293. const uint8_t *bflags = (const uint8_t *)flg_4k;
  294. size_t line_count = 0, line_start = 0;
  295. uint8_t cur_tag = 0;
  296. int prev_cr = 0;
  297. for (size_t blk = 0; blk < n_blocks && line_count < max_lines; blk++) {
  298. uint8_t bf = bflags[blk];
  299. size_t base = blk << 8;
  300. size_t lim = base + 256u;
  301. if (lim > len) lim = len;
  302. if ((bf & 16u) == 0 && !prev_cr) {
  303. cur_tag |= (bf & 15u);
  304. continue;
  305. }
  306. for (size_t i = base; i < lim && line_count < max_lines; i++) {
  307. uint8_t c = buf_4k_aligned[i];
  308. if (c == '\n') {
  309. if (prev_cr) { prev_cr = 0; line_start = i + 1; continue; }
  310. if (i > line_start) {
  311. out_line_offsets[line_count] = (uint32_t)line_start;
  312. out_line_tags[line_count] = cur_tag;
  313. line_count++;
  314. }
  315. cur_tag = 0;
  316. line_start = i + 1;
  317. } else if (c == '\r') {
  318. if (i > line_start) {
  319. out_line_offsets[line_count] = (uint32_t)line_start;
  320. out_line_tags[line_count] = cur_tag;
  321. line_count++;
  322. }
  323. cur_tag = 0;
  324. prev_cr = 1;
  325. line_start = i + 1;
  326. } else {
  327. prev_cr = 0;
  328. if (c == '[') cur_tag |= 1u;
  329. else if (c == '>' || c == '#') cur_tag |= 2u;
  330. else if (c == 'E' || c == 'F' || c == 'W' || c == 'U') cur_tag |= 4u;
  331. else if (c >= '0' && c <= '9') cur_tag |= 8u;
  332. }
  333. }
  334. }
  335. if (line_start < len && line_count < max_lines) {
  336. out_line_offsets[line_count] = (uint32_t)line_start;
  337. out_line_tags[line_count] = cur_tag;
  338. line_count++;
  339. }
  340. free(flg_4k);
  341. if (used_gpu) *used_gpu = 1;
  342. return line_count;
  343. }
  344. int stl_batch_rmt_eye_reduce(
  345. const float *margins_in,
  346. size_t station_count,
  347. float *eye_grid_out,
  348. float *summary_out,
  349. int *used_gpu) {
  350. pthread_once(&g_cl_once, init_opencl_once);
  351. if (used_gpu) *used_gpu = 0;
  352. if (!margins_in || station_count == 0 || !eye_grid_out || !summary_out) return -1;
  353. /* Break-even gate: require >= 4 stations AND available zero-copy iGPU AND 15W TDP headroom */
  354. if (!g_cl_info.available || station_count < 4 || !stl_opencl_has_tdp_headroom()) {
  355. zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
  356. return 0;
  357. }
  358. size_t total_ch = station_count * 24;
  359. size_t in_bytes = ((total_ch * 4 * sizeof(float)) + 4095u) & ~4095u;
  360. size_t out_bytes = ((total_ch * 256 * sizeof(float)) + 4095u) & ~4095u;
  361. void *in_4k = NULL;
  362. void *out_4k = NULL;
  363. if (posix_memalign(&in_4k, 4096, in_bytes) != 0 ||
  364. posix_memalign(&out_4k, 4096, out_bytes) != 0) {
  365. free(in_4k);
  366. free(out_4k);
  367. zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
  368. return 0;
  369. }
  370. memcpy(in_4k, margins_in, total_ch * 4 * sizeof(float));
  371. int gpu_ok = 0;
  372. pthread_mutex_lock(&g_cl_mutex);
  373. cl_int e1 = 0, e2 = 0;
  374. cl_mem m_in = p_clCreateBuffer(g_cl_ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, in_bytes, in_4k, &e1);
  375. cl_mem m_out = p_clCreateBuffer(g_cl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, out_bytes, out_4k, &e2);
  376. if (m_in && m_out && e1 == CL_SUCCESS && e2 == CL_SUCCESS) {
  377. if (p_clSetKernelArg(g_cl_eye_k, 0, sizeof(cl_mem), &m_in) == CL_SUCCESS &&
  378. p_clSetKernelArg(g_cl_eye_k, 1, sizeof(cl_mem), &m_out) == CL_SUCCESS) {
  379. size_t gws[2] = { total_ch, 256 };
  380. if (p_clEnqueueNDRangeKernel(g_cl_queue, g_cl_eye_k, 2, NULL, gws, NULL, 0, NULL, NULL) == CL_SUCCESS) {
  381. cl_int map_err = 0;
  382. void *mapped = p_clEnqueueMapBuffer(
  383. g_cl_queue, m_out, CL_TRUE, CL_MAP_READ, 0, total_ch * 256 * sizeof(float),
  384. 0, NULL, NULL, &map_err);
  385. if (mapped && map_err == CL_SUCCESS) {
  386. memcpy(eye_grid_out, mapped, total_ch * 256 * sizeof(float));
  387. (void)p_clEnqueueUnmapMemObject(g_cl_queue, m_out, mapped, 0, NULL, NULL);
  388. (void)p_clFinish(g_cl_queue);
  389. gpu_ok = 1;
  390. }
  391. }
  392. }
  393. }
  394. if (m_in) (void)p_clReleaseMemObject(m_in);
  395. if (m_out) (void)p_clReleaseMemObject(m_out);
  396. pthread_mutex_unlock(&g_cl_mutex);
  397. free(in_4k);
  398. free(out_4k);
  399. if (!gpu_ok) {
  400. zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
  401. return 0;
  402. }
  403. /* Compute compact per-station scalar min/mean summary on CPU (96 floats) */
  404. for (size_t st = 0; st < station_count; st++) {
  405. float min_t = 999.0f, min_v = 999.0f, sum_t = 0.0f, sum_v = 0.0f, cnt = 0.0f;
  406. for (size_t ch = 0; ch < 24; ch++) {
  407. const float *m = &margins_in[(st * 24 + ch) * 4];
  408. float rx_lo = m[0] < 0 ? -m[0] : m[0];
  409. float rx_hi = m[1] < 0 ? -m[1] : m[1];
  410. float tx_lo = m[2] < 0 ? -m[2] : m[2];
  411. float tx_hi = m[3] < 0 ? -m[3] : m[3];
  412. float ts = rx_lo + rx_hi;
  413. float vs = tx_lo + tx_hi;
  414. if (ts > 0.0f || vs > 0.0f) {
  415. if (ts < min_t) min_t = ts;
  416. if (vs < min_v) min_v = vs;
  417. sum_t += ts;
  418. sum_v += vs;
  419. cnt += 1.0f;
  420. }
  421. }
  422. summary_out[st * 4 + 0] = cnt > 0.0f ? min_t : 0.0f;
  423. summary_out[st * 4 + 1] = cnt > 0.0f ? min_v : 0.0f;
  424. summary_out[st * 4 + 2] = cnt > 0.0f ? (sum_t / cnt) : 0.0f;
  425. summary_out[st * 4 + 3] = cnt > 0.0f ? (sum_v / cnt) : 0.0f;
  426. }
  427. if (used_gpu) *used_gpu = 1;
  428. return 0;
  429. }