Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Misc/ILScope.cs @ 9407

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

#1967: ILNumerics source for experimentation

File size: 9.7 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.Generic;
42using System.Linq;
43using System.Text;
44using System.Threading;
45
46namespace ILNumerics {
47    /// <summary>
48    /// An artificial scope class, used by the ILNumerics memory management
49    /// </summary>
50    public class ILScope : IDisposable {
51
52        internal class ILThreadingContext {
53            public Stack<ILBaseArray> Arrays = new Stack<ILBaseArray>(100);
54            public Stack<ILScope> Scopes = new Stack<ILScope>(100);
55
56            public void RegisterArray(ILBaseArray A) {
57                if (Scopes.Count > 0) /* && A.EnterScope() ) */ { //ho: commented out, because otherwise common local arrays do return false and hence get not registered in implicit conversions here!
58                    Arrays.Push(A);
59                    Scopes.Peek().Count++;
60                }
61            }
62        }
63        [ThreadStatic]
64        private static ILThreadingContext s_threadContext;
65        /// <summary>
66        /// Begins an artificial scope block within a local function block
67        /// </summary>
68        /// <param name="inputArrays">Any <b>input</b> arry, given as parameter for the current function</param>
69        /// <returns>A new scope</returns>
70        /// <remarks><para>The <c>ILScope</c> class plays an important role for the ILNumerics memory management. When writing functions in ILNumerics,
71        /// <c>ILScope</c> is used, to define blocks of artificial scopes for local function blocks. ILNumerics ensures, no memory is left as garbage, once
72        /// such a scope block was left. Furthermore, it garantees, input arrays are kept alive during the execution of the block. By following these
73        /// <a href="http://ilnumerics.net/$GeneralRules.html" target="ILMain">simple rules</a>, ILNumerics is able to optimize the execution of the algorithm regarding
74        /// execution speed and memory footprint.</para></remarks>
75        /// <example><para>The examples demonstrates a custom function in ILNumerics. It demonstrates the use of distinct array types in the function declaration and the use of
76        /// artificial scopes.</para>
77        /// <code><![CDATA[ILRetArray<double> FreqPeaks(ILInArray<double> inData, ILOutArray<double> freq = null, double sampFreq = 44.1) {
78        ///
79        ///    using (ILScope.Enter(inData)) {   
80        ///             
81        ///        ILArray<double> Data = check(inData);
82        ///        ILArray<double> retLength = min(ceil(Data.Length / 2.0 + 1), 5.0);   
83        ///        ILArray<double> Window = stdWindowFunc(Data.Length); 
84        ///        ILArray<double> magnitudes = abs(fft(Data * Window)); 
85        ///        magnitudes = magnitudes[r(0,end / 2 + 1)]; 
86        ///
87        ///        ILArray<double> indices = empty(); 
88        ///        ILArray<double> sorted = sort(magnitudes, indices, descending:true); 
89        ///        if (!isnull(freq))
90        ///            freq.a = (sampFreq / 2.0 / magnitudes.Length * indices)[r(0,retLength-1)]; 
91        ///        return magnitudes[r(0,retLength-1)]; 
92        ///    }
93        ///}]]></code>
94        /// </example>
95        public static IDisposable Enter(params ILBaseArray[] inputArrays) {
96            return new ILScope(inputArrays);
97        }
98        /// <summary>
99        /// Begins an artificial scope block within a local function block
100        /// </summary>
101        /// <param name="inputArrays">Any <b>input</b> arry, given as parameter for the current function</param>
102        /// <returns>A new scope</returns>
103        /// <remarks><para>The <c>ILScope</c> class plays an important role for the ILNumerics memory management. When writing functions in ILNumerics,
104        /// <c>ILScope</c> is used, to define blocks of artificial scopes for local function blocks. ILNumerics ensures, no memory is left as garbage, once
105        /// such a scope block was left. Furthermore, it garantees, input arrays are kept alive during the execution of the block. By following these
106        /// <a href="http://ilnumerics.net/$GeneralRules.html" target="ILMain">simple rules</a>, ILNumerics is able to optimize the execution of the algorithm regarding
107        /// execution speed and memory footprint.</para></remarks>
108        /// <example><para>The examples demonstrates a custom function in ILNumerics. It demonstrates the use of distinct array types in the function declaration and the use of
109        /// artificial scopes.</para>
110        /// <code><![CDATA[ILRetArray<double> FreqPeaks(ILInArray<double> inData, ILOutArray<double> freq = null, double sampFreq = 44.1) {
111        ///
112        ///    using (ILScope.Enter(inData)) {   
113        ///             
114        ///        ILArray<double> Data = check(inData);
115        ///        ILArray<double> retLength = min(ceil(Data.Length / 2.0 + 1), 5.0);   
116        ///        ILArray<double> Window = stdWindowFunc(Data.Length); 
117        ///        ILArray<double> magnitudes = abs(fft(Data * Window)); 
118        ///        magnitudes = magnitudes[r(0,end / 2 + 1)]; 
119        ///
120        ///        ILArray<double> indices = empty(); 
121        ///        ILArray<double> sorted = sort(magnitudes, indices, descending:true); 
122        ///        if (!isnull(freq))
123        ///            freq.a = (sampFreq / 2.0 / magnitudes.Length * indices)[r(0,retLength-1)]; 
124        ///        return magnitudes[r(0,retLength-1)]; 
125        ///    }
126        ///}]]></code>
127        /// </example>
128        public static IDisposable Enter(ILBaseArray inputArrays) {
129            return new ILScope(inputArrays);
130        }
131        /// <summary>
132        /// The threading context - individual for each thread
133        /// </summary>
134        internal static ILThreadingContext Context {
135            get {
136                ILThreadingContext ret = s_threadContext;
137                if (ret == null) {
138                    s_threadContext = new ILThreadingContext();
139                    ret = s_threadContext;
140                }
141                return ret;
142            }
143        }
144
145        internal int Count = 0;
146
147        internal ILScope(ILBaseArray array) {
148             if (!object.Equals(array,null)) {
149                Stack<ILBaseArray> stack = Context.Arrays;
150                int oldStackCount = stack.Count;
151                if (array.EnterScope()) {
152                    stack.Push(array);
153                }
154                Count = stack.Count - oldStackCount;
155            }
156            Context.Scopes.Push(this);
157        }
158        internal ILScope(params ILBaseArray[] inputArrays) {
159            if (inputArrays != null) {
160                Stack<ILBaseArray> stack = Context.Arrays;
161                int oldStackCount = stack.Count;
162                foreach (var a in inputArrays) {
163                    if (!object.Equals(a, null)) {
164                        if (a.EnterScope()) {
165                            stack.Push(a);
166                        }
167                    }
168                }
169                Count = stack.Count - oldStackCount;
170            }
171            Context.Scopes.Push(this);
172           
173        }
174        #region IDisposable Members
175
176        /// <summary>
177        /// Dispose all arrays in this scope
178        /// </summary>
179        public void Dispose() {
180            Stack<ILBaseArray> stack = Context.Arrays;
181            for (int i = Count; i-- > 0; ) {
182                stack.Pop().LeaveScope();
183            }
184#if VERBOSE
185            System.Diagnostics.Debug.Assert(object.Equals(Context.Scopes.Peek(), this));
186            System.Diagnostics.Debug.WriteLine ("Leaving scope {0} - disposing {1} arrays",Context.Scopes.Peek().GetHashCode(),Count);
187#endif
188            Context.Scopes.Pop();
189        }
190
191        #endregion
192
193    }
194}
Note: See TracBrowser for help on using the repository browser.