Я пытаюсь ускорить свой код C#, используя ILGPU для обработки на графическом процессоре. Однако возникают следующие ошибки:
Ошибка CS0117: «Ускоритель» не содержит определения для «Создать».
Accelerator accelerator = Accelerator.Create(...);
Ошибка CS1729: «Контекст» не содержит конструктор, принимающий 0 аргументов.
Context context = new Context();
Моя цель — применить эффект масляной живописи к растровому изображению с помощью ILGPU, и я следовал документации ILGPU. Однако, похоже, я что-то упустил или столкнулся с проблемами совместимости.
Вот упрощенная версия моего кода:
int valu = int.Parse(str[1]);
Bitmap resultImage = new Bitmap(cc);
BitmapData inputImageDa = resultImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData resultImageData = cc.LockBits(new Rectangle(0, 0, resultImage.Width, resultImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
Parallel.For(0, height, y =>
{
byte* inputRow = (byte*)inputImageDa.Scan0 + (y * inputImageDa.Stride);
byte* resultRow = (byte*)resultImageData.Scan0 + (y * resultImageData.Stride);
Parallel.For(0, width, x =>
{
Color dominantColor = GetDominantColor(inputImageDa, x, y, valu);
byte* resultPixel = resultRow + x * 4;
resultPixel[0] = dominantColor.B;
resultPixel[1] = dominantColor.G;
resultPixel[2] = dominantColor.R;
resultPixel[3] = 255;
});
});
}
cc.UnlockBits(resultImageData);
Я был бы признателен за любые рекомендации или предложения о том, как решить эти проблемы и успешно запустить мой код C# с помощью ILGPU для ускорения графического процессора.
Спасибо!
Изменить:
извините, я забыл сказать, что вот код, который я пробовал:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using ILGPU;
using ILGPU.Runtime;
class Program
{
static void Main()
{
// Replace with your image loading logic
Bitmap originalImage = new Bitmap("your_image_path.jpg");
int brushSize = 5; // Adjust the brush size as needed
Bitmap resultImage = ApplyOilPaintingEffect(originalImage, brushSize);
// Save or display the result image
resultImage.Save("result_image.jpg");
}
static Bitmap ApplyOilPaintingEffect(Bitmap originalImage, int brushSize)
{
int width = originalImage.Width;
int height = originalImage.Height;
// Convert Bitmap to ILGPU memory buffer
using (var context = new Context())
{
using (var accelerator = new Accelerator(context, AcceleratorType.Cuda)) // Adjust the AcceleratorType as needed
{
var inputBuffer = originalImage.ToBuffer2D(context, accelerator);
var resultBuffer = accelerator.Allocate(width, height);
// Process pixels on GPU
using (var kernel = accelerator.LoadAutoGroupedStreamKernel())
{
kernel.DefaultNumIterations = inputBuffer.Length;
kernel.Body = (index, inputPixels, outputIndex) =>
{
var value = inputPixels[index];
var hist = new int[256 * 3];
// Collect histogram in the local neighborhood
for (int i = -brushSize; i = 0 && y < height)
{
var neighborPixel = inputPixels[x, y];
hist[neighborPixel.R]++;
hist[256 + neighborPixel.G]++;
hist[512 + neighborPixel.B]++;
}
}
}
// Find the dominant color
int maxIndex = hist.ToList().IndexOf(hist.Max());
int channel = maxIndex % 256;
int color = maxIndex / 256;
outputIndex = index / width;
// Set the dominant color to the output pixel
outputIndex.X = index - outputIndex.X * width;
var dominantColor = new Argb32((color == 0) ? channel : 0, (color == 1) ? channel : 0, (color == 2) ? channel : 0, 255);
inputPixels[index] = dominantColor;
};
kernel(inputBuffer.Length, inputBuffer, resultBuffer);
}
// Convert ILGPU memory buffer back to Bitmap
Bitmap resultImage = resultBuffer.ToBitmap2D(context, accelerator);
return resultImage;
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/777 ... celeration