Skip to content

Commit

Permalink
Simplified API for copying to/from page locked memory. (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ authored Jan 9, 2024
1 parent df4e9af commit 5ac2165
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 69 deletions.
10 changes: 5 additions & 5 deletions Samples/PinnedMemoryCopy/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Samples
// Copyright (c) 2021-2023 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: Program.cs
Expand Down Expand Up @@ -36,7 +36,7 @@ static void PerformPinnedCopyUsingGCHandle(Accelerator accelerator, int dataSize

// Page locked buffers enable async memory transfers
using var scope = accelerator.CreatePageLockFromPinned(array);
bufferOnGPU.View.CopyFromPageLockedAsync(stream, scope);
bufferOnGPU.View.CopyFrom(stream, scope.ArrayView);

//
// Perform other operations...
Expand Down Expand Up @@ -66,7 +66,7 @@ static void PerformPinnedCopyUsingGCAllocateArray(Accelerator accelerator, int d

// Page locked buffers enable async memory transfers
using var scope = accelerator.CreatePageLockFromPinned(array);
bufferOnGPU.View.CopyFromPageLockedAsync(stream, scope);
bufferOnGPU.View.CopyFrom(stream, scope.ArrayView);

//
// Perform other operations...
Expand All @@ -89,7 +89,7 @@ static void PerformPinnedCopyUsingAllocatePageLockedArray(Accelerator accelerato
using var bufferOnGPU = accelerator.Allocate1D<int>(array.Length);
var stream = accelerator.DefaultStream;

bufferOnGPU.View.CopyFromPageLockedAsync(stream, array);
bufferOnGPU.View.CopyFrom(stream, array.ArrayView);

//
// Perform other operations...
Expand All @@ -99,7 +99,7 @@ static void PerformPinnedCopyUsingAllocatePageLockedArray(Accelerator accelerato
stream.Synchronize();

// Retrieve the results into an existing page locked array
bufferOnGPU.View.CopyToPageLockedAsync(stream, array);
bufferOnGPU.View.CopyTo(stream, array.ArrayView);

// Retrieve the results into a new array
// Rely on disabled (default) or automatic page locking behavior
Expand Down
6 changes: 4 additions & 2 deletions Src/ILGPU.Algorithms/Optimization/IOptimizer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2023 ILGPU Project
// Copyright (c) 2023-2024 ILGPU Project
// www.ilgpu.net
//
// File: IOptimizer.cs
Expand Down Expand Up @@ -230,7 +230,9 @@ public OptimizerRun(
/// </summary>
/// <param name="data">The data to load the particles to.</param>
public void LoadParticles(PageLockedArray2D<TNumericType> data) =>
Optimizer.Engine.PositionsView.DataView.CopyToPageLockedAsync(Stream, data);
Optimizer.Engine.PositionsView.DataView.BaseView.CopyTo(
Stream,
data.ArrayView);

/// <summary>
/// Loads all particle positions and converts them into a 2D array.
Expand Down
21 changes: 11 additions & 10 deletions Src/ILGPU.Algorithms/Optimization/OptimizationEngine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU Algorithms
// Copyright (c) 2023 ILGPU Project
// Copyright (c) 2023-2024 ILGPU Project
// www.ilgpu.net
//
// File: OptimizationEngine.cs
Expand All @@ -13,6 +13,7 @@
using ILGPU.Algorithms.Vectors;
using ILGPU.Runtime;
using ILGPU.Util;
using ILGPU;
using System;
using System.Diagnostics;
using System.Numerics;
Expand Down Expand Up @@ -396,9 +397,9 @@ public void LoadBounds(
boundsElementBufferCPU[i, 1] = upperBounds[i];
}

boundsElementView.DataView.CopyFromPageLockedAsync(
boundsElementView.DataView.BaseView.CopyFrom(
stream,
boundsElementBufferCPU);
boundsElementBufferCPU.ArrayView);

ConvertToVectorizedView(
stream,
Expand Down Expand Up @@ -453,7 +454,7 @@ protected virtual void LoadParametersInternal(
AcceleratorStream stream,
ArrayView<TElementType> parameters)
{
parameters.CopyToPageLockedAsync(stream, parametersCPU);
parameters.CopyTo(stream, parametersCPU.ArrayView);
stream.Synchronize();
}

Expand Down Expand Up @@ -528,8 +529,8 @@ protected internal double BeginOptimization(
resultEvalBufferCPU[0] = bestResult;

// Copy position and best result to GPU buffers
resultElementView.CopyFromPageLockedAsync(stream, resultElementBufferCPU);
resultEvalView.BaseView.CopyFromPageLockedAsync(stream, resultEvalBufferCPU);
resultElementView.CopyFrom(stream, resultElementBufferCPU.ArrayView);
resultEvalView.BaseView.CopyFrom(stream, resultEvalBufferCPU.ArrayView);

// Convert our best result view
ConvertToVectorizedView(
Expand Down Expand Up @@ -613,12 +614,12 @@ protected internal OptimizationResult<TElementType, TEvalType> FetchToCPUAsync(
OptimizationResultView<TElementType, TEvalType> resultView)
{
// Copy result to CPU to group by range of numerical values
resultView.PositionView.CopyToPageLockedAsync(
resultView.PositionView.CopyTo(
stream,
resultElementBufferCPU);
resultView.ResultView.BaseView.CopyToPageLockedAsync(
resultElementBufferCPU.ArrayView);
resultView.ResultView.BaseView.CopyTo(
stream,
resultEvalBufferCPU);
resultEvalBufferCPU.ArrayView);

// Return the actual result
return new OptimizationResult<TElementType, TEvalType>(
Expand Down
16 changes: 8 additions & 8 deletions Src/ILGPU.Tests/PageLockedMemory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021-2023 ILGPU Project
// Copyright (c) 2021-2024 ILGPU Project
// www.ilgpu.net
//
// File: PageLockedMemory.cs
Expand Down Expand Up @@ -59,10 +59,10 @@ public unsafe void PinnedUsingGCHandle()
using var buffer = Accelerator.Allocate1D<int>(array.Length);
using var scope = Accelerator.CreatePageLockFromPinned(array);

buffer.View.CopyFromPageLockedAsync(scope);
buffer.View.CopyFrom(scope.ArrayView);
Execute(buffer.Length, buffer.View);

buffer.View.CopyToPageLockedAsync(scope);
buffer.View.CopyTo(scope.ArrayView);
Accelerator.Synchronize();
Verify1D(array, expected);
}
Expand All @@ -81,10 +81,10 @@ public void PinnedUsingGCAllocateArray()
using var buffer = Accelerator.Allocate1D<int>(array.Length);
using var scope = Accelerator.CreatePageLockFromPinned(array);

buffer.View.CopyFromPageLockedAsync(scope);
buffer.View.CopyFrom(scope.ArrayView);
Execute(buffer.Length, buffer.View);

buffer.View.CopyToPageLockedAsync(scope);
buffer.View.CopyTo(scope.ArrayView);
Accelerator.Synchronize();
Verify1D(array, expected);
}
Expand All @@ -107,14 +107,14 @@ public void Copy(long constant)
using var buff = Accelerator.Allocate1D<long>(Length);

// Start copying, create the expected array in the meantime
buff.View.CopyFromPageLockedAsync(array);
buff.View.CopyFrom(array.ArrayView);
var expected = Enumerable.Repeat(constant - 5, Length).ToArray();
Accelerator.Synchronize();

Execute(array.Extent.ToIntIndex(), buff.View);
Accelerator.Synchronize();

buff.View.CopyToPageLockedAsync(array);
buff.View.CopyTo(array.ArrayView);
Accelerator.Synchronize();

Assert.Equal(expected.Length, array.Length);
Expand All @@ -132,7 +132,7 @@ public void GetAsArrayPageLocked()
array[i] = 10;

using var buff = Accelerator.Allocate1D<long>(Length);
buff.View.CopyFromPageLockedAsync(array);
buff.View.CopyFrom(array.ArrayView);

var expected = new int[Length];
for (int i = 0; i < Length; i++)
Expand Down
12 changes: 10 additions & 2 deletions Src/ILGPU/Runtime/ArrayViewExtensions.Generated.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2016-2021 ILGPU Project
// Copyright (c) 2016-2024 ILGPU Project
// www.ilgpu.net
//
// File: ArrayViewExtensions.Generated.tt/ArrayViewExtensions.Generated.cs
Expand Down Expand Up @@ -712,6 +712,7 @@ namespace ILGPU.Runtime
/// <param name="pageLockScope">The page locked memory.</param>
/// <remarks>This method is not supported on accelerators.</remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToPageLockedAsync<T>(
this <#= viewName #> source,
Expand All @@ -729,6 +730,7 @@ namespace ILGPU.Runtime
/// <param name="pageLockScope">The page locked memory.</param>
/// <remarks>This method is not supported on accelerators.</remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToPageLockedAsync<T>(
this <#= viewName #> source,
Expand All @@ -746,6 +748,7 @@ namespace ILGPU.Runtime
/// <param name="pageLockScope">The page locked memory.</param>
/// <remarks>This method is not supported on accelerators.</remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyFromPageLockedAsync<T>(
this <#= viewName #> target,
Expand All @@ -763,6 +766,7 @@ namespace ILGPU.Runtime
/// <param name="pageLockScope">The page locked memory.</param>
/// <remarks>This method is not supported on accelerators.</remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyFromPageLockedAsync<T>(
this <#= viewName #> target,
Expand Down Expand Up @@ -792,6 +796,7 @@ namespace ILGPU.Runtime
/// This method is not supported on accelerators.
/// </remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToPageLockedAsync<T>(
this <#= typeName #><T, <#= strideTypeName #>.<#= strideName #>> source,
Expand All @@ -812,6 +817,7 @@ namespace ILGPU.Runtime
/// This method is not supported on accelerators.
/// </remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToPageLockedAsync<T>(
this <#= typeName #><T, <#= strideTypeName #>.<#= strideName #>> source,
Expand All @@ -832,6 +838,7 @@ namespace ILGPU.Runtime
/// This method is not supported on accelerators.
/// </remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyFromPageLockedAsync<T>(
this <#= typeName #><T, <#= strideTypeName #>.<#= strideName #>> target,
Expand All @@ -855,6 +862,7 @@ namespace ILGPU.Runtime
/// This method is not supported on accelerators.
/// </remarks>
[NotInsideKernel]
[Obsolete("Use PageLockScope.ArrayView instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyFromPageLockedAsync<T>(
this <#= typeName #><T, <#= strideTypeName #>.<#= strideName #>> target,
Expand Down Expand Up @@ -915,7 +923,7 @@ namespace ILGPU.Runtime
.AllocatePageLocked<#= dimension #>D<T>(view.Extent);

// Copy the data
view.CopyToPageLockedAsync(stream, result);
view.BaseView.CopyTo(stream, result.ArrayView);
stream.Synchronize();

return result;
Expand Down
Loading

0 comments on commit 5ac2165

Please sign in to comment.