Skip to content

New-pipeline: runQ #571

Open
Open

Description

@ivogabe

In the new-pipeline, runQ should be re-implemented. This variant of runN allows compilation of Accelerate programs to happen at Haskell compile-time using Template Haskell, instead of Haskell run-time. It is implemented by converting (one of) the last AST(s) to Template Haskell expression.

For the new-pipeline, this will probably be the Linked language. However, we may want to split Linking in two phases, where phase 1 compiles the intra-kernel part of the program, and phase 2 actually loads the functions in memory using the system linker.

Activity

  1. added this to the new-pipeline MVP milestone on Sep 8, 2026
  2. TravisCardwell commented on Sep 30, 2026

    @TravisCardwell

    I have looked into this a bit and discussed with @ivogabe . Here are my notes. If I have made any mistakes, please let me know.

    Context

    With the other run functions, Accelerate programs are compiled at runtime. With runQ, Accelerate programs are instead compiled at compile-time, using Template Haskell. The runQ functions are not implemented in new-pipeline yet. The implementation from master is commented out in both the Native and PTX modules.

    In master, Accelerate programs are implemented using monolithic programs. In new-pipeline, each Accelerate program is implemented using separate kernels that are composed together using intra-kernel "glue code." Currently, this "gluing" is done together with linking, but this needs to be split into separate phases in order to implement runQ. Compilation of kernels and intra-kernel "glue code" should be done using Template Haskell, invoking LLVM. Linking should be done separately, at Haskell runtime.

    Execute

    Type class Execute defines the interface for executing Accelerate programs, implemented by each backend (currently Interpreter, Native, and PTX). It currently has a single Linked data family and a few methods. We need to split this into two steps, using Linked and Glued data families. The methods must be refactored, likely adding a liftGlued method. Here is a draft of the new design:

    class Execute sched kernel where
      data Glued  sched kernel :: Type -> Type  -- compile-time (liftable)
      data Linked sched kernel :: Type -> Type  -- runtime
    
      glueAfunSchedule :: sched kernel () t -> Glued sched kernel t
    
      liftGlued :: Glued sched kernel t -> CodeQ (Glued sched kernel t)
    
      linkGlued :: Glued sched kernel t -> Linked sched kernel t
    
      executeLinked
        :: GFunctionR t
        -> Linked sched kernel (Scheduled sched t)
        -> IOFun (Scheduled sched t)
    
    linkAfunSchedule
      :: Execute sched kernel
      => sched kernel () t
      -> Linked sched kernel t
    linkAfunSchedule = linkGlued . glueAfunSchedule

    With this design, we need to lift many more types, particularly UniformSchedule.

    Native

    The native (CPU) backend has a "gluing and linking" implementation. In the Execute instance in Native.Execute, method linkAfunSchedule is implemented using function linkSchedule. This function generates the intra-kernel "glue code" in function codegenSchedule, compiles it (see Native.Compile), and links it (see Native.Link). Kernels are compiled and linked by function compileKernel during schedule conversion, and they are referenced using function pointers (not symbols) in an imports data structure.

    We currently build shared object files (in a user cache directory) and use dlopen to load shared object files by path. The interface is a single "entry point" function. We lookup that symbol and use the FunPtr directly. We also create a Lifetime for the shared object with a finalizer that calls dlclose. We would like to change this to embed the shared objects instead, like is done in master. Module Native.Embed implements object embedding, and function embed returns FFI declarations for getting the FunPtrs.

    A GHC plugin implemented in Native.Plugin (and submodules) is used to help link everything, but I do not understand the details. It should still be working. We should look into this and see why/if it is needed. Perhaps there is a better way to do the same thing using TH?

    It is recommended to get the native backend working before working on the PTX backend. This likely involves the following:

    • Refactor NativeKernel and split compileKernel
    • Split linkSchedule
    • Implement Glued type
      • Most lifting work should be done in accelerate, as it is not specific to the native backend.
    • Implement runQ
      • We should implement a new runQAt function in Data.Array.Accelerate.Backend and implement runQ using it.
    • Re-enable (or replace?) the plugin
    • Re-enable the test

    PTX

    The PTX (NVIDIA GPU) backend does not compile any "glue code." It interprets the schedule. With the lifting already done to get the native backend working, it should be easy. This likely involves the following:

    • Refactore the Execute instance to work with the new interface
    • Implement runQ (using runQAt)

    Interpreter

    The interpreter, used as a simple reference implementation, is not in a working state. It was used for experimentation and needs to be cleaned up, so there is no need to get it working as part of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions