|
2 | 2 | using Numerics; |
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.Threading; |
5 | 6 | using System.Threading.Tasks; |
6 | 7 |
|
7 | 8 | namespace Utilities |
@@ -374,6 +375,118 @@ public void Test_ParallelAdd_NaNAccumulatorTerminates() |
374 | 375 | Assert.IsTrue(double.IsNaN(result)); |
375 | 376 | } |
376 | 377 |
|
| 378 | + /// <summary> |
| 379 | + /// Verifies signed zero, NaN, and infinity follow double-precision addition semantics. |
| 380 | + /// </summary> |
| 381 | + [TestMethod] |
| 382 | + public void Test_ParallelAdd_HandlesSpecialValues() |
| 383 | + { |
| 384 | + double negativeZero = BitConverter.Int64BitsToDouble(long.MinValue); |
| 385 | + |
| 386 | + double total = negativeZero; |
| 387 | + double result = Tools.ParallelAdd(ref total, negativeZero); |
| 388 | + |
| 389 | + Assert.AreEqual(long.MinValue, BitConverter.DoubleToInt64Bits(total)); |
| 390 | + Assert.AreEqual(long.MinValue, BitConverter.DoubleToInt64Bits(result)); |
| 391 | + |
| 392 | + total = negativeZero; |
| 393 | + result = Tools.ParallelAdd(ref total, 0d); |
| 394 | + |
| 395 | + Assert.AreEqual(0L, BitConverter.DoubleToInt64Bits(total)); |
| 396 | + Assert.AreEqual(0L, BitConverter.DoubleToInt64Bits(result)); |
| 397 | + |
| 398 | + total = 1d; |
| 399 | + result = Tools.ParallelAdd(ref total, double.NaN); |
| 400 | + |
| 401 | + Assert.IsTrue(double.IsNaN(total)); |
| 402 | + Assert.IsTrue(double.IsNaN(result)); |
| 403 | + |
| 404 | + total = double.PositiveInfinity; |
| 405 | + result = Tools.ParallelAdd(ref total, 1d); |
| 406 | + |
| 407 | + Assert.AreEqual(double.PositiveInfinity, total); |
| 408 | + Assert.AreEqual(double.PositiveInfinity, result); |
| 409 | + |
| 410 | + total = double.NegativeInfinity; |
| 411 | + result = Tools.ParallelAdd(ref total, -1d); |
| 412 | + |
| 413 | + Assert.AreEqual(double.NegativeInfinity, total); |
| 414 | + Assert.AreEqual(double.NegativeInfinity, result); |
| 415 | + |
| 416 | + total = double.PositiveInfinity; |
| 417 | + result = Tools.ParallelAdd(ref total, double.NegativeInfinity); |
| 418 | + |
| 419 | + Assert.IsTrue(double.IsNaN(total)); |
| 420 | + Assert.IsTrue(double.IsNaN(result)); |
| 421 | + } |
| 422 | + |
| 423 | + /// <summary> |
| 424 | + /// Verifies concurrent mixed-sign additions are all committed when the accumulator revisits values. |
| 425 | + /// </summary> |
| 426 | + [TestMethod] |
| 427 | + public void Test_ParallelAdd_CommitsMixedSignUpdates() |
| 428 | + { |
| 429 | + const int updateCount = 25000; |
| 430 | + double total = 0d; |
| 431 | + |
| 432 | + Parallel.For( |
| 433 | + 0, |
| 434 | + updateCount, |
| 435 | + index => Tools.ParallelAdd(ref total, index % 2 == 0 ? 2d : -1d)); |
| 436 | + |
| 437 | + Assert.AreEqual(updateCount / 2d, total); |
| 438 | + } |
| 439 | + |
| 440 | + /// <summary> |
| 441 | + /// Verifies a negative-to-positive zero race cannot cause finite concurrent updates to be lost. |
| 442 | + /// </summary> |
| 443 | + [TestMethod] |
| 444 | + public void Test_ParallelAdd_SignedZeroContentionDoesNotLoseUpdates() |
| 445 | + { |
| 446 | + const int rounds = 256; |
| 447 | + double total = 0d; |
| 448 | + bool everyUpdateCommitted = true; |
| 449 | + double negativeZero = BitConverter.Int64BitsToDouble(long.MinValue); |
| 450 | + |
| 451 | + using (var startBarrier = new Barrier(5)) |
| 452 | + using (var finishBarrier = new Barrier(5)) |
| 453 | + { |
| 454 | + Func<double, Task> createWorker = valueToAdd => Task.Factory.StartNew( |
| 455 | + () => |
| 456 | + { |
| 457 | + for (int round = 0; round < rounds; round++) |
| 458 | + { |
| 459 | + startBarrier.SignalAndWait(); |
| 460 | + Tools.ParallelAdd(ref total, valueToAdd); |
| 461 | + finishBarrier.SignalAndWait(); |
| 462 | + } |
| 463 | + }, |
| 464 | + CancellationToken.None, |
| 465 | + TaskCreationOptions.LongRunning, |
| 466 | + TaskScheduler.Default); |
| 467 | + |
| 468 | + Task[] workers = |
| 469 | + { |
| 470 | + createWorker(0d), |
| 471 | + createWorker(0d), |
| 472 | + createWorker(1d), |
| 473 | + createWorker(1d) |
| 474 | + }; |
| 475 | + |
| 476 | + for (int round = 0; round < rounds; round++) |
| 477 | + { |
| 478 | + total = negativeZero; |
| 479 | + startBarrier.SignalAndWait(); |
| 480 | + finishBarrier.SignalAndWait(); |
| 481 | + if (total != 2d) everyUpdateCommitted = false; |
| 482 | + } |
| 483 | + |
| 484 | + Task.WaitAll(workers); |
| 485 | + } |
| 486 | + |
| 487 | + Assert.IsTrue(everyUpdateCommitted, "A finite update was lost during signed-zero contention."); |
| 488 | + } |
| 489 | + |
377 | 490 | /// <summary> |
378 | 491 | /// Testing the Log-sum-exponential function. |
379 | 492 | /// <para> |
|
0 commit comments