Thread Subject: change workspace variable during mex call with immediate effect

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 17 Nov, 2009 15:04:21

Message: 1 of 12

hi all!

i have the following situation:
- WinXP SP3 with Matlab R2009a + CUDA GPU
- Big GUI that calls a the very very (!! :-)) fast algorithm running on the CUDA GPU implemented as a mex-file to interact with matlab
 
For monitoring the calculation status of the algorithm i have introduced a strucutre with some flags and progress variables.
During the calculation I peridically call a timer in the GUI which reads the progress out of the strucutre and shows it in a progress bar.
In the mex file i change the structure each time a new frame has been calculated (by the way it's a algorithm to estimate the optical flow of video).
This function looks something like this:
================================================
bool OfAlgorithmStatus_setStatus(mxArray * StatusStruct, OfAlgorithmStatus_Status Status, int FramesDone, int FramesTotal)
{
  if(OfAlgorithmStatus_isValidStruct(StatusStruct))
  {
    mxSetFieldByNumber(StatusStruct, 0, 0, mxCreateDoubleScalar(Status));
    mxSetFieldByNumber(StatusStruct, 0, 1, mxCreateDoubleScalar(FramesTotal));
    mxSetFieldByNumber(StatusStruct, 0, 2, mxCreateDoubleScalar(FramesDone));

    return true;
  }
  else
    return false;
}
================================================
For me it seems that it changes the structure but only after the mex-call has finished! So the status won't be updated during the calculation. only at the end, but then it's too late :-(
Is this true? Is there another way instead of mxSetFieldByNumber() function to let the changes take effect immediately?

This basic (slow!) version of the calculation is also available in matlab, there the thing with changing the structure during calculation and updating&visualizing it in the GUI works!

Thanks in advance!
Fabian

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 11 Dec, 2009 13:22:19

Message: 2 of 12

hi all!

so far I still have no solution for this problem but I really would need one in the next days!!!
is there no way to kind of force or refresh the "variable synchronization" between matlab and mex file???

please come up with any possiblity to solve this!
thanks in advance!
fab

Subject: change workspace variable during mex call with immediate effect

From: Steven Lord

Date: 11 Dec, 2009 14:26:44

Message: 3 of 12


"Fab Braun" <f1braun@hsr.ch> wrote in message
news:hdue1l$6ci$1@fred.mathworks.com...
> hi all!
>
> i have the following situation:
> - WinXP SP3 with Matlab R2009a + CUDA GPU
> - Big GUI that calls a the very very (!! :-)) fast algorithm running on
> the CUDA GPU implemented as a mex-file to interact with matlab
>
> For monitoring the calculation status of the algorithm i have introduced a
> strucutre with some flags and progress variables.
> During the calculation I peridically call a timer in the GUI which reads
> the progress out of the strucutre and shows it in a progress bar.
> In the mex file i change the structure each time a new frame has been
> calculated (by the way it's a algorithm to estimate the optical flow of
> video).
> This function looks something like this:
> ================================================
> bool OfAlgorithmStatus_setStatus(mxArray * StatusStruct,
> OfAlgorithmStatus_Status Status, int FramesDone, int FramesTotal)
> {
> if(OfAlgorithmStatus_isValidStruct(StatusStruct))
> {
> mxSetFieldByNumber(StatusStruct, 0, 0, mxCreateDoubleScalar(Status));
> mxSetFieldByNumber(StatusStruct, 0, 1,
> mxCreateDoubleScalar(FramesTotal));
> mxSetFieldByNumber(StatusStruct, 0, 2,
> mxCreateDoubleScalar(FramesDone));
>
> return true;
> }
> else
> return false;
> }
> ================================================
> For me it seems that it changes the structure but only after the mex-call
> has finished! So the status won't be updated during the calculation. only
> at the end, but then it's too late :-(
> Is this true?

Modifying the copy of the variable in the MEX-file does not have any impact
on the variables in the workspace from which it was called unless you modify
the prhs pointers, but that's discouraged because it can have side effects
if any of those pointers have been shared due to copy-on-write behavior.

a = 1;
b = a;
mexFileThatModifiesPrhs(b) % set b to be 5
a % is now 5

 Is there another way instead of mxSetFieldByNumber() function to let the
changes take effect immediately?
>
> This basic (slow!) version of the calculation is also available in matlab,
> there the thing with changing the structure during calculation and
> updating&visualizing it in the GUI works!

Yes, because you're changing the variable in the workspace rather than a
copy of it in another workspace.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 12 Dec, 2009 17:50:06

Message: 4 of 12


> Modifying the copy of the variable in the MEX-file does not have any impact
> on the variables in the workspace from which it was called unless you modify
> the prhs pointers, but that's discouraged because it can have side effects
> if any of those pointers have been shared due to copy-on-write behavior.

i do not totally agree with that because of what i experienced:
if i pass the structure (called CalculationStatus -> see below) to my mex file and simply access and change it out of the mex file like this:
##############
StatusStruct = (mxArray *) prhs[4];
mxSetFieldByNumber(StatusStruct, 0, 0, mxCreateDoubleScalar(Status));
mxSetFieldByNumber(StatusStruct, 0, 1, mxCreateDoubleScalar(FramesTotal));
mxSetFieldByNumber(StatusStruct, 0, 2, mxCreateDoubleScalar(FramesDone));
##############

The structure's data WILL BE CHANGED or better said it IS CHANGED to the values i expect BUT only after the mex call has finished!
so the modification on this structure has a direct impact on the structure stored in matlab just as i want but too late!


###### the structure CalculationStatus #########
CalculationStatus = struct( 'Status', 0, ... % calculation in progress --> true, else false
                            'FramesDone', 0, ...
                            'FramesTotal', 55, ...
                            'StopRequested', false) ;
##############

>
> a = 1;
> b = a;
> mexFileThatModifiesPrhs(b) % set b to be 5
> a % is now 5
>
so i guess modifying prhs pointers will give me the same results?!?
the data will be changed in matlab but not during the mex-file call rather just when the mex file has finished.

thanks for your help ;-)
fab

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 12 Dec, 2009 18:09:06

