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