C# 使GPU进行计算的简单方法

C# 使GPU进行计算的简单方法,c#,c++,bitmap,gpu,gpgpu,C#,C++,Bitmap,Gpu,Gpgpu,这是我删除图像红色通道的代码。 有什么办法可以在GPU上实现吗 其实我要的是,, 我有一个源位图数据(指针)和一个目标位图数据(指针)。 我想对源位图进行一些计算,并将结果放入目标位图 这段代码工作得很好,但我处理的是大图像。这就是为什么我希望GPU进行计算,但我不想使用CUDA或ATI的SDK。因为如果我使用CUDA或ATI的SDK,我需要这个图形卡。这就是为什么我需要在所有图形卡上都能工作的东西。我曾尝试使用DirectX,但我没有看到类似的计算结果 有没有任何库可以让GPU做到这一点 p

这是我删除图像红色通道的代码。 有什么办法可以在GPU上实现吗

其实我要的是,, 我有一个源位图数据(指针)和一个目标位图数据(指针)。 我想对源位图进行一些计算,并将结果放入目标位图

这段代码工作得很好,但我处理的是大图像。这就是为什么我希望GPU进行计算,但我不想使用CUDA或ATI的SDK。因为如果我使用CUDA或ATI的SDK,我需要这个图形卡。这就是为什么我需要在所有图形卡上都能工作的东西。我曾尝试使用DirectX,但我没有看到类似的计算结果

有没有任何库可以让GPU做到这一点

 private unsafe void testX()
    {
        Bitmap sourceBmp = (Bitmap)System.Drawing.Bitmap.FromFile(@"C:\cc.jpg");
        BitmapData sourceBmd = sourceBmp.LockBits(new System.Drawing.Rectangle(0, 0, sourceBmp.Width, sourceBmp.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        IntPtr sourcePtr = sourceBmd.Scan0;
        uint* pSource = (uint*)sourcePtr.ToPointer();

        Bitmap destinationBmp = new Bitmap(sourceBmp.Width, sourceBmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        BitmapData destinationBmd = destinationBmp.LockBits(new System.Drawing.Rectangle(0, 0, sourceBmp.Width, sourceBmp.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        IntPtr destinationPtr = destinationBmd.Scan0;
        uint* pDest = (uint*)destinationPtr.ToPointer();

        int _w = sourceBmp.Width;
        int _h = sourceBmp.Height;

        for (int j = 0; j < _h; j++)
        {
            for (int i = 0; i < _w; i++)
            {
                *pDest = (*pSource) & 0xff00ffff;
                pDest++;
                pSource++;
            }
        }

        sourceBmp.UnlockBits(sourceBmd);
        destinationBmp.UnlockBits(destinationBmd);
        if (File.Exists(@"C:\deneme2.jpg"))
            File.Delete(@"C:\deneme2.jpg");
        destinationBmp.Save(@"C:\deneme2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        MessageBox.Show("Done!");
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        testX();
    }
private不安全的void testX()
{
位图源bmp=(位图)System.Drawing.Bitmap.FromFile(@“C:\cc.jpg”);
BitmapData sourceBmd=sourceBmp.LockBits(新的System.Drawing.Rectangle(0,0,sourceBmp.Width,sourceBmp.Height),ImageLockMode.ReadWrite,System.Drawing.Imaging.PixelFormat.Format32bppRgb);
IntPtr sourcePtr=sourceBmd.Scan0;
uint*pSource=(uint*)sourcePtr.ToPointer();
位图目标bmp=新位图(sourceBmp.Width、sourceBmp.Height、System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BitmapData destinationBmd=destinationBmp.LockBits(新的System.Drawing.Rectangle(0,0,sourceBmp.Width,sourceBmp.Height),ImageLockMode.ReadWrite,System.Drawing.Imaging.PixelFormat.Format32bppRgb);
IntPtr destinationPtr=destinationBmd.Scan0;
uint*pDest=(uint*)destinationPtr.ToPointer();
int _w=sourceBmp.Width;
int _h=sourceBmp.Height;
对于(int j=0;j<\u h;j++)
{
对于(int i=0;i<\w;i++)
{
*pDest=(*pSource)和0xff00ffff;
pDest++;
pSource++;
}
}
sourceBmp.UnlockBits(sourceBmd);
destinationBmp.UnlockBits(destinationBmd);
if(File.Exists(@“C:\deneme2.jpg”))
删除(@“C:\deneme2.jpg”);
destinationBmp.Save(@“C:\deneme2.jpg”,System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show(“完成!”);
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
testX();
}
最后,我尝试了很多东西,比如CUDA、OpenGL、OpenCL和ATI的SDK,我需要下载至少300MB的文件才能使用SE SDK。所以,我需要的是一个小型的库或标题,所有图形卡的作品。谢谢你的帮助和回答

对了,我的英语很抱歉


谢谢。

不,GPU没有任何库,相反,您可以根据需要随时编写一个库。

GPGPU编程的标准便携方法是OpenCL。它适用于所有GPU。 对于Intel处理器,它从IvyBridge开始在集成GPU上工作。在SandyBridge上,CPU实际上完成了工作。 图形驱动程序附带了OpenCL支持

编辑
对于Windows,您可以尝试。

我认为,对于这样一个简单的操作,使用GPU不会带来任何性能优势。您需要将源图像数据复制到设备内存,执行处理并将结果图像数据复制回主机,这两者都需要花费一些时间

您可以重写代码,考虑步幅(位图行的长度与4字节对齐)。帮助编译器理解您正在做的事情。更喜欢数组索引,而不是循环中的指针增量

int _ss = sourceBmd.Stride; // correct length of a row
int _sd = destinationBmd.Stride;

for (int j = 0; j < _h; j++)
{
    for (int i = 0; i < _w; i++)
    {
        // each iteration of the internal for loop is now independent
        pDest[i] = pSource[i] & 0xff00ffff;
    }
    pDest+=_sd;
    pSource+=_ss;
}
int\u ss=sourceBmd.Stride;//行的正确长度
int _sd=目标BMD.步幅;
对于(int j=0;j<\u h;j++)
{
对于(int i=0;i<\w;i++)
{
//现在,内部for循环的每个迭代都是独立的
pDest[i]=pSource[i]&0xff00ffff;
}
pDest+=\u-sd;
pSource+=\u ss;
}

如果您不想使用CUDA,为什么要将其张贴到CUDA标签上?GPGPU标签更合适。您可以考虑使用OpenCL,这可能有助于您在CUDA和非CUDA卡上运行代码(一旦编写)。我投票决定关闭这个没有问题的帖子。下次尝试使用与你正在写的文章相关的标题。StackOverflow是一个社区。在发布问题时,您将得到他人的帮助。适当的标题可能会帮助其他用户找到解决他们自己问题的方法(如果与您的类似)。如果您要求一个库来解决问题,但不想要“CUDA或其他东西”,则会产生矛盾,因为“库”是“其他东西”集合的一个元素。但他不想使用CUDA或其他东西。