LAMA
/home/brandes/workspace/LAMA/src/lama/pgas/PGASInterface.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_PGASINTERFACE_HPP_
00034 #define LAMA_PGASINTERFACE_HPP_
00035 
00036 #include <lama/SyncToken.hpp>
00037 
00038 #include <lama/pgas/PGASSyncToken.hpp>
00039 #include <lama/pgas/PGASFunctor.hpp>
00040 
00041 namespace lama
00042 {
00043 
00044 enum PGASCommunicationKind
00045 {
00046     PGASget,
00047     PGASput
00048 };
00049 
00050 class PGASInterface :public Printable
00051 {
00052 //PAGSInterface
00053 public:
00054     virtual void* allocate(size_t size) const = 0;
00055     virtual void free(void* ptr, const size_t size) const = 0;
00056     virtual void syncronizeAll() const = 0;
00057     virtual bool isPinned(const void* const ptr) const = 0;
00058     virtual PGASSyncToken* getSyncToken(int arg1) const = 0;
00059 
00060     virtual PGASCommunicationKind getPreferredCommunicationKind() const = 0;
00061 
00062     virtual void get(void* dst, const void* src, size_t length, int srcpe) const = 0;
00063     virtual void put(void* dst, const void* src, size_t length, int srcpe) const = 0;
00064 
00065     virtual PartitionId getRank() const = 0;
00066     virtual PartitionId getSize() const = 0;
00067 
00068     virtual std::auto_ptr<SyncToken> getAsync(void* dst, const void* src, size_t length, int srcPE) const;
00069     virtual std::auto_ptr<SyncToken> putAsync(void* dst, const void* src, size_t length, int srcPE) const;
00070 
00071     virtual std::auto_ptr<SyncToken> shift(void* dst, const void* src, size_t size, PartitionId destRank,
00072                                            PartitionId srcRank) const;
00073 
00074     virtual std::auto_ptr<SyncToken> broadcast(void* dst, const void* src, size_t length, int srcPE) const;
00075     virtual std::auto_ptr<SyncToken> all2all(void* dst, const void* src, size_t elemSize) const;
00076 
00077     virtual double max(const double val, const PartitionId root) const;
00078     virtual float max(const float val, const PartitionId root) const;
00079     virtual int max(const int val, const PartitionId root) const;
00080     virtual size_t max(const size_t val, const PartitionId root) const;
00081 
00082     virtual double min(const double val, const PartitionId root) const;
00083     virtual float min(const float val, const PartitionId root) const;
00084     virtual int min(const int val, const PartitionId root) const;
00085 
00086     virtual double sum(const double val, const PartitionId root) const;
00087     virtual float sum(const float val, const PartitionId root) const;
00088     virtual int sum(const int val, const PartitionId root) const;
00089     virtual size_t sum(const size_t val, const PartitionId root) const;
00090 
00091     virtual double maxToAll(const double val) const;
00092     virtual float maxToAll(const float val) const;
00093     virtual int maxToAll(const int val) const;
00094     virtual size_t maxToAll(const size_t val) const;
00095 
00096     virtual double minToAll(const double val) const;
00097     virtual float minToAll(const float val) const;
00098     virtual int minToAll(const int val) const;
00099 
00100     virtual double sumToAll(const double val) const;
00101     virtual float sumToAll(const float val) const;
00102     virtual int sumToAll(const int val) const;
00103     virtual size_t sumToAll(const size_t val) const;
00104 
00105     virtual void maxloc(double& d, int& loc, PartitionId root) const;
00106     virtual void maxloc(float& d, int& loc, PartitionId root) const;
00107     virtual void maxloc(int& d, int& loc, PartitionId root) const;
00108 
00109     virtual void swap( void* val, const size_t n, const PartitionId partner ) const;
00110 
00111     virtual void scatter ( void* myvals, const size_t partSize, const PartitionId root, const void* allvals ) const;
00112 
00113     virtual void scatter ( void* myvals, const size_t elemSize, const PartitionId root,
00114                            const void* allvals, const IndexType sizes[] ) const;
00115 
00116     virtual void gather ( void* allvals, const size_t partSize, const PartitionId root, const void* myvals ) const;
00117 
00118     virtual void gather ( void* allvals, const size_t elemSize, const PartitionId root,
00119                           const void* myvals, const IndexType sizes[] ) const;
00120 
00121     virtual void parallelReduction(PGASFunctor& reduction, PartitionId root) const;
00122 protected:
00123     virtual void writeAt( std::ostream& stream ) const = 0;
00124 private:
00125     template<typename T>
00126     T maxToAllImpl(const T val) const;
00127 
00128     template<typename T>
00129     T minToAllImpl(const T val) const;
00130 
00131     template<typename T>
00132     T sumToAllImpl(const T val) const;
00133 
00134     LAMA_LOG_DECL_STATIC_LOGGER( logger );
00135 
00136 //Singleton Pattern
00137 public:
00138     static const PGASInterface* getInstance();
00139     PGASInterface();
00140     virtual ~PGASInterface();
00141 private:
00142     static std::auto_ptr<PGASInterface> sInstance;
00143     static PGASInterface* init();
00144 };
00145 
00146 template<typename T>
00147 T PGASInterface::maxToAllImpl(T val) const
00148 {
00149     syncronizeAll();
00150     T* res = static_cast<T*>(allocate(sizeof(T)));
00151     T* res1 = static_cast<T*>(allocate(sizeof(T)));
00152     syncronizeAll();
00153     *res = max(val,0);
00154     syncronizeAll();
00155     broadcast(res1,res,sizeof(T),0);
00156     T temp = *res1;
00157     free(res,sizeof(T));
00158     free(res1,sizeof(T));
00159     return temp;
00160 }
00161 template<typename T>
00162 T PGASInterface::minToAllImpl(T val) const
00163 {
00164     T* res = static_cast<T*>(allocate(sizeof(T)));
00165     *res = min(val,0);
00166     syncronizeAll();
00167     broadcast(res,res,sizeof(T),0);
00168     T temp = *res;
00169     free(res,sizeof(T));
00170     return temp;
00171 }
00172 template<typename T>
00173 T PGASInterface::sumToAllImpl(T val) const
00174 {
00175     T* res = static_cast<T*>(allocate(sizeof(T)));
00176     *res = sum(val,0);
00177     syncronizeAll();
00178     broadcast(res,res,sizeof(T),0);
00179     T temp = *res;
00180     free(res,sizeof(T));
00181     return temp;
00182 }
00183 }
00184 
00185 #endif // LAMA_PGASINTERFACE_HPP_