package com.elphel.imagej.gpu.jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

/**
 * Stage-2 validation: the convert_direct milestone. Drive the full direct-CLT path natively
 * (NVRTC module + driver-API launches, no JCuda) through the JNA boundary, and compare the
 * computed CLT to the tile_processor_gpu golden (clt/aux_chnN.clt). This is the first real
 * kernel-execution test and the first CDP (calculate_tiles_offsets) run on Blackwell via the shim.
 *
 * Run:
 *   java -Djna.library.path=&lt;dir with libtileproc.so&gt; -cp target/classes:&lt;jna.jar&gt; \
 *        com.elphel.imagej.gpu.jna.Stage2 [data_root] [lwir]
 *   data_root must contain clt/ (default /home/elphel/git/tile_processor_gpu); lwir default 1.
 * By Claude on 2026-06-25.
 */
public class Stage2 {
    public static void main(String[] args) {
        String root = (args.length > 0) ? args[0] : "/home/elphel/git/tile_processor_gpu";
        int    lwir = (args.length > 1) ? Integer.parseInt(args[1]) : 1;
        String srcdir = "/home/elphel/git/tile_processor_gpu/src";
        String devrt  = "/usr/local/cuda/targets/x86_64-linux/lib/libcudadevrt.a";

        System.out.printf("Stage2 (convert_direct via JNA): root=%s lwir=%d%n", root, lwir);
        TpJna lib = Native.load("tileproc", TpJna.class);
        Pointer m = lib.tp_create_module(srcdir, devrt);
        if (m == null) { System.out.println("FAIL: tp_create_module:\n" + lib.tp_last_error()); System.exit(1); }

        double[] maxErr = new double[1];
        int[]    nActive = new int[1];
        int rc = lib.tp_convert_direct_selftest(m, lwir, root, maxErr, nActive);
        if (rc != 0) { System.out.printf("FAIL: selftest rc=%d: %s%n", rc, lib.tp_last_error()); System.exit(2); }

        // CDP can reorder FP accumulation, and NVRTC vs the harness's offline nvcc may differ slightly,
        // so this is a closeness check, not bitwise. Report the number; judge from it.
        boolean ok = (maxErr[0] >= 0) && !Double.isNaN(maxErr[0]) && (maxErr[0] < 1.0) && (nActive[0] > 0);
        System.out.printf("  convert_direct: num_active_tiles=%d  max|CLT-golden|=%.6g  %s%n",
                nActive[0], maxErr[0], ok ? "PASS" : "REVIEW");
        lib.tp_destroy_module(m);
        System.out.println(ok ? "RESULT: PASS (convert_direct matches golden via JNA)" : "RESULT: REVIEW (see maxErr)");
        System.exit(ok ? 0 : 3);
    }
}
