我正在使用 Cudafy.NET 在 GPU 上执行图像处理。我已经设置了我的类以及第一个函数(在 CPU 上运行的 PerformBarrelCorrection)来设置多个线程来执行图像中每个像素的逻辑计算。
但是,每次我在 GPU 上启动该功能时,它都会引发异常:
Cudafy.NET.dll 中出现“Cudafy.Host.CudafyHostException”类型的异常,但未在用户代码中处理
附加信息:CUDA.NET 异常:ErrorInvalidValue。
这是整个类的注释,以显示引发异常的行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;
using System.Drawing;
using System.IO;
namespace AForgeTrial
{
class GPUProcessing
{
public CudafyModule km;
public GPGPU gpu;
public GPUProcessing() {
km = CudafyTranslator.Cudafy();
gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
gpu.LoadModule(km);
}
public Bitmap PerformBarrelCorrection(Bitmap source, double strength, float zoom, int imageWidth, int imageHeight)
{
byte[] bmp = ImageToByteArray2(source);
int halfWidth = imageWidth / 2;
int halfHeight = imageHeight / 2;
double correctionRadius = Math.Sqrt(((imageWidth * imageWidth) + (imageHeight * imageHeight)) / strength);
byte[] dev_src_bmp = gpu.CopyToDevice(bmp);
byte[] dst_bmp = new byte[bmp.Length];
byte[] dev_dst_bmp = gpu.Allocate<byte>(dst_bmp);
double[] correctPass = new double[1];
correctPass[0] = correctionRadius;
double[] dev_correctionRadius = gpu.CopyToDevice<double>(correctPass);
float[] zoomPass = new float[1];
zoomPass[0] = zoom;
float[] dev_zoom = gpu.CopyToDevice<float>(zoomPass);
int[] halfWidthPass = new int[1];
halfWidthPass[0] = halfWidth;
int[] dev_halfWidth = gpu.CopyToDevice<int>(halfWidthPass);
int[] halfHeightPass = new int[1];
halfHeightPass[0] = imageHeight;
int[] dev_halfHeight = gpu.CopyToDevice<int>(halfHeightPass);
//int blksize = ((bmp.Length / 3) / 128) + 1;
// EXCEPTION HAPPENS ON THE LINE BELOW
gpu.Launch((bmp.Length / 3), 1).BarrelCorrectionSingleOperation(dev_src_bmp, dev_dst_bmp, dev_correctionRadius, dev_zoom, dev_halfWidth, dev_halfHeight);
gpu.CopyFromDevice(dev_dst_bmp, dst_bmp);
// Convert dst_bmp to Bitmap and return it
Bitmap result;
using (MemoryStream ms = new MemoryStream(dst_bmp))
{
result = new Bitmap(ms);
}
return result;
}
[Cudafy]
public static void BarrelCorrectionSingleOperation(GThread thread, byte[] src_bmp, byte[] dst_bmp, double[] correctionRadius, float[] zoom, int[] halfWidth, int[] halfHeight)
{
// Move a single byte from source to destination or fill if required
}
public static byte[] ImageToByteArray(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static byte[] ImageToByteArray2(Bitmap img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
public void Close() {
gpu.FreeAll();
}
}
}
有人知道这是什么一回事吗??提前致谢。