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