Beispiel #1
0
        private void ReadFromImpl(object rhs, int nElements)
        {
            if (nElements != Buffer.size)
            {
                // need to reallocate device memory
                if (Buffer.pointer != 0)
                {
                    Free(Buffer);
                }

                Buffer.size = (uint)nElements;
                Alloc(Buffer);
            }

            MemoryBuffer rhsBuf;

            switch (mathDomain)
            {
            case MathDomain.Null:
                throw new ArgumentNullException();

            case MathDomain.Int:
                fixed(int *rhsPtr = (int[])rhs)
                {
                    Debug.Assert(rhsPtr != null);
                    rhsBuf = new MemoryBuffer((PtrT)rhsPtr, (uint)nElements, memorySpace, mathDomain);
                }

                break;

            case MathDomain.Float:
                fixed(float *rhsPtr = (float[])rhs)
                {
                    Debug.Assert(rhsPtr != null);
                    rhsBuf = new MemoryBuffer((PtrT)rhsPtr, (uint)nElements, memorySpace, mathDomain);
                }

                break;

            case MathDomain.Double:
                fixed(double *rhsPtr = (double[])rhs)
                {
                    Debug.Assert(rhsPtr != null);
                    rhsBuf = new MemoryBuffer((PtrT)rhsPtr, (uint)nElements, memorySpace, mathDomain);
                }

                break;

            default:
                throw new NotImplementedException();
            }

            MemoryManagerApi.AutoCopy(Buffer, rhsBuf);
        }
Beispiel #2
0
        internal static void Alloc(MemoryBuffer buffer)
        {
            Debug.Assert(buffer.pointer == 0);

            switch (buffer.memorySpace)
            {
            case MemorySpace.Null:
                throw new ArgumentNullException();

            case MemorySpace.Host:
                MemoryManagerApi.AllocHost(buffer);
                break;

            case MemorySpace.Device:
                MemoryManagerApi.Alloc(buffer);
                break;

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #3
0
        public virtual T[] GetRaw <T>() where T : struct, IEquatable <T>, IFormattable
        {
            Debug.Assert(Buffer.pointer != 0);

            // prepare a buffer host-side
            MemoryBuffer hostBuffer = new MemoryBuffer(0, (uint)Buffer.size, MemorySpace.Host, mathDomain);

            MemoryManagerApi.AllocHost(hostBuffer);
            MemoryManagerApi.AutoCopy(hostBuffer, Buffer);

            object ret = null;

            switch (mathDomain)
            {
            case MathDomain.Null:
                throw new ArgumentNullException();

            case MathDomain.Int:
                ret = new int[hostBuffer.size];
                Marshal.Copy((IntPtr)hostBuffer.pointer, (int[])ret, 0, (int)hostBuffer.size);
                break;

            case MathDomain.Float:
                ret = new float[hostBuffer.size];
                Marshal.Copy((IntPtr)hostBuffer.pointer, (float[])ret, 0, (int)hostBuffer.size);
                break;

            case MathDomain.Double:
                ret = new double[hostBuffer.size];
                Marshal.Copy((IntPtr)hostBuffer.pointer, (double[])ret, 0, (int)hostBuffer.size);
                break;

            default:
                throw new NotImplementedException();
            }


            return((T[])Convert.ChangeType(ret, typeof(T[])));
        }
Beispiel #4
0
        private static void Free(MemoryBuffer buffer)
        {
            Debug.Assert(buffer.pointer != 0);
            switch (buffer.memorySpace)
            {
            case MemorySpace.Null:
                throw new ArgumentNullException();

            case MemorySpace.Host:
                MemoryManagerApi.FreeHost(buffer);
                break;

            case MemorySpace.Device:
                MemoryManagerApi.Free(buffer);
                break;

            default:
                throw new NotImplementedException();
            }

            buffer.pointer = 0;
        }
Beispiel #5
0
 internal void ReadFrom(ContiguousMemoryBuffer rhs)
 {
     Debug.Assert(Buffer.pointer != 0);
     Debug.Assert(rhs.Buffer.pointer != 0);
     MemoryManagerApi.AutoCopy(Buffer, rhs.Buffer);
 }