Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

memory.h

00001 //
00002 // memory.h
00003 //
00004 // Copyright (C) 1996 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifdef __GNUC__
00029 #pragma interface
00030 #endif
00031 
00032 #ifndef _util_group_memory_h
00033 #define _util_group_memory_h
00034 
00035 #include <iostream>
00036 
00037 #include <scconfig.h>
00038 #include <util/class/class.h>
00039 #include <util/group/thread.h>
00040 
00041 #if 0 // this can be used to catch accidental conversions to int
00042 class distsize_t {
00043     friend size_t distsize_to_size(const distsize_t &a);
00044     friend distsize_t operator *(const int &a,const distsize_t &b);
00045     friend distsize_t operator +(const int &a,const distsize_t &b);
00046     friend distsize_t operator -(const int &a,const distsize_t &b);
00047     friend distsize_t operator /(const int &a,const distsize_t &b);
00048     friend distsize_t operator %(const int &a,const distsize_t &b);
00049     friend ostream& operator <<(ostream& o, const distsize_t &s);
00050   private:
00051     unsigned long long s;
00052   public:
00053     distsize_t(): s(999999999999999LL) {}
00054     distsize_t(int a): s(a) {}
00055     distsize_t(unsigned int a): s(a) {}
00056     distsize_t(unsigned long long a): s(a) {}
00057     distsize_t &operator =(const distsize_t &a)
00058         { s=a.s; return *this; }
00059     distsize_t &operator +=(const distsize_t &a)
00060         { s+=a.s; return *this; }
00061     distsize_t operator *(const distsize_t &a) const
00062         { return s*a.s; }
00063     distsize_t operator +(const distsize_t &a) const
00064         { return s+a.s; }
00065     distsize_t operator -(const distsize_t &a) const
00066         { return s-a.s; }
00067     distsize_t operator /(const distsize_t &a) const
00068         { return s/a.s; }
00069     distsize_t operator %(const distsize_t &a) const
00070         { return s%a.s; }
00071     bool operator <(const distsize_t &a) const
00072         { return s<a.s; }
00073     bool operator <=(const distsize_t &a) const
00074         { return s<=a.s; }
00075     bool operator >(const distsize_t &a) const
00076         { return s>a.s; }
00077     bool operator >=(const distsize_t &a) const
00078         { return s>=a.s; }
00079     bool operator ==(const distsize_t &a) const
00080         { return s==a.s; }
00081     distsize_t operator *(const int &a) const
00082         { return s*a; }
00083     distsize_t operator +(const int &a) const
00084         { return s+a; }
00085     distsize_t operator -(const int &a) const
00086         { return s-a; }
00087     distsize_t operator /(const int &a) const
00088         { return s/a; }
00089     distsize_t operator %(const int &a) const
00090         { return s%a; }
00091 };
00092 inline distsize_t operator *(const int &a,const distsize_t &b)
00093 { return a*b.s; }
00094 inline distsize_t operator +(const int &a,const distsize_t &b)
00095 { return a+b.s; }
00096 inline distsize_t operator -(const int &a,const distsize_t &b)
00097 { return a-b.s; }
00098 inline distsize_t operator /(const int &a,const distsize_t &b)
00099 { return a/b.s; }
00100 inline distsize_t operator %(const int &a,const distsize_t &b)
00101 { return a%b.s; }
00102 inline ostream& operator <<(ostream& o, const distsize_t &s) { return o<<s.s; }
00103 inline size_t distsize_to_size(const distsize_t &a) {return a.s;}
00104 #elif defined(HAVE_LONG_LONG)
00105 typedef unsigned long long distsize_t;
00106 typedef long long distssize_t;
00107 inline size_t distsize_to_size(const distsize_t &a) {return a;}
00108 #else
00109 typedef unsigned long distsize_t;
00110 typedef long distssize_t;
00111 inline size_t distsize_to_size(const distsize_t &a) {return a;}
00112 #endif
00113 
00171 class MemoryGrp: public DescribedClass {
00172   private:
00173     Ref<ThreadLock> *locks_;
00174     int nlock_;
00175  
00176     void init_locks();
00177 
00178 
00179   protected:
00180     // derived classes must fill in all these
00181     // ~MemoryGrp deletes the arrays
00182     int me_;
00183     int n_;
00184     distsize_t *offsets_; // offsets_[n_] is the fence for all data
00185 
00186     // set to nonzero for debugging information
00187     int debug_;
00188 
00189     void obtain_local_lock(size_t start, size_t fence);
00190     void release_local_lock(size_t start, size_t fence);
00191   public:
00192     MemoryGrp();
00193     MemoryGrp(const Ref<KeyVal>&);
00194     virtual ~MemoryGrp();
00195     
00197     int me() const { return me_; }
00199     int n() const { return n_; }
00200 
00204     virtual void set_localsize(int) = 0;
00206     size_t localsize() { return distsize_to_size(offsets_[me_+1]-offsets_[me_]); }
00208     virtual void *localdata() = 0;
00210     distsize_t localoffset() { return offsets_[me_]; }
00212     int size(int node)
00213         { return distsize_to_size(offsets_[node+1] - offsets_[node]); }
00215     distsize_t offset(int node) { return offsets_[node]; }
00217     distsize_t totalsize() { return offsets_[n_]; }
00218 
00220     virtual void activate();
00222     virtual void deactivate();
00223 
00225     virtual void *obtain_writeonly(distsize_t offset, int size) = 0;
00231     virtual void *obtain_readwrite(distsize_t offset, int size) = 0;
00233     virtual void *obtain_readonly(distsize_t offset, int size) = 0;
00235     virtual void release_readonly(void *data, distsize_t offset, int size) = 0;
00237     virtual void release_writeonly(void *data, distsize_t offset, int size)=0;
00240     virtual void release_readwrite(void *data, distsize_t offset, int size)=0;
00241 
00242     virtual void sum_reduction(double *data, distsize_t doffset, int dsize);
00243     virtual void sum_reduction_on_node(double *data, int doffset, int dsize,
00244                                        int node = -1);
00245 
00248     virtual void sync() = 0;
00249 
00256     virtual void catchup();
00257 
00259     virtual void print(std::ostream &o = ExEnv::out()) const;
00260 
00266     static MemoryGrp* initial_memorygrp(int &argc, char** argv);
00267     static MemoryGrp* initial_memorygrp();
00270     static void set_default_memorygrp(const Ref<MemoryGrp>&);
00272     static MemoryGrp* get_default_memorygrp();
00273 };
00274 
00275 
00281 template <class data_t>
00282 class MemoryGrpBuf {
00283     Ref<MemoryGrp> grp_;
00284     enum AccessType { None, Read, Write, ReadWrite };
00285     AccessType accesstype_;
00286     data_t *data_;
00287     distsize_t offset_;
00288     int length_;
00289   public:
00293     MemoryGrpBuf(const Ref<MemoryGrp> &);
00298     data_t *writeonly(distsize_t offset, int length);
00303     data_t *readwrite(distsize_t offset, int length);
00308     const data_t *readonly(distsize_t offset, int length);
00312     data_t *writeonly_on_node(int offset, int length, int node = -1);
00313     data_t *readwrite_on_node(int offset, int length, int node = -1);
00314     const data_t *readonly_on_node(int offset, int length, int node = -1);
00318     void release();
00320     int length() const { return length_; }
00321 };
00322 
00324 // MemoryGrpBuf members
00325 
00326 template <class data_t>
00327 MemoryGrpBuf<data_t>::MemoryGrpBuf(const Ref<MemoryGrp> & grp)
00328 {
00329   grp_ = grp;
00330   accesstype_ = None;
00331 }
00332 
00333 template <class data_t>
00334 data_t *
00335 MemoryGrpBuf<data_t>::writeonly(distsize_t offset, int length)
00336 {
00337   if (accesstype_ != None) release();
00338   data_ = (data_t *) grp_->obtain_writeonly(sizeof(data_t)*offset,
00339                                             sizeof(data_t)*length);
00340   offset_ = offset;
00341   length_ = length;
00342   accesstype_ = Write;
00343   return data_;
00344 }
00345 
00346 template <class data_t>
00347 data_t *
00348 MemoryGrpBuf<data_t>::readwrite(distsize_t offset, int length)
00349 {
00350   if (accesstype_ != None) release();
00351   data_ = (data_t *) grp_->obtain_readwrite(sizeof(data_t)*offset,
00352                                             sizeof(data_t)*length);
00353   offset_ = offset;
00354   length_ = length;
00355   accesstype_ = ReadWrite;
00356   return data_;
00357 }
00358 
00359 template <class data_t>
00360 const data_t *
00361 MemoryGrpBuf<data_t>::readonly(distsize_t offset, int length)
00362 {
00363   if (accesstype_ != None) release();
00364   data_ = (data_t *) grp_->obtain_readonly(sizeof(data_t)*offset,
00365                                            sizeof(data_t)*length);
00366   offset_ = offset;
00367   length_ = length;
00368   accesstype_ = Read;
00369   return data_;
00370 }
00371 
00372 template <class data_t>
00373 data_t *
00374 MemoryGrpBuf<data_t>::writeonly_on_node(int offset, int length, int node)
00375 {
00376   if (node == -1) node = grp_->me();
00377   return writeonly(offset + grp_->offset(node)/sizeof(data_t), length);
00378 }
00379 
00380 template <class data_t>
00381 data_t *
00382 MemoryGrpBuf<data_t>::readwrite_on_node(int offset, int length, int node)
00383 {
00384   if (node == -1) node = grp_->me();
00385   return readwrite(offset + grp_->offset(node)/sizeof(data_t), length);
00386 }
00387 
00388 template <class data_t>
00389 const data_t *
00390 MemoryGrpBuf<data_t>::readonly_on_node(int offset, int length, int node)
00391 {
00392   if (node == -1) node = grp_->me();
00393   return readonly(offset + grp_->offset(node)/sizeof(data_t), length);
00394 }
00395 
00396 template <class data_t>
00397 void
00398 MemoryGrpBuf<data_t>::release()
00399 {
00400   if (accesstype_ == Write)
00401       grp_->release_writeonly((data_t *)data_,
00402                               sizeof(data_t)*offset_, sizeof(data_t)*length_);
00403   if (accesstype_ == Read)
00404       grp_->release_readonly(data_, sizeof(data_t)*offset_,
00405                              sizeof(data_t)*length_);
00406   if (accesstype_ == ReadWrite)
00407       grp_->release_readwrite(data_, sizeof(data_t)*offset_,
00408                               sizeof(data_t)*length_);
00409 
00410   accesstype_ = None;
00411 }
00412 
00413 #endif
00414 
00415 // Local Variables:
00416 // mode: c++
00417 // c-file-style: "CLJ"
00418 // End:

Generated at Thu Oct 4 18:08:45 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.