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

Unrealistic result of StdNormalGen. #15

Open
kilasuelika opened this issue Nov 26, 2020 · 4 comments
Open

Unrealistic result of StdNormalGen. #15

kilasuelika opened this issue Nov 26, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@kilasuelika
Copy link

My code:

#include <iostream>
#include <functional>

#include <Eigen/Dense>
#include <EigenRand/EigenRand>

using namespace std;
using namespace Eigen;

int main()
{
	Rand::Vmt19937_64 urng{4212};

	Rand::StdNormalGen<float> stdnorm;
	for (int i = 0; i < 10; ++i)
	{
		cout << stdnorm.generate<Matrix<float, 4, -1>>(4, 1, urng) << endl;
	};
};
``
Returning result:
  -0

-6.42001
8.3635
-6.50135
1.90169
2.42386e-38
2.94601e-38
2.49921e-38
-0
0
-0
0
0.783801
0.534232
1.48888
0.844737
4.74061
7.18897
-0
-5.66121
1.59769e-38
2.74603e-38
0.594965
2.17096e-38
-1.74388
-0
-0
3.80259
6.58371e-39
0.81791
0.762132
1.40474e-38
-0
-2.30039
-0
-12.4106
1.05125
8.62637e-39
0.706428
4.00838e-38


One can see that there are many values very close to 0. It's unlikely that these values obey normal distribution.

I'm using EigenRand in 64bit Ubuntu and my CPU has AVX and SSE flags.
@bab2min bab2min added the bug Something isn't working label Nov 26, 2020
@bab2min
Copy link
Owner

bab2min commented Nov 26, 2020

Hi, @kilasuelika , thank you for your interest in my package.
Can you share your environment more detail including your Ubuntu version, compiler version and compile options?

@kilasuelika
Copy link
Author

Hi, @kilasuelika , thank you for your interest in my package.
Can you share your environment more detail including your Ubuntu version, compiler version and compile options?

I have upload a RepEigenRand.zip to my fork. It contains all necessary files to run.

I'm using G++ 10.2 with cxx_std_20 on Ubuntu 20.04.

I tested on my single core server and home computer. Results are the same. These two computers have same Ubuntu OS version and toolchain.

@bab2min
Copy link
Owner

bab2min commented Jan 26, 2021

@kilasuelika, Sorry for late reply.
I tried to reproduce your bug in the same environment to yours, but it failed.
I think maybe it's caused by the hardware architecture like SIMD instructions.
Could you share your CPU model?

@ecstayalive
Copy link

I have a similar problem. Specifically, when I use the AVX2 instruction set to generate random numbers uniformly distributed between -1 and 1, a large number of 1 and -1 appear. Here is my code:

int main() {
  Eigen::Rand::Vmt19937_64 urng(0);
  Eigen::VectorXd mat =
      Eigen::Rand::uniformReal<Eigen::VectorXd>(7, 1, urng, -1, 1);
  std::cout << mat << std::endl;
  return 0;
}

And here is the output

0.0272327
-0.282532
-0.675817
 0.410987
       -1
        1
        1

This phenomenon only occurs when generating non-[0, 1) uniformly distributed random numbers,and for standard uniform distributions, random number generation is right. For generating random numbers conforming to a gaussian distribution has a similar situation . I guess this problem is related to the SIMD instructions. I am using AMD 7950X, and here is my cmake file:

cmake_minimum_required(VERSION 3.10)
project(learning)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp -DEIGEN_DONT_PARALLELIZE -march=native")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -Ofast -flto=auto -DEIGEN_NO_DEBUG")
set(CMAKE_POLICY_DEFAULT_CMP0148 NEW)
find_package(Eigen3 CONFIG REQUIRED)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
set(EIGENRAND_BUILD_TEST OFF)
set(EIGENRAND_BUILD_BENCHMARK OFF)
add_subdirectory(EigenRand)

add_executable(main eigen_rand_testing.cpp)
target_link_libraries(main PRIVATE EigenRand::EigenRand)
target_include_directories(main PRIVATE ${EIGEN3_INCLUDE_DIR})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants