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

common.cpp: int64_t cluster_seedgen(void) fails to use system entropy source in windows #4293

Open
terrylyons opened this issue Jun 12, 2016 · 8 comments
Labels

Comments

@terrylyons
Copy link

Caffe frequently needs entropy to seed PRGNs. The algorithm it uses to do this is unix specific. If it is not successful (always in windows) it falls back to a simple algebraic approach using the time, pid of the process to generate a seed and logs a warning. This has two bad effects. One the different seeds are far from independent which could be serious in a silent way, and two, output is seriously cluttered with frequent log warnings about lack of a device to produce entropy.

The optimum solution would seem to me to use std::random_device()() which provides a strong platform independent way of generating such entropy using native C++. Certainly this is supported in windows 2013 and works for me.

The first few tests I run with this change look so much more acceptable without all the log messages.

My modifications of common.cpp are included below:

`

if BOOST_NO_CXX11_HDR_RANDOM

else

include

endif

namespace caffe {

// random seeding
int64_t cluster_seedgen(void) {
int64_t s, seed, pid;
FILE* f = fopen("/dev/urandom", "rb");
if (f && fread(&seed, 1, sizeof(seed), f) == sizeof(seed)) {
fclose(f);
return seed;
}

if BOOST_NO_CXX11_HDR_RANDOM

else

std::random_device rd;
if (rd.entropy() >= 32){
unsigned long long seed = rd();
seed <<= 32;
seed += rd();
return seed;
}

endif

LOG(INFO) << "System entropy source not available, "
"using fallback algorithm to generate seed instead.";
if (f)
fclose(f);

pid = getpid();
s = time(NULL);
seed = std::abs(((s * 181) * ((pid - 83) * 359)) % 104729);
return seed;
}

} // namespace caffe
`

@willyd
Copy link
Contributor

willyd commented Sep 12, 2016

I encourage you to submit a PR about this. Please follow the contribution guidelines.

@terrylyons
Copy link
Author

Thanks for proposing that I submit this. Unfortunately, at the current time I really don't have time to do this properly. In particular, I have no idea if I really have to test for the CXX11 headers etc. I do no that one does need to leave the old unix approach in - since Linux systems I used recently seem to always return 0 as the amount of entropy available through the C++ standard device. Bizarre but true!

The small piece of code I wrote seems a good fix to a real problem. So if you or someone else want to put it up then I am comfortable with this. When I have time I will come back to it.

@willyd
Copy link
Contributor

willyd commented Oct 7, 2016

Looks like #3306 or using CPython's way with CryptGenRandom might be a better alternative here.

@terrylyons
Copy link
Author

The std:: random_device approach seems preferred language invariant one for the long term. Microsoft are on record to guarantee that their implementation is cryptographically secure, g++ Linux is said to use (u)random so either way you get a strong Implimentation.

On 7 Oct 2016, at 17:05, Guillaume Dumont <notifications@github.commailto:notifications@github.com> wrote:

Looks like #3306#3306 or using CPython's wayhttps://github.com/python/cpython/blob/4106e4237df5397efc785ce4daa257aecb5bff75/Python/random.c#L65 with CryptGenRandom might be a better alternative here.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com//issues/4293#issuecomment-252290636, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGFm-4DdoEDeumVJePcNewzVBZVh3nJ4ks5qxmv9gaJpZM4Izuz4.

@Licheng314
Copy link

Excuse me, I can't tell how did you modificate the code.
What does it means:
if BOOST_NO_CXX11_HDR_RANDOM
else
include
endif
Can you explain it more clearly?

@Licheng314
Copy link

Licheng314 commented Feb 28, 2017

I just inserted the code below in line 36:

std::random_device rd;
if (rd.entropy() >= 32){
unsigned long long seed = rd();
seed <<= 32;
seed += rd();
return seed;
}

Then the log message not show again. Did I do it correctly?

@terrylyons
Copy link
Author

terrylyons commented Feb 28, 2017 via email

@Licheng314
Copy link

What ever, I only use caffe in Windows by myself.
I modified the caffe as you told, and used the header: .
Can I just inserted the code below in line 36 to solve the problem "fails to use system entropy source in windows"?

std::random_device rd;
if (rd.entropy() >= 32){
unsigned long long seed = rd();
seed <<= 32;
seed += rd();
return seed;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants