fix for intel's compiler

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

fix for intel's compiler

Kakhkhor Abdijalilov
Singleton pattern doesn't work with Intel's C++ compiler 11.1 on
Windows.  I tested it both in 32 and 64 bit modes.

When optimization is enabled (I used O3), each translation unit ends
up creating its own instance of Singleton. For example, this code
fragment doesn't do what it is supposed to do.

// file1.cpp
Settings::instance().evaluationDate() = date1;

// file2.cpp
Settings::instance().evaluationDate() = date2; // file1.cpp still sees date1

// fie3.cpp
Date date3 = Settings::instance().evaluationDate(); // not equal to date2

Overwriting  evaluation date in file2.cpp doesn't actually overwrite
it. The above code will create 3 instances of Settings class and the
program will eventually crash. Somehow static non-const local
variables and templates don't mix on ICC. I modified the original
implementation to uses const static variable and everything worked
well. The new implementation is attached.

Regards,
Kakhkhor Abdijalilov.

//-----------------------------------------
    template <class T>
    class Singleton : private boost::noncopyable {
    public:
        static T& instance() {
            #if defined(QL_ENABLE_SESSIONS)
            Integer id = sessionId();
            #else
            Integer id = 0;
            #endif
            boost::shared_ptr<T>& p = (*instances_)[id];
            if (!p)
                p.reset(new T);
            return *p;
        }
    protected:
        Singleton() {}
    private:
        typedef std::map<Integer, boost::shared_ptr<T> > map_type;
        static const boost::scoped_ptr<map_type> instances_;
    };

    template <typename T>
    const boost::scoped_ptr<Singleton<T>::map_type>
        Singleton<T>::instances_(new Singleton<T>::map_type);
//-----------------------------------------

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Sat, 2010-08-14 at 20:14 -0500, Kakhkhor Abdijalilov wrote:
> Singleton pattern doesn't work with Intel's C++ compiler 11.1 on
> Windows.  I tested it both in 32 and 64 bit modes.
>
> When optimization is enabled (I used O3), each translation unit ends
> up creating its own instance of Singleton. [...] Somehow static
> non-const local variables and templates don't mix on ICC. I modified
> the original implementation to uses const static variable and
> everything worked well. The new implementation is attached.

Ok, thanks.

Luigi


--

Ogden's Law:
The sooner you fall behind, the more time you have to catch up.



------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
I uploaded C++ code into the path system.

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Tue, 2010-08-17 at 18:17 -0500, Kakhkhor Abdijalilov wrote:
> I uploaded C++ code into the path system.

Ok, thanks.

Luigi

--

The nice thing about standards is that there are so many of them to
choose from.
-- Andrew S. Tanenbaum



------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
In reply to this post by Kakhkhor Abdijalilov
On Tue, 2010-08-17 at 18:17 -0500, Kakhkhor Abdijalilov wrote:
> I uploaded C++ code into the path system.

Kakhkhor,
        sorry, but may you upload the whole modified file instead of only the
modified functions?  With the whole file, I can easily use the diff tool
to merge your patch (and be sure that it doesn't conflict with other
changes, if any is made between now and when I do the merge); with only
part of the file, it becomes more difficult.

Thanks,
        Luigi

--

Academic: a term of opprobrium applied to those that do their job well
by those who cannot.
-- Sir Ernest Gowers



------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
> may you upload the whole modified file
It is done.
Regards,
K.A.

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
In reply to this post by Kakhkhor Abdijalilov
On Sat, 2010-08-14 at 20:14 -0500, Kakhkhor Abdijalilov wrote:
> Somehow static non-const local variables and templates don't mix on
> ICC. I modified the original implementation to uses const static
> variable and everything worked well. The new implementation is
> attached.

Kakhkhor,
        do you also need the scoped_ptr below? Would a simple static const
map_type work?

Luigi


> //-----------------------------------------
>     template <class T>
>     class Singleton : private boost::noncopyable {
>     ...
>     private:
>         typedef std::map<Integer, boost::shared_ptr<T> > map_type;
>         static const boost::scoped_ptr<map_type> instances_;
>     };
>
>     template <typename T>
>     const boost::scoped_ptr<Singleton<T>::map_type>
>         Singleton<T>::instances_(new Singleton<T>::map_type);



--

The First Rule of Optimization: Don't do it.
The Second Rule of Optimization (For experts only): Don't do it yet.
-- Michael Jackson



------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
I think it is broken just like the design using scoped_ptr or the
original design. Consider what happens when Singleton's instance is
used in two implementation files.

// file1.cpp
namespace {
    SingletonObject* const ptr = &SingletonObject::instance();
}

// file2.cpp
namespace {
    SingletonObject* const ptr = &SingletonObject::instance();
}

Who get's to build the singleton?

The only way to avoid this problem is to make the instance itself a
static local and forget about session ID. Each session would have to
reset the singleton, if needed.

A half-solution is to make sure that no instance is crated before
main() is launched. That means no static objects using singleton.

Which way we go?

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Wed, 2010-09-01 at 05:24 -0500, Kakhkhor Abdijalilov wrote:
> I think it is broken just like the design using scoped_ptr or the
> original design.

Ok. One way or the other, Singletons are all broken.  But trying to
avoid breaking interfaces for the time being, and in "normal" usage
(i.e., no Singletons initialized before main() starts): was it needed to
put the map into a scoped_ptr, as in:

