Skip to content

Commit

Permalink
Simplified API for copying to/from page locked memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ committed Dec 5, 2022
1 parent 5b7e23f commit 9e35572
Show file tree
Hide file tree
Showing 8 changed files with 971 additions and 414 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 ILGPU Project
// Copyright (c) 2021-2022 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 @@ -67,7 +67,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 @@ -91,7 +91,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 @@ -101,7 +101,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
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 ILGPU Project
// Copyright (c) 2021-2022 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 @@ -82,10 +82,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 @@ -109,14 +109,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 @@ -134,7 +134,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
Loading

0 comments on commit 9e35572

Please sign in to comment.