Skip to content

Commit 2973b2f

Browse files
committed
Add sample
1 parent f494f38 commit 2973b2f

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

Samples/RefTypeAnalyzer/Program.cs

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1-
// See https://aka.ms/new-console-template for more information
1+
using ILGPU;
2+
using ILGPU.Runtime;
23

3-
Console.WriteLine("Hello, World!");
4+
class Program
5+
{
6+
class MyRefType
7+
{
8+
public int Hello => 42;
9+
}
10+
11+
struct MyUnmanagedType
12+
{
13+
public int Hello;
14+
15+
public MyUnmanagedType()
16+
{
17+
Hello = 42;
18+
}
19+
}
20+
21+
void Kernel(Index1D index, ArrayView<int> input, ArrayView<int> output)
22+
{
23+
// This is disallowed, since MyRefType is a reference type
24+
var refType = new MyRefType();
25+
output[index] = input[index] + refType.Hello;
26+
27+
// Allocating arrays of unmanaged types is fine
28+
MyUnmanagedType[] array = [new MyUnmanagedType()];
29+
int[] ints = [0, 1, 2];
30+
31+
// But arrays of reference types are still disallowed
32+
MyRefType[] refs = [new MyRefType()];
33+
}
34+
35+
void Main(string[] args)
36+
{
37+
var context = Context.CreateDefault();
38+
var device = context.GetPreferredDevice(false);
39+
var accelerator = device.CreateAccelerator(context);
40+
41+
var input = accelerator.Allocate1D<int>(1024);
42+
var output = accelerator.Allocate1D<int>(1024);
43+
44+
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ArrayView<int>>(Kernel);
45+
46+
kernel(input.IntExtent, input.View, output.View);
47+
48+
accelerator.Synchronize();
49+
}
50+
}
+13-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFrameworks>$(LibrarySamplesTargetFrameworks)</TargetFrameworks>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>RefTypeAnalyzers</RootNamespace>
8+
</PropertyGroup>
29

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<RootNamespace>RefTypeAnalyzers</RootNamespace>
9-
</PropertyGroup>
10-
11-
<ItemGroup>
12-
<ProjectReference Include="..\..\Src\ILGPU.Analyzers\ILGPU.Analyzers.csproj" />
13-
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\Src\ILGPU\ILGPU.csproj"/>
12+
<ProjectReference Include="..\..\Src\ILGPU.Analyzers\ILGPU.Analyzers.csproj"
13+
OutputItemType="Analyzer"
14+
ReferenceOutputAssembly="false"/>
15+
</ItemGroup>
1416

1517
</Project>

0 commit comments

Comments
 (0)