[9102] | 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 | public partial class ILMath {
|
---|
| 51 | /// <summary>Absolute values of array elements</summary>
|
---|
| 52 | /// <param name="A">Input Array</param>
|
---|
| 53 | /// <returns>Absolute values of array elements</returns>
|
---|
| 54 | /// <remarks><para>If the Input Array is empty, an empty array will be returned.</para>
|
---|
| 55 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 56 | public unsafe static ILRetArray<byte> abs(ILInArray<byte> A) {
|
---|
| 57 | using (ILScope.Enter(A)) {
|
---|
| 58 | return A.C;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | |
---|
| 63 |
|
---|
| 64 | |
---|
| 65 | #region HYCALPER AUTO GENERATED CODE
|
---|
| 66 | |
---|
| 67 | /// <summary>Absolute values of array elements</summary>
|
---|
| 68 | /// <param name="A">Input array</param>
|
---|
| 69 | /// <returns>Absolute values of array elements</returns>
|
---|
| 70 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 71 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 72 | public unsafe static ILRetArray<double> abs (ILInArray< double > A) {
|
---|
| 73 | using (ILScope.Enter(A)) {
|
---|
| 74 | if (A.IsEmpty)
|
---|
| 75 | return new ILRetArray<double>(A.Size);
|
---|
| 76 | ILSize inDim = A.Size;
|
---|
| 77 | double[] arrA = A.GetArrayForRead();
|
---|
| 78 | double [] retArr;
|
---|
| 79 | int outLen = inDim.NumberOfElements;
|
---|
| 80 | bool inplace = true;
|
---|
| 81 |
|
---|
| 82 | if (!A.TryGetStorage4InplaceOp(out retArr)){
|
---|
| 83 | retArr = ILMemoryPool.Pool.New<double>(outLen);
|
---|
| 84 | inplace = false;
|
---|
| 85 | }
|
---|
| 86 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 87 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 88 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 89 | workItemLength = outLen / workItemCount;
|
---|
| 90 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 91 | } else {
|
---|
| 92 | workItemLength = outLen / 2;
|
---|
| 93 | workItemCount = 2;
|
---|
| 94 | }
|
---|
| 95 | } else {
|
---|
| 96 | workItemLength = outLen;
|
---|
| 97 | workItemCount = 1;
|
---|
| 98 | }
|
---|
| 99 | ILDenseStorage<double> retStorage = new ILDenseStorage<double>(retArr, inDim);
|
---|
| 100 |
|
---|
| 101 | Action<object> worker = data => {
|
---|
| 102 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 103 |
|
---|
| 104 | double* cp = ((double*)range.Item4 + range.Item1);
|
---|
| 105 | int len = range.Item2;
|
---|
| 106 | if (range.Item5) {
|
---|
| 107 | // inplace
|
---|
| 108 | while (len > 20) {
|
---|
| 109 | cp[0] = Math.Abs(cp[0] ) /*dummy*/;
|
---|
| 110 | cp[1] = Math.Abs(cp[1] ) /*dummy*/;
|
---|
| 111 | cp[2] = Math.Abs(cp[2] ) /*dummy*/;
|
---|
| 112 | cp[3] = Math.Abs(cp[3] ) /*dummy*/;
|
---|
| 113 | cp[4] = Math.Abs(cp[4] ) /*dummy*/;
|
---|
| 114 | cp[5] = Math.Abs(cp[5] ) /*dummy*/;
|
---|
| 115 | cp[6] = Math.Abs(cp[6] ) /*dummy*/;
|
---|
| 116 | cp[7] = Math.Abs(cp[7] ) /*dummy*/;
|
---|
| 117 | cp[8] = Math.Abs(cp[8] ) /*dummy*/;
|
---|
| 118 | cp[9] = Math.Abs(cp[9] ) /*dummy*/;
|
---|
| 119 | cp[10] = Math.Abs(cp[10] ) /*dummy*/;
|
---|
| 120 | cp[11] = Math.Abs(cp[11] ) /*dummy*/;
|
---|
| 121 | cp[12] = Math.Abs(cp[12] ) /*dummy*/;
|
---|
| 122 | cp[13] = Math.Abs(cp[13] ) /*dummy*/;
|
---|
| 123 | cp[14] = Math.Abs(cp[14] ) /*dummy*/;
|
---|
| 124 | cp[15] = Math.Abs(cp[15] ) /*dummy*/;
|
---|
| 125 | cp[16] = Math.Abs(cp[16] ) /*dummy*/;
|
---|
| 126 | cp[17] = Math.Abs(cp[17] ) /*dummy*/;
|
---|
| 127 | cp[18] = Math.Abs(cp[18] ) /*dummy*/;
|
---|
| 128 | cp[19] = Math.Abs(cp[19] ) /*dummy*/;
|
---|
| 129 | cp[20] = Math.Abs(cp[20] ) /*dummy*/;
|
---|
| 130 | cp+=21; len -= 21;
|
---|
| 131 | }
|
---|
| 132 | while (len-- > 0) {
|
---|
| 133 | *cp = Math.Abs(*cp ) /*dummy*/;
|
---|
| 134 | cp++;
|
---|
| 135 | }
|
---|
| 136 | } else {
|
---|
| 137 | double* ap = ((double*)range.Item3 + range.Item1);
|
---|
| 138 | while (len > 20) {
|
---|
| 139 | cp[0] = Math.Abs(ap[0] ) /*dummy*/;
|
---|
| 140 | cp[1] = Math.Abs(ap[1] ) /*dummy*/;
|
---|
| 141 | cp[2] = Math.Abs(ap[2] ) /*dummy*/;
|
---|
| 142 | cp[3] = Math.Abs(ap[3] ) /*dummy*/;
|
---|
| 143 | cp[4] = Math.Abs(ap[4] ) /*dummy*/;
|
---|
| 144 | cp[5] = Math.Abs(ap[5] ) /*dummy*/;
|
---|
| 145 | cp[6] = Math.Abs(ap[6] ) /*dummy*/;
|
---|
| 146 | cp[7] = Math.Abs(ap[7] ) /*dummy*/;
|
---|
| 147 | cp[8] = Math.Abs(ap[8] ) /*dummy*/;
|
---|
| 148 | cp[9] = Math.Abs(ap[9] ) /*dummy*/;
|
---|
| 149 | cp[10] = Math.Abs(ap[10] ) /*dummy*/;
|
---|
| 150 | cp[11] = Math.Abs(ap[11] ) /*dummy*/;
|
---|
| 151 | cp[12] = Math.Abs(ap[12] ) /*dummy*/;
|
---|
| 152 | cp[13] = Math.Abs(ap[13] ) /*dummy*/;
|
---|
| 153 | cp[14] = Math.Abs(ap[14] ) /*dummy*/;
|
---|
| 154 | cp[15] = Math.Abs(ap[15] ) /*dummy*/;
|
---|
| 155 | cp[16] = Math.Abs(ap[16] ) /*dummy*/;
|
---|
| 156 | cp[17] = Math.Abs(ap[17] ) /*dummy*/;
|
---|
| 157 | cp[18] = Math.Abs(ap[18] ) /*dummy*/;
|
---|
| 158 | cp[19] = Math.Abs(ap[19] ) /*dummy*/;
|
---|
| 159 | cp[20] = Math.Abs(ap[20] ) /*dummy*/;
|
---|
| 160 | ap += 21;
|
---|
| 161 | cp += 21;
|
---|
| 162 | len -= 21;
|
---|
| 163 | }
|
---|
| 164 | while (len-- > 0) {
|
---|
| 165 | *cp = Math.Abs(*ap ) /*dummy*/;
|
---|
| 166 | ap++;
|
---|
| 167 | cp++;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 | fixed ( double* arrAP = arrA)
|
---|
| 174 | fixed ( double* retArrP = retArr) {
|
---|
| 175 | for (; i < workItemCount - 1; i++) {
|
---|
| 176 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 177 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 178 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 179 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 180 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 181 | }
|
---|
| 182 | // the last (or may the only) chunk is done right here
|
---|
| 183 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 184 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 185 |
|
---|
| 186 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 187 | }
|
---|
| 188 | return new ILRetArray<double>(retStorage);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | /// <summary>Absolute values of array elements</summary>
|
---|
| 192 | /// <param name="A">Input array</param>
|
---|
| 193 | /// <returns>Absolute values of array elements</returns>
|
---|
| 194 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 195 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 196 | public unsafe static ILRetArray<float> abs (ILInArray< float > A) {
|
---|
| 197 | using (ILScope.Enter(A)) {
|
---|
| 198 | if (A.IsEmpty)
|
---|
| 199 | return new ILRetArray<float>(A.Size);
|
---|
| 200 | ILSize inDim = A.Size;
|
---|
| 201 | float[] arrA = A.GetArrayForRead();
|
---|
| 202 | float [] retArr;
|
---|
| 203 | int outLen = inDim.NumberOfElements;
|
---|
| 204 | bool inplace = true;
|
---|
| 205 |
|
---|
| 206 | if (!A.TryGetStorage4InplaceOp(out retArr)){
|
---|
| 207 | retArr = ILMemoryPool.Pool.New<float>(outLen);
|
---|
| 208 | inplace = false;
|
---|
| 209 | }
|
---|
| 210 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 211 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 212 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 213 | workItemLength = outLen / workItemCount;
|
---|
| 214 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 215 | } else {
|
---|
| 216 | workItemLength = outLen / 2;
|
---|
| 217 | workItemCount = 2;
|
---|
| 218 | }
|
---|
| 219 | } else {
|
---|
| 220 | workItemLength = outLen;
|
---|
| 221 | workItemCount = 1;
|
---|
| 222 | }
|
---|
| 223 | ILDenseStorage<float> retStorage = new ILDenseStorage<float>(retArr, inDim);
|
---|
| 224 |
|
---|
| 225 | Action<object> worker = data => {
|
---|
| 226 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 227 |
|
---|
| 228 | float* cp = ((float*)range.Item4 + range.Item1);
|
---|
| 229 | int len = range.Item2;
|
---|
| 230 | if (range.Item5) {
|
---|
| 231 | // inplace
|
---|
| 232 | while (len > 20) {
|
---|
| 233 | cp[0] = Math.Abs(cp[0] ) /*dummy*/;
|
---|
| 234 | cp[1] = Math.Abs(cp[1] ) /*dummy*/;
|
---|
| 235 | cp[2] = Math.Abs(cp[2] ) /*dummy*/;
|
---|
| 236 | cp[3] = Math.Abs(cp[3] ) /*dummy*/;
|
---|
| 237 | cp[4] = Math.Abs(cp[4] ) /*dummy*/;
|
---|
| 238 | cp[5] = Math.Abs(cp[5] ) /*dummy*/;
|
---|
| 239 | cp[6] = Math.Abs(cp[6] ) /*dummy*/;
|
---|
| 240 | cp[7] = Math.Abs(cp[7] ) /*dummy*/;
|
---|
| 241 | cp[8] = Math.Abs(cp[8] ) /*dummy*/;
|
---|
| 242 | cp[9] = Math.Abs(cp[9] ) /*dummy*/;
|
---|
| 243 | cp[10] = Math.Abs(cp[10] ) /*dummy*/;
|
---|
| 244 | cp[11] = Math.Abs(cp[11] ) /*dummy*/;
|
---|
| 245 | cp[12] = Math.Abs(cp[12] ) /*dummy*/;
|
---|
| 246 | cp[13] = Math.Abs(cp[13] ) /*dummy*/;
|
---|
| 247 | cp[14] = Math.Abs(cp[14] ) /*dummy*/;
|
---|
| 248 | cp[15] = Math.Abs(cp[15] ) /*dummy*/;
|
---|
| 249 | cp[16] = Math.Abs(cp[16] ) /*dummy*/;
|
---|
| 250 | cp[17] = Math.Abs(cp[17] ) /*dummy*/;
|
---|
| 251 | cp[18] = Math.Abs(cp[18] ) /*dummy*/;
|
---|
| 252 | cp[19] = Math.Abs(cp[19] ) /*dummy*/;
|
---|
| 253 | cp[20] = Math.Abs(cp[20] ) /*dummy*/;
|
---|
| 254 | cp+=21; len -= 21;
|
---|
| 255 | }
|
---|
| 256 | while (len-- > 0) {
|
---|
| 257 | *cp = Math.Abs(*cp ) /*dummy*/;
|
---|
| 258 | cp++;
|
---|
| 259 | }
|
---|
| 260 | } else {
|
---|
| 261 | float* ap = ((float*)range.Item3 + range.Item1);
|
---|
| 262 | while (len > 20) {
|
---|
| 263 | cp[0] = Math.Abs(ap[0] ) /*dummy*/;
|
---|
| 264 | cp[1] = Math.Abs(ap[1] ) /*dummy*/;
|
---|
| 265 | cp[2] = Math.Abs(ap[2] ) /*dummy*/;
|
---|
| 266 | cp[3] = Math.Abs(ap[3] ) /*dummy*/;
|
---|
| 267 | cp[4] = Math.Abs(ap[4] ) /*dummy*/;
|
---|
| 268 | cp[5] = Math.Abs(ap[5] ) /*dummy*/;
|
---|
| 269 | cp[6] = Math.Abs(ap[6] ) /*dummy*/;
|
---|
| 270 | cp[7] = Math.Abs(ap[7] ) /*dummy*/;
|
---|
| 271 | cp[8] = Math.Abs(ap[8] ) /*dummy*/;
|
---|
| 272 | cp[9] = Math.Abs(ap[9] ) /*dummy*/;
|
---|
| 273 | cp[10] = Math.Abs(ap[10] ) /*dummy*/;
|
---|
| 274 | cp[11] = Math.Abs(ap[11] ) /*dummy*/;
|
---|
| 275 | cp[12] = Math.Abs(ap[12] ) /*dummy*/;
|
---|
| 276 | cp[13] = Math.Abs(ap[13] ) /*dummy*/;
|
---|
| 277 | cp[14] = Math.Abs(ap[14] ) /*dummy*/;
|
---|
| 278 | cp[15] = Math.Abs(ap[15] ) /*dummy*/;
|
---|
| 279 | cp[16] = Math.Abs(ap[16] ) /*dummy*/;
|
---|
| 280 | cp[17] = Math.Abs(ap[17] ) /*dummy*/;
|
---|
| 281 | cp[18] = Math.Abs(ap[18] ) /*dummy*/;
|
---|
| 282 | cp[19] = Math.Abs(ap[19] ) /*dummy*/;
|
---|
| 283 | cp[20] = Math.Abs(ap[20] ) /*dummy*/;
|
---|
| 284 | ap += 21;
|
---|
| 285 | cp += 21;
|
---|
| 286 | len -= 21;
|
---|
| 287 | }
|
---|
| 288 | while (len-- > 0) {
|
---|
| 289 | *cp = Math.Abs(*ap ) /*dummy*/;
|
---|
| 290 | ap++;
|
---|
| 291 | cp++;
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 295 | };
|
---|
| 296 |
|
---|
| 297 | fixed ( float* arrAP = arrA)
|
---|
| 298 | fixed ( float* retArrP = retArr) {
|
---|
| 299 | for (; i < workItemCount - 1; i++) {
|
---|
| 300 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 301 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 302 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 303 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 304 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 305 | }
|
---|
| 306 | // the last (or may the only) chunk is done right here
|
---|
| 307 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 308 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 309 |
|
---|
| 310 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 311 | }
|
---|
| 312 | return new ILRetArray<float>(retStorage);
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | /// <summary>Magnitude of array elements</summary>
|
---|
| 316 | /// <param name="A">Input array</param>
|
---|
| 317 | /// <returns>Magnitude of array elements</returns>
|
---|
| 318 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 319 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 320 | public unsafe static ILRetArray<float> abs (ILInArray< fcomplex > A) {
|
---|
| 321 | using (ILScope.Enter(A)) {
|
---|
| 322 | if (A.IsEmpty)
|
---|
| 323 | return new ILRetArray<float>(A.Size);
|
---|
| 324 | ILSize inDim = A.Size;
|
---|
| 325 | fcomplex[] arrA = A.GetArrayForRead();
|
---|
| 326 | float [] retArr;
|
---|
| 327 | int outLen = inDim.NumberOfElements;
|
---|
| 328 | bool inplace = true;
|
---|
| 329 |
|
---|
| 330 | if (true){
|
---|
| 331 | retArr = ILMemoryPool.Pool.New<float>(outLen);
|
---|
| 332 | inplace = false;
|
---|
| 333 | }
|
---|
| 334 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 335 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 336 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 337 | workItemLength = outLen / workItemCount;
|
---|
| 338 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 339 | } else {
|
---|
| 340 | workItemLength = outLen / 2;
|
---|
| 341 | workItemCount = 2;
|
---|
| 342 | }
|
---|
| 343 | } else {
|
---|
| 344 | workItemLength = outLen;
|
---|
| 345 | workItemCount = 1;
|
---|
| 346 | }
|
---|
| 347 | ILDenseStorage<float> retStorage = new ILDenseStorage<float>(retArr, inDim);
|
---|
| 348 |
|
---|
| 349 | Action<object> worker = data => {
|
---|
| 350 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 351 |
|
---|
| 352 | float* cp = ((float*)range.Item4 + range.Item1);
|
---|
| 353 | int len = range.Item2;
|
---|
| 354 | if (range.Item5) {
|
---|
| 355 | // inplace
|
---|
| 356 | while (len > 20) {
|
---|
| 357 | cp[0] = fcomplex.Abs(cp[0] ) /*dummy*/;
|
---|
| 358 | cp[1] = fcomplex.Abs(cp[1] ) /*dummy*/;
|
---|
| 359 | cp[2] = fcomplex.Abs(cp[2] ) /*dummy*/;
|
---|
| 360 | cp[3] = fcomplex.Abs(cp[3] ) /*dummy*/;
|
---|
| 361 | cp[4] = fcomplex.Abs(cp[4] ) /*dummy*/;
|
---|
| 362 | cp[5] = fcomplex.Abs(cp[5] ) /*dummy*/;
|
---|
| 363 | cp[6] = fcomplex.Abs(cp[6] ) /*dummy*/;
|
---|
| 364 | cp[7] = fcomplex.Abs(cp[7] ) /*dummy*/;
|
---|
| 365 | cp[8] = fcomplex.Abs(cp[8] ) /*dummy*/;
|
---|
| 366 | cp[9] = fcomplex.Abs(cp[9] ) /*dummy*/;
|
---|
| 367 | cp[10] = fcomplex.Abs(cp[10] ) /*dummy*/;
|
---|
| 368 | cp[11] = fcomplex.Abs(cp[11] ) /*dummy*/;
|
---|
| 369 | cp[12] = fcomplex.Abs(cp[12] ) /*dummy*/;
|
---|
| 370 | cp[13] = fcomplex.Abs(cp[13] ) /*dummy*/;
|
---|
| 371 | cp[14] = fcomplex.Abs(cp[14] ) /*dummy*/;
|
---|
| 372 | cp[15] = fcomplex.Abs(cp[15] ) /*dummy*/;
|
---|
| 373 | cp[16] = fcomplex.Abs(cp[16] ) /*dummy*/;
|
---|
| 374 | cp[17] = fcomplex.Abs(cp[17] ) /*dummy*/;
|
---|
| 375 | cp[18] = fcomplex.Abs(cp[18] ) /*dummy*/;
|
---|
| 376 | cp[19] = fcomplex.Abs(cp[19] ) /*dummy*/;
|
---|
| 377 | cp[20] = fcomplex.Abs(cp[20] ) /*dummy*/;
|
---|
| 378 | cp+=21; len -= 21;
|
---|
| 379 | }
|
---|
| 380 | while (len-- > 0) {
|
---|
| 381 | *cp = fcomplex.Abs(*cp ) /*dummy*/;
|
---|
| 382 | cp++;
|
---|
| 383 | }
|
---|
| 384 | } else {
|
---|
| 385 | fcomplex* ap = ((fcomplex*)range.Item3 + range.Item1);
|
---|
| 386 | while (len > 20) {
|
---|
| 387 | cp[0] = fcomplex.Abs(ap[0] ) /*dummy*/;
|
---|
| 388 | cp[1] = fcomplex.Abs(ap[1] ) /*dummy*/;
|
---|
| 389 | cp[2] = fcomplex.Abs(ap[2] ) /*dummy*/;
|
---|
| 390 | cp[3] = fcomplex.Abs(ap[3] ) /*dummy*/;
|
---|
| 391 | cp[4] = fcomplex.Abs(ap[4] ) /*dummy*/;
|
---|
| 392 | cp[5] = fcomplex.Abs(ap[5] ) /*dummy*/;
|
---|
| 393 | cp[6] = fcomplex.Abs(ap[6] ) /*dummy*/;
|
---|
| 394 | cp[7] = fcomplex.Abs(ap[7] ) /*dummy*/;
|
---|
| 395 | cp[8] = fcomplex.Abs(ap[8] ) /*dummy*/;
|
---|
| 396 | cp[9] = fcomplex.Abs(ap[9] ) /*dummy*/;
|
---|
| 397 | cp[10] = fcomplex.Abs(ap[10] ) /*dummy*/;
|
---|
| 398 | cp[11] = fcomplex.Abs(ap[11] ) /*dummy*/;
|
---|
| 399 | cp[12] = fcomplex.Abs(ap[12] ) /*dummy*/;
|
---|
| 400 | cp[13] = fcomplex.Abs(ap[13] ) /*dummy*/;
|
---|
| 401 | cp[14] = fcomplex.Abs(ap[14] ) /*dummy*/;
|
---|
| 402 | cp[15] = fcomplex.Abs(ap[15] ) /*dummy*/;
|
---|
| 403 | cp[16] = fcomplex.Abs(ap[16] ) /*dummy*/;
|
---|
| 404 | cp[17] = fcomplex.Abs(ap[17] ) /*dummy*/;
|
---|
| 405 | cp[18] = fcomplex.Abs(ap[18] ) /*dummy*/;
|
---|
| 406 | cp[19] = fcomplex.Abs(ap[19] ) /*dummy*/;
|
---|
| 407 | cp[20] = fcomplex.Abs(ap[20] ) /*dummy*/;
|
---|
| 408 | ap += 21;
|
---|
| 409 | cp += 21;
|
---|
| 410 | len -= 21;
|
---|
| 411 | }
|
---|
| 412 | while (len-- > 0) {
|
---|
| 413 | *cp = fcomplex.Abs(*ap ) /*dummy*/;
|
---|
| 414 | ap++;
|
---|
| 415 | cp++;
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 419 | };
|
---|
| 420 |
|
---|
| 421 | fixed ( fcomplex* arrAP = arrA)
|
---|
| 422 | fixed ( float* retArrP = retArr) {
|
---|
| 423 | for (; i < workItemCount - 1; i++) {
|
---|
| 424 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 425 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 426 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 427 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 428 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 429 | }
|
---|
| 430 | // the last (or may the only) chunk is done right here
|
---|
| 431 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 432 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 433 |
|
---|
| 434 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 435 | }
|
---|
| 436 | return new ILRetArray<float>(retStorage);
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 | /// <summary>Magnitude of array elements</summary>
|
---|
| 440 | /// <param name="A">Input array</param>
|
---|
| 441 | /// <returns>Magnitude of array elements</returns>
|
---|
| 442 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 443 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 444 | public unsafe static ILRetArray<double> abs (ILInArray< complex > A) {
|
---|
| 445 | using (ILScope.Enter(A)) {
|
---|
| 446 | if (A.IsEmpty)
|
---|
| 447 | return new ILRetArray<double>(A.Size);
|
---|
| 448 | ILSize inDim = A.Size;
|
---|
| 449 | complex[] arrA = A.GetArrayForRead();
|
---|
| 450 | double [] retArr;
|
---|
| 451 | int outLen = inDim.NumberOfElements;
|
---|
| 452 | bool inplace = true;
|
---|
| 453 |
|
---|
| 454 | if (true){
|
---|
| 455 | retArr = ILMemoryPool.Pool.New<double>(outLen);
|
---|
| 456 | inplace = false;
|
---|
| 457 | }
|
---|
| 458 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 459 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 460 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 461 | workItemLength = outLen / workItemCount;
|
---|
| 462 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 463 | } else {
|
---|
| 464 | workItemLength = outLen / 2;
|
---|
| 465 | workItemCount = 2;
|
---|
| 466 | }
|
---|
| 467 | } else {
|
---|
| 468 | workItemLength = outLen;
|
---|
| 469 | workItemCount = 1;
|
---|
| 470 | }
|
---|
| 471 | ILDenseStorage<double> retStorage = new ILDenseStorage<double>(retArr, inDim);
|
---|
| 472 |
|
---|
| 473 | Action<object> worker = data => {
|
---|
| 474 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 475 |
|
---|
| 476 | double* cp = ((double*)range.Item4 + range.Item1);
|
---|
| 477 | int len = range.Item2;
|
---|
| 478 | if (range.Item5) {
|
---|
| 479 | // inplace
|
---|
| 480 | while (len > 20) {
|
---|
| 481 | cp[0] = complex.Abs(cp[0] ) /*dummy*/;
|
---|
| 482 | cp[1] = complex.Abs(cp[1] ) /*dummy*/;
|
---|
| 483 | cp[2] = complex.Abs(cp[2] ) /*dummy*/;
|
---|
| 484 | cp[3] = complex.Abs(cp[3] ) /*dummy*/;
|
---|
| 485 | cp[4] = complex.Abs(cp[4] ) /*dummy*/;
|
---|
| 486 | cp[5] = complex.Abs(cp[5] ) /*dummy*/;
|
---|
| 487 | cp[6] = complex.Abs(cp[6] ) /*dummy*/;
|
---|
| 488 | cp[7] = complex.Abs(cp[7] ) /*dummy*/;
|
---|
| 489 | cp[8] = complex.Abs(cp[8] ) /*dummy*/;
|
---|
| 490 | cp[9] = complex.Abs(cp[9] ) /*dummy*/;
|
---|
| 491 | cp[10] = complex.Abs(cp[10] ) /*dummy*/;
|
---|
| 492 | cp[11] = complex.Abs(cp[11] ) /*dummy*/;
|
---|
| 493 | cp[12] = complex.Abs(cp[12] ) /*dummy*/;
|
---|
| 494 | cp[13] = complex.Abs(cp[13] ) /*dummy*/;
|
---|
| 495 | cp[14] = complex.Abs(cp[14] ) /*dummy*/;
|
---|
| 496 | cp[15] = complex.Abs(cp[15] ) /*dummy*/;
|
---|
| 497 | cp[16] = complex.Abs(cp[16] ) /*dummy*/;
|
---|
| 498 | cp[17] = complex.Abs(cp[17] ) /*dummy*/;
|
---|
| 499 | cp[18] = complex.Abs(cp[18] ) /*dummy*/;
|
---|
| 500 | cp[19] = complex.Abs(cp[19] ) /*dummy*/;
|
---|
| 501 | cp[20] = complex.Abs(cp[20] ) /*dummy*/;
|
---|
| 502 | cp+=21; len -= 21;
|
---|
| 503 | }
|
---|
| 504 | while (len-- > 0) {
|
---|
| 505 | *cp = complex.Abs(*cp ) /*dummy*/;
|
---|
| 506 | cp++;
|
---|
| 507 | }
|
---|
| 508 | } else {
|
---|
| 509 | complex* ap = ((complex*)range.Item3 + range.Item1);
|
---|
| 510 | while (len > 20) {
|
---|
| 511 | cp[0] = complex.Abs(ap[0] ) /*dummy*/;
|
---|
| 512 | cp[1] = complex.Abs(ap[1] ) /*dummy*/;
|
---|
| 513 | cp[2] = complex.Abs(ap[2] ) /*dummy*/;
|
---|
| 514 | cp[3] = complex.Abs(ap[3] ) /*dummy*/;
|
---|
| 515 | cp[4] = complex.Abs(ap[4] ) /*dummy*/;
|
---|
| 516 | cp[5] = complex.Abs(ap[5] ) /*dummy*/;
|
---|
| 517 | cp[6] = complex.Abs(ap[6] ) /*dummy*/;
|
---|
| 518 | cp[7] = complex.Abs(ap[7] ) /*dummy*/;
|
---|
| 519 | cp[8] = complex.Abs(ap[8] ) /*dummy*/;
|
---|
| 520 | cp[9] = complex.Abs(ap[9] ) /*dummy*/;
|
---|
| 521 | cp[10] = complex.Abs(ap[10] ) /*dummy*/;
|
---|
| 522 | cp[11] = complex.Abs(ap[11] ) /*dummy*/;
|
---|
| 523 | cp[12] = complex.Abs(ap[12] ) /*dummy*/;
|
---|
| 524 | cp[13] = complex.Abs(ap[13] ) /*dummy*/;
|
---|
| 525 | cp[14] = complex.Abs(ap[14] ) /*dummy*/;
|
---|
| 526 | cp[15] = complex.Abs(ap[15] ) /*dummy*/;
|
---|
| 527 | cp[16] = complex.Abs(ap[16] ) /*dummy*/;
|
---|
| 528 | cp[17] = complex.Abs(ap[17] ) /*dummy*/;
|
---|
| 529 | cp[18] = complex.Abs(ap[18] ) /*dummy*/;
|
---|
| 530 | cp[19] = complex.Abs(ap[19] ) /*dummy*/;
|
---|
| 531 | cp[20] = complex.Abs(ap[20] ) /*dummy*/;
|
---|
| 532 | ap += 21;
|
---|
| 533 | cp += 21;
|
---|
| 534 | len -= 21;
|
---|
| 535 | }
|
---|
| 536 | while (len-- > 0) {
|
---|
| 537 | *cp = complex.Abs(*ap ) /*dummy*/;
|
---|
| 538 | ap++;
|
---|
| 539 | cp++;
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 543 | };
|
---|
| 544 |
|
---|
| 545 | fixed ( complex* arrAP = arrA)
|
---|
| 546 | fixed ( double* retArrP = retArr) {
|
---|
| 547 | for (; i < workItemCount - 1; i++) {
|
---|
| 548 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 549 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 550 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 551 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 552 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 553 | }
|
---|
| 554 | // the last (or may the only) chunk is done right here
|
---|
| 555 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 556 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 557 |
|
---|
| 558 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 559 | }
|
---|
| 560 | return new ILRetArray<double>(retStorage);
|
---|
| 561 | }
|
---|
| 562 | }
|
---|
| 563 | /// <summary>Absolute values of array elements</summary>
|
---|
| 564 | /// <param name="A">Input array</param>
|
---|
| 565 | /// <returns>Absolute values of array elements</returns>
|
---|
| 566 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 567 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 568 | public unsafe static ILRetArray<Int64> abs (ILInArray< Int64 > A) {
|
---|
| 569 | using (ILScope.Enter(A)) {
|
---|
| 570 | if (A.IsEmpty)
|
---|
| 571 | return new ILRetArray<Int64>(A.Size);
|
---|
| 572 | ILSize inDim = A.Size;
|
---|
| 573 | Int64[] arrA = A.GetArrayForRead();
|
---|
| 574 | Int64 [] retArr;
|
---|
| 575 | int outLen = inDim.NumberOfElements;
|
---|
| 576 | bool inplace = true;
|
---|
| 577 |
|
---|
| 578 | if (!A.TryGetStorage4InplaceOp(out retArr)){
|
---|
| 579 | retArr = ILMemoryPool.Pool.New<Int64>(outLen);
|
---|
| 580 | inplace = false;
|
---|
| 581 | }
|
---|
| 582 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 583 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 584 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 585 | workItemLength = outLen / workItemCount;
|
---|
| 586 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 587 | } else {
|
---|
| 588 | workItemLength = outLen / 2;
|
---|
| 589 | workItemCount = 2;
|
---|
| 590 | }
|
---|
| 591 | } else {
|
---|
| 592 | workItemLength = outLen;
|
---|
| 593 | workItemCount = 1;
|
---|
| 594 | }
|
---|
| 595 | ILDenseStorage<Int64> retStorage = new ILDenseStorage<Int64>(retArr, inDim);
|
---|
| 596 |
|
---|
| 597 | Action<object> worker = data => {
|
---|
| 598 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 599 |
|
---|
| 600 | Int64* cp = ((Int64*)range.Item4 + range.Item1);
|
---|
| 601 | int len = range.Item2;
|
---|
| 602 | if (range.Item5) {
|
---|
| 603 | // inplace
|
---|
| 604 | while (len > 20) {
|
---|
| 605 | cp[0] = Math.Abs(cp[0] ) /*dummy*/;
|
---|
| 606 | cp[1] = Math.Abs(cp[1] ) /*dummy*/;
|
---|
| 607 | cp[2] = Math.Abs(cp[2] ) /*dummy*/;
|
---|
| 608 | cp[3] = Math.Abs(cp[3] ) /*dummy*/;
|
---|
| 609 | cp[4] = Math.Abs(cp[4] ) /*dummy*/;
|
---|
| 610 | cp[5] = Math.Abs(cp[5] ) /*dummy*/;
|
---|
| 611 | cp[6] = Math.Abs(cp[6] ) /*dummy*/;
|
---|
| 612 | cp[7] = Math.Abs(cp[7] ) /*dummy*/;
|
---|
| 613 | cp[8] = Math.Abs(cp[8] ) /*dummy*/;
|
---|
| 614 | cp[9] = Math.Abs(cp[9] ) /*dummy*/;
|
---|
| 615 | cp[10] = Math.Abs(cp[10] ) /*dummy*/;
|
---|
| 616 | cp[11] = Math.Abs(cp[11] ) /*dummy*/;
|
---|
| 617 | cp[12] = Math.Abs(cp[12] ) /*dummy*/;
|
---|
| 618 | cp[13] = Math.Abs(cp[13] ) /*dummy*/;
|
---|
| 619 | cp[14] = Math.Abs(cp[14] ) /*dummy*/;
|
---|
| 620 | cp[15] = Math.Abs(cp[15] ) /*dummy*/;
|
---|
| 621 | cp[16] = Math.Abs(cp[16] ) /*dummy*/;
|
---|
| 622 | cp[17] = Math.Abs(cp[17] ) /*dummy*/;
|
---|
| 623 | cp[18] = Math.Abs(cp[18] ) /*dummy*/;
|
---|
| 624 | cp[19] = Math.Abs(cp[19] ) /*dummy*/;
|
---|
| 625 | cp[20] = Math.Abs(cp[20] ) /*dummy*/;
|
---|
| 626 | cp+=21; len -= 21;
|
---|
| 627 | }
|
---|
| 628 | while (len-- > 0) {
|
---|
| 629 | *cp = Math.Abs(*cp ) /*dummy*/;
|
---|
| 630 | cp++;
|
---|
| 631 | }
|
---|
| 632 | } else {
|
---|
| 633 | Int64* ap = ((Int64*)range.Item3 + range.Item1);
|
---|
| 634 | while (len > 20) {
|
---|
| 635 | cp[0] = Math.Abs(ap[0] ) /*dummy*/;
|
---|
| 636 | cp[1] = Math.Abs(ap[1] ) /*dummy*/;
|
---|
| 637 | cp[2] = Math.Abs(ap[2] ) /*dummy*/;
|
---|
| 638 | cp[3] = Math.Abs(ap[3] ) /*dummy*/;
|
---|
| 639 | cp[4] = Math.Abs(ap[4] ) /*dummy*/;
|
---|
| 640 | cp[5] = Math.Abs(ap[5] ) /*dummy*/;
|
---|
| 641 | cp[6] = Math.Abs(ap[6] ) /*dummy*/;
|
---|
| 642 | cp[7] = Math.Abs(ap[7] ) /*dummy*/;
|
---|
| 643 | cp[8] = Math.Abs(ap[8] ) /*dummy*/;
|
---|
| 644 | cp[9] = Math.Abs(ap[9] ) /*dummy*/;
|
---|
| 645 | cp[10] = Math.Abs(ap[10] ) /*dummy*/;
|
---|
| 646 | cp[11] = Math.Abs(ap[11] ) /*dummy*/;
|
---|
| 647 | cp[12] = Math.Abs(ap[12] ) /*dummy*/;
|
---|
| 648 | cp[13] = Math.Abs(ap[13] ) /*dummy*/;
|
---|
| 649 | cp[14] = Math.Abs(ap[14] ) /*dummy*/;
|
---|
| 650 | cp[15] = Math.Abs(ap[15] ) /*dummy*/;
|
---|
| 651 | cp[16] = Math.Abs(ap[16] ) /*dummy*/;
|
---|
| 652 | cp[17] = Math.Abs(ap[17] ) /*dummy*/;
|
---|
| 653 | cp[18] = Math.Abs(ap[18] ) /*dummy*/;
|
---|
| 654 | cp[19] = Math.Abs(ap[19] ) /*dummy*/;
|
---|
| 655 | cp[20] = Math.Abs(ap[20] ) /*dummy*/;
|
---|
| 656 | ap += 21;
|
---|
| 657 | cp += 21;
|
---|
| 658 | len -= 21;
|
---|
| 659 | }
|
---|
| 660 | while (len-- > 0) {
|
---|
| 661 | *cp = Math.Abs(*ap ) /*dummy*/;
|
---|
| 662 | ap++;
|
---|
| 663 | cp++;
|
---|
| 664 | }
|
---|
| 665 | }
|
---|
| 666 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 667 | };
|
---|
| 668 |
|
---|
| 669 | fixed ( Int64* arrAP = arrA)
|
---|
| 670 | fixed ( Int64* retArrP = retArr) {
|
---|
| 671 | for (; i < workItemCount - 1; i++) {
|
---|
| 672 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 673 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 674 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 675 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 676 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 677 | }
|
---|
| 678 | // the last (or may the only) chunk is done right here
|
---|
| 679 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 680 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 681 |
|
---|
| 682 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 683 | }
|
---|
| 684 | return new ILRetArray<Int64>(retStorage);
|
---|
| 685 | }
|
---|
| 686 | }
|
---|
| 687 | /// <summary>Absolute values of array elements</summary>
|
---|
| 688 | /// <param name="A">Input array</param>
|
---|
| 689 | /// <returns>Absolute values of array elements</returns>
|
---|
| 690 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 691 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 692 | public unsafe static ILRetArray<Int32> abs (ILInArray< Int32 > A) {
|
---|
| 693 | using (ILScope.Enter(A)) {
|
---|
| 694 | if (A.IsEmpty)
|
---|
| 695 | return new ILRetArray<Int32>(A.Size);
|
---|
| 696 | ILSize inDim = A.Size;
|
---|
| 697 | Int32[] arrA = A.GetArrayForRead();
|
---|
| 698 | Int32 [] retArr;
|
---|
| 699 | int outLen = inDim.NumberOfElements;
|
---|
| 700 | bool inplace = true;
|
---|
| 701 |
|
---|
| 702 | if (!A.TryGetStorage4InplaceOp(out retArr)){
|
---|
| 703 | retArr = ILMemoryPool.Pool.New<Int32>(outLen);
|
---|
| 704 | inplace = false;
|
---|
| 705 | }
|
---|
| 706 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 707 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 708 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 709 | workItemLength = outLen / workItemCount;
|
---|
| 710 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 711 | } else {
|
---|
| 712 | workItemLength = outLen / 2;
|
---|
| 713 | workItemCount = 2;
|
---|
| 714 | }
|
---|
| 715 | } else {
|
---|
| 716 | workItemLength = outLen;
|
---|
| 717 | workItemCount = 1;
|
---|
| 718 | }
|
---|
| 719 | ILDenseStorage<Int32> retStorage = new ILDenseStorage<Int32>(retArr, inDim);
|
---|
| 720 |
|
---|
| 721 | Action<object> worker = data => {
|
---|
| 722 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 723 |
|
---|
| 724 | Int32* cp = ((Int32*)range.Item4 + range.Item1);
|
---|
| 725 | int len = range.Item2;
|
---|
| 726 | if (range.Item5) {
|
---|
| 727 | // inplace
|
---|
| 728 | while (len > 20) {
|
---|
| 729 | cp[0] = Math.Abs(cp[0] ) /*dummy*/;
|
---|
| 730 | cp[1] = Math.Abs(cp[1] ) /*dummy*/;
|
---|
| 731 | cp[2] = Math.Abs(cp[2] ) /*dummy*/;
|
---|
| 732 | cp[3] = Math.Abs(cp[3] ) /*dummy*/;
|
---|
| 733 | cp[4] = Math.Abs(cp[4] ) /*dummy*/;
|
---|
| 734 | cp[5] = Math.Abs(cp[5] ) /*dummy*/;
|
---|
| 735 | cp[6] = Math.Abs(cp[6] ) /*dummy*/;
|
---|
| 736 | cp[7] = Math.Abs(cp[7] ) /*dummy*/;
|
---|
| 737 | cp[8] = Math.Abs(cp[8] ) /*dummy*/;
|
---|
| 738 | cp[9] = Math.Abs(cp[9] ) /*dummy*/;
|
---|
| 739 | cp[10] = Math.Abs(cp[10] ) /*dummy*/;
|
---|
| 740 | cp[11] = Math.Abs(cp[11] ) /*dummy*/;
|
---|
| 741 | cp[12] = Math.Abs(cp[12] ) /*dummy*/;
|
---|
| 742 | cp[13] = Math.Abs(cp[13] ) /*dummy*/;
|
---|
| 743 | cp[14] = Math.Abs(cp[14] ) /*dummy*/;
|
---|
| 744 | cp[15] = Math.Abs(cp[15] ) /*dummy*/;
|
---|
| 745 | cp[16] = Math.Abs(cp[16] ) /*dummy*/;
|
---|
| 746 | cp[17] = Math.Abs(cp[17] ) /*dummy*/;
|
---|
| 747 | cp[18] = Math.Abs(cp[18] ) /*dummy*/;
|
---|
| 748 | cp[19] = Math.Abs(cp[19] ) /*dummy*/;
|
---|
| 749 | cp[20] = Math.Abs(cp[20] ) /*dummy*/;
|
---|
| 750 | cp+=21; len -= 21;
|
---|
| 751 | }
|
---|
| 752 | while (len-- > 0) {
|
---|
| 753 | *cp = Math.Abs(*cp ) /*dummy*/;
|
---|
| 754 | cp++;
|
---|
| 755 | }
|
---|
| 756 | } else {
|
---|
| 757 | Int32* ap = ((Int32*)range.Item3 + range.Item1);
|
---|
| 758 | while (len > 20) {
|
---|
| 759 | cp[0] = Math.Abs(ap[0] ) /*dummy*/;
|
---|
| 760 | cp[1] = Math.Abs(ap[1] ) /*dummy*/;
|
---|
| 761 | cp[2] = Math.Abs(ap[2] ) /*dummy*/;
|
---|
| 762 | cp[3] = Math.Abs(ap[3] ) /*dummy*/;
|
---|
| 763 | cp[4] = Math.Abs(ap[4] ) /*dummy*/;
|
---|
| 764 | cp[5] = Math.Abs(ap[5] ) /*dummy*/;
|
---|
| 765 | cp[6] = Math.Abs(ap[6] ) /*dummy*/;
|
---|
| 766 | cp[7] = Math.Abs(ap[7] ) /*dummy*/;
|
---|
| 767 | cp[8] = Math.Abs(ap[8] ) /*dummy*/;
|
---|
| 768 | cp[9] = Math.Abs(ap[9] ) /*dummy*/;
|
---|
| 769 | cp[10] = Math.Abs(ap[10] ) /*dummy*/;
|
---|
| 770 | cp[11] = Math.Abs(ap[11] ) /*dummy*/;
|
---|
| 771 | cp[12] = Math.Abs(ap[12] ) /*dummy*/;
|
---|
| 772 | cp[13] = Math.Abs(ap[13] ) /*dummy*/;
|
---|
| 773 | cp[14] = Math.Abs(ap[14] ) /*dummy*/;
|
---|
| 774 | cp[15] = Math.Abs(ap[15] ) /*dummy*/;
|
---|
| 775 | cp[16] = Math.Abs(ap[16] ) /*dummy*/;
|
---|
| 776 | cp[17] = Math.Abs(ap[17] ) /*dummy*/;
|
---|
| 777 | cp[18] = Math.Abs(ap[18] ) /*dummy*/;
|
---|
| 778 | cp[19] = Math.Abs(ap[19] ) /*dummy*/;
|
---|
| 779 | cp[20] = Math.Abs(ap[20] ) /*dummy*/;
|
---|
| 780 | ap += 21;
|
---|
| 781 | cp += 21;
|
---|
| 782 | len -= 21;
|
---|
| 783 | }
|
---|
| 784 | while (len-- > 0) {
|
---|
| 785 | *cp = Math.Abs(*ap ) /*dummy*/;
|
---|
| 786 | ap++;
|
---|
| 787 | cp++;
|
---|
| 788 | }
|
---|
| 789 | }
|
---|
| 790 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 791 | };
|
---|
| 792 |
|
---|
| 793 | fixed ( Int32* arrAP = arrA)
|
---|
| 794 | fixed ( Int32* retArrP = retArr) {
|
---|
| 795 | for (; i < workItemCount - 1; i++) {
|
---|
| 796 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 797 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 798 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 799 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 800 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 801 | }
|
---|
| 802 | // the last (or may the only) chunk is done right here
|
---|
| 803 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 804 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 805 |
|
---|
| 806 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 807 | }
|
---|
| 808 | return new ILRetArray<Int32>(retStorage);
|
---|
| 809 | }
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
| 813 |
|
---|
| 814 | }
|
---|
| 815 | } |
---|