Skip to content

Commit

Permalink
Moving rand generator to a designated file in utils
Browse files Browse the repository at this point in the history
Signed-off-by: Koren-Brand <koren@ingonyama.com>
  • Loading branch information
Koren-Brand committed Nov 28, 2024
1 parent b3c8c46 commit ddf4f3a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
9 changes: 3 additions & 6 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.cpp"
#include "host_math.h"
#include "storage.h"

Expand Down Expand Up @@ -689,18 +690,14 @@ 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)
// accessing 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] = distribution(field_rand_generator);
value.limbs_storage.limbs[i] = distribution(rand_generator);
while (lt(Field{get_modulus()}, value))
value = value - Field{get_modulus()};
return value;
Expand Down
2 changes: 1 addition & 1 deletion icicle/include/icicle/fields/quartic_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class QuarticExtensionField
FF::reduce(
(CONFIG::nonresidue_is_negative
? (FF::mul_wide(xs.real, x0) + FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))
: (FF::mul_wide(xs.real, x0)) - FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))),
: (FF::mul_wide(xs.real, x0))-FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))),
FF::reduce(
(CONFIG::nonresidue_is_negative
? FWide::neg(FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im3, x2)))
Expand Down
12 changes: 4 additions & 8 deletions icicle/include/icicle/fields/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(1))
#endif
storage<1>
{
storage<1> {
static constexpr unsigned LC = 1;
uint32_t limbs[1];
};
Expand All @@ -28,8 +27,7 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(1))
#endif
storage<3>
{
storage<3> {
static constexpr unsigned LC = 3;
uint32_t limbs[3];
};
Expand All @@ -40,8 +38,7 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(LIMBS_COUNT))
#endif
storage
{
storage {
static_assert(LIMBS_COUNT % 2 == 0, "odd number of limbs is not supported\n");
static constexpr unsigned LC = LIMBS_COUNT;
union { // works only with even LIMBS_COUNT
Expand All @@ -55,7 +52,6 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(LIMBS_COUNT))
#endif
storage_array
{
storage_array {
storage<LIMBS_COUNT> storages[OMEGAS_COUNT];
};
5 changes: 5 additions & 0 deletions icicle/include/icicle/utils/rand_gen.cpp
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); }
8 changes: 3 additions & 5 deletions icicle/tests/test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <gtest/gtest.h>
#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
#include "icicle/utils/rand_gen.cpp"

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,15 +23,15 @@ class IcicleTestBase : public ::testing::Test
{
public:
static inline std::vector<std::string> s_registered_devices;
static inline std::string s_main_device = "CPU";
static inline std::string s_main_device = UNKOWN_DEVICE;
static inline std::string s_ref_device = "CPU"; // assuming always present
// 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;
scalar_t::seed_rand_generator(seed);
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 ddf4f3a

Please sign in to comment.