|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
Use Octave's interpreter in my C++ program...Hello,
I would like to use Octave in a C++ multithreaded application. Is there something like this (see code below) in Octave so I can get an instance of Octave and use it throughout the life of my OctaveUser object? I'll even take something close to this. :) class OctaveUser { private: OctaveInstance *p_oi; public: OctaveUser() { oi = OctaveFactory::createInstance(); } void cppMethod() { Matrix m(2,2); m(0,0)=1; m(0,1)=2; m(1,0)=3; m(1,1)=4; oi.feval("invert", m); // call 'testFromCpp.m' file. octave_value_list result = feval ("testFromCpp", octave_value (m), 1 ); ...//do something with the value in C++ } ~OctaveUser() { delete _oi; } }; Thanks! Molamini |
|
|
Re: Use Octave's interpreter in my C++ program...On May 22, 2008, at 5:26 PM, jmoliere@... wrote: > Hello, > I would like to use Octave in a C++ multithreaded application. Is > there > something like this (see code below) in Octave so I can get an > instance of > Octave and use it throughout the life of my OctaveUser object? I'll > even > take something close to this. :) > > class OctaveUser > { > private: > OctaveInstance *p_oi; > public: > OctaveUser() > { > oi = OctaveFactory::createInstance(); > } > void cppMethod() > { > Matrix m(2,2); > m(0,0)=1; m(0,1)=2; > m(1,0)=3; m(1,1)=4; > oi.feval("invert", m); > > // call 'testFromCpp.m' file. > octave_value_list result = feval ("testFromCpp", octave_value > (m), > 1 ); > ...//do something with the value in C++ > } > > ~OctaveUser() > { > delete _oi; > } > }; > > Thanks! > Molamini > Included is a bit of pseudo-code that I wrote to an Octave user offline from the mailing list that I think would be the "meat" of such a class. This listing, however, is unique to the Octave sources since the change to the sumbol_table in a singleton class as described in the explanation. Hope this helps. If I had more time, I would implement it, but would be willing to give help if you want to muck around with this. However, take note of David Bateman's warning about thread-safety. I suppose if you ensure you only call into octave one command at a time, then Bob is your uncle. <begin previous email> I think the better way of doing this would be to create a symbol_record and insert it into the symbol_table singleton class. Then you could use the octave_call() function one the variable that you just pushed into the octave environment. Then, pull the resulting variable out of the symbol_table and pull the data from the contained octave_value(). Below is a bit of pseudo-code that seems like it should work. I hope this helps. If this does end up working, would you be willing to write and email to the mailing list with how you did it. I think this would be useful for others to know about, since I haven't seen someone do this before. Also, not that the pseudocode I give below is based on the most recent octave from the source code repository. Between 3.0.x and what is in the repository, I think there was a major change in the symbol table. Before, it was just a global variable. Now it is a singleton class. The changes between 3.0.1 and the current repository tip are significant, but the basic idea should be the same. unsigned int imageArry[1024*768]; unsigned int resultImageArry[1024*768]; captureImage (imageArry); Matrix octImageArry(768,1024); // Note, octave indexes as row x col // COPY YOUR IMAGE DATA INTO THIS MATRIX for (int row = 0; row < size; ++row) { for (int column = 0; column < size; ++column) { // I'm not sure how your image is indexed in memory, so you should populate the matrix properly octImageArry (row, column) = imageArry[col][row]; } } // Wrap your octave_value Matrix up in a symbol_record object symbol_table::symbol_record symrec_imageArry ("imageArry", octImageArry); // Insert your symbol_record into the symbol_table symbol_table::insert_symbol_record (symrec_imageArry); // Call your routine to process the image octave_call("resultImageArry=processImageData(imageArry);"); // Pull the newly created symbol record called "resultImageArry" out of the symbol_table symbol_record symrec_resultImageArry = symbol_table::find_symbol("resultImageArry"); // Pull the octave_value out of the result symbol_record octave_value octResultImageArry = symrec_resultImageArry.varval(); // COPY THE DATA OUT OF THIS OCTAVE_VALUE AND BACK INTO YOUR C/C++ ARRAY <end previous email> |
|
|
Re: Use Octave's interpreter in my C++ program...Hello Molamini, i am looking for exactly the same solution if you do happen to find on,
would you mind to let me know thanks bernd
|
|
|
Re: Use Octave's interpreter in my C++ program...On May 22, 2008, at 5:41 PM, John Swensen wrote: > > On May 22, 2008, at 5:26 PM, jmoliere@... wrote: > >> Hello, >> I would like to use Octave in a C++ multithreaded application. Is >> there >> something like this (see code below) in Octave so I can get an >> instance of >> Octave and use it throughout the life of my OctaveUser object? >> I'll even >> take something close to this. :) >> >> class OctaveUser >> { >> private: >> OctaveInstance *p_oi; >> public: >> OctaveUser() >> { >> oi = OctaveFactory::createInstance(); >> } >> void cppMethod() >> { >> Matrix m(2,2); >> m(0,0)=1; m(0,1)=2; >> m(1,0)=3; m(1,1)=4; >> oi.feval("invert", m); >> >> // call 'testFromCpp.m' file. >> octave_value_list result = feval ("testFromCpp", octave_value >> (m), >> 1 ); >> ...//do something with the value in C++ >> } >> >> ~OctaveUser() >> { >> delete _oi; >> } >> }; >> >> Thanks! >> Molamini >> code I sent in my last email. I have attached a tgz file with the simple class and a test program. This is based on the concept of starting up octave_main() in embedded mode, pushing and pulling variables from the symbol table, and calling eval string to perform actions. Would the maintainers ever be interested in incorporating a more full-fledged version into Octave. As with the MEX interface, we could make the functions act like the Matlab Engine counterparts. John Swensen |
|
|
Re: Use Octave's interpreter in my C++ program...John Swensen wrote:
> > So I went ahead and converted a class I had written for the Matlab > Engine into a similar class for Octave. It is based on the > pseudo-code I sent in my last email. I have attached a tgz file with > the simple class and a test program. This is based on the concept of > starting up octave_main() in embedded mode, pushing and pulling > variables from the symbol table, and calling eval string to perform > actions. Would the maintainers ever be interested in incorporating a > more full-fledged version into Octave. Hey, who's a maintainer? It all depends as far as I see on your motivation to maintain the code.. However, the general line drawn on what goings into Octave or octave-forge is based on whether the equivalent functionality is a core matlab functionality. There are exceptions of course... > As with the MEX interface, we could make the functions act like the > Matlab Engine counterparts. Is the Matlab Engine your code or is it something more generic? If its generic then it makes sense to try and have a class that performs with Octave with the same API such that application written for one interface can use the other. D. -- David Bateman David.Bateman@... Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob) 91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax) The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary |
|
|
Re: Use Octave's interpreter in my C++ program...On May 23, 2008, at 10:44 AM, David Bateman wrote: > >> As with the MEX interface, we could make the functions act like the >> Matlab Engine counterparts. > Is the Matlab Engine your code or is it something more generic? If its > generic then it makes sense to try and have a class that performs with > Octave with the same API such that application written for one > interface > can use the other. > > D. > So the Matlab Engine is a core Matlab functionality. It is *not* a class, but a collection of functions. You can see the API at http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f29148.html&http://www.google.com/search ?client=safari&rls=en-us&q=Matlab+Engine&ie=UTF-8&oe=UTF-8. Since people have already implemented a bunch/all of the mxArray functions, it might make sense to add these engine functions. As you can see in the class I wrote, implementing the functions listed in the URL I included will be trivial; simply converting my class into a non-class with the same functionality and the function names that match the Matlab Engine function. However, if we do so, is there a way to ensure that they are only used when it is compiled with the mkoctfile --link-stand-alone option? John Swensen |
|
|
Re: Use Octave's interpreter in my C++ program...John Swensen wrote:
> > On May 23, 2008, at 10:44 AM, David Bateman wrote: >> >>> As with the MEX interface, we could make the functions act like the >>> Matlab Engine counterparts. >> Is the Matlab Engine your code or is it something more generic? If its >> generic then it makes sense to try and have a class that performs with >> Octave with the same API such that application written for one interface >> can use the other. >> >> D. >> > > So the Matlab Engine is a core Matlab functionality. It is *not* a > class, but a collection of functions. You can see the API at > http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f29148.html&http://www.google.com/search?client=safari&rls=en-us&q=Matlab+Engine&ie=UTF-8&oe=UTF-8. > > > Since people have already implemented a bunch/all of the mxArray > functions, it might make sense to add these engine functions. As you > can see in the class I wrote, implementing the functions listed in > the URL I included will be trivial; simply converting my class into a > non-class with the same functionality and the function names that > match the Matlab Engine function. However, if we do so, is there a > way to ensure that they are only used when it is compiled with the > mkoctfile --link-stand-alone option? > > John Swensen > > the engOpen command from the above link it appears that in fact Matlab uses something like a fork/exec to create an entirely new process with the Matlab instance running in it, rather than use a thread in the current process, thus avoiding all the issues of thread safety. So what you are proposing in fact goes beyond what is implemented in the Matlab engine.. D. -- David Bateman David.Bateman@... Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob) 91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax) The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary |
|
|
|
| Free embeddable forum powered by Nabble | Forum Help |