Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace random function to make CI reproducible #665

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ jobs:
- name: Run C++ field Tests
working-directory: ./icicle/build/tests
if: needs.check-changed-files.outputs.cpp == 'true'
run: ctest
run: ctest --output-on-failure
7 changes: 4 additions & 3 deletions icicle/include/icicle/fields/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endif // __CUDACC__

#include "icicle/errors.h"
#include "icicle/utils/rand_gen.hpp"
#include "host_math.h"
#include "storage.h"

Expand Down Expand Up @@ -689,14 +690,14 @@ class Field
return rv;
}

// NOTE this function is used for test and examples - it assumed it is executed on a single-thread (no two threads
// accessing rand_generator at the same time)
static HOST_INLINE Field rand_host()
{
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<unsigned> distribution;
Field value{};
for (unsigned i = 0; i < TLC; i++)
value.limbs_storage.limbs[i] = distribution(generator);
value.limbs_storage.limbs[i] = distribution(rand_generator);
while (lt(Field{get_modulus()}, value))
value = value - Field{get_modulus()};
return value;
Expand Down
5 changes: 5 additions & 0 deletions icicle/include/icicle/utils/rand_gen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <random>

inline std::mt19937 rand_generator = std::mt19937{std::random_device{}()};
static void seed_rand_generator(unsigned seed) { rand_generator.seed(seed); }
7 changes: 7 additions & 0 deletions icicle/tests/test_base.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once
#include <time.h>
#include <random>

#include <gtest/gtest.h>
#include "icicle/runtime.h"
#include "icicle/utils/log.h"
#include "icicle/utils/rand_gen.hpp"

using FpMiliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>;
#define START_TIMER(timer) auto timer##_start = std::chrono::high_resolution_clock::now();
Expand All @@ -25,6 +28,10 @@ class IcicleTestBase : public ::testing::Test
// SetUpTestSuite/TearDownTestSuite are called once for the entire test suite
static void SetUpTestSuite()
{
unsigned seed = time(NULL);
srand(seed);
ICICLE_LOG_INFO << "Seed for tests is: " << seed;
Koren-Brand marked this conversation as resolved.
Show resolved Hide resolved
seed_rand_generator(seed);
#ifdef BACKEND_BUILD_DIR
setenv("ICICLE_BACKEND_INSTALL_DIR", BACKEND_BUILD_DIR, 0 /*=replace*/);
#endif
Expand Down
Loading