Пример #1
0
 /// <summary>
 /// For IDisposable
 /// </summary>
 /// <param name="fDisposing"></param>
 protected virtual void Dispose(bool fDisposing)
 {
     if (fDisposing && !disposed)
     {
         _devVar.Dispose();
         disposed = true;
         // the _texref reference is not destroyed explicitly, as it is done automatically when module is unloaded
     }
     if (!fDisposing && !disposed)
     {
         Debug.WriteLine(String.Format("ManagedCUDA not-disposed warning: {0}", this.GetType()));
     }
 }
Пример #2
0
        //Clean up before closing
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isRunning = false;
            isInit    = false;
            cuda_vbo_resource.Dispose();
            texref.Dispose();
            dvfield.Dispose();
            vxfield.Dispose();
            vyfield.Dispose();

            planc2r.Dispose();
            planr2c.Dispose();

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.DeleteBuffers(1, ref vbo);

            stopwatch.Dispose();
            ctx.Dispose();
        }
Пример #3
0
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isRunning = false;

            //Cleanup
            if (graphicsres != null)
            {
                graphicsres.Dispose();
            }
            if (g_mparticles != null)
            {
                g_mparticles.Dispose();
            }
            if (stopwatch != null)
            {
                stopwatch.Dispose();
            }

            if (texref != null)
            {
                texref.Dispose();
            }
            if (g_dvfield != null)
            {
                g_dvfield.Dispose();
            }
            if (g_vxfield != null)
            {
                g_vxfield.Dispose();
            }
            if (g_vyfield != null)
            {
                g_vyfield.Dispose();
            }

            if (g_planc2r != null)
            {
                g_planc2r.Dispose();
            }
            if (g_planr2c != null)
            {
                g_planr2c.Dispose();
            }

            if (g_pVB != null)
            {
                g_pVB.Dispose();
            }
            if (g_pTexture != null)
            {
                g_pTexture.Dispose();
            }

            if (device != null)
            {
                device.Dispose();
            }
            if (d3d != null)
            {
                d3d.Dispose();
            }

            if (ctx != null)
            {
                ctx.Dispose();
            }
        }
        private void myWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //Stop render loop before closing
            if (frameTimer != null)
            {
                frameTimer.Tick -= new EventHandler(frameTimer_Tick);
                frameTimer.Stop();
            }

            //Cleanup
            if (graphicsres != null)
            {
                graphicsres.Dispose();
            }
            if (g_mparticles != null)
            {
                g_mparticles.Dispose();
            }
            if (stopwatch != null)
            {
                stopwatch.Dispose();
            }

            if (texref != null)
            {
                texref.Dispose();
            }
            if (g_dvfield != null)
            {
                g_dvfield.Dispose();
            }
            if (g_vxfield != null)
            {
                g_vxfield.Dispose();
            }
            if (g_vyfield != null)
            {
                g_vyfield.Dispose();
            }

            if (g_planc2r != null)
            {
                g_planc2r.Dispose();
            }
            if (g_planr2c != null)
            {
                g_planr2c.Dispose();
            }

            if (g_pVB != null)
            {
                g_pVB.Dispose();
            }
            if (g_pTexture != null)
            {
                g_pTexture.Dispose();
            }

            if (device != null)
            {
                device.Dispose();
            }
            if (d3d != null)
            {
                d3d.Dispose();
            }

            if (ctx != null)
            {
                ctx.Dispose();
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            const int nx = 2048;
            const int ny = 2048;

            // shifts applied to x and y data
            const int x_shift = 5;
            const int y_shift = 7;

            ShrQATest.shrQAStart(args);

            if ((nx%TILE_DIM != 0)  || (ny%TILE_DIM != 0))
            {
                Console.Write("nx and ny must be multiples of TILE_DIM\n");
                ShrQATest.shrQAFinishExit(args, ShrQATest.eQAstatus.QA_WAIVED);
            }

            // execution configuration parameters
            dim3 grid = new dim3(nx/TILE_DIM, ny/TILE_DIM, 1);
            dim3 threads = new dim3(TILE_DIM, TILE_DIM, 1);

            // This will pick the best possible CUDA capable device
            int devID = findCudaDevice(args);

            //Load Kernel image from resources
            string resName;
            if (IntPtr.Size == 8)
                resName = "simplePitchLinearTexture_x64.ptx";
            else
                resName = "simplePitchLinearTexture.ptx";

            string resNamespace = "simplePitchLinearTexture";
            string resource = resNamespace + "." + resName;
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
            if (stream == null) throw new ArgumentException("Kernel not found in resources.");
            byte[] kernels = new byte[stream.Length];

            int bytesToRead = (int)stream.Length;
            while (bytesToRead > 0)
            {
                bytesToRead -= stream.Read(kernels, (int)stream.Position, bytesToRead);
            }

            CudaKernel PLKernel = ctx.LoadKernelPTX(kernels, "shiftPitchLinear");
            CudaKernel ArrayKernel = ctx.LoadKernelPTX(kernels, "shiftArray");

            CudaStopWatch stopwatch = new CudaStopWatch();

            // ----------------------------------
            // Host allocation and initialization
            // ----------------------------------

            float[] h_idata = new float[nx * ny];
            float[] h_odata = new float[nx * ny];
            float[] gold = new float[nx * ny];

            for (int i = 0; i < nx * ny; ++i) h_idata[i] = (float)i;

            // ------------------------
            // Device memory allocation
            // ------------------------

            // Pitch linear input data
            CudaPitchedDeviceVariable<float> d_idataPL = new CudaPitchedDeviceVariable<float>(nx, ny);

            // Array input data
            CudaArray2D d_idataArray = new CudaArray2D(CUArrayFormat.Float, nx, ny, CudaArray2DNumChannels.One);

            // Pitch linear output data
            CudaPitchedDeviceVariable<float> d_odata = new CudaPitchedDeviceVariable<float>(nx, ny);

            // ------------------------
            // copy host data to device
            // ------------------------

            // Pitch linear
            d_idataPL.CopyToDevice(h_idata);

            // Array
            d_idataArray.CopyFromHostToThis<float>(h_idata);

            // ----------------------
            // Bind texture to memory
            // ----------------------

            // Pitch linear
            CudaTextureLinearPitched2D<float> texRefPL = new CudaTextureLinearPitched2D<float>(PLKernel, "texRefPL", CUAddressMode.Wrap, CUFilterMode.Point, CUTexRefSetFlags.NormalizedCoordinates, CUArrayFormat.Float, d_idataPL);
            CudaTextureArray2D texRefArray = new CudaTextureArray2D(ArrayKernel, "texRefArray", CUAddressMode.Wrap, CUFilterMode.Point, CUTexRefSetFlags.NormalizedCoordinates, d_idataArray);

            // ---------------------
            // reference calculation
            // ---------------------

            for (int j = 0; j < ny; j++)
            {
                int jshift = (j + y_shift) % ny;
                for (int i = 0; i < nx; i++)
                {
                    int ishift = (i + x_shift) % nx;
                    gold[j * nx + i] = h_idata[jshift * nx + ishift];
                }
            }

            // ----------------
            // shiftPitchLinear
            // ----------------

            ctx.ClearMemory(d_odata.DevicePointer, 0, d_odata.TotalSizeInBytes);
            PLKernel.BlockDimensions = threads;
            PLKernel.GridDimensions = grid;
            stopwatch.Start();
            for (int i=0; i < NUM_REPS; i++)
            {
                PLKernel.Run(d_odata.DevicePointer, (int)(d_odata.Pitch/sizeof(float)), nx, ny, x_shift, y_shift);
            }
            stopwatch.Stop();
            stopwatch.StopEvent.Synchronize();
            float timePL = stopwatch.GetElapsedTime();

            // check results
            d_odata.CopyToHost(h_odata);

            bool res = cutComparef(gold, h_odata);

            bool success = true;
            if (res == false) {
                Console.Write("*** shiftPitchLinear failed ***\n");
                success = false;
            }

            // ----------
            // shiftArray
            // ----------

            ctx.ClearMemory(d_odata.DevicePointer, 0, d_odata.TotalSizeInBytes);
            ArrayKernel.BlockDimensions = threads;
            ArrayKernel.GridDimensions = grid;
            stopwatch.Start();
            for (int i=0; i < NUM_REPS; i++) {
                ArrayKernel.Run(d_odata.DevicePointer, (int)(d_odata.Pitch/sizeof(float)), nx, ny, x_shift, y_shift);

            }

            stopwatch.Stop();
            stopwatch.StopEvent.Synchronize();
            float timeArray = stopwatch.GetElapsedTime();

            // check results
            d_odata.CopyToHost(h_odata);

            res = cutComparef(gold, h_odata);

            if (res == false) {
                Console.Write("*** shiftArray failed ***\n");
                success = false;
            }

            float bandwidthPL = 2.0f*1000.0f*nx*ny*sizeof(float)/(1e+9f)/(timePL/NUM_REPS);
            float bandwidthArray = 2.0f*1000.0f*nx*ny*sizeof(float)/(1e+9f)/(timeArray/NUM_REPS);
            Console.Write("\nBandwidth (GB/s) for pitch linear: {0}; for array: {1}\n",
                bandwidthPL, bandwidthArray);

            float fetchRatePL = nx*ny/1e+6f/(timePL/(1000.0f*NUM_REPS));
            float fetchRateArray = nx*ny/1e+6f/(timeArray/(1000.0f*NUM_REPS));
            Console.Write("\nTexture fetch rate (Mpix/s) for pitch linear: {0}; for array: {1}\n\n",
                fetchRatePL, fetchRateArray);

            // cleanup
            texRefPL.Dispose();
            texRefArray.Dispose();
            d_idataPL.Dispose();
            d_idataArray.Dispose();
            d_odata.Dispose();
            stopwatch.Dispose();
            ctx.Dispose();

            ShrQATest.shrQAFinishExit(args, (success == true) ? ShrQATest.eQAstatus.QA_PASSED : ShrQATest.eQAstatus.QA_FAILED);
        }
