Exemple #1
0
        /// <summary>
        /// Copy data from this array to host
        /// </summary>
        /// <typeparam name="T">Host array base type</typeparam>
        /// <param name="aHostDest">Destination</param>
        public void CopyFromThisToHost <T>(T[] aHostDest) where T : struct
        {
            GCHandle handle = GCHandle.Alloc(aHostDest, GCHandleType.Pinned);

            try
            {
                CUDAMemCpy2D copyParams = new CUDAMemCpy2D();
                copyParams.dstHost       = handle.AddrOfPinnedObject();
                copyParams.dstMemoryType = CUMemoryType.Host;
                copyParams.srcArray      = _cuArray;
                copyParams.srcMemoryType = CUMemoryType.Array;
                copyParams.Height        = _arrayDescriptor.Height;
                copyParams.WidthInBytes  = _arrayDescriptor.Width * Marshal.SizeOf(typeof(T));

                res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2D_v2(ref copyParams);
                Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2D", res));
            }
            finally
            {
                handle.Free();
            }

            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
Exemple #2
0
 /// <summary>
 /// A raw unaligned copy method
 /// </summary>
 /// <param name="aCopyParameters"></param>
 public void CopyDataUnaligned(CUDAMemCpy2D aCopyParameters)
 {
     res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2DUnaligned_v2(ref aCopyParameters);
     Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2DUnaligned", res));
     if (res != CUResult.Success)
     {
         throw new CudaException(res);
     }
 }
Exemple #3
0
        /// <summary>
        /// Copy array to array
        /// </summary>
        /// <param name="aDestArray"></param>
        public void CopyFromThisToArray(CudaArray2D aDestArray)
        {
            CUDAMemCpy2D copyParams = new CUDAMemCpy2D();

            copyParams.srcArray      = _cuArray;
            copyParams.srcMemoryType = CUMemoryType.Array;
            copyParams.dstArray      = aDestArray.CUArray;
            copyParams.dstMemoryType = CUMemoryType.Array;
            copyParams.Height        = aDestArray.ArrayDescriptor.Height;
            copyParams.WidthInBytes  = aDestArray.ArrayDescriptor.Width * CudaHelperMethods.GetChannelSize(aDestArray.ArrayDescriptor.Format) * aDestArray.ArrayDescriptor.NumChannels;

            res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2D_v2(ref copyParams);
            Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2D", res));
            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
Exemple #4
0
        /// <summary>
        /// Copy data from this array to host
        /// </summary>
        /// <param name="aHostDest">IntPtr to destination in host memory</param>
        /// <param name="aElementSizeInBytes"></param>
        public void CopyFromThisToHost(IntPtr aHostDest, SizeT aElementSizeInBytes)
        {
            CUDAMemCpy2D copyParams = new CUDAMemCpy2D();

            copyParams.dstHost       = aHostDest;
            copyParams.dstMemoryType = CUMemoryType.Host;
            copyParams.srcArray      = _cuArray;
            copyParams.srcMemoryType = CUMemoryType.Array;
            copyParams.Height        = _arrayDescriptor.Height;
            copyParams.WidthInBytes  = _arrayDescriptor.Width * aElementSizeInBytes;

            res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2D_v2(ref copyParams);
            Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2D", res));
            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
Exemple #5
0
        /// <summary>
        /// Copy from this array to a pitched device variable
        /// </summary>
        /// <typeparam name="T">device variable base type</typeparam>
        /// <param name="aDeviceVariable">Destination</param>
        public void CopyFromThisToDevice <T>(CudaPitchedDeviceVariable <T> aDeviceVariable) where T : struct
        {
            CUDAMemCpy2D copyParams = new CUDAMemCpy2D();

            copyParams.dstDevice     = aDeviceVariable.DevicePointer;
            copyParams.dstMemoryType = CUMemoryType.Device;
            copyParams.dstPitch      = aDeviceVariable.Pitch;
            copyParams.srcArray      = _cuArray;
            copyParams.srcMemoryType = CUMemoryType.Array;
            copyParams.Height        = aDeviceVariable.Height;
            copyParams.WidthInBytes  = aDeviceVariable.WidthInBytes;

            res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2D_v2(ref copyParams);
            Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2D", res));
            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
Exemple #6
0
        /// <summary>
        /// Asynchron copy 2D Array to host
        /// </summary>
        /// <param name="deviceArray"></param>
        /// <param name="stream"></param>
        public void AsyncCopyFromArray2D(CUarray deviceArray, CUstream stream)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            CUDAMemCpy2D cpyProps = new CUDAMemCpy2D();

            cpyProps.srcArray      = deviceArray;
            cpyProps.srcMemoryType = CUMemoryType.Array;
            cpyProps.dstHost       = _intPtr;
            cpyProps.dstMemoryType = CUMemoryType.Host;
            cpyProps.dstPitch      = _pitchInBytes;
            cpyProps.WidthInBytes  = _width * _typeSize;
            cpyProps.Height        = _height;

            res = DriverAPINativeMethods.AsynchronousMemcpy_v2.cuMemcpy2DAsync_v2(ref cpyProps, stream);
            Debug.Write("");//Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2DAsync", res));
            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
Exemple #7
0
        /// <summary>
        /// Synchron copy device to host
        /// </summary>
        /// <param name="devicePtr"></param>
        /// <param name="pitchDevice"></param>
        public void SynchronCopyFromDevice(CUdeviceptr devicePtr, SizeT pitchDevice)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            CUDAMemCpy2D cpyProps = new CUDAMemCpy2D();

            cpyProps.srcDevice     = devicePtr;
            cpyProps.srcMemoryType = CUMemoryType.Device;
            cpyProps.srcPitch      = pitchDevice;
            cpyProps.dstHost       = _intPtr;
            cpyProps.dstMemoryType = CUMemoryType.Host;
            cpyProps.dstPitch      = _pitchInBytes;
            cpyProps.WidthInBytes  = _width * _typeSize;
            cpyProps.Height        = _height;

            res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy2D_v2(ref cpyProps);
            Debug.Write("");//Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuMemcpy2D_v2", res));
            if (res != CUResult.Success)
            {
                throw new CudaException(res);
            }
        }
        private void Image_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.Point position = GetPositionWithDpi(e);
            int pX = (int)position.X;
            int pY = (int)position.Y;

            Point pixel = GetImagePixelFromMouseCoordinate(position);

            uchar4[] p = new uchar4[1];

            if (!double.IsInfinity(pixel.X) && !double.IsInfinity(pixel.Y))
            {
                d3dimage.Lock();

                _graphicsres.MapAllResources();
                CudaArray2D arr = _graphicsres[0].GetMappedArray2D(0, 0);

                CUDAMemCpy2D copy   = new CUDAMemCpy2D();
                GCHandle     handle = GCHandle.Alloc(p, GCHandleType.Pinned);
                copy.dstHost       = handle.AddrOfPinnedObject();
                copy.srcArray      = arr.CUArray;
                copy.srcMemoryType = CUMemoryType.Array;
                copy.dstMemoryType = CUMemoryType.Host;
                copy.Height        = 1;
                copy.WidthInBytes  = 4;
                copy.srcXInBytes   = (int)pixel.X * 4;
                copy.srcY          = (int)pixel.Y;

                arr.CopyData(copy);

                _graphicsres.UnmapAllResources();
                arr.Dispose();

                handle.Free();
                d3dimage.Unlock();
            }
            SetValue(ColorOfPixelProperty, Color.FromArgb(p[0].w, p[0].z, p[0].y, p[0].x));
            SetValue(PixelCoordinateProperty, pixel);

            if (_clicked)
            {
                _viewShiftX += (-_lastX + pX) / _scaleFac * _projFac;
                _viewShiftY += (-_lastY + pY) / _scaleFac * _projFac;
                _lastX       = pX;
                _lastY       = pY;

                SlimDX.Matrix matScale   = SlimDX.Matrix.Scaling(_scaleFac, _scaleFac, 1);
                float         shiftScale = Math.Min((float)ActualWidthDpi, (float)ActualHeightDpi);
                SlimDX.Matrix matTrans   = SlimDX.Matrix.Translation(_viewShiftX / shiftScale * _scaleFac, _viewShiftY / shiftScale * _scaleFac, 0);

                SlimDX.Matrix mat = matScale * matTrans;

                SlimDX.Matrix rotMat = new SlimDX.Matrix();
                switch (_rotation)
                {
                case Rotation._0:
                    rotMat = SlimDX.Matrix.RotationZ(0);
                    break;

                case Rotation._90:
                    rotMat = SlimDX.Matrix.RotationZ((float)(90.0 / 180.0 * Math.PI));
                    break;

                case Rotation._180:
                    rotMat = SlimDX.Matrix.RotationZ((float)(180.0 / 180.0 * Math.PI));
                    break;

                case Rotation._270:
                    rotMat = SlimDX.Matrix.RotationZ((float)(270.0 / 180.0 * Math.PI));
                    break;
                }

                _device.SetTransform(TransformState.View, rotMat * mat);

                updateFrame();
            }
        }