Skip to content

Instantly share code, notes, and snippets.

@lostmsu
Created September 9, 2021 17:19
Show Gist options
  • Save lostmsu/debe2a5f4565189f9e722f1dfeb6050f to your computer and use it in GitHub Desktop.
Save lostmsu/debe2a5f4565189f9e722f1dfeb6050f to your computer and use it in GitHub Desktop.
ILGPU crashes when trying to run a specialized kernel
// <PackageReference Include="ILGPU" Version="1.0.0-beta3" />
// <PackageReference Include="TypeNum" Version="0.2.0-CI-20210909-064812" />
using System.Runtime.InteropServices;
using ILGPU;
using ILGPU.Runtime;
using ILGPU.Runtime.CPU;
using TypeNum;
var compute = Context.CreateDefaultAutoDebug();
var cpu = CPUDevice.Default.CreateCPUAccelerator(compute);
var leftData = cpu.Allocate1D(new[] { 1f, 2f, 5f });
var rightData = cpu.Allocate1D(new[] { 2f, 3f, 5f, 8f });
ArrayView<float> result = cpu.Allocate1D<float>(12).View;
var kernel = cpu.LoadAutoGroupedStreamKernel<
Index1D,
ArrayView<float>, ArrayView<float>,
ArrayView<float>,
ArrayView<long>, ArrayView<long>,
SpecializedValue<EquatableFixedArray<N2<bool>, bool>>,
SpecializedValue<EquatableFixedArray<N2<bool>, bool>>>(Kernel<Add, N2<bool>>);
kernel(12,
leftData.View, rightData.View,
result,
cpu.Allocate1D(new long[] { 4, 3 }).View,
cpu.Allocate1D(new long[] { 3, 1 }).View,
SpecializedValue.New(EquatableFixedArray.New(new FixedArray<N2<bool>, bool> {
[0] = false, [1] = true,
})),
SpecializedValue.New(EquatableFixedArray.New(new FixedArray<N2<bool>, bool> {
[0] = true, [1] = false,
})));
static void Kernel<TOp, TSize>(
Index1D index, //TODO: LongIndex1D
ArrayView<float> left, ArrayView<float> right,
ArrayView<float> @out,
ArrayView<long> dimList, ArrayView<long> prodList,
SpecializedValue<EquatableFixedArray<TSize, bool>> complistL,
SpecializedValue<EquatableFixedArray<TSize, bool>> complistR)
where TOp : unmanaged, IBinaryOp
where TSize : unmanaged, INumeral<bool> {
int ndims = default(TSize).Num;
LongIndex1D idx_l = 0;
LongIndex1D idx_r = 0;
for (int i = 0; i < ndims; i++) {
LongIndex1D idx_ret = (index / prodList[i]) % dimList[i];
idx_l = complistL.Value.Data.UnsafeRef(i)
? idx_ret + dimList[i] * idx_l
: idx_l;
idx_r = complistR.Value.Data.UnsafeRef(i)
? idx_ret + dimList[i] * idx_r
: idx_r;
}
@out[index] = default(TOp).Apply(left[idx_l], right[idx_r]);
}
internal interface IBinaryOp { float Apply(float left, float right); }
internal struct Add : IBinaryOp { public float Apply(float left, float right) => left + right; }
[StructLayout(LayoutKind.Sequential)]
public struct EquatableFixedArray<TSize, T> : IEquatable<EquatableFixedArray<TSize, T>>
where TSize : unmanaged, INumeral<T>
where T : unmanaged, IEquatable<T> {
public FixedArray<TSize, T> Data;
public EquatableFixedArray(FixedArray<TSize, T> data) {
this.Data = data;
}
public bool Equals(EquatableFixedArray<TSize, T> other) {
for (int i = 0; i < this.Data.Count; i++) {
if (!this.Data[i].Equals(other.Data[i]))
return false;
}
return true;
}
public override bool Equals(object? obj)
=> obj is EquatableFixedArray<TSize, T> other && this.Equals(other);
}
public static class EquatableFixedArray {
public static EquatableFixedArray<TSize, T> New<TSize, T>(FixedArray<TSize, T> data)
where TSize : unmanaged, INumeral<T>
where T : unmanaged, IEquatable<T>
=> new() { Data = data };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment