runmat 0.4.5

High-performance MATLAB/Octave syntax mathematical runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use clap::parser::ValueSource;
use clap::{ArgMatches, Parser, Subcommand};
use runmat_config::{PlotBackend, PlotMode};
use runmat_server_client::auth::CredentialStoreMode;
use std::path::PathBuf;
use uuid::Uuid;

use crate::cli::parse::{parse_bool_env, parse_figure_size, parse_log_level_env};
use crate::cli::remote::{FsCommand, OrgCommand, ProjectCommand, RemoteCommand};
use crate::cli::value_types::{
    CaptureFiguresMode, CompressionAlg, FigureSize, GcPreset, LogLevel, OptLevel,
};

#[derive(Parser, Clone)]
#[command(
    name = "runmat",
    version = env!("CARGO_PKG_VERSION"),
    about = "High-performance MATLAB/Octave code runtime",
    long_about = r#"
RunMat is a modern, high-performance runtime for MATLAB/Octave.

It is built in Rust, and features a V8-inspired tiered execution model with a 
baseline interpreter feeding an optimizing JIT compiler built on Cranelift.

Key features:
• JIT compilation with Cranelift for optimal performance
• Generational garbage collection with configurable policies
• High-performance BLAS/LAPACK operations
• Jupyter kernel protocol support with async execution
• Fast startup with snapshotting capabilities
• World-class error messages and debugging
• Compatible with MATLAB/Octave syntax and semantics

Performance Features:
• Multi-tier execution: interpreter + JIT compiler
• Adaptive optimization based on hotspot profiling
• Generational GC with write barriers and concurrent collection
• SIMD-optimized mathematical operations
• Zero-copy memory management where possible

Examples:
  runmat                                   # Start interactive REPL with JIT
  runmat --no-jit                          # Start REPL with interpreter only
  runmat --gc-preset low-latency           # Optimize GC for low latency
  runmat script.m                          # Execute MATLAB/Octave script
  runmat --emit-bytecode script.m           # Emit bytecode disassembly
  runmat --install-kernel                  # Install as Jupyter kernel
  runmat kernel                            # Start Jupyter kernel
  runmat kernel-connection connection.json # Start with connection file
  runmat version --detailed                # Show detailed version information
"#,
    after_help = r#"
Environment Variables:
  RUNMAT_DEBUG=1              Enable debug logging
  RUNMAT_LOG_LEVEL=debug      Set log level (error, warn, info, debug, trace)
  RUNMAT_KERNEL_IP=127.0.0.1  Kernel IP address  
  RUNMAT_KERNEL_KEY=<key>     Kernel authentication key
  RUNMAT_TIMEOUT=300          Execution timeout in seconds
  RUNMAT_CALLSTACK_LIMIT=200  Maximum call stack frames to record
  RUNMAT_ERROR_NAMESPACE=RunMat Error identifier namespace prefix override
  RUNMAT_CONFIG=<path>        Path to configuration file
  RUNMAT_SNAPSHOT_PATH=<path> Snapshot file to preload standard library
  
  Garbage Collector:
  RUNMAT_GC_PRESET=<preset>   GC preset (low-latency, high-throughput, low-memory, debug)
  RUNMAT_GC_YOUNG_SIZE=<mb>   Young generation size in MB
  RUNMAT_GC_THREADS=<n>       Number of GC threads
  
  JIT Compiler:
  RUNMAT_JIT_ENABLE=1         Enable JIT compilation (default: true)
  RUNMAT_JIT_THRESHOLD=<n>    JIT compilation threshold (default: 10)
  RUNMAT_JIT_OPT_LEVEL=<level> JIT optimization level (none, size, speed, aggressive; default: speed)

For more information, visit: https://github.com/runmat-org/runmat
"#
)]
#[command(propagate_version = true)]
pub struct Cli {
    /// Enable debug logging
    #[arg(short, long, env = "RUNMAT_DEBUG", value_parser = parse_bool_env)]
    pub debug: bool,

    /// Set log level
    #[arg(long, value_enum, env = "RUNMAT_LOG_LEVEL", default_value = "warn", value_parser = parse_log_level_env)]
    pub log_level: LogLevel,

    /// Execution timeout in seconds
    #[arg(long, env = "RUNMAT_TIMEOUT", default_value = "300")]
    pub timeout: u64,

    /// Maximum number of call stack frames to record
    #[arg(long, env = "RUNMAT_CALLSTACK_LIMIT", default_value = "200")]
    pub callstack_limit: usize,

    /// Emit bytecode disassembly for a script (stdout if omitted path)
    #[arg(long, value_name = "PATH", num_args = 0..=1, default_missing_value = "-")]
    pub emit_bytecode: Option<PathBuf>,

    /// Error identifier namespace prefix
    #[arg(long, env = "RUNMAT_ERROR_NAMESPACE")]
    pub error_namespace: Option<String>,

    /// Configuration file path
    #[arg(long, env = "RUNMAT_CONFIG")]
    pub config: Option<PathBuf>,

    /// Disable JIT compilation (use interpreter only)
    #[arg(long, env = "RUNMAT_JIT_DISABLE", value_parser = parse_bool_env)]
    pub no_jit: bool,

    /// JIT compilation threshold (number of executions before JIT)
    #[arg(long, env = "RUNMAT_JIT_THRESHOLD", default_value = "10")]
    pub jit_threshold: u32,

    /// JIT optimization level (none, size, speed, aggressive)
    #[arg(
        long,
        value_enum,
        env = "RUNMAT_JIT_OPT_LEVEL",
        default_value = "speed"
    )]
    pub jit_opt_level: OptLevel,

    /// GC configuration preset
    #[arg(long, value_enum, env = "RUNMAT_GC_PRESET")]
    pub gc_preset: Option<GcPreset>,

    /// Young generation size in MB
    #[arg(long, env = "RUNMAT_GC_YOUNG_SIZE")]
    pub gc_young_size: Option<usize>,

    /// Maximum number of GC threads
    #[arg(long, env = "RUNMAT_GC_THREADS")]
    pub gc_threads: Option<usize>,

    /// Enable GC statistics collection
    #[arg(long, env = "RUNMAT_GC_STATS", value_parser = parse_bool_env)]
    pub gc_stats: bool,

    /// Verbose output for REPL and execution
    #[arg(short, long)]
    pub verbose: bool,

    /// Snapshot file to preload standard library
    #[arg(long, env = "RUNMAT_SNAPSHOT_PATH")]
    pub snapshot: Option<PathBuf>,

    /// Plotting mode
    #[arg(long, value_enum, env = "RUNMAT_PLOT_MODE")]
    pub plot_mode: Option<PlotMode>,

    /// Force headless plotting mode
    #[arg(long, env = "RUNMAT_PLOT_HEADLESS", value_parser = parse_bool_env)]
    pub plot_headless: bool,

    /// Plotting backend
    #[arg(long, value_enum, env = "RUNMAT_PLOT_BACKEND")]
    pub plot_backend: Option<PlotBackend>,

    /// Override scatter target points for GPU decimation
    #[arg(long)]
    pub plot_scatter_target: Option<u32>,

    /// Override surface vertex budget for GPU LOD
    #[arg(long)]
    pub plot_surface_vertex_budget: Option<u64>,

    /// Directory where run artifacts are written
    #[arg(long, env = "RUNMAT_ARTIFACTS_DIR")]
    pub artifacts_dir: Option<PathBuf>,

    /// Path to write artifact manifest JSON
    #[arg(long, env = "RUNMAT_ARTIFACTS_MANIFEST")]
    pub artifacts_manifest: Option<PathBuf>,

    /// Figure capture mode when artifact output is enabled
    #[arg(
        long,
        value_enum,
        env = "RUNMAT_CAPTURE_FIGURES",
        default_value = "auto"
    )]
    pub capture_figures: CaptureFiguresMode,

    /// Figure export size (WIDTHxHEIGHT)
    #[arg(long, env = "RUNMAT_FIGURE_SIZE", default_value = "1280x720", value_parser = parse_figure_size)]
    pub figure_size: FigureSize,

    /// Maximum number of figures to export
    #[arg(long, env = "RUNMAT_MAX_FIGURES", default_value = "8")]
    pub max_figures: usize,

    /// Generate sample configuration file
    #[arg(long)]
    pub generate_config: bool,

    /// Install RunMat as a Jupyter kernel
    #[arg(long)]
    pub install_kernel: bool,

    /// Command to execute
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// MATLAB script file to execute (alternative to subcommands)
    pub script: Option<PathBuf>,
}

