[Ipopt] Ipopt Initialize
Nandini Chakravorti
nchakravorti at gmail.com
Wed Dec 10 09:26:03 EST 2008
Sorry, forgot to post it to the mailing list.
The call stack is given below. IpOptWrapper is my wrapper dll which
communicates with the c# interface.
IpOptWrapper.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char>
>::assign() + 0xa6 bytes C++
IpOptWrapper.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char>
>::operator=() + 0x22 bytes C++
IpOptWrapper.dll!std::_Locinfo::_Locinfo_ctor() + 0x39 bytes
IpOptWrapper.dll!std::_Locinfo::_Locinfo() + 0xb1 bytes C++
IpOptWrapper.dll!std::ctype<char>::ctype<char>() + 0x68 bytes C++
IpOptWrapper.dll!std::ctype<char>::_Getcat() + 0x55 bytes C++
IpOptWrapper.dll!std::use_facet<std::ctype<char> >() + 0xc6 bytes C++
IpOptWrapper.dll!std::basic_ios<char,std::char_traits<char> >::widen() +
0x3e bytes C++
IpOptWrapper.dll!std::basic_ifstream<char,std::char_traits<char>
>::basic_ifstream<char,std::char_traits<char> >() + 0x81 bytes C++
IpOptWrapper.dll!Ipopt::IpoptApplication::Initialize() + 0x143 bytes C++
Thanks,
Nandini
On Wed, Dec 10, 2008 at 12:48 PM, Nandini Chakravorti <
nchakravorti at gmail.com> wrote:
> Damien/Ali,
>
> Giving more information about the code.
>
>
> class
> Wrapper
>
> {
>
> private:
>
> SmartPtr<IpoptApplication> Impl;
>
> SmartPtr<TNLP> mynlp;
> public:
>
> Wrapper::Wrapper(
> bool create_console_out ,bool create_empty )
>
> {
>
> mynlp =
> new IpOptNLP(this);
>
> Impl =
> new IpoptApplication();
> }
>
> ApplicationReturnStatus Wrapper::Initialize(
> char* paramsFile)
>
> {
>
> ApplicationReturnStatus ret;
>
> ret = Impl->Initialize(paramsFile);
>
> return ret;
>
> }
> };
>
> *And my NLP is :*
>
> class
> IpOptNLP: public TNLP
>
> {
>
> private:
>
> Wrapper* m_pcUnManagedWrapper;
>
> public:
>
> IpOptNLP();
>
> virtual
> ~IpOptNLP();
>
> IpOptNLP::IpOptNLP(Wrapper* pcWrapper)
>
> {
>
> m_pcUnManagedWrapper = pcWrapper;
>
> }
>
> virtual bool get_nlp_info(int& n, int& m, int& nnz_jac_g, int& nnz_h_lag,
> IndexStyleEnum& index_style);
>
> virtual bool get_bounds_info(int n, double* x_l, double* x_u, int m,
> double* g_l, double* g_u);
>
> virtual bool get_starting_point(int n, bool init_x, double* x, boolinit_z,
> double* z_L, double* z_U,
>
> int m, bool init_lambda, double* lambda);
>
> virtual bool eval_f(int n, const double* x, bool new_x, double&
> obj_value);
>
> virtual bool eval_grad_f(int n, const Number* x, bool new_x, double*
> grad_f);
>
> virtual bool eval_g(int n, const double* x, bool new_x, int m, double* g);
>
>
> virtual bool eval_jac_g(int n, const double* x, bool new_x,
>
> int m, int nele_jac, int* iRow, int *jCol,
>
> double* values);
>
> virtual bool eval_h(int n, const double* x, bool new_x,
>
> double obj_factor, int m, const double* lambda,
>
> bool new_lambda, int nele_hess, int* iRow,
>
> int* jCol, double* values);
>
> virtual void finalize_solution(SolverReturn status, int n, const double*
> x,
>
> const double* z_L, const double* z_U,
>
> int m, const double* g, const double* lambda,
>
> double obj_value,
>
> const IpoptData* ip_data,
>
> IpoptCalculatedQuantities* ip_cq);
>
> virtual bool intermediate_callback(AlgorithmMode mode,
>
> int iter, double obj_value,
>
> double inf_pr, double inf_du,
>
> double mu, double d_norm,
>
> double regularization_size,
>
> double alpha_du, double alpha_pr,
>
> int ls_trials,
>
> const IpoptData* ip_data,
>
> IpoptCalculatedQuantities* ip_cq);
>
> };
>
> *And I have c export function like*
>
>
> #define
> IPOPT_API __declspec( dllexport )
>
> #define
> PLSHANDLE32(name) struct name##__ { int unused; }; \
>
> typedef const struct name##__ * name;
>
>
>
> Wrapper*HToPtrDialogFilters(HIPOPTHANDLE h) {assert(h);
> return (Wrapper* )h;};
>
> HIPOPTHANDLE PtrToHIPOPTHANDLE(Wrapper*p) {assert(p);
> return (HIPOPTHANDLE )p;};
>
> void
> IPOPT_API IpOptCreate(HIPOPTHANDLE *hIpoptHandle)
>
> {
>
> Wrapper* pFilters =
> new Wrapper(false, false);
>
> *hIpoptHandle = PtrToHIPOPTHANDLE(pFilters);
>
> }
>
> ApplicationReturnStatus IPOPT_API IpOptInitializeNLP(HIPOPTHANDLE
> hIpoptHandle
> ) {
>
> return HToPtrDialogFilters(hIpoptHandle)->Initialize(NULL);
>
> }
> *At the c# end*
> I import these 2 functions like
>
> class
> Helper
>
> {
>
> [
> DllImport("IPOPTWrapper.dll", CallingConvention = CallingConvention.Cdecl,
> EntryPoint = "IpOptCreate")]
>
> unsafe public static extern void IpOptCreate(ref IntPtr handle);
>
> [
> DllImport("IPOPTWrapper.dll", CallingConvention = CallingConvention.Cdecl,
> EntryPoint = "IpOptInitializeNLP")]
>
> unsafe public static extern void IpOptInitializeNLP(IntPtr handle);
>
> }
>
> *And in my main C# class*
>
> returnStatus =
> IPOPTReturnStatus.Solve_Succeeded; //IPOPTReturnStatus is an enum defined
> in the above mentioned class - ie *Helper* class
>
> IntPtr
> handle = IntPtr.Zero; // Handle to the IPOPT helper
>
> Helper.IpOptCreate(ref handle);
>
> returnStatus =
> Helper.IpOptInitializeNLP(handle);
> Now, my code throws an exception at the Initialize. But, if I try to Invoke
> the Impl->Initialize(paramsFile) from within the constructor of the *
> Wrapper* class, then Ipopt Initializes. But, then again the next
> call(involving the Impl pointer) to the dll from the C# end throws up an
> exception.
>
> Thanks,
> Nandini
>
>
> On Tue, Dec 9, 2008 at 5:52 PM, Damien Hocking <damien at khubla.com> wrote:
>
>> Nandini,
>>
>> Is that your own Wrapper class? We'd need to see that before offering
>> hints on why your ref counts are going to zero. Whenever I want to wrap a
>> C++ solver in C# I always use IntPtrs and manage memory myself, otherwise
>> you can fall foul of aggressive memory cleanups on the C# side.
>>
>> private IntPtr IPOPTSolver_; //etc etc
>>
>>
>> Damien
>>
>> ------------------------------
>> *From:* Nandini Chakravorti [mailto:nchakravorti at gmail.com]
>> *To:* ipopt at list.coin-or.org
>> *Sent:* Tue, 09 Dec 2008 08:18:39 -0700
>> *Subject:* [Ipopt] Ipopt Initialize
>>
>> Hi,
>>
>> I am using the Ipopt lib(Release version) from C#. So, I have a dll
>> wrapper for the ipopt.lib. So, after I run the Initialize method I get an
>> exception saying ""Attempted to read or write protected memory. This is
>> often an indication that other memory is corrupt."
>>
>> Giving a C++ snippet of my code below:
>>
>>
>>
>> class Wrapper
>>
>> {
>>
>> private:
>> SmartPtr<IpoptApplication> Impl;
>> SmartPtr<TNLP> mynlp;
>>
>> public:
>>
>> ApplicationReturnStatus
>> Initialize()
>> {
>>
>> return Impl->Initialize(
>> );
>>
>> }
>>
>> };
>>
>> class
>> IpOptNLP: public TNLP
>>
>> {
>>
>> private:
>>
>> Wrapper* m_pcUnManagedWrapper;
>>
>> public:
>>
>> IpOptNLP();
>>
>> virtual ~IpOptNLP();
>>
>> IpOptNLP(Wrapper* pcWrapper) { m_pcUnManagedWrapper = pcWrapper;}
>>
>> virtual
>> bool get_nlp_info(int& n, int& m, int& nnz_jac_g, int& nnz_h_lag,
>> IndexStyleEnum& index_style); //And so on
>>
>> }
>>
>>
>>
>> Now when I invoke the Initialize function from C#(after exporting the dll)
>> I get an exception when *Impl->Initialize(**)* is getting invoked. And
>> while debugging I see the reference count for both Impl and mynlp are zero.
>>
>> Can someone please point out where I might be going wrong?
>>
>> Thanks,
>>
>> Nandini
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://list.coin-or.org/pipermail/ipopt/attachments/20081210/eefb719c/attachment-0001.html
More information about the Ipopt
mailing list