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

Use global variables to split up integration loops into different functions #400

Open
3 tasks
sg-s opened this issue Apr 13, 2019 · 8 comments
Open
3 tasks
Assignees
Labels
C++ architectural change to the C++ code enhancement no-issue-activity

Comments

@sg-s
Copy link
Owner

sg-s commented Apr 13, 2019

  • define xolotl_network, pointers to outputs outside mexFunction
  • write integrations in subfunctions in mexTemplate
  • check that this is just as fast as a raw loop
@sg-s sg-s added enhancement C++ architectural change to the C++ code labels Apr 13, 2019
@sg-s sg-s self-assigned this Apr 13, 2019
@sg-s
Copy link
Owner Author

sg-s commented Apr 14, 2019

it looks like global variables persist across multiple calls to the binary -- need to wait for Mathworks support to clarify behaviour.

@sg-s
Copy link
Owner Author

sg-s commented Apr 15, 2019

apparently global variables do persist. interesting.

@alec-hoyland
Copy link
Collaborator

If a variable is instantiated inside of a function and marked as global, it should persist in the workspace. What's surprising to me is that this behavior applies to mex functions too.

The workaround is manual garbage collection at the end of each iteration of a loop.

@sg-s
Copy link
Owner Author

sg-s commented Apr 15, 2019

The behaviour is stranger than you imagine:

#include "mex.h" 
int wow = 0; 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { 
void testfunc(void); 
for (int i = 0; i < 1e3; i ++) {testfunc();} 
mexPrintf("wow = %i\n",wow); 
} 
void testfunc() { 
wow++; 
} 

I compile it with mex. Now, calling it multiple times gives the following output:

wow = 1000 
wow = 2000 
wow = 3000 

showing that the global variable "wow" persists across function calls. (where??)

Calling "clear mex" resets the counter

clear mex 
wow = 1000 

@alec-hoyland
Copy link
Collaborator

wtf

@sg-s
Copy link
Owner Author

sg-s commented Apr 16, 2019

apparently this is expected behaviour according to mathworks support

@sg-s
Copy link
Owner Author

sg-s commented Apr 16, 2019

this is what they had to say:

"mexMakeMemoryPersistent" only applies to memory allocated by MATLAB memory allocation routines such as "mxCalloc", "mxMalloc", "mxRealloc", and the different "mxCreate*" functions (https://www.mathworks.com/help/matlab/create-or-delete-array.html). Global variables in C++ are initialized only once during compilation. The value will persist across subsequent execution of the code, which is why the variable "wow" is increasing in value every time your function is being called.
To clear global variables, you can just call clear mex as you have done before.
MathWorks does not actively discourage the usage of global variables in MEX files. However, it is up to the developer to exercise caution and keep track of global variables since every function has access to them.
Global variables should not cause memory leaks as they are not dynamically allocated. They will reside in the same memory space across subsequent program execution.

@github-actions
Copy link

Stale issue message

@sg-s sg-s reopened this Aug 14, 2019
@sg-s sg-s reopened this Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ architectural change to the C++ code enhancement no-issue-activity
Projects
None yet
Development

No branches or pull requests

2 participants