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); }
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()); } }