Skip to content

Commit

Permalink
Changed random function to use a better generator
Browse files Browse the repository at this point in the history
Signed-off-by: Koren-Brand <[email protected]>
  • Loading branch information
Koren-Brand committed Nov 26, 2024
1 parent c64c029 commit f934685
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion icicle/include/icicle/fields/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,18 @@ class Field
return rv;
}

inline static std::mt19937 field_rand_generator = std::mt19937{};

static void seed_rand_generator(unsigned seed) { field_rand_generator.seed(seed); }

// NOTE this function is used for test and examples - it assumed it is executed on a single-thread (no two threads
// accessing field_rand_generator at the same time)
static HOST_INLINE Field rand_host()
{
std::uniform_int_distribution<unsigned> distribution;
Field value{};
for (unsigned i = 0; i < TLC; i++)
value.limbs_storage.limbs[i] = rand();
value.limbs_storage.limbs[i] = distribution(field_rand_generator);
while (lt(Field{get_modulus()}, value))
value = value - Field{get_modulus()};
return value;
Expand Down
8 changes: 6 additions & 2 deletions icicle/tests/test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "icicle/runtime.h"
#include "icicle/utils/log.h"

#include "icicle/fields/field_config.h"
using namespace field_config; // To have access to the Field rand-gen

using FpMiliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>;
#define START_TIMER(timer) auto timer##_start = std::chrono::high_resolution_clock::now();
#define END_TIMER(timer, msg, enable) \
Expand All @@ -22,14 +25,15 @@ class IcicleTestBase : public ::testing::Test
{
public:
static inline std::vector<std::string> s_registered_devices;
static inline std::string s_main_device = UNKOWN_DEVICE;
static inline std::string s_main_device = "CPU";
static inline std::string s_ref_device = "CPU"; // assuming always present
// SetUpTestSuite/TearDownTestSuite are called once for the entire test suite
static void SetUpTestSuite()
{
auto seed = time(NULL);
unsigned seed = time(NULL);
srand(seed);
ICICLE_LOG_INFO << "Seed for tests is: " << seed;
scalar_t::seed_rand_generator(seed);
#ifdef BACKEND_BUILD_DIR
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
#endif
Expand Down

0 comments on commit f934685

Please sign in to comment.