Table of Contents

Class FenceLocation

Namespace
DotCompute.Abstractions.Memory
Assembly
DotCompute.Abstractions.dll

Specifies where memory fences should be inserted in kernel code.

public sealed class FenceLocation
Inheritance
FenceLocation
Inherited Members

Remarks

Fence location controls precise placement of memory ordering operations. This enables fine-grained performance tuning by inserting fences only where needed.

Usage Example:

// Insert fence after all writes, before reads
var location = new FenceLocation
{
    AfterWrites = true,
    BeforeReads = true
};
provider.InsertFence(FenceType.Device, location);

Performance Optimization: Strategic fence placement can reduce overhead by 30-50% compared to pervasive fencing. Profile your kernel to identify critical synchronization points.

Properties

Acquire

Gets a fence location configured for acquire semantics (pre-read fence).

public static FenceLocation Acquire { get; }

Property Value

FenceLocation

Remarks

Acquire semantics ensure all subsequent reads observe prior writes. Use this for consumer threads reading published data.

AfterWrites

Gets or sets whether the fence should be inserted after all write operations.

public bool AfterWrites { get; init; }

Property Value

bool

True to insert fence after stores/writes, false otherwise.

Remarks

Post-write fences implement release semantics: all prior writes become visible to other threads after the fence.

Use Case: Producer threads releasing data to consumers.

Example:

data[tid] = compute_value();  // Write
__threadfence();              // Post-write fence
flag[tid] = READY;            // Signal ready

AtEntry

Gets or sets whether the fence should be inserted at kernel entry.

public bool AtEntry { get; init; }

Property Value

bool

True to insert fence at the very beginning of kernel execution, false otherwise.

Remarks

Entry fences ensure that all threads observe the same initial memory state. Useful for kernels that depend on data prepared by previous kernels or host code.

Overhead: ~10-100ns per kernel launch depending on fence type.

AtExit

Gets or sets whether the fence should be inserted at kernel exit.

public bool AtExit { get; init; }

Property Value

bool

True to insert fence at the very end of kernel execution, false otherwise.

Remarks

Exit fences ensure that all memory operations complete before kernel termination. Guarantees that host code or subsequent kernels observe all updates.

Overhead: ~10-100ns per kernel launch depending on fence type.

BeforeReads

Gets or sets whether the fence should be inserted before all read operations.

public bool BeforeReads { get; init; }

Property Value

bool

True to insert fence before loads/reads, false otherwise.

Remarks

Pre-read fences implement acquire semantics: all subsequent reads observe values written by other threads before the fence.

Use Case: Consumer threads acquiring data from producers.

Example:

while (flag[producer] != READY) { }  // Wait
__threadfence();                     // Pre-read fence
value = data[producer];              // Read

FullBarrier

Gets a fence location configured for full barrier semantics (post-write + pre-read).

public static FenceLocation FullBarrier { get; }

Property Value

FenceLocation

Remarks

Full barriers provide both release and acquire semantics, ensuring complete memory consistency. Highest overhead but strongest guarantee.

InstructionIndex

Gets or sets the specific instruction index where the fence should be inserted.

public int? InstructionIndex { get; init; }

Property Value

int?

The zero-based instruction index, or null to use other location properties.

Remarks

When specified, the fence is inserted at the exact instruction offset in the kernel's intermediate representation (PTX, SPIR-V, etc.). This provides maximum control but requires deep knowledge of compiled code.

KernelEntry

Gets a fence location at kernel entry.

public static FenceLocation KernelEntry { get; }

Property Value

FenceLocation

Remarks

Entry fences ensure consistent initial memory state across all threads.

KernelExit

Gets a fence location at kernel exit.

public static FenceLocation KernelExit { get; }

Property Value

FenceLocation

Remarks

Exit fences ensure all memory operations complete before kernel termination.

Release

Gets a fence location configured for release semantics (post-write fence).

public static FenceLocation Release { get; }

Property Value

FenceLocation

Remarks

Release semantics ensure all prior writes are visible before subsequent operations. Use this for producer threads publishing data.