Skip to content

Commit

Permalink
Random module test adjustments (#23908)
Browse files Browse the repository at this point in the history
A follow up to #23752

- fixes a few tests that were failing on 32 bit linux
- evidently the PCG random number generator will produce floating point
values that are more prone to floating point rounding errors on 32 bit
systems (as compared to the NPB random number generator).
   - the tests have been adjusted to use NPB instead of PCG
- removes deprecated symbols from some random module tests and adjusts
good files
- follow up to [this
comment](#23752 (comment))

**Testing:**
- [ ] failing tests passing on 32 bit linux
- [x] adjusted tests passing
- [x] paratest

[ trivial - not reviewed ]
  • Loading branch information
jeremiah-corrado authored Nov 27, 2023
2 parents ad9f732 + c3f0e1e commit 6d45247
Show file tree
Hide file tree
Showing 27 changed files with 322 additions and 739 deletions.
9 changes: 5 additions & 4 deletions modules/standard/Random.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,8 @@ module Random {
if isNothingType(probType) {
return _choiceUniform(stream, X, size, replace);
} else {
return _choiceProbabilities(stream, X, size, replace, prob);
var realStream = new randomStream(real, seed=stream.seed);
return _choiceProbabilities(realStream, X, size, replace, prob);
}
}

Expand Down Expand Up @@ -978,7 +979,7 @@ module Random {
// Begin sampling
if isNothingType(sizeType) {
// Return 1 sample
var randNum = stream.getNext(resultType=real);
var randNum = stream.getNext();
var (found, idx) = Search.binarySearch(cumulativeArr, randNum);
return X.dim(0).orderToIndex(idx);
} else {
Expand All @@ -997,7 +998,7 @@ module Random {

if replace {
for sample in samples {
var randNum = stream.getNext(resultType=real);
var randNum = stream.getNext();
var (found, idx) = Search.binarySearch(cumulativeArr, randNum);
sample = X.dim(0).orderToIndex(idx);
}
Expand All @@ -1014,7 +1015,7 @@ module Random {
}

var remainingSamples = samples.sizeAs(int) - indicesChosen.sizeAs(int);
for randNum in stream.iterate({1..(samples.sizeAs(int) - indicesChosen.sizeAs(int))}, resultType=real) {
for randNum in stream.iterate({1..(samples.sizeAs(int) - indicesChosen.sizeAs(int))}) {
// A potential optimization: Generate rand nums ahead of time
// and do a multi-target binary search to find all of their positions
var (found, indexChosen) = Search.binarySearch(cumulativeArr, randNum);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,80 +1,74 @@
use choiceTestUtils, Random;

proc main() {
var pcg = createRandomStream(real, algorithm=RNG.PCG, seed=42);
runTests(pcg);
var pcgInt = createRandomStream(int, algorithm=RNG.PCG, seed=420);
runTests(pcgInt);
}
var stream = new randomStream(int, seed=420);

test(stream, [1], trials=10);
test(stream, [1, 2, 3]);

var uints: [1..3] uint = [100:uint, 100:uint, 400:uint];
test(stream, uints);

var real32s: [1..4] real(32) = [0.1:real(32), 0.2:real(32), 0.3:real(32), 0.4:real(32)];
test(stream, real32s);

proc runTests(stream) {
test(stream, [1], trials=10);
test(stream, [1, 2, 3]);

var uints: [1..3] uint = [100:uint, 100:uint, 400:uint];
test(stream, uints);

var real32s: [1..4] real(32) = [0.1:real(32), 0.2:real(32), 0.3:real(32), 0.4:real(32)];
test(stream, real32s);

// user-defined type
var rArr = [new R(1), new R(2), new R(3)];
test(stream, rArr, trials=1);

var pArr = [1, 1, 2];
test(stream, rArr, prob=pArr, size=2, replace=false, trials=1);

// offset & strided domain
var strideDom = {10..#10 by 3};
var strideArr: [strideDom] int;
var strideProb: [strideDom] real;
for i in strideDom {
strideArr[i] = i;
strideProb[i] = i;
}


test(stream, strideArr, size=20);
test(stream, strideArr, prob=strideProb, size=20);

// prob array
test(stream, [1,2], prob=[0, 1], trials=10);
test(stream, [1,2], prob=[0.1, 0.9]);
test(stream, [1,1,2], prob=[0.1, 0.4, 0.5]);

// Numeric types
var preal32: [0..1] real(32) = 1;
test(stream, [1,2], prob=preal32, size=1000, trials=1);
var pint: [0..1] int = 1;
test(stream, [1,2], prob=pint, size=1000, trials=1);
var pint32: [0..1] int(32) = 1;
test(stream, [1,2], prob=pint32, size=1000, trials=1);
var puint32: [0..1] uint(32) = 1;
test(stream, [1,2], prob=puint32, size=1000, trials=1);
var puint16: [0..1] uint(16) = 1;
test(stream, [1,2], prob=puint16, size=1000, trials=1);

// replace=false
test(stream, [1,2,3,4], size=4, replace=false, trials=1);

//// domain-type size
var A = [1,2,3,4];
var p = [0.1, 0.2, 0.3, 0.4];
test(stream, A, prob=p, size={0..3});
test(stream, A, prob=p, size={0..3, 0..2});
test(stream, A, prob=p, size={0..3, 0..2, 0..1});

// Ensure we maintain reference to domain passed in
var dom = {0..4};
var B: [dom] int;

var ret = stream.choice([1,2,3,4,5], size=dom);

dom = {0..9};
if ret.size != 10 then
writeln('error: domain reference not maintained');
// user-defined type
var rArr = [new R(1), new R(2), new R(3)];
test(stream, rArr, trials=1);

var pArr = [1, 1, 2];
test(stream, rArr, prob=pArr, size=2, replace=false, trials=1);

// offset & strided domain
var strideDom = {10..#10 by 3};
var strideArr: [strideDom] int;
var strideProb: [strideDom] real;
for i in strideDom {
strideArr[i] = i;
strideProb[i] = i;
}


test(stream, strideArr, size=20);
test(stream, strideArr, prob=strideProb, size=20);

// prob array
test(stream, [1,2], prob=[0, 1], trials=10);
test(stream, [1,2], prob=[0.1, 0.9]);
test(stream, [1,1,2], prob=[0.1, 0.4, 0.5]);

// Numeric types
var preal32: [0..1] real(32) = 1;
test(stream, [1,2], prob=preal32, size=1000, trials=1);
var pint: [0..1] int = 1;
test(stream, [1,2], prob=pint, size=1000, trials=1);
var pint32: [0..1] int(32) = 1;
test(stream, [1,2], prob=pint32, size=1000, trials=1);
var puint32: [0..1] uint(32) = 1;
test(stream, [1,2], prob=puint32, size=1000, trials=1);
var puint16: [0..1] uint(16) = 1;
test(stream, [1,2], prob=puint16, size=1000, trials=1);

// replace=false
test(stream, [1,2,3,4], size=4, replace=false, trials=1);

//// domain-type size
var A = [1,2,3,4];
var p = [0.1, 0.2, 0.3, 0.4];
test(stream, A, prob=p, size={0..3});
test(stream, A, prob=p, size={0..3, 0..2});
test(stream, A, prob=p, size={0..3, 0..2, 0..1});

// Ensure we maintain reference to domain passed in
var dom = {0..4};
var B: [dom] int;

var ret = stream.choice([1,2,3,4,5], size=dom);

dom = {0..9};
if ret.size != 10 then
writeln('error: domain reference not maintained');


/* User-defined type */
record R {
var value = 0;
Expand Down
Loading

0 comments on commit 6d45247

Please sign in to comment.