public IDeviceMemory Allocate(long byteCount)
        {
            long size = PadToAlignment(byteCount, MemoryAlignment);

            lock (locker)
            {
                //   allocatedSize += size;
                if (pools.TryGetValue(size, out Queue <IDeviceMemory> sizedPool))
                {
                    if (sizedPool.Count > 0)
                    {
                        IDeviceMemory result = sizedPool.Dequeue();

                        // 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 (result != null)
                        {
                            return(result);
                        }
                    }
                }
                else
                {
                    sizedPool = new Queue <IDeviceMemory>();
                    pools.Add(size, sizedPool);
                }

                CUdeviceptr buffer;
                try
                {
                    try
                    {
                        // If control flow gets to this point, sizedPool exists in the dictionary and is empty.
                        context.SetCurrent();
                        buffer = context.AllocateMemory(size);
                    }
                    catch (ManagedCuda.CudaException)
                    {
                        FreeMemory(false);
                        buffer = context.AllocateMemory(size);
                    }
                }
                catch (ManagedCuda.CudaException)
                {
                    FreeMemory(true);
                    buffer = context.AllocateMemory(size);
                }

                BasicDeviceMemory devMemory = null;
                devMemory = new BasicDeviceMemory(buffer, () =>
                {
                    lock (locker)
                    {
                        sizedPool.Enqueue(devMemory);
                    }
                });

                return(devMemory);
            }
        }
        /// <summary>
        /// Allocates the specified byte count.
        /// </summary>
        /// <param name="byteCount">The byte count.</param>
        /// <returns>IDeviceMemory.</returns>
        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();

                    // 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(result != null)
                        return result;
                }
            }
            else
            {
                sizedPool = new Queue<IDeviceMemory>();
                pools.Add(size, sizedPool);
            }

            // If control flow gets to this point, sizedPool exists in the dictionary and is empty.

            var buffer = context.AllocateMemory(size);
            BasicDeviceMemory devMemory = null;
            devMemory = new BasicDeviceMemory(buffer, () =>
            {
                sizedPool.Enqueue(devMemory);
            });

            return devMemory;
        }
Пример #3
0
        public IDeviceMemory Allocate(long byteCount)
        {
            ulong size = PadToAlignment(byteCount, MemoryAlignment);

            lock (locker)
            {
                CUdeviceptr buffer = AllocateMemory(size);

                BasicDeviceMemory devMemory = null;
                devMemory = new BasicDeviceMemory(buffer, () =>
                {
                    lock (locker)
                    {
                        m_usedAddr2Size.Remove(devMemory.Pointer.Pointer);
                    }
                });

                return(devMemory);
            }
        }