使用Cudafy时避免使用nvcc编译
我正在使用Cudafy并希望我的用户能够在不安装CUDA SDK的情况下使用CUDA,但他们可以使用Cudafy DLL。 为了避免在CudafyTranslator.Cudafy(types)
自动完成nvcc编译,我使用以下方法:
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string modulePath = Path.Combine(directory, myType.Name + ".cdfy"); CudafyModule km = CudafyModule.TryDeserialize(modulePath); if (ReferenceEquals(km, null) || !km.TryVerifyChecksums()) { km = CudafyTranslator.Cudafy(types); km.Serialize(modulePath); } GPU.LoadModule(km);
其中types
是System.Type
的数组。
问题出在第三行, TryDeserialize
总是返回null
。 我已检查文件是否存在且modulePath
是否正确且文件是否存在。 有人可以就这件事情说清楚吗?
如果它不意味着重写我的Cudafy模块,我准备改变我的方法。
这是我的function(部分基于你的代码btw):
public static void LoadTypeModule(GPGPU gpu, Type typeToCudafy) { Console.WriteLine("Loading module "+typeToCudafy.Name); string appFolder = AppDomain.CurrentDomain.BaseDirectory; string typeModulePath = Path.Combine(appFolder, typeToCudafy.Name + ".cdfy"); CudafyModule cudaModule = CudafyModule.TryDeserialize(typeModulePath); if (cudaModule == null || !cudaModule.TryVerifyChecksums()) { // if failed to open module: // translate the type's code to cuda code cudaModule = CudafyTranslator.Cudafy(typeToCudafy); cudaModule.Serialize(); // save to file } // load module to cuda memory, don't unload previous modules gpu.LoadModule(cudaModule, false); }
现在调用函数:
MyClassWithCudaKernels classModule = new MyClassWithCudaKernels(); LoadTypeModule(gpu, classModule.GetType());