Skip to content

execution/execution_context.hpp

Namespaces

Name
sgns
sgns::sgprocessing
Artifact and manifest binary serialization.

Classes

Name
struct sgns::sgprocessing::ProgressEvent
struct sgns::sgprocessing::CancellationToken
struct sgns::sgprocessing::ExecutionContext

Types

Name
enum class RenderStage
enum class MNNStage

Defines

Name
SGPROCMGR_EXECUTION_CONTEXT_HPP
Execution context data contracts.

Types Documentation

enum RenderStage

Enumerator Value Description
COMPILE 0 Shader compilation.
BUILD_PIPELINE 1 Pipeline creation.
DRAW 2 Draw submission.
READBACK 3 Readback from framebuffer.

Standardized pipeline stages for RenderProcessor (D-12). Matches the four coarse checkpoints in D-04 for RenderProcessor.

enum MNNStage

Enumerator Value Description
LOAD_MODEL 0 Model file loaded / MNN interpreter created.
CREATE_SESSION 1 MNN session created.
RUN 2 Inference executed.
READ_OUTPUT 3 Output tensor read.

Standardized pipeline stages for MNN processors (D-12). Matches the coarse checkpoints in D-04 for MNN processors.

Macros Documentation

define SGPROCMGR_EXECUTION_CONTEXT_HPP

#define SGPROCMGR_EXECUTION_CONTEXT_HPP 

Execution context data contracts.

Execution context types for Phase 07: Cancellable Execution Context.

Defines CancellationToken (callback-based cooperative cancellation), ExecutionContext (bundles cancel token, progress callback, deadline, budgets), ProgressEvent (stage-boundary progress event), and standardized stage enums per processor type (RenderStage, MNNStage).

Source code

#pragma once
#ifndef SGPROCMGR_EXECUTION_CONTEXT_HPP
#define SGPROCMGR_EXECUTION_CONTEXT_HPP

#include <atomic>
#include <cstdint>
#include <functional>
#include <string>
#include <vector>

namespace sgns::sgprocessing
{

    enum class RenderStage
    {
        COMPILE        = 0,  
        BUILD_PIPELINE = 1,  
        DRAW           = 2,  
        READBACK       = 3   
    };

    enum class MNNStage
    {
        LOAD_MODEL     = 0,  
        CREATE_SESSION = 1,  
        RUN            = 2,  
        READ_OUTPUT    = 3   
    };

    struct ProgressEvent
    {
        std::string pass_id;               
        RenderStage render_stage = RenderStage::COMPILE;  
        MNNStage    mnn_stage    = MNNStage::LOAD_MODEL;  
        float       percent      = 0.0f;   

        static ProgressEvent ForRender( std::string passId, RenderStage stage, float pct )
        {
            ProgressEvent ev;
            ev.pass_id      = std::move( passId );
            ev.render_stage = stage;
            ev.percent      = pct;
            return ev;
        }

        static ProgressEvent ForMNN( std::string passId, MNNStage stage, float pct )
        {
            ProgressEvent ev;
            ev.pass_id   = std::move( passId );
            ev.mnn_stage = stage;
            ev.percent   = pct;
            return ev;
        }
    };

    struct CancellationToken
    {
        CancellationToken() = default;
        CancellationToken( const CancellationToken & )            = delete;
        CancellationToken &operator=( const CancellationToken & ) = delete;
        CancellationToken( CancellationToken && )                 = delete;
        CancellationToken &operator=( CancellationToken && )      = delete;

        void Cancel()
        {
            bool expected = false;
            if ( m_cancelled.compare_exchange_strong( expected, true,
                  std::memory_order_release, std::memory_order_acquire ) )
            {
                if ( m_cancelCallback )
                {
                    m_cancelCallback();
                }
            }
        }

        bool IsCancelled() const
        {
            return m_cancelled.load( std::memory_order_acquire );
        }

        void SetCallback( std::function<void()> callback )
        {
            m_cancelCallback = std::move( callback );
        }

    private:
        std::function<void()> m_cancelCallback;
        std::atomic<bool>     m_cancelled{ false };
    };

    struct ExecutionContext
    {
        ExecutionContext() = default;
        ExecutionContext( const ExecutionContext & )            = delete;
        ExecutionContext &operator=( const ExecutionContext & ) = delete;
        ExecutionContext( ExecutionContext && )                 = delete;
        ExecutionContext &operator=( ExecutionContext && )      = delete;

        CancellationToken                     cancelToken;           
        std::function<void( ProgressEvent )>   progressCallback;     

        std::function<void( const std::vector<uint8_t> &quantizedBytes,
                             const std::vector<uint8_t> &preQuantizeBytes )> rawOutputCapture;

        uint64_t                              deadlineMs            = 0;  
        uint64_t                              gpuMemoryBudget       = 0;  
        uint64_t                              maxOutputArtifactBytes = 0; 

        static std::unique_ptr<ExecutionContext> NoOp()
        {
            auto ctx = std::make_unique<ExecutionContext>();
            ctx->cancelToken.SetCallback( []() {} );
            ctx->progressCallback = []( const ProgressEvent & ) {};
            // Deliberately left unset (nullptr), unlike progressCallback: rawOutputCapture
            // is opt-in only, so production and test callers that never set it explicitly
            // pay zero capture-path cost. Do not "fix" this to match progressCallback.
            return ctx;
        }
    };

} // namespace sgns::sgprocessing

#endif // SGPROCMGR_EXECUTION_CONTEXT_HPP

Updated on 2026-09-17 at 06:29:15 +0000