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 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics;
|
---|
44 | using ILNumerics.Exceptions;
|
---|
45 | using ILNumerics.Storage;
|
---|
46 | using ILNumerics.Misc;
|
---|
47 |
|
---|
48 |
|
---|
49 | namespace ILNumerics {
|
---|
50 |
|
---|
51 | public partial class ILMath {
|
---|
52 |
|
---|
53 | |
---|
54 |
|
---|
55 | |
---|
56 | #region HYCALPER AUTO GENERATED CODE
|
---|
57 | |
---|
58 | /// <summary>Round to nearest integer</summary>
|
---|
59 | /// <param name="A">Input array</param>
|
---|
60 | /// <returns>Array of same size as A with elements rounded to nearest integer</returns>
|
---|
61 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
62 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
63 | public unsafe static ILRetArray<complex> round (ILInArray< complex > A) {
|
---|
64 | using (ILScope.Enter(A)) {
|
---|
65 | if (A.IsEmpty)
|
---|
66 | return new ILRetArray<complex>(A.Size);
|
---|
67 | ILSize inDim = A.Size;
|
---|
68 | complex[] arrA = A.GetArrayForRead();
|
---|
69 | complex [] retArr;
|
---|
70 | int outLen = inDim.NumberOfElements;
|
---|
71 | bool inplace = true;
|
---|
72 |
|
---|
73 | if (!A.TryGetStorage4InplaceOp(out retArr)) {
|
---|
74 | retArr = ILMemoryPool.Pool.New<complex>(outLen);
|
---|
75 | inplace = false;
|
---|
76 | }
|
---|
77 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
78 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
79 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
80 | workItemLength = outLen / workItemCount;
|
---|
81 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
82 | } else {
|
---|
83 | workItemLength = outLen / 2;
|
---|
84 | workItemCount = 2;
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | workItemLength = outLen;
|
---|
88 | workItemCount = 1;
|
---|
89 | }
|
---|
90 | ILDenseStorage<complex> retStorage = new ILDenseStorage<complex>(retArr, inDim);
|
---|
91 |
|
---|
92 | Action<object> worker = data => {
|
---|
93 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
94 |
|
---|
95 | complex* cp = ((complex*)range.Item4 + range.Item1);
|
---|
96 | int len = range.Item2;
|
---|
97 | if (range.Item5) {
|
---|
98 | // inplace
|
---|
99 | while (len > 20) {
|
---|
100 | cp[0] = complex.Round(cp[0] ) /*dummy*/;
|
---|
101 | cp[1] = complex.Round(cp[1] ) /*dummy*/;
|
---|
102 | cp[2] = complex.Round(cp[2] ) /*dummy*/;
|
---|
103 | cp[3] = complex.Round(cp[3] ) /*dummy*/;
|
---|
104 | cp[4] = complex.Round(cp[4] ) /*dummy*/;
|
---|
105 | cp[5] = complex.Round(cp[5] ) /*dummy*/;
|
---|
106 | cp[6] = complex.Round(cp[6] ) /*dummy*/;
|
---|
107 | cp[7] = complex.Round(cp[7] ) /*dummy*/;
|
---|
108 | cp[8] = complex.Round(cp[8] ) /*dummy*/;
|
---|
109 | cp[9] = complex.Round(cp[9] ) /*dummy*/;
|
---|
110 | cp[10] = complex.Round(cp[10] ) /*dummy*/;
|
---|
111 | cp[11] = complex.Round(cp[11] ) /*dummy*/;
|
---|
112 | cp[12] = complex.Round(cp[12] ) /*dummy*/;
|
---|
113 | cp[13] = complex.Round(cp[13] ) /*dummy*/;
|
---|
114 | cp[14] = complex.Round(cp[14] ) /*dummy*/;
|
---|
115 | cp[15] = complex.Round(cp[15] ) /*dummy*/;
|
---|
116 | cp[16] = complex.Round(cp[16] ) /*dummy*/;
|
---|
117 | cp[17] = complex.Round(cp[17] ) /*dummy*/;
|
---|
118 | cp[18] = complex.Round(cp[18] ) /*dummy*/;
|
---|
119 | cp[19] = complex.Round(cp[19] ) /*dummy*/;
|
---|
120 | cp[20] = complex.Round(cp[20] ) /*dummy*/;
|
---|
121 | cp+=21; len -= 21;
|
---|
122 | }
|
---|
123 | while (len-- > 0) {
|
---|
124 | *cp = complex.Round(*cp ) /*dummy*/;
|
---|
125 | cp++;
|
---|
126 | }
|
---|
127 | } else {
|
---|
128 | complex* ap = ((complex*)range.Item3 + range.Item1);
|
---|
129 | while (len > 20) {
|
---|
130 | cp[0] = complex.Round(ap[0] ) /*dummy*/;
|
---|
131 | cp[1] = complex.Round(ap[1] ) /*dummy*/;
|
---|
132 | cp[2] = complex.Round(ap[2] ) /*dummy*/;
|
---|
133 | cp[3] = complex.Round(ap[3] ) /*dummy*/;
|
---|
134 | cp[4] = complex.Round(ap[4] ) /*dummy*/;
|
---|
135 | cp[5] = complex.Round(ap[5] ) /*dummy*/;
|
---|
136 | cp[6] = complex.Round(ap[6] ) /*dummy*/;
|
---|
137 | cp[7] = complex.Round(ap[7] ) /*dummy*/;
|
---|
138 | cp[8] = complex.Round(ap[8] ) /*dummy*/;
|
---|
139 | cp[9] = complex.Round(ap[9] ) /*dummy*/;
|
---|
140 | cp[10] = complex.Round(ap[10] ) /*dummy*/;
|
---|
141 | cp[11] = complex.Round(ap[11] ) /*dummy*/;
|
---|
142 | cp[12] = complex.Round(ap[12] ) /*dummy*/;
|
---|
143 | cp[13] = complex.Round(ap[13] ) /*dummy*/;
|
---|
144 | cp[14] = complex.Round(ap[14] ) /*dummy*/;
|
---|
145 | cp[15] = complex.Round(ap[15] ) /*dummy*/;
|
---|
146 | cp[16] = complex.Round(ap[16] ) /*dummy*/;
|
---|
147 | cp[17] = complex.Round(ap[17] ) /*dummy*/;
|
---|
148 | cp[18] = complex.Round(ap[18] ) /*dummy*/;
|
---|
149 | cp[19] = complex.Round(ap[19] ) /*dummy*/;
|
---|
150 | cp[20] = complex.Round(ap[20] ) /*dummy*/;
|
---|
151 | ap += 21;
|
---|
152 | cp += 21;
|
---|
153 | len -= 21;
|
---|
154 | }
|
---|
155 | while (len-- > 0) {
|
---|
156 | *cp = complex.Round(*ap ) /*dummy*/;
|
---|
157 | ap++;
|
---|
158 | cp++;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
162 | };
|
---|
163 |
|
---|
164 | fixed ( complex* arrAP = arrA)
|
---|
165 | fixed ( complex* retArrP = retArr) {
|
---|
166 | for (; i < workItemCount - 1; i++) {
|
---|
167 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
168 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
169 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
170 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
171 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
172 | }
|
---|
173 | // the last (or may the only) chunk is done right here
|
---|
174 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
175 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
176 |
|
---|
177 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
178 | }
|
---|
179 | return new ILRetArray<complex>(retStorage);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | /// <summary>Round to nearest integer</summary>
|
---|
183 | /// <param name="A">Input array</param>
|
---|
184 | /// <returns>Array of same size as A with elements rounded to nearest integer</returns>
|
---|
185 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
186 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
187 | public unsafe static ILRetArray<fcomplex> round (ILInArray< fcomplex > A) {
|
---|
188 | using (ILScope.Enter(A)) {
|
---|
189 | if (A.IsEmpty)
|
---|
190 | return new ILRetArray<fcomplex>(A.Size);
|
---|
191 | ILSize inDim = A.Size;
|
---|
192 | fcomplex[] arrA = A.GetArrayForRead();
|
---|
193 | fcomplex [] retArr;
|
---|
194 | int outLen = inDim.NumberOfElements;
|
---|
195 | bool inplace = true;
|
---|
196 |
|
---|
197 | if (!A.TryGetStorage4InplaceOp(out retArr)) {
|
---|
198 | retArr = ILMemoryPool.Pool.New<fcomplex>(outLen);
|
---|
199 | inplace = false;
|
---|
200 | }
|
---|
201 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
202 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
203 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
204 | workItemLength = outLen / workItemCount;
|
---|
205 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
206 | } else {
|
---|
207 | workItemLength = outLen / 2;
|
---|
208 | workItemCount = 2;
|
---|
209 | }
|
---|
210 | } else {
|
---|
211 | workItemLength = outLen;
|
---|
212 | workItemCount = 1;
|
---|
213 | }
|
---|
214 | ILDenseStorage<fcomplex> retStorage = new ILDenseStorage<fcomplex>(retArr, inDim);
|
---|
215 |
|
---|
216 | Action<object> worker = data => {
|
---|
217 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
218 |
|
---|
219 | fcomplex* cp = ((fcomplex*)range.Item4 + range.Item1);
|
---|
220 | int len = range.Item2;
|
---|
221 | if (range.Item5) {
|
---|
222 | // inplace
|
---|
223 | while (len > 20) {
|
---|
224 | cp[0] = fcomplex.Round(cp[0] ) /*dummy*/;
|
---|
225 | cp[1] = fcomplex.Round(cp[1] ) /*dummy*/;
|
---|
226 | cp[2] = fcomplex.Round(cp[2] ) /*dummy*/;
|
---|
227 | cp[3] = fcomplex.Round(cp[3] ) /*dummy*/;
|
---|
228 | cp[4] = fcomplex.Round(cp[4] ) /*dummy*/;
|
---|
229 | cp[5] = fcomplex.Round(cp[5] ) /*dummy*/;
|
---|
230 | cp[6] = fcomplex.Round(cp[6] ) /*dummy*/;
|
---|
231 | cp[7] = fcomplex.Round(cp[7] ) /*dummy*/;
|
---|
232 | cp[8] = fcomplex.Round(cp[8] ) /*dummy*/;
|
---|
233 | cp[9] = fcomplex.Round(cp[9] ) /*dummy*/;
|
---|
234 | cp[10] = fcomplex.Round(cp[10] ) /*dummy*/;
|
---|
235 | cp[11] = fcomplex.Round(cp[11] ) /*dummy*/;
|
---|
236 | cp[12] = fcomplex.Round(cp[12] ) /*dummy*/;
|
---|
237 | cp[13] = fcomplex.Round(cp[13] ) /*dummy*/;
|
---|
238 | cp[14] = fcomplex.Round(cp[14] ) /*dummy*/;
|
---|
239 | cp[15] = fcomplex.Round(cp[15] ) /*dummy*/;
|
---|
240 | cp[16] = fcomplex.Round(cp[16] ) /*dummy*/;
|
---|
241 | cp[17] = fcomplex.Round(cp[17] ) /*dummy*/;
|
---|
242 | cp[18] = fcomplex.Round(cp[18] ) /*dummy*/;
|
---|
243 | cp[19] = fcomplex.Round(cp[19] ) /*dummy*/;
|
---|
244 | cp[20] = fcomplex.Round(cp[20] ) /*dummy*/;
|
---|
245 | cp+=21; len -= 21;
|
---|
246 | }
|
---|
247 | while (len-- > 0) {
|
---|
248 | *cp = fcomplex.Round(*cp ) /*dummy*/;
|
---|
249 | cp++;
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | fcomplex* ap = ((fcomplex*)range.Item3 + range.Item1);
|
---|
253 | while (len > 20) {
|
---|
254 | cp[0] = fcomplex.Round(ap[0] ) /*dummy*/;
|
---|
255 | cp[1] = fcomplex.Round(ap[1] ) /*dummy*/;
|
---|
256 | cp[2] = fcomplex.Round(ap[2] ) /*dummy*/;
|
---|
257 | cp[3] = fcomplex.Round(ap[3] ) /*dummy*/;
|
---|
258 | cp[4] = fcomplex.Round(ap[4] ) /*dummy*/;
|
---|
259 | cp[5] = fcomplex.Round(ap[5] ) /*dummy*/;
|
---|
260 | cp[6] = fcomplex.Round(ap[6] ) /*dummy*/;
|
---|
261 | cp[7] = fcomplex.Round(ap[7] ) /*dummy*/;
|
---|
262 | cp[8] = fcomplex.Round(ap[8] ) /*dummy*/;
|
---|
263 | cp[9] = fcomplex.Round(ap[9] ) /*dummy*/;
|
---|
264 | cp[10] = fcomplex.Round(ap[10] ) /*dummy*/;
|
---|
265 | cp[11] = fcomplex.Round(ap[11] ) /*dummy*/;
|
---|
266 | cp[12] = fcomplex.Round(ap[12] ) /*dummy*/;
|
---|
267 | cp[13] = fcomplex.Round(ap[13] ) /*dummy*/;
|
---|
268 | cp[14] = fcomplex.Round(ap[14] ) /*dummy*/;
|
---|
269 | cp[15] = fcomplex.Round(ap[15] ) /*dummy*/;
|
---|
270 | cp[16] = fcomplex.Round(ap[16] ) /*dummy*/;
|
---|
271 | cp[17] = fcomplex.Round(ap[17] ) /*dummy*/;
|
---|
272 | cp[18] = fcomplex.Round(ap[18] ) /*dummy*/;
|
---|
273 | cp[19] = fcomplex.Round(ap[19] ) /*dummy*/;
|
---|
274 | cp[20] = fcomplex.Round(ap[20] ) /*dummy*/;
|
---|
275 | ap += 21;
|
---|
276 | cp += 21;
|
---|
277 | len -= 21;
|
---|
278 | }
|
---|
279 | while (len-- > 0) {
|
---|
280 | *cp = fcomplex.Round(*ap ) /*dummy*/;
|
---|
281 | ap++;
|
---|
282 | cp++;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
286 | };
|
---|
287 |
|
---|
288 | fixed ( fcomplex* arrAP = arrA)
|
---|
289 | fixed ( fcomplex* retArrP = retArr) {
|
---|
290 | for (; i < workItemCount - 1; i++) {
|
---|
291 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
292 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
293 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
294 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
295 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
296 | }
|
---|
297 | // the last (or may the only) chunk is done right here
|
---|
298 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
299 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
300 |
|
---|
301 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
302 | }
|
---|
303 | return new ILRetArray<fcomplex>(retStorage);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | /// <summary>Round to nearest integer</summary>
|
---|
307 | /// <param name="A">Input array</param>
|
---|
308 | /// <returns>Array of same size as A with elements rounded to nearest integer</returns>
|
---|
309 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
310 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
311 | public unsafe static ILRetArray<float> round (ILInArray< float > A) {
|
---|
312 | using (ILScope.Enter(A)) {
|
---|
313 | if (A.IsEmpty)
|
---|
314 | return new ILRetArray<float>(A.Size);
|
---|
315 | ILSize inDim = A.Size;
|
---|
316 | float[] arrA = A.GetArrayForRead();
|
---|
317 | float [] retArr;
|
---|
318 | int outLen = inDim.NumberOfElements;
|
---|
319 | bool inplace = true;
|
---|
320 |
|
---|
321 | if (!A.TryGetStorage4InplaceOp(out retArr)) {
|
---|
322 | retArr = ILMemoryPool.Pool.New<float>(outLen);
|
---|
323 | inplace = false;
|
---|
324 | }
|
---|
325 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
326 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
327 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
328 | workItemLength = outLen / workItemCount;
|
---|
329 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
330 | } else {
|
---|
331 | workItemLength = outLen / 2;
|
---|
332 | workItemCount = 2;
|
---|
333 | }
|
---|
334 | } else {
|
---|
335 | workItemLength = outLen;
|
---|
336 | workItemCount = 1;
|
---|
337 | }
|
---|
338 | ILDenseStorage<float> retStorage = new ILDenseStorage<float>(retArr, inDim);
|
---|
339 |
|
---|
340 | Action<object> worker = data => {
|
---|
341 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
342 |
|
---|
343 | float* cp = ((float*)range.Item4 + range.Item1);
|
---|
344 | int len = range.Item2;
|
---|
345 | if (range.Item5) {
|
---|
346 | // inplace
|
---|
347 | while (len > 20) {
|
---|
348 | cp[0] = (float)Math.Round(cp[0] ) /*dummy*/;
|
---|
349 | cp[1] = (float)Math.Round(cp[1] ) /*dummy*/;
|
---|
350 | cp[2] = (float)Math.Round(cp[2] ) /*dummy*/;
|
---|
351 | cp[3] = (float)Math.Round(cp[3] ) /*dummy*/;
|
---|
352 | cp[4] = (float)Math.Round(cp[4] ) /*dummy*/;
|
---|
353 | cp[5] = (float)Math.Round(cp[5] ) /*dummy*/;
|
---|
354 | cp[6] = (float)Math.Round(cp[6] ) /*dummy*/;
|
---|
355 | cp[7] = (float)Math.Round(cp[7] ) /*dummy*/;
|
---|
356 | cp[8] = (float)Math.Round(cp[8] ) /*dummy*/;
|
---|
357 | cp[9] = (float)Math.Round(cp[9] ) /*dummy*/;
|
---|
358 | cp[10] = (float)Math.Round(cp[10] ) /*dummy*/;
|
---|
359 | cp[11] = (float)Math.Round(cp[11] ) /*dummy*/;
|
---|
360 | cp[12] = (float)Math.Round(cp[12] ) /*dummy*/;
|
---|
361 | cp[13] = (float)Math.Round(cp[13] ) /*dummy*/;
|
---|
362 | cp[14] = (float)Math.Round(cp[14] ) /*dummy*/;
|
---|
363 | cp[15] = (float)Math.Round(cp[15] ) /*dummy*/;
|
---|
364 | cp[16] = (float)Math.Round(cp[16] ) /*dummy*/;
|
---|
365 | cp[17] = (float)Math.Round(cp[17] ) /*dummy*/;
|
---|
366 | cp[18] = (float)Math.Round(cp[18] ) /*dummy*/;
|
---|
367 | cp[19] = (float)Math.Round(cp[19] ) /*dummy*/;
|
---|
368 | cp[20] = (float)Math.Round(cp[20] ) /*dummy*/;
|
---|
369 | cp+=21; len -= 21;
|
---|
370 | }
|
---|
371 | while (len-- > 0) {
|
---|
372 | *cp = (float)Math.Round(*cp ) /*dummy*/;
|
---|
373 | cp++;
|
---|
374 | }
|
---|
375 | } else {
|
---|
376 | float* ap = ((float*)range.Item3 + range.Item1);
|
---|
377 | while (len > 20) {
|
---|
378 | cp[0] = (float)Math.Round(ap[0] ) /*dummy*/;
|
---|
379 | cp[1] = (float)Math.Round(ap[1] ) /*dummy*/;
|
---|
380 | cp[2] = (float)Math.Round(ap[2] ) /*dummy*/;
|
---|
381 | cp[3] = (float)Math.Round(ap[3] ) /*dummy*/;
|
---|
382 | cp[4] = (float)Math.Round(ap[4] ) /*dummy*/;
|
---|
383 | cp[5] = (float)Math.Round(ap[5] ) /*dummy*/;
|
---|
384 | cp[6] = (float)Math.Round(ap[6] ) /*dummy*/;
|
---|
385 | cp[7] = (float)Math.Round(ap[7] ) /*dummy*/;
|
---|
386 | cp[8] = (float)Math.Round(ap[8] ) /*dummy*/;
|
---|
387 | cp[9] = (float)Math.Round(ap[9] ) /*dummy*/;
|
---|
388 | cp[10] = (float)Math.Round(ap[10] ) /*dummy*/;
|
---|
389 | cp[11] = (float)Math.Round(ap[11] ) /*dummy*/;
|
---|
390 | cp[12] = (float)Math.Round(ap[12] ) /*dummy*/;
|
---|
391 | cp[13] = (float)Math.Round(ap[13] ) /*dummy*/;
|
---|
392 | cp[14] = (float)Math.Round(ap[14] ) /*dummy*/;
|
---|
393 | cp[15] = (float)Math.Round(ap[15] ) /*dummy*/;
|
---|
394 | cp[16] = (float)Math.Round(ap[16] ) /*dummy*/;
|
---|
395 | cp[17] = (float)Math.Round(ap[17] ) /*dummy*/;
|
---|
396 | cp[18] = (float)Math.Round(ap[18] ) /*dummy*/;
|
---|
397 | cp[19] = (float)Math.Round(ap[19] ) /*dummy*/;
|
---|
398 | cp[20] = (float)Math.Round(ap[20] ) /*dummy*/;
|
---|
399 | ap += 21;
|
---|
400 | cp += 21;
|
---|
401 | len -= 21;
|
---|
402 | }
|
---|
403 | while (len-- > 0) {
|
---|
404 | *cp = (float)Math.Round(*ap ) /*dummy*/;
|
---|
405 | ap++;
|
---|
406 | cp++;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
410 | };
|
---|
411 |
|
---|
412 | fixed ( float* arrAP = arrA)
|
---|
413 | fixed ( float* retArrP = retArr) {
|
---|
414 | for (; i < workItemCount - 1; i++) {
|
---|
415 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
416 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
417 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
418 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
419 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
420 | }
|
---|
421 | // the last (or may the only) chunk is done right here
|
---|
422 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
423 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
424 |
|
---|
425 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
426 | }
|
---|
427 | return new ILRetArray<float>(retStorage);
|
---|
428 | }
|
---|
429 | }
|
---|
430 | /// <summary>Round to nearest integer</summary>
|
---|
431 | /// <param name="A">Input array</param>
|
---|
432 | /// <returns>Array of same size as A with elements rounded to nearest integer</returns>
|
---|
433 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
434 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
435 | public unsafe static ILRetArray<double> round (ILInArray< double > A) {
|
---|
436 | using (ILScope.Enter(A)) {
|
---|
437 | if (A.IsEmpty)
|
---|
438 | return new ILRetArray<double>(A.Size);
|
---|
439 | ILSize inDim = A.Size;
|
---|
440 | double[] arrA = A.GetArrayForRead();
|
---|
441 | double [] retArr;
|
---|
442 | int outLen = inDim.NumberOfElements;
|
---|
443 | bool inplace = true;
|
---|
444 |
|
---|
445 | if (!A.TryGetStorage4InplaceOp(out retArr)) {
|
---|
446 | retArr = ILMemoryPool.Pool.New<double>(outLen);
|
---|
447 | inplace = false;
|
---|
448 | }
|
---|
449 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
450 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
451 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
452 | workItemLength = outLen / workItemCount;
|
---|
453 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
454 | } else {
|
---|
455 | workItemLength = outLen / 2;
|
---|
456 | workItemCount = 2;
|
---|
457 | }
|
---|
458 | } else {
|
---|
459 | workItemLength = outLen;
|
---|
460 | workItemCount = 1;
|
---|
461 | }
|
---|
462 | ILDenseStorage<double> retStorage = new ILDenseStorage<double>(retArr, inDim);
|
---|
463 |
|
---|
464 | Action<object> worker = data => {
|
---|
465 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
466 |
|
---|
467 | double* cp = ((double*)range.Item4 + range.Item1);
|
---|
468 | int len = range.Item2;
|
---|
469 | if (range.Item5) {
|
---|
470 | // inplace
|
---|
471 | while (len > 20) {
|
---|
472 | cp[0] = Math.Round(cp[0] ) /*dummy*/;
|
---|
473 | cp[1] = Math.Round(cp[1] ) /*dummy*/;
|
---|
474 | cp[2] = Math.Round(cp[2] ) /*dummy*/;
|
---|
475 | cp[3] = Math.Round(cp[3] ) /*dummy*/;
|
---|
476 | cp[4] = Math.Round(cp[4] ) /*dummy*/;
|
---|
477 | cp[5] = Math.Round(cp[5] ) /*dummy*/;
|
---|
478 | cp[6] = Math.Round(cp[6] ) /*dummy*/;
|
---|
479 | cp[7] = Math.Round(cp[7] ) /*dummy*/;
|
---|
480 | cp[8] = Math.Round(cp[8] ) /*dummy*/;
|
---|
481 | cp[9] = Math.Round(cp[9] ) /*dummy*/;
|
---|
482 | cp[10] = Math.Round(cp[10] ) /*dummy*/;
|
---|
483 | cp[11] = Math.Round(cp[11] ) /*dummy*/;
|
---|
484 | cp[12] = Math.Round(cp[12] ) /*dummy*/;
|
---|
485 | cp[13] = Math.Round(cp[13] ) /*dummy*/;
|
---|
486 | cp[14] = Math.Round(cp[14] ) /*dummy*/;
|
---|
487 | cp[15] = Math.Round(cp[15] ) /*dummy*/;
|
---|
488 | cp[16] = Math.Round(cp[16] ) /*dummy*/;
|
---|
489 | cp[17] = Math.Round(cp[17] ) /*dummy*/;
|
---|
490 | cp[18] = Math.Round(cp[18] ) /*dummy*/;
|
---|
491 | cp[19] = Math.Round(cp[19] ) /*dummy*/;
|
---|
492 | cp[20] = Math.Round(cp[20] ) /*dummy*/;
|
---|
493 | cp+=21; len -= 21;
|
---|
494 | }
|
---|
495 | while (len-- > 0) {
|
---|
496 | *cp = Math.Round(*cp ) /*dummy*/;
|
---|
497 | cp++;
|
---|
498 | }
|
---|
499 | } else {
|
---|
500 | double* ap = ((double*)range.Item3 + range.Item1);
|
---|
501 | while (len > 20) {
|
---|
502 | cp[0] = Math.Round(ap[0] ) /*dummy*/;
|
---|
503 | cp[1] = Math.Round(ap[1] ) /*dummy*/;
|
---|
504 | cp[2] = Math.Round(ap[2] ) /*dummy*/;
|
---|
505 | cp[3] = Math.Round(ap[3] ) /*dummy*/;
|
---|
506 | cp[4] = Math.Round(ap[4] ) /*dummy*/;
|
---|
507 | cp[5] = Math.Round(ap[5] ) /*dummy*/;
|
---|
508 | cp[6] = Math.Round(ap[6] ) /*dummy*/;
|
---|
509 | cp[7] = Math.Round(ap[7] ) /*dummy*/;
|
---|
510 | cp[8] = Math.Round(ap[8] ) /*dummy*/;
|
---|
511 | cp[9] = Math.Round(ap[9] ) /*dummy*/;
|
---|
512 | cp[10] = Math.Round(ap[10] ) /*dummy*/;
|
---|
513 | cp[11] = Math.Round(ap[11] ) /*dummy*/;
|
---|
514 | cp[12] = Math.Round(ap[12] ) /*dummy*/;
|
---|
515 | cp[13] = Math.Round(ap[13] ) /*dummy*/;
|
---|
516 | cp[14] = Math.Round(ap[14] ) /*dummy*/;
|
---|
517 | cp[15] = Math.Round(ap[15] ) /*dummy*/;
|
---|
518 | cp[16] = Math.Round(ap[16] ) /*dummy*/;
|
---|
519 | cp[17] = Math.Round(ap[17] ) /*dummy*/;
|
---|
520 | cp[18] = Math.Round(ap[18] ) /*dummy*/;
|
---|
521 | cp[19] = Math.Round(ap[19] ) /*dummy*/;
|
---|
522 | cp[20] = Math.Round(ap[20] ) /*dummy*/;
|
---|
523 | ap += 21;
|
---|
524 | cp += 21;
|
---|
525 | len -= 21;
|
---|
526 | }
|
---|
527 | while (len-- > 0) {
|
---|
528 | *cp = Math.Round(*ap ) /*dummy*/;
|
---|
529 | ap++;
|
---|
530 | cp++;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
534 | };
|
---|
535 |
|
---|
536 | fixed ( double* arrAP = arrA)
|
---|
537 | fixed ( double* retArrP = retArr) {
|
---|
538 | for (; i < workItemCount - 1; i++) {
|
---|
539 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
540 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
541 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
542 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
543 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
544 | }
|
---|
545 | // the last (or may the only) chunk is done right here
|
---|
546 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
547 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
548 |
|
---|
549 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
550 | }
|
---|
551 | return new ILRetArray<double>(retStorage);
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
556 |
|
---|
557 | }
|
---|
558 | }
|
---|