Home /Modules/cpu/SLGPUDevice.sleela

SLGPUDevice.sleela

lib/cpu/SLGPUDevice.sleela · 145 line(s) · sleela

<< lib/cpu/SLGPUDevice.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGPUDevice โ€” a graphics/compute processor a Sleela
     executor can offload work to when the GPU option is enabled. It models the
     real spec that matters for compute: a number of compute units each running
     many lanes (SIMT), a core clock, and a block of VRAM (its own SLRAM). A
     kernel is a canonical-opcode program dispatched across a grid of lanes; the
     device executes it against VRAM. A native-access path (nativeDispatch)
     crosses the explicit bridge to a real host GPU (CUDA/OpenCL/Metal/Vulkan)
     when available, mirroring the native engine's bridge discipline.
   
   This is the "native GPU access" option: a program that needs the GPU โ€” SIMD
   math, large parallel maps โ€” asks the device to run a kernel, rather than
   grinding it on the scalar CPU.
   
   Family: cpu. Composes SLRAM (VRAM) and SLSleelaVM (per-lane kernel engine).
   Enabled via the GPU option on SLCPURuntime / SLVM. >>
#sleela 2.5
material;
class SLGPUDevice {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10502;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-09";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  < e.g. "SLeeLa Compute GPU" >
  String model;
  < number of parallel compute units >
  int computeUnits;
  < SIMT lanes per unit (warp/wavefront width) >
  int lanesPerUnit;
  < core clock >
  int clockMHz;
  < VRAM capacity >
  int vramMiB;

  < 1 when the GPU option is on >
  int enabled;
  < 1 if a real host GPU is reachable over the bridge >
  int nativeAvailable;

  < device memory >
  SLRAM vram;
  < executes a kernel's canonical opcodes (per-lane model) >
  SLSleelaVM kernelVM;
  SLSleelaOpcode op;

  int kernelsRun;
  < observable output of the last kernel >
  int lastResult;

  void configure(String name, int units, int lanes, int core, int vramMegabytes) {
    model = name; computeUnits = units; lanesPerUnit = lanes;
    clockMHz = core; vramMiB = vramMegabytes;
    enabled = 0; nativeAvailable = 0; kernelsRun = 0; lastResult = 0;
  }

  void enable()  {
    enabled = 1;
    < 256K words/MiB >
    vram = new SLRAM(); vram.configure(vramMiB * 262144, 32); vram.allocate();
    kernelVM = new SLSleelaVM(); kernelVM.configure(1 << 16, 4096, 1024);
    op = new SLSleelaOpcode(); op.configure(0, "OP_NOP");
    probeNative();
  }
  void disable() { enabled = 0; }
  boolean isEnabled() { return enabled == 1; }

  < Total parallel lanes across the device. >
  int totalLanes() { return computeUnits * lanesPerUnit; }

  < Ask the host whether a real GPU is present (bridge). Falls back to the >
  < modelled SIMT engine when none is available, so GPU code always runs. >
  void probeNative() { nativeAvailable = native sleela_gpu_available(); }
  boolean nativeAccess() { return nativeAvailable == 1; }

  < --- VRAM access (host<->device copies are typically via DMA) --- >
  void writeVram(int addr, int value) { if (vram != null && addr >= 0 && addr < vram.capacityWords()) { vram.writeWord(addr, value); } }
  int readVram(int addr)              { if (vram == null || addr < 0 || addr >= vram.capacityWords()) { return 0; } return vram.readWord(addr); }
  SLRAM deviceMemory() { return vram; }

  < Build a kernel into the device's kernel engine (canonical opcodes). >
  int emitKernel(int opcode, int arg) { return kernelVM.emit(opcode, arg); }

  < Dispatch the built kernel across `gridLanes` lanes. In the modelled path the >
  < kernel engine runs the opcode stream (per-lane state would be SIMT-expanded >
  < by the native path); the native path offloads to the host GPU. >
  int dispatch(int gridLanes, int budget) {
    if (enabled == 0) { return -1; }
    if (nativeAvailable == 1) {
      lastResult = native sleela_gpu_dispatch(gridLanes, budget);
    } else {
      kernelVM.run(budget);
      lastResult = kernelVM.printed();
    }
    kernelsRun = kernelsRun + 1;
    return lastResult;
  }

  SLSleelaVM kernelEngine() { return kernelVM; }
  int kernelCount()  { return kernelsRun; }
  int result()       { return lastResult; }
  String name()      { return model; }
  int units()        { return computeUnits; }
  int lanes()        { return lanesPerUnit; }
  int clock()        { return clockMHz; }
  int vramCapacity() { return vramMiB; }
}