private:
  static const boost::scoped_ptr<map_type> instances_;

or would the following work, too?

private:
  static map_type instances_;


Luigi


--

Though this be madness, yet there is method in't.
-- Hamlet, Act II, scene II



------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
private:
 static map_type instances_;


It works. No need to for scoped_ptr. I tested it with Intel compiler.

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
I could run several examples but the test suite crashes instantly with
memory access violation error. Can someone try the test suite with the
singleton attached below?

////////////////////////////////////////////////////////////////////
#ifndef quantlib_singleton_hpp
#define quantlib_singleton_hpp

#include <ql/types.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <map>

namespace QuantLib {

    #if defined(QL_ENABLE_SESSIONS)
    Integer sessionId();
    #endif

    template <class T>
    class Singleton : private boost::noncopyable {
    public:
        static T& instance() {
            #if defined(QL_ENABLE_SESSIONS)
            Integer id = sessionId();
            #else
            Integer id = 0;
            #endif
            boost::shared_ptr<T>& p = instances_[id];
            if (!p)
                p.reset(new T);
            return *p;
        }

    protected:
        Singleton() {}
    private:
        typedef std::map<Integer, boost::shared_ptr<T> > map_type;
        static map_type instances_;
    };

    template <typename T>
    typename Singleton<T>::map_type Singleton<T>::instances_;

}

#endif

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Thu, 2010-09-02 at 02:49 -0500, Kakhkhor Abdijalilov wrote:
> I could run several examples but the test suite crashes instantly with
> memory access violation error. Can someone try the test suite with the
> singleton attached below?

Tried it on an Ubuntu system with gcc 4.4.3.  Worked fine...

Luigi


--

The shortest way to do many things is to do only one thing at once.
-- Samuel Smiles



------------------------------------------------------------------------------
Automate Storage Tiering Simply
Optimize IT performance and efficiency through flexible, powerful,
automated storage tiering capabilities. View this brief to learn how
you can reduce costs and improve performance.
http://p.sf.net/sfu/dell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
Thanks a lot. That is encouraging.

What if the instance() method is synchronized with mutex, could it
solve static singleton problem?

------------------------------------------------------------------------------
Automate Storage Tiering Simply
Optimize IT performance and efficiency through flexible, powerful,
automated storage tiering capabilities. View this brief to learn how
you can reduce costs and improve performance.
http://p.sf.net/sfu/dell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Fri, 2010-09-10 at 10:25 -0500, Kakhkhor Abdijalilov wrote:
> Thanks a lot. That is encouraging.
>
> What if the instance() method is synchronized with mutex, could it
> solve static singleton problem?

I'm not sure it's a synchronization problem. Are you? (Also, we'd have
the brand new problem of choosing a threading library.)

Luigi



--

Testing can never demonstrate the absence of errors in software, only
their presence.
-- W.E. Dijkstra



------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
It worked with all examples, but the test suite crashes with access
violation error. I suspect it as to do something with compiler
settings. Bjam uses different compiler settings for icc and it must be
run with preset environment variables. Some of the settings could be
different from Visual Studio project settings. I will recompiled boost
and QuantLib and try it again.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
Did anyone run the test suite with boost 1.44? It is failing under
Visual Studio 2008 as well as under ICC. In the past v1.01 test suite
run fine under Visual Studio 2008, but now it is crashing with memory
access violation error message. Maybe something is wrong with boost?

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Luigi Ballabio
On Thu, 2010-09-16 at 05:01 -0500, Kakhkhor Abdijalilov wrote:
> Did anyone run the test suite with boost 1.44? It is failing under
> Visual Studio 2008 as well as under ICC. In the past v1.01 test suite
> run fine under Visual Studio 2008, but now it is crashing with memory
> access violation error message. Maybe something is wrong with boost?

It's running with gcc on Linux.  I might try a Windows box, but it's
going to take me some time.  If anyone could report success with VC2008
and Bppst 1.44, that would be of help.

Luigi


--

All generalizations are dangerous, even this one.
-- Alexandre Dumas



------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Ferdinando M. Ametrano-3
On Thu, Sep 16, 2010 at 1:50 PM, Luigi Ballabio
<[hidden email]> wrote:
> If anyone could report success with VC2008
> and Bppst 1.44, that would be of help.

no problem with the current trunk on my Windows XP box using VC2008
Express and boost 1.44 release configuration

ciao -- Nando

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
In reply to this post by Kakhkhor Abdijalilov
Unfortunately, I couldn't get it to pass the test suit. The suite
fails even with the stock QL on Visual Studio (it crashes instantly).
I suspect there is some conflict between compiler setting used to
compiler boost and QL. Reinstalling and recompiling didn't help. I
would have to reformat HD or get a new box at some point in the
future. Maybe someone could test it for us on Visual Studio? The file
is singleton.hpp and available from sourceforge.

The problem is with the test suite only. Everything else works fine on
ICC and Visual Studio. If the new singleton passes all tests on Visual
Studio, we could keep it. Complete ICC support would require more than
just fixing singleton. Let's postpone it until v1.2. I am planing to
work on it after transition to VS2010.

Regards,
Kakhkhor Abdijalilov.

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: fix for intel's compiler

Kakhkhor Abdijalilov
Sounds good to me. Also, please feel free to rename interface
functions if you think it fits better QuantLib's naming convention.

Regards,
Kakhkhor Abdijalilov.

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev