Backends/SiaNet.Backend.TensorSharp/Cuda/ContextState/PoolingDeviceAllocator.cs
/// <summary>
/// Allocates the specified byte count.
/// </summary>
/// <param name="byteCount">The byte count.</param>
/// <returns>IDeviceMemory.</returns>
Allocates the specified byte count.
Allocates the specified byte count .
public IDeviceMemory Allocate(long byteCount)
        {
            var size = PadToAlignment(byteCount, MemoryAlignment);
            Queue<IDeviceMemory> sizedPool;
            if (pools.TryGetValue(size, out sizedPool))
            {
                if (sizedPool.Count > 0)
                {
                    var result = sizedPool.Dequeue();
                    if(result != null)
                        return result;
                }
            }
            else
            {
                sizedPool = new Queue<IDeviceMemory>();
                pools.Add(size, sizedPool);
            }
            var buffer = context.AllocateMemory(size);
            BasicDeviceMemory devMemory = null;
            devMemory = new BasicDeviceMemory(buffer, () =>
            {
                sizedPool.Enqueue(devMemory);
            });
            return devMemory;
        }
public IDeviceMemory Allocate ( long byteCount ) { var size = PadToAlignment ( byteCount , MemoryAlignment ) ; Queue < IDeviceMemory > sizedPool ; if ( pools . TryGetValue ( size , out sizedPool ) ) { if ( sizedPool . Count > 0 ) { var result = sizedPool . Dequeue ( ) ; if ( result != null ) return result ; } } else { sizedPool = new Queue < IDeviceMemory > ( ) ; pools . Add ( size , sizedPool ) ; } var buffer = context . AllocateMemory ( size ) ; BasicDeviceMemory devMemory = null ; devMemory = new BasicDeviceMemory ( buffer , ( ) => { sizedPool . Enqueue ( devMemory ) ; } ) ; return devMemory ; }
Allocates the specified byte count.
Allocates the specified byte count .
// HACK  bizarrely, Queue.Dequeue appears to sometimes return null, even when there are many elements in the queue, // and when the queue is only ever accessed from one thread. // If control flow gets to this point, sizedPool exists in the dictionary and is empty.
None
byteCount
long
The byte count.
The byte count .