SINGA-6 Update the implementation of singletons to make them thread-safe#5
Merged
Conversation
Update the implementation of Singleton to make it thread-safe (using C++11's static construction). The ASingleton (constructor with Argument) was used for sharing the Mshadow::Random among Layers and Params. To make it thread-safe 1. we change it to TSingleton (Thread specfic singlton). 2. we add a construtor without arguments for Mshadow::Random which uses a seed generated based on the system clock. 3. we replace the rand/srand in Mshaodw::Random<cpu> with C++11's random functions, i.e., from random.h Now each thread will have a separate Mshadow::Random object if it calls TSingleton<Random<cpu>>::Instance() to generate random numbers. Hence it is thread-safe.
Member
Author
|
To avoid frequent changes of the configuration files, cifar10 example's configuration files are renamed to cluster.conf.example and model.conf.example. Users are suggested to copy *.conf.example to *.conf to run the program. TODO add *.conf in .gitignore to ignore the cluster.conf and example.conf. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thanks to Xuan Liu (from google) for pointing out that the Singleton implementation in SINGA is not thread-safe. Discussions on efficient thread-safe singleton are available at http://stackoverflow.com/questions/2576022/efficient-thread-safe-singleton-in-c.
This pull request updates the utils/singleton.h file to make the two singletons thread-safe.
In specific, for the Singleton class, we simple use C++11's static construction as suggested in Stackoverflow.
The ASingleton (constructor with Argument) was used for sharing the Mshadow::Random among Layers and Params. To make it thread-safe,
Now each thread will have a separate Mshadow::Random object if it calls TSingleton<Random>::Instance() to generate random numbers.
Hence it is thread-safe.