Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Misc/ILMemoryPool.cs @ 9426

Last change on this file since 9426 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 8.1 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections;
42using System.Collections.Generic;
43using System.Text;
44using ILNumerics.Exceptions;
45using ILNumerics.Misc;
46
47namespace ILNumerics {
48    /// <summary>
49    /// The ILNumerics Memory Pool is the heart of the ILNumerics memory management.
50    /// </summary>
51    /// <remarks>The pool reduces the pressure on the systems memory done by larger objects.
52    /// <para>Arrays created in ILNumerics will try to reclaim its memory from this pool. If attempt fails, the memory is gathered from the managed heap normally.</para>
53    /// <para>Disposed array objects register their underlying storage in the pool for later reusing. The process is triggered by a deterministic disposal
54    /// pattern in conjunction with <a href="http://ilnumerics.net/$Arrays.html">ILNumerics array types</a> and <a href="http://ilnumerics.net/$FunctionRules.html">ILNumerics Function Rules</a>.</para></remarks>
55    /// <see cref=""/>
56    public sealed class ILMemoryPool {
57        /// <summary>
58        /// The only global ILMemoryPool instance
59        /// </summary>
60        public static readonly ILMemoryPool Pool = new ILMemoryPool();
61        internal static readonly Dictionary<Type,IILMemoryPool> Pools = new Dictionary<Type,IILMemoryPool>();
62        ///// <summary>
63        ///// todo: remove!!
64        ///// </summary>
65        ///// <typeparam name="T"></typeparam>
66        ///// <returns></returns>
67        //public void SetIncrementLenghtPercent<T>(double value) {
68        //    ILMemoryPoolInternal<T>.Pool.MaxRequestedLengthIncrease = value;
69        //}
70        //public double GetIncrementLenghtPercent<T>()
71        //{
72        //    return ILMemoryPoolInternal<T>.Pool.MaxRequestedLengthIncrease;
73        //}
74        internal long MinArrayLength<T>()
75        {
76            return ILMemoryPoolInternal<T>.Pool.MinArrayLength;
77        }
78        //public void SetMinArrayLength<T>(long value) {
79        //    ILMemoryPoolInternal<T>.Pool.MinArrayLength = value;
80        //}
81        /// <summary>
82        /// Get new array of type T from memory pool.
83        /// </summary>
84        /// <typeparam name="T">Element type</typeparam>
85        /// <param name="length">Size of T[]</param>
86        /// <param name="clear">If true, set the elements in T[] to default(T)</param>
87        /// <param name="cleared">Always true if <paramref name="clear"/> was set. Otherwise this will be true if the array was newly created instead of being recycled from the pool. False otherwise.</param>
88        /// <remarks><para>If the pool contains an unused matching element of sufficient size this element will be returned. If <paramref name="clear"/> was false,
89        /// the result might still contain the old data.</para>
90        /// <para>If the pool does not contain a matching element a new one is created.</para>
91        /// <para><b>Note:</b> The returned array may be larger than requested if it was recycled from the pool.</para></remarks>
92        /// <returns>An array of type T of at least length <paramref name="length"/></returns>
93        public T[] New<T>(long length, bool clear, out bool cleared) {
94            return ILMemoryPoolInternal<T>.Pool.New(length, clear, out cleared);
95        }
96
97        /// <summary>
98        /// Get a new array of type T from the memory pool
99        /// </summary>
100        /// <typeparam name="T">Element type</typeparam>
101        /// <param name="length">Required minimum length</param>
102        /// <remarks>The returned array is only guaranteed to have at least length <paramref name="length"/>. If it is recycled from the pool it may be longer.
103        /// There is no guarantee on the values contained in the returned array. To force these to have a default value use <see cref="ILMemoryPool.New&lt;T&gt;(long length, bool clear, out bool cleared)"/></remarks>
104        /// <returns>An array of type T of at least length <paramref name="length"/>.</returns>
105        public T[] New<T>(long length)
106        {
107            return ILMemoryPoolInternal<T>.Pool.New(length);
108        }
109
110        /// <summary>
111        /// Return a array of type T that is not needed anymore to the pool.
112        /// </summary>
113        /// <typeparam name="T">Element type</typeparam>
114        /// <param name="array">The array to add to the pool</param>
115        /// <remarks>You may also "free" objects not retrieved from the pool, in which case they are registered in the pool.</remarks>
116        public void Free<T>(T[] array) {
117            ILMemoryPoolInternal<T>.Pool.Free(array);
118        }
119
120       
121        /// <summary>
122        /// Get maximal potential size of the memory pool for objects of type T
123        /// </summary>
124        /// <typeparam name="T">Element type</typeparam>
125        /// <returns>Maximal number of bytes in pool</returns>
126        public long MaxBytes<T>() {
127            return ILMemoryPoolInternal<T>.Pool.MaxBytes;
128        }
129
130        /// <summary>
131        /// Reset &amp; reconfigure the pool
132        /// </summary>
133        /// <param name="MinPoolArrayLen">Minimum length for array object to be stored inside the pool</param>
134        /// <param name="maxSizeMB">Overall size the memory pool consumes at most</param>
135        /// <remarks>Reset will dispose all objects currently hold in the pool!</remarks>
136        public void Reset<T>(long MinPoolArrayLen, int maxSizeMB) {
137            ILMemoryPoolInternal<T>.Pool.Reset(MinPoolArrayLen, maxSizeMB);
138        }
139
140        /// <summary>
141        /// Give information about pool state
142        /// </summary>
143        /// <param name="shortVersion">true (default): abbreviate infos to: current MB in Pool, reclaimed MB for livetime, reclaimed objects for livetime. False: give full info</param>
144        /// <returns>Infos about current pool state</returns>
145        public string Info(bool shortVersion = true) {
146            if (Pools.ContainsKey(typeof(double))) {
147                return Pools[typeof(double)].Info(shortVersion);
148            } else if (Pools.Count > 0) {
149                foreach (IILMemoryPool pool in Pools.Values) {
150                    return pool.Info(shortVersion);
151                }
152            }
153            return "no pool found";
154        }
155
156    }
157}
Note: See TracBrowser for help on using the repository browser.