32 #ifndef _util_group_memory_h 33 #define _util_group_memory_h 38 #include <util/class/class.h> 39 #include <util/group/thread.h> 43 #if 0 // this can be used to catch accidental conversions to int 45 friend size_t distsize_to_size(
const distsize_t &a);
51 friend ostream& operator <<(ostream& o,
const distsize_t &s);
60 { s=a.s;
return *
this; }
62 { s+=a.s;
return *
this; }
104 inline ostream& operator <<(ostream& o,
const distsize_t &s) {
return o<<s.s; }
105 inline size_t distsize_to_size(
const distsize_t &a) {
return a.s;}
106 #elif defined(HAVE_LONG_LONG) 108 typedef long long distssize_t;
109 inline size_t distsize_to_size(
const distsize_t &a) {
return a;}
111 typedef unsigned long distsize_t;
112 typedef long distssize_t;
113 inline size_t distsize_to_size(
const distsize_t &a) {
return a;}
137 distsize_t *offsets_;
142 void obtain_local_lock(
size_t start,
size_t fence);
143 void release_local_lock(
size_t start,
size_t fence);
150 int me()
const {
return me_; }
152 int n()
const {
return n_; }
157 virtual void set_localsize(
size_t) = 0;
159 size_t localsize() {
return distsize_to_size(offsets_[me_+1]-offsets_[me_]); }
161 virtual void *localdata() = 0;
166 {
return distsize_to_size(offsets_[node+1] - offsets_[node]); }
168 distsize_t
offset(
int node) {
return offsets_[node]; }
173 virtual void activate();
175 virtual void deactivate();
178 virtual void *obtain_writeonly(distsize_t offset,
int size) = 0;
184 virtual void *obtain_readwrite(distsize_t offset,
int size) = 0;
186 virtual void *obtain_readonly(distsize_t offset,
int size) = 0;
188 virtual void release_readonly(
void *data, distsize_t offset,
int size) = 0;
190 virtual void release_writeonly(
void *data, distsize_t offset,
int size)=0;
193 virtual void release_readwrite(
void *data, distsize_t offset,
int size)=0;
195 virtual void sum_reduction(
double *data, distsize_t doffset,
int dsize);
196 virtual void sum_reduction_on_node(
double *data,
size_t doffset,
int dsize,
202 virtual void sync() = 0;
208 virtual void* malloc_local(
size_t nbyte);
209 virtual double* malloc_local_double(
size_t ndouble);
212 virtual void free_local(
void *data);
213 virtual void free_local_double(
double *data);
221 virtual void catchup();
224 virtual void print(std::ostream &o =
ExEnv::out0())
const;
233 static MemoryGrp* initial_memorygrp(
int &argc,
char** argv);
243 static MemoryGrp* get_default_memorygrp();
252 template <
class data_t>
255 enum AccessType { None, Read, Write, ReadWrite };
256 AccessType accesstype_;
269 data_t *writeonly(distsize_t offset,
int length);
274 data_t *readwrite(distsize_t offset,
int length);
279 const data_t *readonly(distsize_t offset,
int length);
283 data_t *writeonly_on_node(
size_t offset,
int length,
int node = -1);
284 data_t *readwrite_on_node(
size_t offset,
int length,
int node = -1);
285 const data_t *readonly_on_node(
size_t offset,
int length,
int node = -1);
297 template <
class data_t>
304 template <
class data_t>
308 if (accesstype_ != None) release();
309 data_ = (data_t *) grp_->obtain_writeonly(
sizeof(data_t)*offset,
310 sizeof(data_t)*length);
317 template <
class data_t>
321 if (accesstype_ != None) release();
322 data_ = (data_t *) grp_->obtain_readwrite(
sizeof(data_t)*offset,
323 sizeof(data_t)*length);
326 accesstype_ = ReadWrite;
330 template <
class data_t>
334 if (accesstype_ != None) release();
335 data_ = (data_t *) grp_->obtain_readonly(
sizeof(data_t)*offset,
336 sizeof(data_t)*length);
343 template <
class data_t>
347 if (node == -1) node = grp_->me();
348 return writeonly(offset + grp_->offset(node)/
sizeof(data_t), length);
351 template <
class data_t>
355 if (node == -1) node = grp_->me();
356 return readwrite(offset + grp_->offset(node)/
sizeof(data_t), length);
359 template <
class data_t>
363 if (node == -1) node = grp_->me();
364 return readonly(offset + grp_->offset(node)/
sizeof(data_t), length);
367 template <
class data_t>
371 if (accesstype_ == Write)
372 grp_->release_writeonly((data_t *)data_,
373 sizeof(data_t)*offset_,
sizeof(data_t)*length_);
374 if (accesstype_ == Read)
375 grp_->release_readonly(data_,
sizeof(data_t)*offset_,
376 sizeof(data_t)*length_);
377 if (accesstype_ == ReadWrite)
378 grp_->release_readwrite(data_,
sizeof(data_t)*offset_,
379 sizeof(data_t)*length_);
void release()
Release the access to the chunk of global memory that was obtained with writeonly, readwrite, readonly, writeonly_on_node, readwrite_on_node, and readonly_on_node.
Definition: memory.h:369
const data_t * readonly(distsize_t offset, int length)
Request read only access to global memory at the global address offset and with size length...
Definition: memory.h:332
distsize_t totalsize()
Returns the sum of all memory allocated on all nodes.
Definition: memory.h:170
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:244
data_t * writeonly(distsize_t offset, int length)
Request write only access to global memory at the global address offset and with size length...
Definition: memory.h:306
A template class that maintains references counts.
Definition: ref.h:332
MemoryGrpBuf(const Ref< MemoryGrp > &)
Creates a new MemoryGrpBuf given a MemoryGrp reference.
Definition: memory.h:298
data_t * readwrite(distsize_t offset, int length)
Request read write access to global memory at the global address offset and with size length...
Definition: memory.h:319
The MemoryGrp abstract class provides a way of accessing distributed memory in a parallel machine...
Definition: memory.h:124
data_t * writeonly_on_node(size_t offset, int length, int node=-1)
These behave like writeonly, readwrite, and readonly, except the offset is local to the node specifie...
Definition: memory.h:345
int length() const
The length of the current bit of memory.
Definition: memory.h:291
int n() const
Returns how many nodes there are.
Definition: memory.h:152
int size(int node)
Returns the amount of memory residing on node.
Definition: memory.h:165
int me() const
Returns who I am.
Definition: memory.h:150
static std::ostream & out0()
Return an ostream that writes from node 0.
distsize_t offset(int node)
Returns the global offset to node's memory.
Definition: memory.h:168
The MemoryGrpBuf class provides access to pieces of the global shared memory that have been obtained ...
Definition: memory.h:253
size_t localsize()
Returns the amount of memory residing locally on me().
Definition: memory.h:159
distsize_t localoffset()
Returns the global offset to this node's memory.
Definition: memory.h:163