memory.h
//
// memory.h
//
// Copyright (C) 1996 Limit Point Systems, Inc.
//
// Author: Curtis Janssen <cljanss@limitpt.com>
// Maintainer: LPS
//
// This file is part of the SC Toolkit.
//
// The SC Toolkit is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// The SC Toolkit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// The U.S. Government is granted a limited license as per AL 91-7.
//
#ifdef __GNUC__
#pragma interface
#endif
#ifndef _util_group_memory_h
#define _util_group_memory_h
#include <iostream>
#include <scconfig.h>
#include <util/class/class.h>
#include <util/group/thread.h>
namespace sc {
#if 0 // this can be used to catch accidental conversions to int
class distsize_t {
friend size_t distsize_to_size(const distsize_t &a);
friend distsize_t operator *(const int &a,const distsize_t &b);
friend distsize_t operator +(const int &a,const distsize_t &b);
friend distsize_t operator -(const int &a,const distsize_t &b);
friend distsize_t operator /(const int &a,const distsize_t &b);
friend distsize_t operator %(const int &a,const distsize_t &b);
friend ostream& operator <<(ostream& o, const distsize_t &s);
private:
unsigned long long s;
public:
distsize_t(): s(999999999999999LL) {}
distsize_t(int a): s(a) {}
distsize_t(unsigned int a): s(a) {}
distsize_t(unsigned long long a): s(a) {}
distsize_t &operator =(const distsize_t &a)
{ s=a.s; return *this; }
distsize_t &operator +=(const distsize_t &a)
{ s+=a.s; return *this; }
distsize_t operator *(const distsize_t &a) const
{ return s*a.s; }
distsize_t operator +(const distsize_t &a) const
{ return s+a.s; }
distsize_t operator -(const distsize_t &a) const
{ return s-a.s; }
distsize_t operator /(const distsize_t &a) const
{ return s/a.s; }
distsize_t operator %(const distsize_t &a) const
{ return s%a.s; }
bool operator <(const distsize_t &a) const
{ return s<a.s; }
bool operator <=(const distsize_t &a) const
{ return s<=a.s; }
bool operator >(const distsize_t &a) const
{ return s>a.s; }
bool operator >=(const distsize_t &a) const
{ return s>=a.s; }
bool operator ==(const distsize_t &a) const
{ return s==a.s; }
distsize_t operator *(const int &a) const
{ return s*a; }
distsize_t operator +(const int &a) const
{ return s+a; }
distsize_t operator -(const int &a) const
{ return s-a; }
distsize_t operator /(const int &a) const
{ return s/a; }
distsize_t operator %(const int &a) const
{ return s%a; }
};
inline distsize_t operator *(const int &a,const distsize_t &b)
{ return a*b.s; }
inline distsize_t operator +(const int &a,const distsize_t &b)
{ return a+b.s; }
inline distsize_t operator -(const int &a,const distsize_t &b)
{ return a-b.s; }
inline distsize_t operator /(const int &a,const distsize_t &b)
{ return a/b.s; }
inline distsize_t operator %(const int &a,const distsize_t &b)
{ return a%b.s; }
inline ostream& operator <<(ostream& o, const distsize_t &s) { return o<<s.s; }
inline size_t distsize_to_size(const distsize_t &a) {return a.s;}
#elif defined(HAVE_LONG_LONG)
typedef unsigned long long distsize_t;
typedef long long distssize_t;
inline size_t distsize_to_size(const distsize_t &a) {return a;}
#else
typedef unsigned long distsize_t;
typedef long distssize_t;
inline size_t distsize_to_size(const distsize_t &a) {return a;}
#endif
class MemoryGrp: public DescribedClass {
private:
Ref<ThreadLock> *locks_;
int nlock_;
void init_locks();
protected:
// derived classes must fill in all these
// ~MemoryGrp deletes the arrays
int me_;
int n_;
distsize_t *offsets_; // offsets_[n_] is the fence for all data
// set to nonzero for debugging information
int debug_;
void obtain_local_lock(size_t start, size_t fence);
void release_local_lock(size_t start, size_t fence);
public:
MemoryGrp();
MemoryGrp(const Ref<KeyVal>&);
virtual ~MemoryGrp();
int me() const { return me_; }
int n() const { return n_; }
virtual void set_localsize(size_t) = 0;
size_t localsize() { return distsize_to_size(offsets_[me_+1]-offsets_[me_]); }
virtual void *localdata() = 0;
distsize_t localoffset() { return offsets_[me_]; }
int size(int node)
{ return distsize_to_size(offsets_[node+1] - offsets_[node]); }
distsize_t offset(int node) { return offsets_[node]; }
distsize_t totalsize() { return offsets_[n_]; }
virtual void activate();
virtual void deactivate();
virtual void *obtain_writeonly(distsize_t offset, int size) = 0;
virtual void *obtain_readwrite(distsize_t offset, int size) = 0;
virtual void *obtain_readonly(distsize_t offset, int size) = 0;
virtual void release_readonly(void *data, distsize_t offset, int size) = 0;
virtual void release_writeonly(void *data, distsize_t offset, int size)=0;
virtual void release_readwrite(void *data, distsize_t offset, int size)=0;
virtual void sum_reduction(double *data, distsize_t doffset, int dsize);
virtual void sum_reduction_on_node(double *data, size_t doffset, int dsize,
int node = -1);
virtual void sync() = 0;
virtual void* malloc_local(size_t nbyte);
virtual double* malloc_local_double(size_t ndouble);
virtual void free_local(void *data);
virtual void free_local_double(double *data);
virtual void catchup();
virtual void print(std::ostream &o = ExEnv::out0()) const;
static MemoryGrp* initial_memorygrp(int &argc, char** argv);
static MemoryGrp* initial_memorygrp();
static void set_default_memorygrp(const Ref<MemoryGrp>&);
static MemoryGrp* get_default_memorygrp();
};
template <class data_t>
class MemoryGrpBuf {
Ref<MemoryGrp> grp_;
enum AccessType { None, Read, Write, ReadWrite };
AccessType accesstype_;
data_t *data_;
distsize_t offset_;
int length_;
public:
MemoryGrpBuf(const Ref<MemoryGrp> &);
data_t *writeonly(distsize_t offset, int length);
data_t *readwrite(distsize_t offset, int length);
const data_t *readonly(distsize_t offset, int length);
data_t *writeonly_on_node(size_t offset, int length, int node = -1);
data_t *readwrite_on_node(size_t offset, int length, int node = -1);
const data_t *readonly_on_node(size_t offset, int length, int node = -1);
void release();
int length() const { return length_; }
};
// MemoryGrpBuf members
template <class data_t>
MemoryGrpBuf<data_t>::MemoryGrpBuf(const Ref<MemoryGrp> & grp)
{
grp_ = grp;
accesstype_ = None;
}
template <class data_t>
data_t *
MemoryGrpBuf<data_t>::writeonly(distsize_t offset, int length)
{
if (accesstype_ != None) release();
data_ = (data_t *) grp_->obtain_writeonly(sizeof(data_t)*offset,
sizeof(data_t)*length);
offset_ = offset;
length_ = length;
accesstype_ = Write;
return data_;
}
template <class data_t>
data_t *
MemoryGrpBuf<data_t>::readwrite(distsize_t offset, int length)
{
if (accesstype_ != None) release();
data_ = (data_t *) grp_->obtain_readwrite(sizeof(data_t)*offset,
sizeof(data_t)*length);
offset_ = offset;
length_ = length;
accesstype_ = ReadWrite;
return data_;
}
template <class data_t>
const data_t *
MemoryGrpBuf<data_t>::readonly(distsize_t offset, int length)
{
if (accesstype_ != None) release();
data_ = (data_t *) grp_->obtain_readonly(sizeof(data_t)*offset,
sizeof(data_t)*length);
offset_ = offset;
length_ = length;
accesstype_ = Read;
return data_;
}
template <class data_t>
data_t *
MemoryGrpBuf<data_t>::writeonly_on_node(size_t offset, int length, int node)
{
if (node == -1) node = grp_->me();
return writeonly(offset + grp_->offset(node)/sizeof(data_t), length);
}
template <class data_t>
data_t *
MemoryGrpBuf<data_t>::readwrite_on_node(size_t offset, int length, int node)
{
if (node == -1) node = grp_->me();
return readwrite(offset + grp_->offset(node)/sizeof(data_t), length);
}
template <class data_t>
const data_t *
MemoryGrpBuf<data_t>::readonly_on_node(size_t offset, int length, int node)
{
if (node == -1) node = grp_->me();
return readonly(offset + grp_->offset(node)/sizeof(data_t), length);
}
template <class data_t>
void
MemoryGrpBuf<data_t>::release()
{
if (accesstype_ == Write)
grp_->release_writeonly((data_t *)data_,
sizeof(data_t)*offset_, sizeof(data_t)*length_);
if (accesstype_ == Read)
grp_->release_readonly(data_, sizeof(data_t)*offset_,
sizeof(data_t)*length_);
if (accesstype_ == ReadWrite)
grp_->release_readwrite(data_, sizeof(data_t)*offset_,
sizeof(data_t)*length_);
accesstype_ = None;
}
}
#endif
// Local Variables:
// mode: c++
// c-file-style: "CLJ"
// End: