static void Main(string[] args) { HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0); GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp"); uint height = image.Height, width = image.Width; ushort[] inputPixels = image.PixelsUShort; float[] imageFloat = new float[width * height]; float[] imageCompute = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageFloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //create and bind surface dynamic wrapper = runner.Wrap(new Program()); wrapper.Sobel(texObj, imageCompute, (int)width, (int)height); ushort[] outputPixel = new ushort[width * height]; for (int i = 0; i < width * height; ++i) { outputPixel[i] = (ushort)imageCompute[i]; } GrayBitmap imageSobel = new GrayBitmap(width, height); imageSobel.PixelsUShort = outputPixel; imageSobel.Save("../../output-03-surface/sobel.bmp"); }
static void Main(string[] args) { HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0); GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp"); uint height = image.Height, width = image.Width; ushort[] inputPixels = image.PixelsUShort; float[] imageFloat = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageFloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //bind surface cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArraySurf; cuda.MallocArray(out cuArraySurf, ref channelDescSurf, width, height, cudaMallocArrayFlags.cudaArraySurfaceLoadStore); //create cudaResourceDesc for surface cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(cuArraySurf); //create surface object cudaSurfaceObject_t surfObj; cuda.CreateSurfaceObject(out surfObj, ref resDescSurf); dynamic wrapper = runner.Wrap(new Program()); wrapper.Sobel(texObj, surfObj, (int)width, (int)height); //pinned float array to allow the copy of the surface object on the host float[] imageCompute = new float[width * height]; GCHandle handle = GCHandle.Alloc(imageCompute, GCHandleType.Pinned); IntPtr dest = handle.AddrOfPinnedObject(); cuda.MemcpyFromArray(dest, cuArraySurf, 0, 0, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyDeviceToHost); ushort[] outputPixel = new ushort[width * height]; for (int i = 0; i < width * height; ++i) { outputPixel[i] = (ushort)imageCompute[i]; } GrayBitmap imageSobel = new GrayBitmap(width, height); imageSobel.PixelsUShort = outputPixel; imageSobel.Save("../../output-3-surface/sobel.bmp"); }
unsafe static void Main(string[] args) { HybRunner runner = HybRunner.Cuda(@"Textures and Surfaces_CUDA.dll").SetDistrib(32, 32, 16, 16, 1, 0); Bitmap baseImage = (Bitmap)Image.FromFile("lena512.bmp"); int height = baseImage.Height, width = baseImage.Width; byte[] inputPixels = new byte[width * height]; ReadImage(inputPixels, baseImage, width, height); float[] imagefloat = new float[width * height]; for (int i = 0; i < width * height; ++i) { imagefloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imagefloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, width, height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //bind surface cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArraySurf; cuda.MallocArray(out cuArraySurf, ref channelDescSurf, width, height, cudaMallocArrayFlags.cudaArraySurfaceLoadStore); //create cudaResourceDesc cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(cuArraySurf); //create surface object cudaSurfaceObject_t surfObj; cuda.CreateSurfaceObject(out surfObj, ref resDescSurf); dynamic wrapper = runner.Wrap(new Program()); // call kernek wrapper.Sobel(texObj, surfObj, width, height); float[] imageSobel = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageSobel[i] = 128.0F; } GCHandle handle = GCHandle.Alloc(imageSobel, GCHandleType.Pinned); IntPtr dest = handle.AddrOfPinnedObject(); cuda.MemcpyFromArray(dest, cuArraySurf, 0, 0, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyDeviceToHost); byte[] imageSobelByte = new byte[width * height]; for (int i = 0; i < width * height; ++i) { imageSobelByte[i] = (byte)imageSobel[i]; } SaveImage("lenaSobel512.bmp", imageSobelByte, width, height); cuda.DestroySurfaceObject(surfObj); cuda.DestroyTextureObject(texObj); }