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