예제 #1
0
        /// <summary>
        /// Adds two matrices on the GPU
        /// </summary>
        /// <param name="one">The first matrix</param>
        /// <param name="two">The second matrix</param>
        /// <returns>The result of the addition</returns>
        public BufferedFastMatrix <T> Add(BufferedFastMatrix <T> one, BufferedFastMatrix <T> two)
        {
            if (one == null || two == null)
            {
                throw new ArgumentNullException();
            }
            if ((one.Rows != two.Rows) || (one.Columns != two.Columns))
            {
                throw new BadDimensionException(one.Rows, one.Columns, two.Rows,
                                                two.Columns);
            }

            MemoryBuffer2D <T> resultBuffer;

            one.CopyToGPU();
            two.CopyToGPU();

            resultBuffer = accelerator.Allocate <T>(one.Rows, one.Columns);

            one.WaitForCopy(); //this function call is currently not required,
                               //will come up with a better solution later but for now I'm just
                               //gonna leave it here
            two.WaitForCopy();

            GPUAddKernel(resultBuffer.Extent, one.buffer.View, two.buffer.View, resultBuffer.View);
            accelerator.Synchronize();

            var tempArray = resultBuffer.GetAs2DArray();

            accelerator.Synchronize();

            BufferedFastMatrix <T> returnMatrix = new BufferedFastMatrix <T>(tempArray);

            return(returnMatrix);
        }
예제 #2
0
        /// <summary>
        /// Transposes a matrix on the GPU
        /// </summary>
        /// <param name="matrix">The matrix</param>
        /// <returns>The transposed matrix</returns>
        public BufferedFastMatrix <T> Transpose(BufferedFastMatrix <T> matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException();
            }
            Accelerator        accelerator;
            MemoryBuffer2D <T> resultBuffer;

            matrix.CopyToGPU();

            accelerator  = HardwareAcceleratorManager.GPUAccelerator;
            resultBuffer = accelerator.Allocate <T>(matrix.Columns, matrix.Rows);
            var kernel = GPUTransposeKernel;

            matrix.WaitForCopy();

            kernel(resultBuffer.Extent, matrix.buffer.View, resultBuffer.View);
            accelerator.Synchronize();

            var tempArray = resultBuffer.GetAs2DArray();

            accelerator.Synchronize();

            BufferedFastMatrix <T> returnMatrix = new BufferedFastMatrix <T>(tempArray);

            return(returnMatrix);
        }
예제 #3
0
        /// <summary>
        /// Subtracts two matrices on the GPU
        /// </summary>
        /// <param name="one">The first matrix</param>
        /// <param name="two">The second matrix</param>
        /// <returns>The result of the subtraction (one - two) </returns>
        public BufferedFastMatrix <T> Subtract(BufferedFastMatrix <T> one, BufferedFastMatrix <T> two)
        {
            if (one == null || two == null)
            {
                throw new ArgumentNullException();
            }
            if ((one.Rows != two.Rows) || (one.Columns != two.Columns))
            {
                throw new BadDimensionException(one.Rows, one.Columns, two.Rows,
                                                two.Columns);
            }

            MemoryBuffer2D <T> resultBuffer;

            //start tasks
            one.CopyToGPU();
            two.CopyToGPU();

            resultBuffer = accelerator.Allocate <T>(one.Rows, one.Columns);

            one.WaitForCopy();
            two.WaitForCopy();

            GPUSubKernel(resultBuffer.Extent, one.buffer.View, two.buffer.View, resultBuffer.View);

            accelerator.Synchronize();

            var tempArray = resultBuffer.GetAs2DArray();

            accelerator.Synchronize();

            BufferedFastMatrix <T> returnMatrix = new BufferedFastMatrix <T>(tempArray);

            return(returnMatrix);
        }
예제 #4
0
        public unsafe BufferedFastMatrix <T> AddShared(BufferedFastMatrix <T> one, BufferedFastMatrix <T> two)
        {
            if (one == null || two == null)
            {
                throw new ArgumentNullException();
            }
            if ((one.Rows != two.Rows) || (one.Columns != two.Columns))
            {
                throw new BadDimensionException(one.Rows, one.Columns, two.Rows,
                                                two.Columns);
            }

            MemoryBuffer2D <T> resultBuffer;

            one.CopyToGPU();
            two.CopyToGPU();

            resultBuffer = accelerator.Allocate <T>(one.Rows, one.Columns);

            one.WaitForCopy();
            two.WaitForCopy();

            KernelConfig config = new KernelConfig(accelerator.MaxGridSize.X, accelerator.MaxNumThreadsPerGroup);

            AddSharedKernel(config, one.buffer.View, two.buffer.View, resultBuffer);
            Console.WriteLine(accelerator.MaxGridSize.X);
            accelerator.Synchronize();

            var tempArray = resultBuffer.GetAs2DArray();

            accelerator.Synchronize();

            BufferedFastMatrix <T> returnMatrix = new BufferedFastMatrix <T>(tempArray);

            return(returnMatrix);
        }