Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:19:19+00:00 2026-06-08T21:19:19+00:00

I have a Singleton class to have only one single instance of my MPIProxy

  • 0

I have a Singleton class to have only one single instance of my MPIProxy class (as it otherwise would result in to many constructions).

To create the single MPIProxy class I have the following typedef:

typedef Singleton<MPIProxy_> MPIProxySingleton;

However to access the instance I need the following code:

int myrank = MPIProxySingleton::instance().getRank();

I would prefer to have allow the clients to write instead of MPIProxySingleton::instance().xy() simply MPIProxy.xy(). Where .xy is a function of the MPIProxy_ class. Such that the code above would look like:

int myrank = MPIProxy.getRank();

I have tried the follwing:

typedef MPIProxySingleton::instance() MPIProxy;

However this result in compile errors:

/Users/david/Desktop/GSOC/miind/miind/./libs/MPILib/include/utilities/MPIProxy.hpp:158:31: error: C++ requires a type specifier for all declarations
typedef Singleton<MPIProxy_>::instance() MPIProxy;
~~~~~~~                       ^
/Users/david/Desktop/GSOC/miind/miind/./libs/MPILib/include/utilities/MPIProxy.hpp:158:41: error: expected ';' after top level declarator
typedef Singleton<MPIProxy_>::instance() MPIProxy;

Here is the code of my Singleton class:

/** Singleton holder template class.
 * Template class to create singletons. A singleton instance of class
 * MyType is created and accessed using
 * \code
 * typedef Singleton<MyType> MySingletonType;
 * MyType& myRef = MySingletonType::instance()
 * // ... do something ...
 * \endcode
 */
template<class T>
class Singleton
{
public:
    // disallow creation, copying and assignment

    /** Deleted constructor to disallow explicit construction.
     * Is not defined.
     */
    Singleton()=delete;

    /** Deleted copy constructor to disallow explicit copying.
     * Is not defined.
     * @param S A singleton object.
     */
    Singleton(const Singleton& S)=delete;

    /** Deleted assignment operator to disallow explicit assignment.
     * @param S A singleton object.
     * @return The current singleton.
     */
    Singleton& operator=(const Singleton& S)=delete;

    /** Return a reference to the only instance of \c Singleton<T>.
     * @return A reference to the instance of the object.
     */
    static T& instance();

    /** Destructor.
     */
    ~Singleton();

private:


    /** Create method. Creates the singleton instance (a Meyers singleton, ie.
     * a function static object) upon the first call to \c instance().
     */
    static void create();

    /** Pointer to the instance.
     */
    static T* pInstance_;

    /** Status of the singleton. True if the singleton was destroyed.
     */
    static bool destroyed_;

};

/** Returns the unique instance of class T. If it was already
 *  deleted an exception is thrown. If the class T was never used
 *  before a new instance is generated.
 *
 * @return Unique instance of class T
 */
template<class T> T& Singleton<T>::instance()
{
    if (!pInstance_) {
        if (destroyed_) {
            // dead reference
            throw Exception("The instance was already destroyed");
        } else {
            // initial creation
            create();
        }
    }
    return *pInstance_;
}

template<class T> Singleton<T>::~Singleton()
{
    pInstance_ = 0;
    destroyed_ = true;
}

template<class T> void Singleton<T>::create()
{
    static T theInstance;
    pInstance_ = &theInstance;
}

template<class T> T* Singleton<T>::pInstance_ = 0;
template<class T> bool Singleton<T>::destroyed_ = false;

And here the header of my MPIProxy_ class

class MPIProxy_ {
public:
    /**
     * destructor
     */
    virtual ~MPIProxy_();

    /**
     * wrapper method to return the process id, if mpi is disabled it returns 0
     * @return the world rank of a process
     */
    int getRank() const;

    /**
     * wrapper method to return the size, if MPI is disabled it returns 1
     * @return
     */
    int getSize() const;

    /**
     * wrapper for mpi barrier
     */
    void barrier();

    /**
     * waits until all request stored in the vector _mpiStatus are finished
     */
    void waitAll();

    /**
     * Broadcast the value from root
     * @param value The value to be broadcast
     * @param root The root process
     */
    template<typename T>
    void broadcast(T& value, int root);

    /**
     * asynchronous receive operation the mpi status is stored in _mpiStatus
     * @param source The source of the message
     * @param tag The tag of the message
     * @param value The value received
     */
    template<typename T>
    void irecv(int source, int tag, T& value) const;

    /**
     * asynchronous send operation the mpi status is stored in _mpiStatus
     * @param dest The destination of the message
     * @param tag The tag of the message
     * @param value The value sended
     */
    template<typename T>
    void isend(int dest, int tag, const T& value) const;


private:
    /**
     * Declare the Singleton class a friend to allow construction of the MPIProxy_ class
     */
    friend class Singleton<MPIProxy_>;
    /**
     * constructor sets the MPI rank and size
     */
    MPIProxy_();

#ifdef ENABLE_MPI
    /**
     * stores the mpi statuses
     */
    static std::vector<boost::mpi::request> _mpiStatus;
#endif

    /**
     * storage of the rank to avoid function calls
     */
    static int _rank;

    /**
     * storage of the size to avoid function calls
     */
    static int _size;
};

template<typename T>
void MPIProxy_::broadcast(T& value, int root) {
#ifdef ENABLE_MPI
    mpi::communicator world;
    boost::mpi::broadcast(world, value, root);
#endif
}

template<typename T>
void MPIProxy_::irecv(int source, int tag, T& value) const {
#ifdef ENABLE_MPI
    mpi::communicator world;
    _mpiStatus.push_back(world.irecv(source, tag, value));
#else
    MPILib::utilities::Exception("MPI Code called from serial code in irecv");
#endif
}

template<typename T>
void MPIProxy_::isend(int dest, int tag, const T& value) const {
#ifdef ENABLE_MPI
    mpi::communicator world;
    _mpiStatus.push_back(world.isend(dest, tag, value));
#else
    MPILib::utilities::Exception("MPI Code called from serial code in isend");
#endif
}

typedef Singleton<MPIProxy_> MPIProxySingleton;
//typedef MPIProxySingleton::instance() MPIProxy;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:19:20+00:00Added an answer on June 8, 2026 at 9:19 pm

    Wrapper functions are always a good option in such cases:

    MPIProxy_ &  MPIProxy()
    {
       return MPIProxySingleton::instance();
    }
    

    Then use it as:

    MPIProxy().xy();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, I have following Singleton Database Class: class Database { private static $instance;
In a web app, I need to have only one instance of a class
Suppose I have a Singleton class (any class can get the instance): class data
I have a timer in my class that is I only ever want one
I have a singleton class for global access to config information. This singleton class
I have a singleton class which is being used throughout the app. I am
Assume I have a singleton class in an external lib to my application. But
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I have written a Singleton Class for managing iAds.The iAds pop up after 5
I have created a singleton class to keep track of my data on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.