text: // Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
  //
  // Permission is hereby granted, free of charge, to any person obtaining a copy
  // of this software and associated documentation files (the "Software"), to deal
  // in the Software without restriction, including without limitation the rights
  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the Software is
  // furnished to do so, subject to the following conditions:
  //
  // The above copyright notice and this permission notice shall be included in
  // all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  // THE SOFTWARE.
  
  #include <stdio.h>
  #include <gtest/gtest.h>
  
  #include <hip/hip_runtime.h>
  #include <rocrand/rocrand.h>
  #include <rocrand/rocrand_sobol32_precomputed.h>
  
  #include <rng/generator_type.hpp>
  #include <rng/generators.hpp>
  
  #include "test_common.hpp"
  #include "test_rocrand_common.hpp"
  
  TEST(rocrand_sobol32_qrng_tests, uniform_uint_test)
  {
      const size_t size = 1313;
      unsigned int * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(unsigned int) * size));
  
      rocrand_sobol32 g;
      ROCRAND_CHECK(g.generate(data, size));
      HIP_CHECK(hipDeviceSynchronize());
  
      unsigned int host_data[size];
      HIP_CHECK(hipMemcpy(host_data, data, sizeof(unsigned int) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      unsigned long long sum = 0;
      for(size_t i = 0; i < size; i++)
      {
          sum += host_data[i];
      }
      const unsigned int mean = sum / size;
      ASSERT_NEAR(mean, UINT_MAX / 2, UINT_MAX / 20);
  
      HIP_CHECK(hipFree(data));
  }
  
  TEST(rocrand_sobol32_qrng_tests, uniform_float_test)
  {
      const size_t size = 1313;
      float * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(float) * size));
  
      rocrand_sobol32 g;
      ROCRAND_CHECK(g.generate(data, size));
      HIP_CHECK(hipDeviceSynchronize());
  
      float host_data[size];
      HIP_CHECK(hipMemcpy(host_data, data, sizeof(float) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      double sum = 0;
      for(size_t i = 0; i < size; i++)
      {
          ASSERT_GT(host_data[i], 0.0f);
          ASSERT_LE(host_data[i], 1.0f);
          sum += host_data[i];
      }
      const float mean = sum / size;
      ASSERT_NEAR(mean, 0.5f, 0.05f);
  
      HIP_CHECK(hipFree(data));
  }
  
  TEST(rocrand_sobol32_qrng_tests, normal_float_test)
  {
      const size_t size = 1313;
      float * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(float) * size));
  
      rocrand_sobol32 g;
      ROCRAND_CHECK(g.generate_normal(data, size, 2.0f, 5.0f));
      HIP_CHECK(hipDeviceSynchronize());
  
      float host_data[size];
      HIP_CHECK(hipMemcpy(host_data, data, sizeof(float) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      float mean = 0.0f;
      for(size_t i = 0; i < size; i++)
      {
          mean += host_data[i];
      }
      mean = mean / size;
  
      float std = 0.0f;
      for(size_t i = 0; i < size; i++)
      {
          std += std::pow(host_data[i] - mean, 2);
      }
      std = sqrt(std / size);
  
      EXPECT_NEAR(2.0f, mean, 0.4f); // 20%
      EXPECT_NEAR(5.0f, std, 1.0f); // 20%
  
      HIP_CHECK(hipFree(data));
  }
  
  TEST(rocrand_sobol32_qrng_tests, poisson_test)
  {
      const size_t size = 1313;
      unsigned int * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(unsigned int) * size));
  
      rocrand_sobol32 g;
      ROCRAND_CHECK(g.generate_poisson(data, size, 5.5));
      HIP_CHECK(hipDeviceSynchronize());
  
      unsigned int host_data[size];
      HIP_CHECK(hipMemcpy(host_data, data, sizeof(unsigned int) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      double mean = 0.0;
      for(size_t i = 0; i < size; i++)
      {
          mean += host_data[i];
      }
      mean = mean / size;
  
      double var = 0.0;
      for(size_t i = 0; i < size; i++)
      {
          double x = host_data[i] - mean;
          var += x * x;
      }
      var = var / size;
  
      EXPECT_NEAR(mean, 5.5, std::max(1.0, 5.5 * 1e-2));
      EXPECT_NEAR(var, 5.5, std::max(1.0, 5.5 * 1e-2));
  
      HIP_CHECK(hipFree(data));
  }
  
  TEST(rocrand_sobol32_qrng_tests, dimesions_test)
  {
      const size_t size = 12345;
      float * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(float) * size));
  
      rocrand_sobol32 g;
  
      ROCRAND_CHECK(g.generate(data, size));
  
      ROCRAND_CHECK(g.set_dimensions(4));
      EXPECT_EQ(g.generate(data, size), ROCRAND_STATUS_LENGTH_NOT_MULTIPLE);
  
      ROCRAND_CHECK(g.set_dimensions(15));
      ROCRAND_CHECK(g.generate(data, size));
  
      HIP_CHECK(hipDeviceSynchronize());
      HIP_CHECK(hipFree(data));
  }
  
  // Check if the numbers generated by first generate() call are different from
  // the numbers generated by the 2nd call (same generator)
  TEST(rocrand_sobol32_qrng_tests, state_progress_test)
  {
      // Device data
      const size_t size = 1025;
      unsigned int * data;
      HIP_CHECK(hipMallocHelper(&data, sizeof(unsigned int) * size));
  
      // Generator
      rocrand_sobol32 g0;
  
      // Generate using g0 and copy to host
      ROCRAND_CHECK(g0.generate(data, size));
      HIP_CHECK(hipDeviceSynchronize());
  
      unsigned int host_data1[size];
      HIP_CHECK(hipMemcpy(host_data1, data, sizeof(unsigned int) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      // Generate using g0 and copy to host
      ROCRAND_CHECK(g0.generate(data, size));
      HIP_CHECK(hipDeviceSynchronize());
  
      unsigned int host_data2[size];
      HIP_CHECK(hipMemcpy(host_data2, data, sizeof(unsigned int) * size, hipMemcpyDeviceToHost));
      HIP_CHECK(hipDeviceSynchronize());
  
      size_t same = 0;
      for(size_t i = 0; i < size; i++)
      {
          if(host_data1[i] == host_data2[i]) same++;
      }
      // It may happen that numbers are the same, so we
      // just make sure that most of them are different.
      EXPECT_LT(same, static_cast<size_t>(0.01f * size));
  
      HIP_CHECK(hipFree(data));
  }
  
  TEST(rocrand_sobol32_qrng_tests, discard_test)
  {
      rocrand_sobol32::engine_type engine1(&h_sobol32_direction_vectors[32], 678);
      rocrand_sobol32::engine_type engine2(&h_sobol32_direction_vectors[32], 676);
  
      EXPECT_NE(engine1(), engine2());
  
      engine2.discard();
  
      EXPECT_NE(engine1(), engine2());
  
      engine2.discard();
  
      EXPECT_EQ(engine1(), engine2());
      EXPECT_EQ(engine1(), engine2());
  
      const unsigned int ds[] = {
          0, 1, 4, 37, 583, 7452,
          21032, 35678, 66778, 10313475, 82120230
      };
  
      for (auto d : ds)
      {
          for (unsigned int i = 0; i < d; i++)
          {
              engine1.discard();
          }
          engine2.discard(d);
  
          EXPECT_EQ(engine1(), engine2());
      }
  }
  
  TEST(rocrand_sobol32_qrng_tests, discard_stride_test)
  {
      rocrand_sobol32::engine_type engine1(&h_sobol32_direction_vectors[64], 123);
      rocrand_sobol32::engine_type engine2(&h_sobol32_direction_vectors[64], 123);
  
      EXPECT_EQ(engine1(), engine2());
  
      const unsigned int ds[] = {
          1, 10, 12, 20, 4, 5, 30
      };
  
      for (auto d : ds)
      {
          engine1.discard(1 << d);
          engine2.discard_stride(1 << d);
  
          EXPECT_EQ(engine1(), engine2());
      }
  }
  