Any soultions to this old thread of calling quantlib from managed code?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Any soultions to this old thread of calling quantlib from managed code?

Cliffy
Hi,

I have a similar issue to this old thread -

http://old.nabble.com/Settings%3A%3Ainstance%28%29.evaluationDate%28%29-produces-exception-in-managed-code-to25479768.html#a25479768

I wrap quantlib.lib to a managed C++ dll and referenced it from an ASP.NET aspx web page in C#. It looks fine until it calls Settings::instance().evaluationDate().

I have a different exception but I think the cause is the same -

"The function must be called from the default domain"

It fails in
allocation a static variable in singleton.hpp

static std::map<Integer, boost::shared_ptr<T> > instances_;


 template <class T>
    #if defined(QL_PATCH_MSVC) && defined(_MANAGED)
    inline  // this seems to be required when CLR support is enabled
    #endif
    inline T& Singleton<T>::instance() {
        static std::map<Integer, boost::shared_ptr<T> > instances_;

I did some search on the web, found some same issues but did not find any solutions.
e.g.

http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7

And there quite a few posts on the web about problems using static variable in unmanaged codes in a managed application.

Anyone has success in implementing for quantlib as described? Or any suggestions are very apprecilated.

Also, is SWIG the answer? I've never used it. Have tried to load QuantLib_vc9.sln from ..\QuantLib-SWIG-1.0\CSharp to 2008 C# built fine but get some errors when running the examples. Any documents explain how to run quantlib SWIG?


Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Any soultions to this old thread of calling quantlib from managed code?

Nathan Abbott
The source of the problem is that there are AppDomains in the .Net world.  An AppDomain is a light-weight process. Every thread in the CLR lives in an AppDomain. See http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx for more details.

Native C++ static variables does not know anything about AppDomains. Therefore they should not be initialize in the code of managed compiled cpp files. You can get by with this if your code is in the primary AppDomain.
This is not the case if you are writing an ASP.Net solution. In ASP.Net, the Internet Information Server (IIS) is the primary AppDomain.

I came across the same problem went I wrote unit tests for my application. Nunit.exe has the primary AppDomain and placed my tests in a secondary AppDomain. The workaround I used is to set each QuantLib static variable in a global function that uses  "#pragma managed".  See http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7

Here is an example.

#pragma managed(push, off)
void SetQuantLibEvaluationDate(QuantLib::Date date)
{
    QuantLib::Settings::instance().evaluationDate() = date;
}
#pragma managed(pop)

Since you are using ASP.Net, you should defined QL_ENABLE_SESSIONS and write a Integer sessionID() function that returns an unique integer for each user. Otherwise, all of your users will shared the same settings. 




On Sat, Mar 6, 2010 at 7:02 PM, Cliffy <[hidden email]> wrote:

Hi,

I have a similar issue to this old thread -

http://old.nabble.com/Settings%3A%3Ainstance%28%29.evaluationDate%28%29-produces-exception-in-managed-code-to25479768.html#a25479768

I wrap quantlib.lib to a managed C++ dll and referenced it from an ASP.NET
aspx web page in C#. It looks fine until it calls
Settings::instance().evaluationDate().

I have a different exception but I think the cause is the same -

"The function must be called from the default domain"

It fails in
allocation a static variable in singleton.hpp

static std::map<Integer, boost::shared_ptr<T> > instances_;


 template <class T>
   #if defined(QL_PATCH_MSVC) && defined(_MANAGED)
   inline  // this seems to be required when CLR support is enabled
   #endif
   inline T& Singleton<T>::instance() {
       static std::map<Integer, boost::shared_ptr<T> > instances_;

I did some search on the web, found some same issues but did not find any
solutions.
e.g.

http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7

And there quite a few posts on the web about problems using static variable
in unmanaged codes in a managed application.

Anyone has success in implementing for quantlib as described? Or any
suggestions are very apprecilated.

Thanks
--
View this message in context: http://old.nabble.com/Any-soultions-to-this-old-thread-of-calling-quantlib-from-managed-code--tp27808812p27808812.html
Sent from the quantlib-users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Any soultions to this old thread of calling quantlib from managed code?

Cliffy
Great, Nathan

Thanks for your help. By the way do you think if there is any issues if I wrap #pragma managed(push, off) and #pragma managed(pop) around the definition of singleton template (i.e. in singleton.hpp) since I am not sure if there are any other implicit calls to  Singleton::instance.  I have tried it and it seems to be working but I am not sure if this could cause other issues. oh, great point for QL_ENABLE_SESSIONS as well ,

Regards,

Cliffy





Nathan Abbott wrote
The source of the problem is that there are AppDomains in the .Net world.  An
AppDomain is a light-weight process. Every thread in the CLR lives in an
AppDomain. See http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspxfor
more details.

Native C++ static variables does not know anything about AppDomains.
Therefore they should not be initialize in the code of managed compiled cpp
files. You can get by with this if your code is in the primary AppDomain.
This is not the case if you are writing an ASP.Net solution. In ASP.Net, the
Internet Information Server (IIS) is the primary AppDomain.

I came across the same problem went I wrote unit tests for my application.
Nunit.exe has the primary AppDomain and placed my tests in a secondary
AppDomain. The workaround I used is to set each QuantLib static variable in
a global function that uses  "#pragma managed".  See
http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7

Here is an example.

#pragma managed(push, off)
void SetQuantLibEvaluationDate(QuantLib::Date date)
{
    QuantLib::Settings::instance().evaluationDate() = date;
}
#pragma managed(pop)

Since you are using ASP.Net, you should defined QL_ENABLE_SESSIONS and write
a Integer sessionID() function that returns an unique integer for each user.
Otherwise, all of your users will shared the same settings.




On Sat, Mar 6, 2010 at 7:02 PM, Cliffy <cfhgtwn@yahoo.com> wrote:

>
> Hi,
>
> I have a similar issue to this old thread -
>
>
> http://old.nabble.com/Settings%3A%3Ainstance%28%29.evaluationDate%28%29-produces-exception-in-managed-code-to25479768.html#a25479768
>
> I wrap quantlib.lib to a managed C++ dll and referenced it from an ASP.NET
> aspx web page in C#. It looks fine until it calls
> Settings::instance().evaluationDate().
>
> I have a different exception but I think the cause is the same -
>
> "The function must be called from the default domain"
>
> It fails in
> allocation a static variable in singleton.hpp
>
> static std::map<Integer, boost::shared_ptr<T> > instances_;
>
>
>  template <class T>
>    #if defined(QL_PATCH_MSVC) && defined(_MANAGED)
>    inline  // this seems to be required when CLR support is enabled
>    #endif
>    inline T& Singleton<T>::instance() {
>        static std::map<Integer, boost::shared_ptr<T> > instances_;
>
> I did some search on the web, found some same issues but did not find any
> solutions.
> e.g.
>
>
> http://social.msdn.microsoft.com/Forums/en-US/clr/thread/cd3c34a6-84f9-4e2b-a483-12e179eb84b7
>
> And there quite a few posts on the web about problems using static variable
> in unmanaged codes in a managed application.
>
> Anyone has success in implementing for quantlib as described? Or any
> suggestions are very apprecilated.
>
> Thanks
> --
> View this message in context:
> http://old.nabble.com/Any-soultions-to-this-old-thread-of-calling-quantlib-from-managed-code--tp27808812p27808812.html
> Sent from the quantlib-users mailing list archive at Nabble.com.
>
>
>
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> QuantLib-users mailing list
> QuantLib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
QuantLib-users mailing list
QuantLib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Any soultions to this old thread of calling quantlib from managed code?

Nathan Abbott
By the way do you think if there is any issues if I
wrap #pragma managed(push, off) and #pragma managed(pop) around the
definition of singleton template (i.e. in singleton.hpp


That is a good question. I have to think about.  If you do it, I think that you should also do it for all the classes that inherits from Singleton. In fact, I think you should do it for all class that are defined only in header files especially the classes that also has any static variables. An example of this is the currencies classes.  I had to write wrapper functions for them also.

for example

#pragma managed(push, off)
QuantLib::Currency CreateUSDCurrency()
{
    return QuantLib::USDCurrency();
}
#pragma managed(pop)


#pragma managed(push, off)
QuantLib::Currency CreateEURCurrency()
{
    return QuantLib::EURCurrency();
}
#pragma managed(pop)






------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users