Beispiel #1
0
        internal TPointer GetArrayPointer()
        {
            if (!Mapper.IsAllocated(m_array))
            {
                lock (PinnedArrayTracker.ExecuteLock)
                {
                    m_array = NewFromEmpty((ulong)m_size);
                    if (m_data == null)
                    {
                        //Console.WriteLine("Creating data in C land");

                        // No CIL allocation, create a new multi-array
                        m_dirty = true;
                    }
                    else
                    {
                        //Console.WriteLine("Wraping CIL data");

                        // Existing CIL allocation, wrap it
                        Mapper.SetData(m_array, PinnedArrayTracker.CreatePinnedArray(m_data));
                    }
                }
            }

            return(m_array);
        }
        /// <summary>
        /// Releases the memory reserved by the pointer
        /// </summary>
        public static void Dispose(TPointer self)
        {
            try
            {
                if (Mapper.IsAllocated(self))
                {
                    //Console.WriteLine("Destroying multi_array {0}", this.pointer.ToInt64());

                    var data = GetData(self);
                    lock (PinnedArrayTracker.ExecuteLock)
                    {
                        if (PinnedArrayTracker.DecReference(data))
                        {
                            Mapper.SetData(self, IntPtr.Zero);
                        }

                        Mapper.Destroy(self);
                    }
                }
            }
            finally
            {
                self.Pointer = IntPtr.Zero;
            }
        }
Beispiel #3
0
        private void Sync()
        {
            if (m_dirty)
            {
                // Console.WriteLine("Calling sync");
                if (!Mapper.IsAllocated(m_array))
                {
                    throw new InvalidOperationException("Array cannot be dirty and not allocated");
                }

                Mapper.Sync(m_array);
                PinnedArrayTracker.Release();
                m_dirty = false;
            }
        }
        /// <summary>
        /// Sets the data pointer
        /// </summary>
        /// <param name="self">Self.</param>
        /// <param name="value">Value.</param>
        public static void SetData(TPointer self, IntPtr value)
        {
            var prevPtr = GetData(self);

            if (prevPtr != value)
            {
                PinnedArrayTracker.DecReference(prevPtr);
                PinnedArrayTracker.IncReference(value);
                lock (PinnedArrayTracker.ExecuteLock)
                {
                    Mapper.SetData(self, value);

                    /*if (PinnedArrayTracker.IsManagedData(value))
                     *      Mapper.SetExternal(self, true);*/
                }
            }
        }
Beispiel #5
0
        public void Allocate()
        {
            if (Mapper.IsAllocated(m_array))
            {
                if (m_data == null)
                {
                    //Console.WriteLine("Copy data from C land");

                    //In this case, data resides in C land,
                    // so we copy it back to CIL and free it in C land
                    m_data = new TData[m_size];

                    using (var t = NewFromEmpty((ulong)m_size))
                    {
                        Mapper.SetData(t, PinnedArrayTracker.CreatePinnedArray(m_data));
                        Mapper.Copy(t, m_array);
                        Mapper.Sync(t);
                        m_array.Dispose();
                        PinnedArrayTracker.Release();
                    }

                    m_dirty = false;
                }
                else
                {
                    //Data resides in CIL so detach the data and release the array
                    //Console.WriteLine("Release data in C land");
                    Sync();
                    Mapper.SetData(m_array, IntPtr.Zero);
                    m_array.Dispose();
                }
            }
            else
            {
                //Console.WriteLine("Accessed array that was never in any operation");
                Sync();
                if (m_data == null)
                {
                    m_data = new TData[m_size];
                }
            }
        }
        /// <summary>
        /// Creates a new view
        /// </summary>
        public static TPointer CreateNewView(NumCIL.Generic.NdArray <TData> a)
        {
            var ac = a.DataAccessor;

            lock (PinnedArrayTracker.ExecuteLock)
            {
                if (ac is BohriumDataAccessorBase <TData, TPointer, TAccess> )
                {
                    var acp = ((BohriumDataAccessorBase <TData, TPointer, TAccess>)a.DataAccessor).GetArrayPointer();


                    return(Mapper.NewView(
                               acp,
                               (ulong)a.Shape.Dimensions.Length,
                               a.Shape.Offset,
                               a.Shape.Dimensions.Select(x => x.Length).ToArray(),
                               a.Shape.Dimensions.Select(x => x.Stride).ToArray()
                               ));
                }
                else
                {
                    TPointer res;
                    using (var acp = Mapper.NewEmpty((ulong)a.DataAccessor.Length))
                    {
                        res = Mapper.NewView(
                            acp,
                            (ulong)a.Shape.Dimensions.Length,
                            a.Shape.Offset,
                            a.Shape.Dimensions.Select(x => x.Length).ToArray(),
                            a.Shape.Dimensions.Select(x => x.Stride).ToArray()
                            );
                    }

                    Mapper.SetData(res, PinnedArrayTracker.CreatePinnedArray(a.DataAccessor.AsArray()));
                    return(res);
                }

                //Console.WriteLine("Created multi_array from NdArray: {0}", this.pointer.ToInt64());
            }
        }
Beispiel #7
0
 /// <summary>
 /// Flushes pending operations in the VEM, note that this does not flush all pending instructions
 /// </summary>
 public static void Flush()
 {
     PinnedArrayTracker.ReleaseInternal();
 }