C# 由于不明确的原因,Alea GPU中出现NullReferenceException

C# 由于不明确的原因,Alea GPU中出现NullReferenceException,c#,aleagpu,C#,Aleagpu,在我的项目中使用Alea GPU时,我遇到了异常的NullReferenceException,在我添加了Parallel之前,一切都很正常。对于GPU之后的代码。默认值。对于,我可以得到的最小代码是重现错误: [GpuManaged] void fun() { int[] gpu_arr = new int[5]; Gpu.Default.For(0, 5, x => { gpu_arr[x] = 1; }); int[] arr

在我的项目中使用Alea GPU时,我遇到了异常的
NullReferenceException
,在我添加了
Parallel之前,一切都很正常。对于
GPU之后的
代码。默认值。对于
,我可以得到的最小代码是重现错误:

[GpuManaged]
void fun()
{
    int[] gpu_arr = new int[5];
    Gpu.Default.For(0, 5, x =>
    {
        gpu_arr[x] = 1;
    });

    int[] arr = new int[10]; //moving this line above Gpu.Default.For solves the problem
    Parallel.For(0, 10, y =>
    {
        arr[y] = 1; //the exception won't be thrown after deleting this line
    });
}
例外情况详情:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Alea
  StackTrace:
       at Alea.Builders.ToUnmanagedMarshaler.ca3c0a8545c9ef8bb625e1642f06cc3b0(ToUnmanagedMarshalContext c51dcb01051c4c98d3eeb37994cb59840, IRType cced00522c9c21aa0da268f3a19ebc3a4, Object cfc437ec92b1c7022feaad7b85c894b89, IntPtr c935f4057f51a7fe14b62c80b19464d48)
       at Alea.Builders.clo@4253-523.Invoke(ToUnmanagedMarshalContext ctx, IRType irType, Object clrObject, IntPtr buffer)
       at Alea.Constructs.IRStructType.MarshalToUnmanaged(ToUnmanagedMarshalContext ctx, Object[] clrFields, IntPtr buffer, FSharpFunc`2 marshal)
       at Alea.Builders.ToUnmanagedMarshaler.ca3c0a8545c9ef8bb625e1642f06cc3b0(ToUnmanagedMarshalContext c51dcb01051c4c98d3eeb37994cb59840, IRType cced00522c9c21aa0da268f3a19ebc3a4, Object cfc437ec92b1c7022feaad7b85c894b89, IntPtr c935f4057f51a7fe14b62c80b19464d48)
       at Alea.Builders.Marshal@4304.Invoke(IRType irType, Object clrObject, IntPtr buffer)
       at Alea.Builders.ToUnmanagedMarshaler.Marshal[b](ToUnmanagedMarshalContext ctx, IRType irType, b clrObject, IntPtr buffer)
       at A.cf5aded17df9f7cc4c132234dda010fa7.clo@2275-622.Invoke(Int32 i, Object param)
       at Microsoft.FSharp.Primitives.Basics.List.loop@504-16[T](FSharpFunc`3 f, Int32 n, FSharpList`1 x)
       at Microsoft.FSharp.Primitives.Basics.List.iteri[T](FSharpFunc`2 action, FSharpList`1 x)
       at A.cf5aded17df9f7cc4c132234dda010fa7.clo@2272-621.Invoke(Unit _arg59)
       at Alea.Context.LockEval[e,f](e l, FSharpFunc`2 f)
       at Alea.Kernel.c771089de7170797e4f5abe71ebafe73a(FSharpOption`1 ca0018dd2e778e1acd84b6ef7eb063a47, LaunchParam cecd3f8288b62aa7a1097708339c27039, FSharpList`1 ca4856957094a49b77a445a80fc96b563)
       at Alea.Kernel.LaunchRaw(LaunchParam lp, FSharpOption`1 instanceOpt, FSharpList`1 args)
       at Alea.Parallel.Device.DeviceFor.For(Gpu gpu, Int32 fromInclusive, Int32 toExclusive, Action`1 op)
       at Alea.Parallel.GpuExtension.For(Gpu gpu, Int32 fromInclusive, Int32 toExclusive, Action`1 op)

我已经用控制台和windows窗体对其进行了测试,结果相同。那么,我做错了什么?或者这是一个bug?

你能发布堆栈跟踪和异常消息吗?是的,当然,我已经编辑了帖子。如果你在并行块内的赋值周围添加
lock(arr)
,会怎么样?第一个
GPU.Default.For
块是否相关?与
lock
的结果相同。但我发现了奇怪的事情(变通方法),移动
int[]arr=newint[10]以上
Gpu.Default.For
解决了这个问题!所以看起来Alea在初始化之前访问了
arr
(不是吗?),但为什么它访问的数组甚至不在
Gpu.Default.For
范围内?这是自动内存管理系统中的问题吗?我希望有人能解释一下。@AhmedOsama不是
并行的。
在“正常”C#线程池中运行,而不是在GPU相关内存/线程中运行?这意味着阵列是在GPU相关内存中创建的(据我所知),但迭代是在“正常”C#空间中运行的。重新安排数组初始化可能会更改实际的字节分配和在其上运行的
语句的
。为什么在
[GpuManaged]
块(具有数组访问)中有
并行.For