Commit 1bf9e9e4 authored by Andrey Filippov's avatar Andrey Filippov

CLAUDE: Add RT detector skeleton (com.elphel.imagej.cuas.rt)

Pure-Java prototype CuasRTDetector + RtParams parameters class +
CuasRTLib JNA stub (activate when JNA added to pom.xml).
No dependencies added; build is clean. See design discussion in
attic/imagej-elphel-internal/handoffs/2026-05-21_RealTimeDesign.md.
Co-authored-by: 's avatarClaude <claude@elphel.com>
parent 9dacd956
This diff is collapsed.
/**
** CuasRTLib.java
**
** Copyright (C) 2026 Elphel, Inc.
**
** -----------------------------------------------------------------------------**
**
** CuasRTLib.java is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
** -----------------------------------------------------------------------------**
*/
package com.elphel.imagej.cuas.rt;
/**
* JNA binding to libcuasrt.so — the native CUDA real-time detector library.
*
* STATUS: stub — activate when JNA is added to pom.xml.
*
* -------------------------------------------------------------------------
* TO ACTIVATE: add to pom.xml dependencies:
*
* <dependency>
* <groupId>net.java.dev.jna</groupId>
* <artifactId>jna</artifactId>
* <version>5.14.0</version>
* </dependency>
*
* Then replace this class with the active interface (sketch below).
* -------------------------------------------------------------------------
*
* ACTIVE INTERFACE SKETCH (not compiled — JNA not yet on classpath):
*
* import com.sun.jna.Library;
* import com.sun.jna.Native;
* import com.sun.jna.Pointer;
* import com.sun.jna.Structure;
* import java.util.Arrays;
* import java.util.List;
*
* // RtParamsJna: JNA Structure mapping to C RtParams in rt_lib.h
* public static class RtParamsJna extends Structure {
* public int max_vel;
* public int width;
* public int height;
* public float mem_coeff;
* public float obs_coeff;
* public float leaky_slope;
* public float peak_thresh;
*
* @Override protected List<String> getFieldOrder() {
* return Arrays.asList("max_vel","width","height",
* "mem_coeff","obs_coeff","leaky_slope","peak_thresh");
* }
* }
*
* public interface CuasRTLib extends Library {
* CuasRTLib INSTANCE = Native.load("cuasrt", CuasRTLib.class);
*
* // Create GPU context; returns opaque handle.
* Pointer cuasrt_create(int width, int height, RtParamsJna p);
*
* // Destroy GPU context and free all GPU memory.
* void cuasrt_destroy(Pointer ctx);
*
* // Layer 1 matched filter.
* // fpixels: [nframes * width * height], row-major, host memory.
* // layer1Out: [n_chan * width * height] output, n_chan = (2*max_vel+1)^2.
* void cuasrt_layer1(Pointer ctx, float[] fpixels, int nframes, float[] layer1Out);
*
* // Update accumulation buffer; returns number of peaks found.
* // layer1Out: [n_chan * width * height] from cuasrt_layer1.
* // accumBuf: [n_chan * width * height] persistent state (in/out).
* // peaksXy: output [(x,y) pairs], length >= maxPeaks*2.
* int cuasrt_update_accum(Pointer ctx,
* float[] layer1Out, float[] accumBuf,
* RtParamsJna p, float[] peaksXy, int maxPeaks);
* }
*
* -------------------------------------------------------------------------
* LIBRARY PATH:
* Set -Djna.library.path=/path/to/cuas_rt_gpu/build at JVM startup,
* or place libcuasrt.so on LD_LIBRARY_PATH.
* libcudart.so must also be accessible (ships with CUDA toolkit).
*
* CUDA CONTEXT:
* libcuasrt.so creates its own CUDA context (device 0).
* It runs independently of the JCuda tile-processor context.
* When both pipelines run together, context sharing via cudaSetDevice
* (CUDA 12 primary context) can be added without API changes.
* -------------------------------------------------------------------------
*/
public final class CuasRTLib {
// Placeholder — replace with active JNA interface once JNA is in pom.xml.
private CuasRTLib() {}
}
/**
** RtParams.java
**
** Copyright (C) 2026 Elphel, Inc.
**
** -----------------------------------------------------------------------------**
**
** RtParams.java is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
** -----------------------------------------------------------------------------**
*/
package com.elphel.imagej.cuas.rt;
/**
* Runtime parameters for the real-time CUAS target detector.
*
* Mirrors the RtParams C struct in cuas_rt_gpu/src/rt_lib.h.
* When JNA is added to pom.xml this class will gain a JNA Structure counterpart
* (RtParamsJna extends com.sun.jna.Structure) for direct memory mapping.
*
* Velocity channels:
* X and Y each span [-maxVel .. +maxVel] pixels/sample.
* Total channels: nChan() = (2*maxVel+1)^2 = 25 for maxVel=2.
* Channel index: chanIdx(ivx,ivy) = (ivy+maxVel)*nVel() + (ivx+maxVel)
* Units are pixels per input sample; input is already temporally
* averaged/decimated for the specific pyramid level, so the same
* maxVel applies at every pyramid level.
*/
public class RtParams {
/** Velocity range per axis [-maxVel .. +maxVel] px/sample. Start with 2; try 1 and 3. */
public int maxVel = 2;
/** Accumulation buffer memory coefficient (high persistence). */
public float memCoeff = 0.9f;
/** New observation weight (typically 1 - memCoeff). */
public float obsCoeff = 0.1f;
/**
* Leaky ReLU negative slope inside accumulation loop.
* Controls competition vs. pure linear accumulation:
* slope=1.0 → linear (maximum SNR improvement, no competition)
* slope→0 → hard ReLU (strong competition, earlier saturation)
* Tune experimentally; start near 1.0 and reduce toward 0.01.
*/
public float leakySlope = 0.1f;
/** Accumulation buffer peak detection threshold for Stage 2 trigger. */
public float peakThresh = 0.5f;
public RtParams() {}
public RtParams(int maxVel, float memCoeff, float obsCoeff,
float leakySlope, float peakThresh) {
this.maxVel = maxVel;
this.memCoeff = memCoeff;
this.obsCoeff = obsCoeff;
this.leakySlope = leakySlope;
this.peakThresh = peakThresh;
}
/** Number of velocity channels per axis: 2*maxVel+1. */
public int nVel() { return 2 * maxVel + 1; }
/** Total 2D velocity channels: nVel()^2. */
public int nChan() { int n = nVel(); return n * n; }
/**
* Flat channel index for velocity (ivx, ivy), both in [-maxVel..+maxVel].
* ivy is the major index (row), ivx is the minor index (column).
*/
public int chanIdx(int ivx, int ivy) {
return (ivy + maxVel) * nVel() + (ivx + maxVel);
}
/** Recover ivx from channel index. */
public int ivxFromChan(int ich) { return (ich % nVel()) - maxVel; }
/** Recover ivy from channel index. */
public int ivyFromChan(int ich) { return (ich / nVel()) - maxVel; }
@Override
public String toString() {
return String.format(
"RtParams{maxVel=%d, nChan=%d, mem=%.3f, obs=%.3f, leaky=%.3f, thresh=%.3f}",
maxVel, nChan(), memCoeff, obsCoeff, leakySlope, peakThresh);
}
}
/**
* Real-time CUAS target detector — skeleton package.
*
* Design discussion: attic/imagej-elphel-internal/handoffs/2026-05-21_RealTimeDesign.md
*
* Contents:
* RtParams — runtime parameters (mirrors RtParams C struct in libcuasrt.so)
* CuasRTDetector — pure-Java prototype; reference implementation for tuning
* CuasRTLib — JNA binding stub (activate when JNA added to pom.xml)
*
* Implementation order (next week):
* 1. Tune CuasRTDetector against real data at a single pyramid level
* 2. Port to C++/CUDA in cuas_rt_gpu, validate numerically against Java
* 3. Wire CuasRTLib JNA binding, run Java → libcuasrt.so end-to-end
* 4. Extend to full temporal pyramid
*/
package com.elphel.imagej.cuas.rt;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment