问题
I'm using Cudafy and would like my users to be able to use CUDA without installing the CUDA SDK, but they can use the Cudafy DLL. To avoid nvcc compilation done automatically in CudafyTranslator.Cudafy(types)
, I'm using the following approach:
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);
Where types
is an array of System.Type
s.
The problem is in the third line, TryDeserialize
always returns null
. I have checked that the file exists and the modulePath
is correct and the file exists. Can someone please shed some light on the matter?
I'm ready to change my approach if it doesn't mean re-writing my Cudafy modules.
回答1:
This is my function (which is partially based on your code 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);
}
Now calling the function:
MyClassWithCudaKernels classModule = new MyClassWithCudaKernels();
LoadTypeModule(gpu, classModule.GetType());
来源:https://stackoverflow.com/questions/29430655/avoiding-nvcc-compilation-when-using-cudafy