-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample projects demonstrating cuda IPC from both sides
- Loading branch information
Showing
9 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System; | ||
|
||
[assembly: CLSCompliant(true)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$(LibrarySamplesTargetFrameworks)</TargetFrameworks> | ||
<OutputType>Exe</OutputType> | ||
<LangVersion>8.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<EnableNETAnalyzers>true</EnableNETAnalyzers> | ||
<AnalysisMode>AllEnabledByDefault</AnalysisMode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Src\ILGPU\ILGPU.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// --------------------------------------------------------------------------------------- | ||
// ILGPU Samples | ||
// Copyright (c) 2021-2022 ILGPU Project | ||
// www.ilgpu.net | ||
// | ||
// File: Program.cs | ||
// | ||
// This file is part of ILGPU and is distributed under the University of Illinois Open | ||
// Source License. See LICENSE.txt for details. | ||
// --------------------------------------------------------------------------------------- | ||
|
||
using ILGPU; | ||
using ILGPU.Runtime; | ||
using ILGPU.Runtime.Cuda; | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace CudaIPC.Child | ||
{ | ||
class Program | ||
{ | ||
/// <summary> | ||
/// A simple kernel writing the index to the data view. | ||
/// </summary> | ||
/// <param name="index">The current thread index.</param> | ||
/// <param name="dataView">The view pointing to our memory buffer.</param> | ||
static void SimpleKernel( | ||
Index1D index, | ||
ArrayView<int> dataView) | ||
{ | ||
dataView[index] = index; | ||
} | ||
|
||
/// <summary> | ||
/// Accepts a cuda device id, an ipc memory handle as hexstring and its length as arguments. | ||
/// It then maps that memory and executes a simple kernel on it. | ||
/// </summary> | ||
static void Main(string[] args) | ||
{ | ||
if (args.Length != 3) | ||
{ | ||
Console.WriteLine("There should be 3 arguments:"); | ||
Console.WriteLine("<device id> <ipc memory handle> <length>"); | ||
return; | ||
} | ||
|
||
// Parse arguments | ||
int deviceId = int.Parse(args[0], CultureInfo.InvariantCulture); | ||
CudaIpcMemHandle ipcMemHandle = new CudaIpcMemHandle(Convert.FromHexString(args[1])); | ||
int length = int.Parse(args[2], CultureInfo.InvariantCulture); | ||
|
||
// Set up the correct accelerator | ||
using Context context = Context.CreateDefault(); | ||
CudaDevice device = context.GetCudaDevice(deviceId); | ||
using CudaAccelerator accelerator = device.CreateCudaAccelerator(context); | ||
// device.PrintInformation(); | ||
|
||
// Map exported memory | ||
MemoryBuffer cudaIpcMemoryBuffer = | ||
accelerator.MapFromIpcMemHandle(ipcMemHandle, length, sizeof(int), CudaIpcMemFlags.LazyEnablePeerAccess); | ||
ArrayView<int> arrayView = cudaIpcMemoryBuffer.AsArrayView<int>(0, length); | ||
|
||
// load and execute kernel | ||
Action<Index1D, ArrayView<int>> loadedSimpleKernel = | ||
accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>>( | ||
SimpleKernel); | ||
loadedSimpleKernel(arrayView.IntExtent, arrayView); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System; | ||
|
||
[assembly: CLSCompliant(true)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$(LibrarySamplesTargetFrameworks)</TargetFrameworks> | ||
<OutputType>Exe</OutputType> | ||
<LangVersion>8.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<EnableNETAnalyzers>true</EnableNETAnalyzers> | ||
<AnalysisMode>AllEnabledByDefault</AnalysisMode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Src\ILGPU\ILGPU.csproj" /> | ||
<ProjectReference Include="..\CudaIPC.Child\CudaIPC.Child.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// --------------------------------------------------------------------------------------- | ||
// ILGPU Samples | ||
// Copyright (c) 2021-2022 ILGPU Project | ||
// www.ilgpu.net | ||
// | ||
// File: Program.cs | ||
// | ||
// This file is part of ILGPU and is distributed under the University of Illinois Open | ||
// Source License. See LICENSE.txt for details. | ||
// --------------------------------------------------------------------------------------- | ||
|
||
using ILGPU; | ||
using ILGPU.Runtime; | ||
using ILGPU.Runtime.Cuda; | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace CudaIPC.Host | ||
{ | ||
class Program | ||
{ | ||
/// <summary> | ||
/// Exports memory for other processes using CUDA IPC. | ||
/// </summary> | ||
static void Main() | ||
{ | ||
// Create main context | ||
using var context = Context.CreateDefault(); | ||
|
||
// For each available CUDA device... | ||
foreach (var device in context.GetCudaDevices()) | ||
{ | ||
// Create accelerator for the given device | ||
using CudaAccelerator accelerator = device.CreateCudaAccelerator(context); | ||
|
||
if (!device.HasIpcSupport) | ||
{ | ||
Console.WriteLine($"{device.Name} does not support inter process comunication!"); | ||
continue; | ||
} | ||
|
||
using MemoryBuffer1D<int, Stride1D.Dense> buffer = accelerator.Allocate1D<int>(64); | ||
|
||
// Export memory for other processes | ||
CudaIpcMemHandle cudaIpcMemHandle = accelerator.GetIpcMemoryHandle(buffer); | ||
string handleHex = Convert.ToHexString(cudaIpcMemHandle); | ||
|
||
// Launch CudaIPC.Child | ||
var arguments = $"{device.DeviceId} {handleHex} {buffer.Length}"; | ||
Console.WriteLine(arguments); | ||
var childProcess = Process.Start( | ||
OperatingSystem.IsWindows() ? | ||
"CudaIPC.Child.exe" : "CudaIPC.Child", | ||
arguments | ||
); | ||
childProcess?.WaitForExit(); | ||
|
||
// Gets changed buffer data onto the CPU and print it. | ||
int[] bufferData = buffer.GetAsArray1D(); | ||
Console.WriteLine(String.Join(" ", bufferData)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters