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

Preventing "static initialization order fiasco" in OpenBlas #654

Closed
gjongren opened this issue Sep 30, 2015 · 2 comments
Closed

Preventing "static initialization order fiasco" in OpenBlas #654

gjongren opened this issue Sep 30, 2015 · 2 comments

Comments

@gjongren
Copy link

I am using OpenBLAS 0.2.14 compiled for dynamic dispatch based on CPU type. When initializing a C++ vector container (that under the hood uses openblas for copying elements) as a global/static variable I ran into the well-known "static initialization order fiasco". The problem was that the function
void CONSTRUCTOR gotoblas_init(void) in file memory.c was called AFTER the initialization of my vector, instead of before. This resulted in segmentation violation since the gotoblas pointer is not initialized before the underlying call to openblas via the vector container is made.

What I did to fix this was to change change the definition of CONSTRUCTOR in memory.c to

define CONSTRUCTOR attribute ((constructor (101)))

i.e., giving it a priority. According to the discussion on
http://stackoverflow.com/questions/11106875/attribute-constructor-call-order-confusion
this should make sure the gotoblas_init function is run before all other static initializations without constructor attribute, thus avoiding the problem.

It would be great if this issue could be fixed for future versions of openblas, as not being able to use openblas functionality in static/global variables is a serious problem..

@xianyi
Copy link
Collaborator

xianyi commented Sep 30, 2015

Thank you for your feedback and patch. I will merge it on develop branch.

@gjongren
Copy link
Author

gjongren commented Oct 1, 2015

Excellent! :-)
By the way, I forgot to mention that I also changed the DESTRUCTOR define to
#define DESTRUCTOR attribute ((destructor (101)))
so that it is runs the last.

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

No branches or pull requests

2 participants