#[derive(Clone, Debug, Default)]
pub struct CliOverrideSources {
    pub debug: bool,
    pub log_level: bool,
    pub timeout: bool,
    pub callstack_limit: bool,
    pub jit_threshold: bool,
    pub jit_opt_level: bool,
    pub gc_stats: bool,
    pub verbose: bool,
}

impl CliOverrideSources {
    pub fn from_matches(matches: &ArgMatches) -> Self {
        Self {
            debug: Self::was_provided(matches, "debug"),
            log_level: Self::was_provided(matches, "log_level"),
            timeout: Self::was_provided(matches, "timeout"),
            callstack_limit: Self::was_provided(matches, "callstack_limit"),
            jit_threshold: Self::was_provided(matches, "jit_threshold"),
            jit_opt_level: Self::was_provided(matches, "jit_opt_level"),
            gc_stats: Self::was_provided(matches, "gc_stats"),
            verbose: Self::was_provided(matches, "verbose"),
        }
    }

    fn was_provided(matches: &ArgMatches, id: &str) -> bool {
        matches
            .value_source(id)
            .is_some_and(|source| source != ValueSource::DefaultValue)
    }
}

#[derive(Subcommand, Clone)]
pub enum Commands {
    /// Start interactive REPL
    Repl {
        /// Enable verbose output
        #[arg(short, long)]
        verbose: bool,
    },
    /// Start Jupyter kernel
    Kernel {
        /// Kernel IP address
        #[arg(long, env = "RUNMAT_KERNEL_IP", default_value = "127.0.0.1")]
        ip: String,
        /// Kernel authentication key
        #[arg(long, env = "RUNMAT_KERNEL_KEY")]
        key: Option<String>,
        /// Transport protocol
        #[arg(long, default_value = "tcp")]
        transport: String,
        /// Signature scheme
        #[arg(long, default_value = "hmac-sha256")]
        signature_scheme: String,
        /// Shell socket port (0 for auto-assign)
        #[arg(long, env = "RUNMAT_SHELL_PORT", default_value = "0")]
        shell_port: u16,
        /// IOPub socket port (0 for auto-assign)
        #[arg(long, env = "RUNMAT_IOPUB_PORT", default_value = "0")]
        iopub_port: u16,
        /// Stdin socket port (0 for auto-assign)
        #[arg(long, env = "RUNMAT_STDIN_PORT", default_value = "0")]
        stdin_port: u16,
        /// Control socket port (0 for auto-assign)
        #[arg(long, env = "RUNMAT_CONTROL_PORT", default_value = "0")]
        control_port: u16,
        /// Heartbeat socket port (0 for auto-assign)
        #[arg(long, env = "RUNMAT_HB_PORT", default_value = "0")]
        hb_port: u16,
        /// Write connection file to path
        #[arg(long)]
        connection_file: Option<PathBuf>,
    },
    /// Start kernel with connection file
    KernelConnection {
        /// Path to Jupyter connection file
        connection_file: PathBuf,
    },
    /// Execute MATLAB script file
    Run {
        /// Script file to execute
        file: PathBuf,
        /// Arguments to pass to script
        #[arg(last = true)]
        args: Vec<String>,
    },
    /// Show version information
    Version {
        /// Show detailed version information
        #[arg(long)]
        detailed: bool,
    },
    /// Show system information
    Info,
    /// Show acceleration provider information
    AccelInfo {
        /// Output provider information and telemetry as JSON
        #[arg(long)]
        json: bool,
        /// Reset provider telemetry counters after printing
        #[arg(long)]
        reset: bool,
    },
    /// Apply auto-offload calibration from suite telemetry results
    #[cfg(feature = "wgpu")]
    AccelCalibrate {
        /// Path to suite results JSON produced by the benchmark harness
        input: PathBuf,
        /// Preview updates without persisting the calibration cache
        #[arg(long)]
        dry_run: bool,
        /// Emit calibration outcome as JSON
        #[arg(long)]
        json: bool,
    },
    /// Garbage collection utilities
    Gc {
        #[command(subcommand)]
        gc_command: GcCommand,
    },
    /// Performance benchmarking
    Benchmark {
        /// Script file to benchmark
        file: PathBuf,
        /// Number of iterations
        #[arg(short, long, default_value = "10")]
        iterations: u32,
        /// Enable JIT for benchmark
        #[arg(long)]
        jit: bool,
    },
    /// Snapshot management
    Snapshot {
        #[command(subcommand)]
        snapshot_command: SnapshotCommand,
    },
    /// Configuration management
    Config {
        #[command(subcommand)]
        config_command: ConfigCommand,
    },
    /// Run against remote filesystem
    Remote {
        #[command(subcommand)]
        remote_command: RemoteCommand,
    },
    /// Authenticate with RunMat server
    Login {
        /// Server URL
        #[arg(long)]
        server: Option<String>,
        /// API key or access token
        #[arg(long)]
        api_key: Option<String>,
        /// Email for interactive login
        #[arg(long)]
        email: Option<String>,
        /// Credential storage mode: auto, secure, file, memory
        #[arg(long, default_value = "file")]
        credential_store: CredentialStoreMode,
        /// Default organization id
        #[arg(long)]
        org: Option<Uuid>,
        /// Default project id
        #[arg(long)]
        project: Option<Uuid>,
    },
    /// Organization management
    Org {
        #[command(subcommand)]
        org_command: OrgCommand,
    },
    /// Project management
    Project {
        #[command(subcommand)]
        project_command: ProjectCommand,
    },
    /// Remote filesystem commands
    Fs {
        #[command(subcommand)]
        fs_command: FsCommand,
    },
}

#[derive(Subcommand, Clone)]
pub enum GcCommand {
    /// Show GC statistics
    Stats,
    /// Force minor collection
    Minor,
    /// Force major collection
    Major,
    /// Show current configuration
    Config,
    /// Test GC under stress
    Stress {
        /// Number of allocations
        #[arg(short, long, default_value = "10000")]
        allocations: usize,
    },
}

#[derive(Subcommand, Clone)]
pub enum SnapshotCommand {
    /// Create a new snapshot
    Create {
        /// Output snapshot file
        #[arg(short, long)]
        output: PathBuf,
        /// Optimization level
        #[arg(short = 'O', long, value_enum, default_value = "speed")]
        optimization: OptLevel,
        /// Compression algorithm
        #[arg(short, long, value_enum)]
        compression: Option<CompressionAlg>,
    },
    /// Load and inspect a snapshot
    Info {
        /// Snapshot file to inspect
        snapshot: PathBuf,
    },
    /// List available presets
    Presets,
    /// Validate a snapshot file
    Validate {
        /// Snapshot file to validate
        snapshot: PathBuf,
    },
}

#[derive(Subcommand, Clone)]
pub enum ConfigCommand {
    /// Show current configuration
    Show,
    /// Generate sample configuration file
    Generate {
        /// Output file path
        #[arg(short, long, default_value = ".runmat.yaml")]
        output: PathBuf,
    },
    /// Validate configuration file
    Validate {
        /// Config file to validate
        config_file: PathBuf,
    },
    /// Show configuration file locations
    Paths,
}