| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- /*
- * opencl_batch.c — Intel Gen7.5..Gen9.5 OpenCL 1.2 Zero-Copy Shared Memory Batch Offload (v0.8.1.19)
- *
- * Strictly OFF the real-time short serial path!
- * Dynamically loads libOpenCL.so.1 via dlopen; automatically falls back to pure
- * x86-64 SSE2 CPU kernels on 2nd-Gen Sandy Bridge, missing ICDs, below break-even
- * threshold S*, or when 15W U-series TDP headroom would throttle CPU clocks.
- */
- #define _GNU_SOURCE
- #include "stl_accel.h"
- #include <dlfcn.h>
- #include <pthread.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- /* Zig pure-SSE2 CPU fallbacks (zig_kernels.zig) */
- extern size_t zig_batch_rescan_logs_cpu(
- const uint8_t *buf_ptr,
- size_t len,
- uint32_t *out_offsets,
- uint8_t *out_tags,
- size_t max_lines);
- extern void zig_rmt_eye_reduce_cpu(
- const float *margins_in,
- size_t station_count,
- float *eye_grid_out,
- float *summary_out);
- /* Minimal Khronos OpenCL 1.2 C ABI definitions (no external headers required) */
- typedef int32_t cl_int;
- typedef uint32_t cl_uint;
- typedef uint64_t cl_ulong;
- typedef uint64_t cl_bitfield;
- typedef cl_uint cl_bool;
- typedef void *cl_platform_id;
- typedef void *cl_device_id;
- typedef void *cl_context;
- typedef void *cl_command_queue;
- typedef void *cl_mem;
- typedef void *cl_program;
- typedef void *cl_kernel;
- typedef void *cl_event;
- #define CL_SUCCESS 0
- #define CL_TRUE 1u
- #define CL_DEVICE_TYPE_GPU (1ULL << 2)
- #define CL_DEVICE_NAME 0x102Bu
- #define CL_DEVICE_MAX_COMPUTE_UNITS 0x1002u
- #define CL_DEVICE_HOST_UNIFIED_MEMORY 0x1035u
- #define CL_MEM_READ_WRITE (1ULL << 0)
- #define CL_MEM_WRITE_ONLY (1ULL << 1)
- #define CL_MEM_READ_ONLY (1ULL << 2)
- #define CL_MEM_USE_HOST_PTR (1ULL << 3)
- #define CL_MAP_READ (1ULL << 0)
- typedef cl_int (*fn_clGetPlatformIDs)(cl_uint, cl_platform_id *, cl_uint *);
- typedef cl_int (*fn_clGetDeviceIDs)(cl_platform_id, cl_bitfield, cl_uint, cl_device_id *, cl_uint *);
- typedef cl_int (*fn_clGetDeviceInfo)(cl_device_id, cl_uint, size_t, void *, size_t *);
- typedef cl_context (*fn_clCreateContext)(const intptr_t *, cl_uint, const cl_device_id *, void *, void *, cl_int *);
- typedef cl_command_queue (*fn_clCreateCommandQueue)(cl_context, cl_device_id, cl_bitfield, cl_int *);
- typedef cl_program (*fn_clCreateProgramWithSource)(cl_context, cl_uint, const char **, const size_t *, cl_int *);
- typedef cl_int (*fn_clBuildProgram)(cl_program, cl_uint, const cl_device_id *, const char *, void *, void *);
- typedef cl_kernel (*fn_clCreateKernel)(cl_program, const char *, cl_int *);
- typedef cl_mem (*fn_clCreateBuffer)(cl_context, cl_bitfield, size_t, void *, cl_int *);
- typedef cl_int (*fn_clSetKernelArg)(cl_kernel, cl_uint, size_t, const void *);
- typedef cl_int (*fn_clEnqueueNDRangeKernel)(
- cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *, const size_t *, cl_uint, const cl_event *, cl_event *);
- typedef void *(*fn_clEnqueueMapBuffer)(
- cl_command_queue, cl_mem, cl_bool, cl_bitfield, size_t, size_t, cl_uint, const cl_event *, cl_event *, cl_int *);
- typedef cl_int (*fn_clEnqueueUnmapMemObject)(
- cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *);
- typedef cl_int (*fn_clFinish)(cl_command_queue);
- typedef cl_int (*fn_clReleaseMemObject)(cl_mem);
- static const char *k_opencl_src =
- "__kernel void rmt_eye_surface(\n"
- " __global const float4 *margins,\n"
- " __global float *eye_out)\n"
- "{\n"
- " int ch_idx = get_global_id(0);\n"
- " int pix = get_global_id(1);\n"
- " int row = pix >> 4;\n"
- " int col = pix & 15;\n"
- " float4 m = fabs(margins[ch_idx]);\n"
- " float ht = fmax((m.x + m.y) * 0.5f, 0.001f);\n"
- " float hv = fmax((m.z + m.w) * 0.5f, 0.001f);\n"
- " float nx = fabs(((float)col - 7.5f) / 7.5f * 32.0f) / ht;\n"
- " float ny = fabs(((float)row - 7.5f) / 7.5f * 32.0f) / hv;\n"
- " eye_out[ch_idx * 256 + pix] = fmax(0.0f, 1.0f - (nx * nx + ny * ny));\n"
- "}\n"
- "__kernel void batch_line_tags(\n"
- " __global const uchar *buf,\n"
- " uint len,\n"
- " __global uchar *block_flags)\n"
- "{\n"
- " uint blk = get_global_id(0);\n"
- " uint base = blk << 8;\n"
- " uint lim = base + 256u;\n"
- " if (lim > len) lim = len;\n"
- " uchar flg = 0u;\n"
- " for (uint i = base; i < lim; i++) {\n"
- " uchar c = buf[i];\n"
- " if (c == 10u || c == 13u) flg |= 8u;\n"
- " else if (c == 91u) flg |= 1u;\n"
- " else if (c == 62u || c == 35u) flg |= 2u;\n"
- " else if (c == 69u || c == 70u || c == 87u || c == 85u) flg |= 4u;\n"
- " }\n"
- " block_flags[blk] = flg;\n"
- "}\n";
- static pthread_once_t g_cl_once = PTHREAD_ONCE_INIT;
- static pthread_mutex_t g_cl_mutex = PTHREAD_MUTEX_INITIALIZER;
- static StlOpenClInfo g_cl_info;
- static void *g_cl_lib = NULL;
- static cl_context g_cl_ctx = NULL;
- static cl_command_queue g_cl_queue = NULL;
- static cl_kernel g_cl_eye_k = NULL;
- static cl_kernel g_cl_log_k = NULL;
- static fn_clCreateBuffer p_clCreateBuffer = NULL;
- static fn_clSetKernelArg p_clSetKernelArg = NULL;
- static fn_clEnqueueNDRangeKernel p_clEnqueueNDRangeKernel = NULL;
- static fn_clEnqueueMapBuffer p_clEnqueueMapBuffer = NULL;
- static fn_clEnqueueUnmapMemObject p_clEnqueueUnmapMemObject = NULL;
- static fn_clFinish p_clFinish = NULL;
- static fn_clReleaseMemObject p_clReleaseMemObject = NULL;
- static void init_opencl_once(void) {
- memset(&g_cl_info, 0, sizeof(g_cl_info));
- g_cl_info.break_even_bytes = 262144ULL; /* S* = 256 KiB */
- snprintf(g_cl_info.device_name, sizeof(g_cl_info.device_name), "cpu-simd-fallback");
- g_cl_lib = dlopen("libOpenCL.so.1", RTLD_LAZY | RTLD_LOCAL);
- if (!g_cl_lib) return;
- fn_clGetPlatformIDs p_getPlats = (fn_clGetPlatformIDs)dlsym(g_cl_lib, "clGetPlatformIDs");
- fn_clGetDeviceIDs p_getDevs = (fn_clGetDeviceIDs)dlsym(g_cl_lib, "clGetDeviceIDs");
- fn_clGetDeviceInfo p_getInfo = (fn_clGetDeviceInfo)dlsym(g_cl_lib, "clGetDeviceInfo");
- fn_clCreateContext p_mkCtx = (fn_clCreateContext)dlsym(g_cl_lib, "clCreateContext");
- fn_clCreateCommandQueue p_mkQ = (fn_clCreateCommandQueue)dlsym(g_cl_lib, "clCreateCommandQueue");
- fn_clCreateProgramWithSource p_mkProg =
- (fn_clCreateProgramWithSource)dlsym(g_cl_lib, "clCreateProgramWithSource");
- fn_clBuildProgram p_bldProg = (fn_clBuildProgram)dlsym(g_cl_lib, "clBuildProgram");
- fn_clCreateKernel p_mkKern = (fn_clCreateKernel)dlsym(g_cl_lib, "clCreateKernel");
- p_clCreateBuffer = (fn_clCreateBuffer)dlsym(g_cl_lib, "clCreateBuffer");
- p_clSetKernelArg = (fn_clSetKernelArg)dlsym(g_cl_lib, "clSetKernelArg");
- p_clEnqueueNDRangeKernel = (fn_clEnqueueNDRangeKernel)dlsym(g_cl_lib, "clEnqueueNDRangeKernel");
- p_clEnqueueMapBuffer = (fn_clEnqueueMapBuffer)dlsym(g_cl_lib, "clEnqueueMapBuffer");
- p_clEnqueueUnmapMemObject = (fn_clEnqueueUnmapMemObject)dlsym(g_cl_lib, "clEnqueueUnmapMemObject");
- p_clFinish = (fn_clFinish)dlsym(g_cl_lib, "clFinish");
- p_clReleaseMemObject = (fn_clReleaseMemObject)dlsym(g_cl_lib, "clReleaseMemObject");
- if (!p_getPlats || !p_getDevs || !p_getInfo || !p_mkCtx || !p_mkQ || !p_mkProg ||
- !p_bldProg || !p_mkKern || !p_clCreateBuffer || !p_clSetKernelArg ||
- !p_clEnqueueNDRangeKernel || !p_clEnqueueMapBuffer || !p_clEnqueueUnmapMemObject ||
- !p_clFinish || !p_clReleaseMemObject) {
- return;
- }
- cl_platform_id plats[8];
- cl_uint nplats = 0;
- if (p_getPlats(8, plats, &nplats) != CL_SUCCESS || nplats == 0) return;
- cl_device_id chosen_dev = NULL;
- for (cl_uint p = 0; p < nplats && !chosen_dev; p++) {
- cl_device_id devs[4];
- cl_uint ndevs = 0;
- if (p_getDevs(plats[p], CL_DEVICE_TYPE_GPU, 4, devs, &ndevs) == CL_SUCCESS && ndevs > 0) {
- chosen_dev = devs[0];
- }
- }
- if (!chosen_dev) return;
- char name_buf[96];
- memset(name_buf, 0, sizeof(name_buf));
- (void)p_getInfo(chosen_dev, CL_DEVICE_NAME, sizeof(name_buf) - 1, name_buf, NULL);
- cl_uint cus = 0;
- (void)p_getInfo(chosen_dev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cus), &cus, NULL);
- cl_bool unified = 0;
- (void)p_getInfo(chosen_dev, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(unified), &unified, NULL);
- cl_int err = 0;
- cl_context ctx = p_mkCtx(NULL, 1, &chosen_dev, NULL, NULL, &err);
- if (!ctx || err != CL_SUCCESS) return;
- cl_command_queue q = p_mkQ(ctx, chosen_dev, 0, &err);
- if (!q || err != CL_SUCCESS) return;
- size_t src_len = strlen(k_opencl_src);
- cl_program prog = p_mkProg(ctx, 1, &k_opencl_src, &src_len, &err);
- if (!prog || err != CL_SUCCESS) return;
- if (p_bldProg(prog, 1, &chosen_dev, "-cl-fast-relaxed-math", NULL, NULL) != CL_SUCCESS) return;
- cl_kernel kern = p_mkKern(prog, "rmt_eye_surface", &err);
- if (!kern || err != CL_SUCCESS) return;
- cl_kernel log_kern = p_mkKern(prog, "batch_line_tags", &err);
- g_cl_ctx = ctx;
- g_cl_queue = q;
- g_cl_eye_k = kern;
- g_cl_log_k = (err == CL_SUCCESS) ? log_kern : NULL;
- g_cl_info.available = 1;
- g_cl_info.zero_copy_llc = (unified == CL_TRUE) ? 1 : 0;
- g_cl_info.compute_units = cus;
- snprintf(g_cl_info.device_name, sizeof(g_cl_info.device_name), "%s", name_buf);
- }
- static int read_sysfs_u64(const char *path, uint64_t *out) {
- FILE *f = fopen(path, "r");
- if (!f) return 0;
- unsigned long long v = 0;
- int ok = (fscanf(f, "%llu", &v) == 1);
- fclose(f);
- if (ok) *out = (uint64_t)v;
- return ok;
- }
- /*
- * Guard against 15W U-series package power throttling:
- * If RAPL PL1 <= 16W and CPU load or frequency droop indicates package TDP pressure,
- * keep iGPU idle so CPU cores retain full Turbo clock for serial/protocol actors.
- */
- int stl_opencl_has_tdp_headroom(void) {
- uint64_t pl1_uw = 0;
- if (read_sysfs_u64("/sys/class/powercap/intel-rapl:0/constraint_0_power_limit_uw", &pl1_uw)) {
- if (pl1_uw > 0 && pl1_uw <= 16000000ULL) {
- /* 15W U-series SKU: check CPU loadavg and current scaling frequency */
- double load1 = 0.0;
- if (getloadavg(&load1, 1) == 1) {
- long ncores = sysconf(_SC_NPROCESSORS_ONLN);
- if (ncores > 0 && load1 > (double)ncores * 0.55) {
- return 0;
- }
- }
- }
- }
- uint64_t cur_khz = 0, max_khz = 0;
- if (read_sysfs_u64("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", &cur_khz) &&
- read_sysfs_u64("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", &max_khz)) {
- if (max_khz > 0 && cur_khz * 100ULL < max_khz * 50ULL) {
- /* Only if heavily throttled under active load */
- double load1 = 0.0;
- if (getloadavg(&load1, 1) == 1 && load1 > 1.5) return 0;
- }
- }
- return 1;
- }
- void stl_opencl_query_info(StlOpenClInfo *out_info) {
- pthread_once(&g_cl_once, init_opencl_once);
- if (!out_info) return;
- *out_info = g_cl_info;
- }
- size_t stl_batch_rescan_logs(
- const uint8_t *buf_4k_aligned,
- size_t len,
- uint32_t *out_line_offsets,
- uint8_t *out_line_tags,
- size_t max_lines,
- int *used_gpu) {
- pthread_once(&g_cl_once, init_opencl_once);
- if (used_gpu) *used_gpu = 0;
- if (!buf_4k_aligned || len == 0 || !out_line_offsets || !out_line_tags || max_lines == 0) {
- return 0;
- }
- /* Break-even & 15W TDP headroom gate: short logs or missing iGPU stay on pure SSE2 CPU */
- if (!g_cl_info.available || !g_cl_log_k || len < (size_t)g_cl_info.break_even_bytes ||
- ((uintptr_t)buf_4k_aligned & 4095u) != 0 || !stl_opencl_has_tdp_headroom()) {
- return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
- }
- size_t n_blocks = (len + 255u) >> 8;
- size_t in_bytes = (len + 4095u) & ~4095u;
- size_t flg_bytes = (n_blocks + 4095u) & ~4095u;
- void *flg_4k = NULL;
- if (posix_memalign(&flg_4k, 4096, flg_bytes) != 0) {
- return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
- }
- int gpu_ok = 0;
- pthread_mutex_lock(&g_cl_mutex);
- cl_int e1 = 0, e2 = 0;
- cl_mem m_in = p_clCreateBuffer(
- g_cl_ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, in_bytes, (void *)buf_4k_aligned, &e1);
- cl_mem m_flg = p_clCreateBuffer(
- g_cl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, flg_bytes, flg_4k, &e2);
- cl_uint u_len = (cl_uint)(len > 0xFFFFFFFFu ? 0xFFFFFFFFu : len);
- if (m_in && m_flg && e1 == CL_SUCCESS && e2 == CL_SUCCESS) {
- if (p_clSetKernelArg(g_cl_log_k, 0, sizeof(cl_mem), &m_in) == CL_SUCCESS &&
- p_clSetKernelArg(g_cl_log_k, 1, sizeof(cl_uint), &u_len) == CL_SUCCESS &&
- p_clSetKernelArg(g_cl_log_k, 2, sizeof(cl_mem), &m_flg) == CL_SUCCESS) {
- size_t gws[1] = { n_blocks };
- if (p_clEnqueueNDRangeKernel(g_cl_queue, g_cl_log_k, 1, NULL, gws, NULL, 0, NULL, NULL) == CL_SUCCESS) {
- cl_int map_err = 0;
- uint8_t *mapped = (uint8_t *)p_clEnqueueMapBuffer(
- g_cl_queue, m_flg, CL_TRUE, CL_MAP_READ, 0, n_blocks, 0, NULL, NULL, &map_err);
- if (mapped && map_err == CL_SUCCESS) {
- gpu_ok = 1;
- (void)p_clEnqueueUnmapMemObject(g_cl_queue, m_flg, mapped, 0, NULL, NULL);
- (void)p_clFinish(g_cl_queue);
- }
- }
- }
- }
- if (m_in) (void)p_clReleaseMemObject(m_in);
- if (m_flg) (void)p_clReleaseMemObject(m_flg);
- pthread_mutex_unlock(&g_cl_mutex);
- if (!gpu_ok) {
- free(flg_4k);
- return zig_batch_rescan_logs_cpu(buf_4k_aligned, len, out_line_offsets, out_line_tags, max_lines);
- }
- /* Prefix-compact 256B block flags from zero-copy iGPU pass into exact line offsets & tags */
- const uint8_t *bflags = (const uint8_t *)flg_4k;
- size_t line_count = 0, line_start = 0;
- uint8_t cur_tag = 0;
- int prev_cr = 0;
- for (size_t blk = 0; blk < n_blocks && line_count < max_lines; blk++) {
- uint8_t bf = bflags[blk];
- size_t base = blk << 8;
- size_t lim = base + 256u;
- if (lim > len) lim = len;
- if ((bf & 8u) == 0 && !prev_cr) {
- cur_tag |= (bf & 7u);
- continue;
- }
- for (size_t i = base; i < lim && line_count < max_lines; i++) {
- uint8_t c = buf_4k_aligned[i];
- if (c == '\n') {
- if (prev_cr) { prev_cr = 0; line_start = i + 1; continue; }
- if (i > line_start) {
- out_line_offsets[line_count] = (uint32_t)line_start;
- out_line_tags[line_count] = cur_tag;
- line_count++;
- }
- cur_tag = 0;
- line_start = i + 1;
- } else if (c == '\r') {
- if (i > line_start) {
- out_line_offsets[line_count] = (uint32_t)line_start;
- out_line_tags[line_count] = cur_tag;
- line_count++;
- }
- cur_tag = 0;
- prev_cr = 1;
- line_start = i + 1;
- } else {
- prev_cr = 0;
- if (c == '[') cur_tag |= 1u;
- else if (c == '>' || c == '#') cur_tag |= 2u;
- else if (c == 'E' || c == 'F' || c == 'W' || c == 'U') cur_tag |= 4u;
- }
- }
- }
- if (line_start < len && line_count < max_lines) {
- out_line_offsets[line_count] = (uint32_t)line_start;
- out_line_tags[line_count] = cur_tag;
- line_count++;
- }
- free(flg_4k);
- if (used_gpu) *used_gpu = 1;
- return line_count;
- }
- int stl_batch_rmt_eye_reduce(
- const float *margins_in,
- size_t station_count,
- float *eye_grid_out,
- float *summary_out,
- int *used_gpu) {
- pthread_once(&g_cl_once, init_opencl_once);
- if (used_gpu) *used_gpu = 0;
- if (!margins_in || station_count == 0 || !eye_grid_out || !summary_out) return -1;
- /* Break-even gate: require >= 4 stations AND available zero-copy iGPU AND 15W TDP headroom */
- if (!g_cl_info.available || station_count < 4 || !stl_opencl_has_tdp_headroom()) {
- zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
- return 0;
- }
- size_t total_ch = station_count * 24;
- size_t in_bytes = ((total_ch * 4 * sizeof(float)) + 4095u) & ~4095u;
- size_t out_bytes = ((total_ch * 256 * sizeof(float)) + 4095u) & ~4095u;
- void *in_4k = NULL;
- void *out_4k = NULL;
- if (posix_memalign(&in_4k, 4096, in_bytes) != 0 ||
- posix_memalign(&out_4k, 4096, out_bytes) != 0) {
- free(in_4k);
- free(out_4k);
- zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
- return 0;
- }
- memcpy(in_4k, margins_in, total_ch * 4 * sizeof(float));
- int gpu_ok = 0;
- pthread_mutex_lock(&g_cl_mutex);
- cl_int e1 = 0, e2 = 0;
- cl_mem m_in = p_clCreateBuffer(g_cl_ctx, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, in_bytes, in_4k, &e1);
- cl_mem m_out = p_clCreateBuffer(g_cl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, out_bytes, out_4k, &e2);
- if (m_in && m_out && e1 == CL_SUCCESS && e2 == CL_SUCCESS) {
- if (p_clSetKernelArg(g_cl_eye_k, 0, sizeof(cl_mem), &m_in) == CL_SUCCESS &&
- p_clSetKernelArg(g_cl_eye_k, 1, sizeof(cl_mem), &m_out) == CL_SUCCESS) {
- size_t gws[2] = { total_ch, 256 };
- if (p_clEnqueueNDRangeKernel(g_cl_queue, g_cl_eye_k, 2, NULL, gws, NULL, 0, NULL, NULL) == CL_SUCCESS) {
- cl_int map_err = 0;
- void *mapped = p_clEnqueueMapBuffer(
- g_cl_queue, m_out, CL_TRUE, CL_MAP_READ, 0, total_ch * 256 * sizeof(float),
- 0, NULL, NULL, &map_err);
- if (mapped && map_err == CL_SUCCESS) {
- memcpy(eye_grid_out, mapped, total_ch * 256 * sizeof(float));
- (void)p_clEnqueueUnmapMemObject(g_cl_queue, m_out, mapped, 0, NULL, NULL);
- (void)p_clFinish(g_cl_queue);
- gpu_ok = 1;
- }
- }
- }
- }
- if (m_in) (void)p_clReleaseMemObject(m_in);
- if (m_out) (void)p_clReleaseMemObject(m_out);
- pthread_mutex_unlock(&g_cl_mutex);
- free(in_4k);
- free(out_4k);
- if (!gpu_ok) {
- zig_rmt_eye_reduce_cpu(margins_in, station_count, eye_grid_out, summary_out);
- return 0;
- }
- /* Compute compact per-station scalar min/mean summary on CPU (96 floats) */
- for (size_t st = 0; st < station_count; st++) {
- float min_t = 999.0f, min_v = 999.0f, sum_t = 0.0f, sum_v = 0.0f, cnt = 0.0f;
- for (size_t ch = 0; ch < 24; ch++) {
- const float *m = &margins_in[(st * 24 + ch) * 4];
- float rx_lo = m[0] < 0 ? -m[0] : m[0];
- float rx_hi = m[1] < 0 ? -m[1] : m[1];
- float tx_lo = m[2] < 0 ? -m[2] : m[2];
- float tx_hi = m[3] < 0 ? -m[3] : m[3];
- float ts = rx_lo + rx_hi;
- float vs = tx_lo + tx_hi;
- if (ts > 0.0f || vs > 0.0f) {
- if (ts < min_t) min_t = ts;
- if (vs < min_v) min_v = vs;
- sum_t += ts;
- sum_v += vs;
- cnt += 1.0f;
- }
- }
- summary_out[st * 4 + 0] = cnt > 0.0f ? min_t : 0.0f;
- summary_out[st * 4 + 1] = cnt > 0.0f ? min_v : 0.0f;
- summary_out[st * 4 + 2] = cnt > 0.0f ? (sum_t / cnt) : 0.0f;
- summary_out[st * 4 + 3] = cnt > 0.0f ? (sum_v / cnt) : 0.0f;
- }
- if (used_gpu) *used_gpu = 1;
- return 0;
- }
|