Skip to content

Commit

Permalink
Fix sinLUT tests (chapel-lang#24547)
Browse files Browse the repository at this point in the history
Fixes the .good files for the sinLUT tests I added. These were relying
on random numbers with a seed that varied. This PR passes a seed value
to `srand` that is constant.

Also, due to platform differences in the `rand` algorithm, this PR adds
a PREDIFF to validate that Calculated and Lookup give the same answer,
since putting just the value returned in the good file is not portable.

tested on MacOS and Linux, with and without comm

[Reviewed by @benharsh]
  • Loading branch information
jabraham17 authored Mar 7, 2024
2 parents 7d0ceb3 + bdb0fe8 commit c5d7ab5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 45 deletions.
23 changes: 23 additions & 0 deletions test/studies/sinLUT/PREDIFF
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

cp $2 $2.prediff.tmp
> $2

# check that Chapel Calculated == Chapel Lookup
calc=`sed '1q;d' $2.prediff.tmp | sed 's/.*: //'`
lookup=`sed '2q;d' $2.prediff.tmp | sed 's/.*: //'`
if [ "$calc" = "$lookup" ]; then
echo "Chapel Calculated == Chapel Lookup" >> $2
else
echo "Chapel Calculated != Chapel Lookup" >> $2
fi
# check that C Caclulated == C Lookup
calc=`sed '3q;d' $2.prediff.tmp | sed 's/.*: //'`
lookup=`sed '4q;d' $2.prediff.tmp | sed 's/.*: //'`
if [ "$calc" = "$lookup" ]; then
echo "C Calculated == C Lookup" >> $2
else
echo "C Calculated != C Lookup" >> $2
fi

rm $2.prediff.tmp
11 changes: 4 additions & 7 deletions test/studies/sinLUT/c_sin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#include "c_sin.h"

static float table[256];

float c_sin(unsigned int size, int iterations, float* resArray)
float c_sin(unsigned int size, int iterations, unsigned int seed, float* resArray)
{
time_t t;
srand((unsigned )time(&t));
srand(seed);

for (int i=0; i < iterations; i++)
{
Expand All @@ -28,11 +26,10 @@ void fillTable(unsigned int size)
table[i] = sin(2.0 * 3.1415927 * (float) i / (float) size);
}

float c_table(unsigned int size, int iterations, float* resArray)
float c_table(unsigned int size, int iterations, unsigned int seed, float* resArray)
{
time_t t;
fillTable(size);
srand((unsigned )time(&t));
srand(seed);

for (int i=0; i < iterations; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions test/studies/sinLUT/c_sin.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
float c_sin(unsigned int size, int iterations, float* resArray);
float c_sin(unsigned int size, int iterations, unsigned int seed, float* resArray);
void fillTable(unsigned int size);
float c_table(unsigned int size, int iterations, float* resArray);
float c_table(unsigned int size, int iterations, unsigned int seed, float* resArray);

16 changes: 11 additions & 5 deletions test/studies/sinLUT/chplSin.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use Time;
use Math;
use CTypes;

config const seed = 314159;
config const seed: uint(32) = 314159;
config const correctness = false;

extern proc c_sin(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_sin(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);

proc sin_calc(size: uint(32), iterations : int(32)): real(32) {

Expand Down Expand Up @@ -76,15 +82,15 @@ proc main()
var resArray = allocate(real(32), iterations);

c_calc.start();
var c_calc_res = c_sin(size, iterations, resArray);
var c_calc_res = c_sin(size, iterations, seed, resArray);
c_calc.stop();
write("C Calculated: ");
if correctness
then writeln(c_calc_res);
else writeln(c_calc.elapsed(), " seconds");

c_lookup.start();
var table_calc_res = c_table(size, iterations, resArray);
var table_calc_res = c_table(size, iterations, seed, resArray);
c_lookup.stop();
write("C Lookup: ");
if correctness
Expand Down
6 changes: 2 additions & 4 deletions test/studies/sinLUT/chplSin.good
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Chapel Calculated: -1.2311
Chapel Lookup: -1.2311
C Calculated: -1.2311
C Lookup: -1.2311
Chapel Calculated == Chapel Lookup
C Calculated == C Lookup
16 changes: 11 additions & 5 deletions test/studies/sinLUT/chplSinBlock.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ use Math;
use CTypes;
use BlockDist;

config const seed = 314159;
config const seed: uint(32) = 314159;
config const correctness = false;

extern proc c_sin(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_sin(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);

proc sin_calc(size: uint(32), iterations : int(32)): real(32) {

Expand Down Expand Up @@ -115,15 +121,15 @@ proc main()
var resArray = allocate(real(32), iterations);

c_calc.start();
var c_calc_res = c_sin(size, iterations, resArray);
var c_calc_res = c_sin(size, iterations, seed, resArray);
c_calc.stop();
write("C Calculated: ");
if correctness
then writeln(c_calc_res);
else writeln(c_calc.elapsed(), " seconds");

c_lookup.start();
var table_calc_res = c_table(size, iterations, resArray);
var table_calc_res = c_table(size, iterations, seed, resArray);
c_lookup.stop();
write("C Lookup: ");
if correctness
Expand Down
6 changes: 2 additions & 4 deletions test/studies/sinLUT/chplSinBlock.good
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Chapel Calculated: -1.2311
Chapel Lookup: -1.2311
C Calculated: -1.2311
C Lookup: -1.2311
Chapel Calculated == Chapel Lookup
C Calculated == C Lookup
16 changes: 11 additions & 5 deletions test/studies/sinLUT/chplSinReplicated.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ use Math;
use CTypes;
use ReplicatedDist;

config const seed = 314159;
config const seed: uint(32) = 314159;
config const correctness = false;

extern proc c_sin(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_sin(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);

proc sin_calc(size: uint(32), iterations : int(32)): real(32) {

Expand Down Expand Up @@ -114,15 +120,15 @@ proc main()
var resArray = allocate(real(32), iterations);

c_calc.start();
var c_calc_res = c_sin(size, iterations, resArray);
var c_calc_res = c_sin(size, iterations, seed, resArray);
c_calc.stop();
write("C Calculated: ");
if correctness
then writeln(c_calc_res);
else writeln(c_calc.elapsed(), " seconds");

c_lookup.start();
var table_calc_res = c_table(size, iterations, resArray);
var table_calc_res = c_table(size, iterations, seed, resArray);
c_lookup.stop();
write("C Lookup: ");
if correctness
Expand Down
6 changes: 2 additions & 4 deletions test/studies/sinLUT/chplSinReplicated.good
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Chapel Calculated: -1.2311
Chapel Lookup: -1.2311
C Calculated: -1.2311
C Lookup: -1.2311
Chapel Calculated == Chapel Lookup
C Calculated == C Lookup
16 changes: 11 additions & 5 deletions test/studies/sinLUT/chplSinReplicatedReplicand.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ use Math;
use CTypes;
use ReplicatedDist;

config const seed = 314159;
config const seed: uint(32) = 314159;
config const correctness = false;

extern proc c_sin(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32), iterations: int(32), resArray: c_ptr(real(32))): real(32);
extern proc c_sin(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);
extern proc c_table(size: uint(32),
iterations: int(32),
seed: uint(32),
resArray: c_ptr(real(32))): real(32);

proc sin_calc(size: uint(32), iterations : int(32)): real(32) {

Expand Down Expand Up @@ -114,15 +120,15 @@ proc main()
var resArray = allocate(real(32), iterations);

c_calc.start();
var c_calc_res = c_sin(size, iterations, resArray);
var c_calc_res = c_sin(size, iterations, seed, resArray);
c_calc.stop();
write("C Calculated: ");
if correctness
then writeln(c_calc_res);
else writeln(c_calc.elapsed(), " seconds");

c_lookup.start();
var table_calc_res = c_table(size, iterations, resArray);
var table_calc_res = c_table(size, iterations, seed, resArray);
c_lookup.stop();
write("C Lookup: ");
if correctness
Expand Down
6 changes: 2 additions & 4 deletions test/studies/sinLUT/chplSinReplicatedReplicand.good
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Chapel Calculated: -1.2311
Chapel Lookup: -1.2311
C Calculated: -1.2311
C Lookup: -1.2311
Chapel Calculated == Chapel Lookup
C Calculated == C Lookup

0 comments on commit c5d7ab5

Please sign in to comment.