Пример #6
0
        static void Main(string[] args)
        {
            const int nx = 2048;
            const int ny = 2048;

            // shifts applied to x and y data
            const int x_shift = 5;
            const int y_shift = 7;

            ShrQATest.shrQAStart(args);

            if ((nx % TILE_DIM != 0) || (ny % TILE_DIM != 0))
            {
                Console.Write("nx and ny must be multiples of TILE_DIM\n");
                ShrQATest.shrQAFinishExit(args, ShrQATest.eQAstatus.QA_WAIVED);
            }

            // execution configuration parameters
            dim3 grid    = new dim3(nx / TILE_DIM, ny / TILE_DIM, 1);
            dim3 threads = new dim3(TILE_DIM, TILE_DIM, 1);

            // This will pick the best possible CUDA capable device
            int devID = findCudaDevice(args);


            //Load Kernel image from resources
            string resName;

            if (IntPtr.Size == 8)
            {
                resName = "simplePitchLinearTexture_x64.ptx";
            }
            else
            {
                resName = "simplePitchLinearTexture.ptx";
            }

            string resNamespace = "simplePitchLinearTexture";
            string resource     = resNamespace + "." + resName;
            Stream stream       = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

            if (stream == null)
            {
                throw new ArgumentException("Kernel not found in resources.");
            }
            byte[] kernels = new byte[stream.Length];

            int bytesToRead = (int)stream.Length;

            while (bytesToRead > 0)
            {
                bytesToRead -= stream.Read(kernels, (int)stream.Position, bytesToRead);
            }

            CudaKernel PLKernel    = ctx.LoadKernelPTX(kernels, "shiftPitchLinear");
            CudaKernel ArrayKernel = ctx.LoadKernelPTX(kernels, "shiftArray");

            CudaStopWatch stopwatch = new CudaStopWatch();

            // ----------------------------------
            // Host allocation and initialization
            // ----------------------------------

            float[] h_idata = new float[nx * ny];
            float[] h_odata = new float[nx * ny];
            float[] gold    = new float[nx * ny];

            for (int i = 0; i < nx * ny; ++i)
            {
                h_idata[i] = (float)i;
            }

            // ------------------------
            // Device memory allocation
            // ------------------------

            // Pitch linear input data
            CudaPitchedDeviceVariable <float> d_idataPL = new CudaPitchedDeviceVariable <float>(nx, ny);

            // Array input data
            CudaArray2D d_idataArray = new CudaArray2D(CUArrayFormat.Float, nx, ny, CudaArray2DNumChannels.One);

            // Pitch linear output data
            CudaPitchedDeviceVariable <float> d_odata = new CudaPitchedDeviceVariable <float>(nx, ny);

            // ------------------------
            // copy host data to device
            // ------------------------

            // Pitch linear
            d_idataPL.CopyToDevice(h_idata);

            // Array
            d_idataArray.CopyFromHostToThis <float>(h_idata);

            // ----------------------
            // Bind texture to memory
            // ----------------------

            // Pitch linear
            CudaTextureLinearPitched2D <float> texRefPL = new CudaTextureLinearPitched2D <float>(PLKernel, "texRefPL", CUAddressMode.Wrap, CUFilterMode.Point, CUTexRefSetFlags.NormalizedCoordinates, CUArrayFormat.Float, d_idataPL);
            CudaTextureArray2D texRefArray = new CudaTextureArray2D(ArrayKernel, "texRefArray", CUAddressMode.Wrap, CUFilterMode.Point, CUTexRefSetFlags.NormalizedCoordinates, d_idataArray);

            // ---------------------
            // reference calculation
            // ---------------------

            for (int j = 0; j < ny; j++)
            {
                int jshift = (j + y_shift) % ny;
                for (int i = 0; i < nx; i++)
                {
                    int ishift = (i + x_shift) % nx;
                    gold[j * nx + i] = h_idata[jshift * nx + ishift];
                }
            }

            // ----------------
            // shiftPitchLinear
            // ----------------

            ctx.ClearMemory(d_odata.DevicePointer, 0, d_odata.TotalSizeInBytes);
            PLKernel.BlockDimensions = threads;
            PLKernel.GridDimensions  = grid;
            stopwatch.Start();
            for (int i = 0; i < NUM_REPS; i++)
            {
                PLKernel.Run(d_odata.DevicePointer, (int)(d_odata.Pitch / sizeof(float)), nx, ny, x_shift, y_shift);
            }
            stopwatch.Stop();
            stopwatch.StopEvent.Synchronize();
            float timePL = stopwatch.GetElapsedTime();

            // check results
            d_odata.CopyToHost(h_odata);

            bool res = cutComparef(gold, h_odata);

            bool success = true;

            if (res == false)
            {
                Console.Write("*** shiftPitchLinear failed ***\n");
                success = false;
            }

            // ----------
            // shiftArray
            // ----------

            ctx.ClearMemory(d_odata.DevicePointer, 0, d_odata.TotalSizeInBytes);
            ArrayKernel.BlockDimensions = threads;
            ArrayKernel.GridDimensions  = grid;
            stopwatch.Start();
            for (int i = 0; i < NUM_REPS; i++)
            {
                ArrayKernel.Run(d_odata.DevicePointer, (int)(d_odata.Pitch / sizeof(float)), nx, ny, x_shift, y_shift);
            }

            stopwatch.Stop();
            stopwatch.StopEvent.Synchronize();
            float timeArray = stopwatch.GetElapsedTime();

            // check results
            d_odata.CopyToHost(h_odata);

            res = cutComparef(gold, h_odata);

            if (res == false)
            {
                Console.Write("*** shiftArray failed ***\n");
                success = false;
            }

            float bandwidthPL    = 2.0f * 1000.0f * nx * ny * sizeof(float) / (1e+9f) / (timePL / NUM_REPS);
            float bandwidthArray = 2.0f * 1000.0f * nx * ny * sizeof(float) / (1e+9f) / (timeArray / NUM_REPS);

            Console.Write("\nBandwidth (GB/s) for pitch linear: {0}; for array: {1}\n",
                          bandwidthPL, bandwidthArray);

            float fetchRatePL    = nx * ny / 1e+6f / (timePL / (1000.0f * NUM_REPS));
            float fetchRateArray = nx * ny / 1e+6f / (timeArray / (1000.0f * NUM_REPS));

            Console.Write("\nTexture fetch rate (Mpix/s) for pitch linear: {0}; for array: {1}\n\n",
                          fetchRatePL, fetchRateArray);


            // cleanup
            texRefPL.Dispose();
            texRefArray.Dispose();
            d_idataPL.Dispose();
            d_idataArray.Dispose();
            d_odata.Dispose();
            stopwatch.Dispose();
            ctx.Dispose();

            ShrQATest.shrQAFinishExit(args, (success == true) ? ShrQATest.eQAstatus.QA_PASSED : ShrQATest.eQAstatus.QA_FAILED);
        }
