コード例 #1
0
ファイル: Tensor.cs プロジェクト: johnsaad/emgutf
        /// <summary>
        /// Get a copy of the tensor data as a managed array
        /// </summary>
        /// <param name="jagged">If true, return a jagged array, otherwise, a flatten single dimension array</param>
        /// <returns>A copy of the tensor data as a managed array</returns>
        public Array GetData(bool jagged = true)
        {
            DataType type = this.Type;
            Type     t    = TfInvoke.GetNativeType(type);

            if (t == null)
            {
                return(null);
            }

            Array array;
            int   byteSize = ByteSize;

            if (jagged)
            {
                int[] dim = this.Dim;
                array = Array.CreateInstance(t, dim);
            }
            else
            {
                int len = byteSize / Marshal.SizeOf(t);
                array = Array.CreateInstance(t, len);
            }

            /*
             * int[] dim = Dim;
             * int byteSize = ByteSize;
             * Array array;
             *
             * if (dim.Length == 0)
             * {
             *  int len = byteSize/ Marshal.SizeOf(t);
             *  array = Array.CreateInstance(t, len);
             * } else if (jagged)
             *  array = Array.CreateInstance(t, dim);
             * else
             * {
             *  int len = 0;
             *  if (dim.Length > 0)
             *      len = dim[0];
             *  for (int i = 1; i < dim.Length; i++)
             *      len *= dim[i];
             *  array = Array.CreateInstance(t, len);
             * }*/
            GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);

            TfInvoke.tfeMemcpy(handle.AddrOfPinnedObject(), DataPointer, byteSize);
            handle.Free();
            return(array);
        }