Message: 5 of 12

i just tried to create the structure inside the mex file:
#############
mxArray* OfAlgorithmStatus_createStruct(/*const mxArray * StatusStruct*/)
{
  const char *field_names[] = {"Status", "FramesDone", "FramesTotal", "StopRequested"};
  mwSize dims[2] = {1, 1};

  return mxCreateStructArray(1, dims, 4, field_names);
}
#############

so at the beginning of the mex file instead of executing as before:
#############
  mxArray * StatusStruct;
StatusStruct = (mxArray *) prhs[4];
#############
... i now execute the following and then also change the structure with the functions mentioned in the last post
#############
  mxArray * StatusStruct;
  StatusStruct = OfAlgorithmStatus_createStruct();
  prhs[4] = StatusStruct;
#############

but now nothing will be changed in the original structure passed to the mex-file in matlab :-(
what am i doing wrong?
i guess this is what you proposed in your post?
or did i get something wrong?

thanks a lot!
fab

Subject: change workspace variable during mex call with immediate effect

From: Jan Simon

Date: 12 Dec, 2009 22:56:02

Message: 6 of 12

Dear Fab!

> For monitoring the calculation status of the algorithm i have introduced a strucutre with some flags and progress variables.
> During the calculation I peridically call a timer in the GUI which reads the progress out of the strucutre and shows it in a progress bar.
> In the mex file i change the structure each time a new frame has been calculated (by the way it's a algorithm to estimate the optical flow of video).

Do I understand correctly:
You have a GUI, which calls a MEX function, which starts a CUDA process.
The GUI uses a timer, which should update a progress idicator with some values obtained from the MEX process.
But the timer cannot obtain the values from the MEX process, because the MEX does not immediately write to variables in the workspace.

If so, why do you need the timer? You could let the MEX update the progress idicator directly. Instead of modifying variables, which should be grabbed from the timer, the MEX can set the progress bar or other GUI elements.

But perhaps I did not get the point.
Good night, Jan

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 13 Dec, 2009 10:18:01

Message: 7 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> Do I understand correctly:
yes you do, that's exactly what I want!!!
your solution would sound perfect and easy, BUT I definitely can't do it in this simple way in my project.

fact is that it is quite a big project (bachelor thesis) where the GUI is used to interface different (optical flow estimation) algorithms (either Matlab, pure-C- or CUDA-mex-files).
to ensure transparaency and code quality I NEED to update the progress bar out of the GUI, else I end up with a terrible mess!

but your idea is great!
the different algorithms (mex-files) are called out of a certain class (called OpticalFlowAlgorithm) which contains the struct (CalculationStatus) which I try to update. In this class i could now introduce a new member function (e.g.: calculationStatusSet(...)) which changes the struct's values. Therefore i just need to call the member function of a certain object out of the mex file!!! that's it!!!
but is this possible?

so to be specific I need to call the member function (e.g.: OpticalFlowAlgorithmObject.calculationStatusSet(...)) out of the mex file.
i guess therfore i would have to pass the object (e.g.: OpticalFlowAlgorithmObject) of the OpticalFlowAlgorithm class to the mex-file which then is used to access its member function (e.g.: calculationStatusSet(...)).
i just read the reference, but it seems that i can only specify the function name and not its object. :-/
###########
int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *functionName);
###########
any ideas?!?

sorry for my special wishes :-S
hope this works somehow?!?

thanks in advance....have a nice sunday!
fab

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 13 Dec, 2009 11:27:01

Message: 8 of 12

I just did some trials to call the object's member funciton as described in the last post.
therefore I tried the following:

since the instance/object of the OpticalFlowAlgorithm class is called Object in all the memberfunctions and the mex file is executed in one of these memberfunctions i tried to access the desired memberfunction with the prefix Object as follows:
########
    mxArray *rhs[2];
    rhs[0] = mxCreateDoubleScalar(Status);
    rhs[1] = mxCreateDoubleScalar(FramesDone);
    mexCallMATLAB(0, NULL, 2, &rhs[0], "Object.calculationStatusSet");
########

but this doesn't seem to work (unknown funciton ... ). even not if i remove the "Object." prefix...any other ideas...how can i get access to this objects specific memberfunciton :-/
is there a possiblity to work with an ID/index/handle-ID or whatever of the object for finally being able to call its member???

would be great if i would find a solution for this ....
thanks a lot to all of you for your help!
fab

Subject: change workspace variable during mex call with immediate effect

From: Jan Simon

Date: 13 Dec, 2009 19:40:28

Message: 9 of 12

Dear Fab!

I've found this:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/173912
There Ryan Ollos writes:
    if ( mxIsClass(prhs[0], "function_handle") )
        mexCallMATLAB(nlhs, plhs, nrhs, prhs, "feval");
    else
        mexErrMsgTxt("First Input is not a function handle.");

So if you can create a function handle to your object's member function, you could forward it to the Mex and call it with the masked FEVAL.

Another idea would be to pass the object to the Mex, and forward it back to Matlab by mexCallMATLAB to a function, which takes the object as input again. Finally this Matlab function calls the member function again.
Ok, this seems to be more complex than expected. I'm sure, that this can be easier, but it could at least work!

Further idea: Create a "state machine": A Matlab function, which stores a handle to the object's member function persistenly. Then if called from the Mex through MexCallMATLAB (why is this not called mexCallMatlab?!) the Mex has not to care about the object's properties, members etc.

I admit, these ideas are hacks. But if you are in a hurry, clean ideas may be too challenging.

Good luck, Jan

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 13 Dec, 2009 21:28:03

Message: 10 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hg3fvc$doj$1@fred.mathworks.com>...
> Another idea would be to pass the object to the Mex, and forward it back to Matlab by mexCallMATLAB to a function, which takes the object as input again. Finally this Matlab function calls the member function again.

hei jan!

thanks a lot for your help!
it works when i pass the object to the mex file and use it as a first argument in the funciton call (so now i call the member function calculationStatusSet(Object, Status, FramesDone) of the object OpticalFlowAlgorithmObject which is of the class OpticalFlowAlgorithm!
############
    mxArray *rhs[3];
    rhs[0] = OpticalFlowAlgorithmObject; //object
    rhs[1] = mxCreateDoubleScalar(Status);
    rhs[2] = mxCreateDoubleScalar(FramesDone);
    mexCallMATLAB(0, NULL, 3, &rhs[0], "calculationStatusSet");
############

BUUUUUT - i think this task is accursed!! :-( - i just found out that the callback of the timer used to update the progress bar is not executed during the mex file call.
any ideas?
i think i'm running out of time :-/ maybe i just leave this as it is - not working :-(.

maybe someone has a glorious idea?!?
thanks!
fab

Subject: change workspace variable during mex call with immediate effect

From: Jan Simon

Date: 13 Dec, 2009 22:13:02

Message: 11 of 12

Dear Fab!

> BUUUUUT - i think this task is accursed!! :-( - i just found out that the callback of the timer used to update the progress bar is not executed during the mex file call.

To be honest: I'm relieved.
I thought that there is a magic trick in Matlab 2009 to allow (timer) callbacks to run during a Mex is executing --- I still have to program compatible to Matlab 6.5...

Ideas:
1. Start the CUDA in a new thread and let it write the progress value to the disk. Then your GUI can read the file... Ugly, do not use the hard disk for interprocess communication. But if you are in a hurry :-|

2. Stop struggeling. Progress indicators are for non-scientific users! They do not give any information, which can be published.

3. Let your Mex draw a primitive WAITBAR and/or write the values to the command window. Fancy GUI's are for non-cientif users! They do not...

4. Let the Mex call the callback of the timer directly!

Kind regards, Jan

Subject: change workspace variable during mex call with immediate effect

From: Fab Braun

Date: 16 Dec, 2009 14:26:04

Message: 12 of 12

hi jan!

allright, thanks a lot for your quick response again!
then i think i will choose point 2 :-)
the thesis is nearly finished as well as the report...therefore i really don't want to spend anymore time with coding.

but at least i now know the possibilities which i can mention for future developers!

thanks a lot, have a nice Christmas and so on ... ;-)
fab

Tags for this Thread

Everyone's Tags:

mex

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex Fab Braun 17 Nov, 2009 10:09:19
rssFeed for this Thread

Contact us at files@mathworks.com