Пример #7
0
        private void btn_openImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Images|*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png;*.gif";
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            bmp_src = new Bitmap(ofd.FileName);

            if (bmp_src.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Only 24-bit RGB images are supported!");
                bmp_src  = null;
                bmp_mask = null;
                bmp_res  = null;
                if (npp_bmp_src != null)
                {
                    npp_bmp_src.Dispose();
                }
                if (npp_bmp_res != null)
                {
                    npp_bmp_res.Dispose();
                }
                if (npp_bmp_mask != null)
                {
                    npp_bmp_mask.Dispose();
                }
                if (d_bmp_src != null)
                {
                    d_bmp_src.Dispose();
                }
                if (d_bmp_res != null)
                {
                    d_bmp_res.Dispose();
                }
                if (d_bmp_mask != null)
                {
                    d_bmp_mask.Dispose();
                }
                return;
            }
            width    = bmp_src.Width;
            height   = bmp_src.Height;
            marker   = new int[width * height];
            bmp_res  = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            bmp_mask = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            SetPalette(bmp_mask);
            pictureBox_src.Image = bmp_src;

            selection.x      = (int)Math.Ceiling(width * 0.1);
            selection.y      = (int)Math.Ceiling(height * 0.1);
            selection.width  = width - 2 * selection.x;
            selection.height = height - 2 * selection.y;

            if (npp_bmp_src != null)
            {
                npp_bmp_src.Dispose();
            }
            if (npp_bmp_res != null)
            {
                npp_bmp_res.Dispose();
            }
            if (npp_bmp_mask != null)
            {
                npp_bmp_mask.Dispose();
            }
            if (d_bmp_src != null)
            {
                d_bmp_src.Dispose();
            }
            if (d_bmp_res != null)
            {
                d_bmp_res.Dispose();
            }
            if (d_bmp_mask != null)
            {
                d_bmp_mask.Dispose();
            }

            NPPImage_8uC3 npp_temp = new NPPImage_8uC3(width, height);
            CudaPitchedDeviceVariable <uchar3> d_bmp_temp = new CudaPitchedDeviceVariable <uchar3>(npp_temp.DevicePointer, width, height, npp_temp.Pitch);

            npp_temp.CopyToDevice(bmp_src);

            npp_bmp_src  = new NPPImage_8uC4(width, height);
            npp_bmp_res  = new NPPImage_8uC4(width, height);
            npp_bmp_mask = new NPPImage_8uC1(width, height);
            d_bmp_src    = new CudaPitchedDeviceVariable <uchar4>(npp_bmp_src.DevicePointer, width, height, npp_bmp_src.Pitch);
            d_bmp_res    = new CudaPitchedDeviceVariable <uchar4>(npp_bmp_res.DevicePointer, width, height, npp_bmp_res.Pitch);
            d_bmp_mask   = new CudaPitchedDeviceVariable <byte>(npp_bmp_mask.DevicePointer, width, height, npp_bmp_mask.Pitch);

            grabcut = new GrabCut(d_bmp_src, d_bmp_mask, width, height);
            grabcut.grabCutUtils.convertRGBToRGBA(d_bmp_src, d_bmp_temp, width, height);
            d_bmp_temp.Dispose();
            npp_temp.Dispose();
        }