首页 > 解决方案 > 如何在 C# Hybridizer CUDA 代码中创建新对象

问题描述

Hybridizer 的文档中所述,您不能使用新的 x() 方法在 Hybridizer CUDA 代码中实例化对象。

我尝试使用此方法,但它会给出与使用 typeof(x) 相关的错误

[ERROR] : 0X6079 : EnrichMethodDependencyGraph failed for method System.Type -- Exception: System.NullReferenceException: Object reference not set to an instance of an object.

我也试过像这样使用它:

Activator.CreateInstance<x>()

但这只会给出一个不同的错误,我在网上找不到任何信息。

[ERROR] : 0X6079 : EnrichMethodDependencyGraph failed for method System.Activator -- Exception: System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

实例化一个对象应该是一件很简单的事情,但是我在网上找不到任何方法......有谁知道怎么做?

标签: c#hybridizer

解决方案


使用typeof(x)需要对象类型信息,这在 GPU 端不可用。Activator.CreateInstance<x>本质上是一样的。

限制不是来自new关键字,而是来自GPU端没有实现垃圾收集的胖子。

您的问题的可能解决方法:

  1. 您的类型可以替换为原始类型:您可以在 GPU 上分配原始类型数组。

  2. 您可以根据预先分配的实例数组和原子索引定义自己的分配器。


推荐阅读