| 1 | // | 
|---|
| 2 | // file.h | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef __GNUC__ | 
|---|
| 29 | #pragma interface | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef _util_group_file_h | 
|---|
| 33 | #define _util_group_file_h | 
|---|
| 34 |  | 
|---|
| 35 | #include <iostream> | 
|---|
| 36 |  | 
|---|
| 37 | #include <scconfig.h> | 
|---|
| 38 | #include <util/class/class.h> | 
|---|
| 39 | #include <util/group/thread.h> | 
|---|
| 40 | #include <util/group/memory.h> | 
|---|
| 41 |  | 
|---|
| 42 | namespace sc { | 
|---|
| 43 |  | 
|---|
| 44 | /** The FileGrp abstract class provides a way of accessing distributed | 
|---|
| 45 | file in a parallel machine.  Several specializations are available.  For | 
|---|
| 46 | one processor, ProcFileGrp provides a simple stub implementation. | 
|---|
| 47 | Otherwise, the specializations that should work are MPIIOIFileGrp and MTMPIFileGrp. | 
|---|
| 48 |  | 
|---|
| 49 | If a FileGrp is not given to the program, then one will be automatically | 
|---|
| 50 | chosen depending on which MessageGrp is used by default, the type of | 
|---|
| 51 | machine on which the code was compiled, and what options were given at | 
|---|
| 52 | configuration time. | 
|---|
| 53 | */ | 
|---|
| 54 |  | 
|---|
| 55 | class FileGrp: public DescribedClass { | 
|---|
| 56 | private: | 
|---|
| 57 | int datafile_; | 
|---|
| 58 | char *filename_; | 
|---|
| 59 |  | 
|---|
| 60 | Ref<ThreadLock> *locks_; | 
|---|
| 61 | int nlock_; | 
|---|
| 62 |  | 
|---|
| 63 | void init_locks(); | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | protected: | 
|---|
| 67 |  | 
|---|
| 68 | // derived classes must fill in all these | 
|---|
| 69 | // ~FileGrp deletes the arrays | 
|---|
| 70 | int me_; | 
|---|
| 71 | int n_; | 
|---|
| 72 | distsize_t *offsets_; // offsets_[n_] is the fence for all data | 
|---|
| 73 |  | 
|---|
| 74 | // set to nonzero for debugging information | 
|---|
| 75 | int debug_; | 
|---|
| 76 |  | 
|---|
| 77 | void obtain_local_lock(size_t start, size_t fence); | 
|---|
| 78 | void release_local_lock(size_t start, size_t fence); | 
|---|
| 79 | public: | 
|---|
| 80 | FileGrp(); | 
|---|
| 81 | FileGrp(const Ref<KeyVal>&); | 
|---|
| 82 | virtual ~FileGrp(); | 
|---|
| 83 |  | 
|---|
| 84 | /// Opens the files | 
|---|
| 85 | void open(); | 
|---|
| 86 | /// Closes the files | 
|---|
| 87 | void close(); | 
|---|
| 88 | /// Sets the filename for the FileGrp | 
|---|
| 89 | void set_filename(char *name); | 
|---|
| 90 | /// Returns the filename for the FileGrp | 
|---|
| 91 | const char* get_filename() const { return datafile_; }; | 
|---|
| 92 |  | 
|---|
| 93 | /// Returns who I am. | 
|---|
| 94 | int me() const { return me_; } | 
|---|
| 95 | /// Returns how many nodes there are. | 
|---|
| 96 | int n() const { return n_; } | 
|---|
| 97 |  | 
|---|
| 98 | /** Set the size of locally held data. | 
|---|
| 99 | When data is accessed using a global offset counting | 
|---|
| 100 | starts at node 0 and proceeds up to node n() - 1. */ | 
|---|
| 101 | virtual void set_localsize(size_t) = 0; | 
|---|
| 102 | /// Returns the amount of data residing locally on me(). | 
|---|
| 103 | size_t localsize() { return distsize_to_size(offsets_[me_+1]-offsets_[me_]); } | 
|---|
| 104 | /// Returns the global offset to this node's data. | 
|---|
| 105 | distsize_t localoffset() { return offsets_[me_]; } | 
|---|
| 106 | /// Returns the amount of data residing on node. | 
|---|
| 107 | int size(int node) | 
|---|
| 108 | { return distsize_to_size(offsets_[node+1] - offsets_[node]); } | 
|---|
| 109 | /// Returns the global offset to node's data. | 
|---|
| 110 | distsize_t offset(int node) { return offsets_[node]; } | 
|---|
| 111 | /// Returns the sum of all data allocated on all nodes. | 
|---|
| 112 | distsize_t totalsize() { return offsets_[n_]; } | 
|---|
| 113 |  | 
|---|
| 114 | /// Activate is called before the data is to be used. | 
|---|
| 115 | virtual void activate(); | 
|---|
| 116 | /// Deactivate is called after the data has been used. | 
|---|
| 117 | virtual void deactivate(); | 
|---|
| 118 |  | 
|---|
| 119 | /// This gives write access to the data location.  No locking is done. | 
|---|
| 120 | virtual void *obtain_writeonly(distsize_t offset, int size) = 0; | 
|---|
| 121 | /** Only one thread can have an unreleased obtain_readwrite at a time. | 
|---|
| 122 | The actual file region locked can be larger than that requested. | 
|---|
| 123 | If the file region is already locked this will block.  For this | 
|---|
| 124 | reason, data should be held as read/write for as short a time as | 
|---|
| 125 | possible. */ | 
|---|
| 126 | virtual void *obtain_readwrite(distsize_t offset, int size) = 0; | 
|---|
| 127 | /// This gives read access to the file location.  No locking is done. | 
|---|
| 128 | virtual void *obtain_readonly(distsize_t offset, int size) = 0; | 
|---|
| 129 | /// This is called when read access is no longer needed. | 
|---|
| 130 | virtual void release_readonly(void *data, distsize_t offset, int size) = 0; | 
|---|
| 131 | /// This is called when write access is no longer needed. | 
|---|
| 132 | virtual void release_writeonly(void *data, distsize_t offset, int size)=0; | 
|---|
| 133 | /** This is called when read/write access is no longer needed. | 
|---|
| 134 | The data will be unlocked. */ | 
|---|
| 135 | virtual void release_readwrite(void *data, distsize_t offset, int size)=0; | 
|---|
| 136 |  | 
|---|
| 137 | virtual void sum_reduction(double *data, distsize_t doffset, int dsize); | 
|---|
| 138 | virtual void sum_reduction_on_node(double *data, size_t doffset, int dsize, | 
|---|
| 139 | int node = -1); | 
|---|
| 140 |  | 
|---|
| 141 | /** Synchronizes all the nodes.  Consider using this when the way you | 
|---|
| 142 | you access data changes. */ | 
|---|
| 143 | virtual void sync() = 0; | 
|---|
| 144 |  | 
|---|
| 145 | /** Processes outstanding requests. Some file group implementations | 
|---|
| 146 | don't have access to real shared memory or even active messages. | 
|---|
| 147 | Instead, requests are processed whenever certain file group | 
|---|
| 148 | routines are called.  This can cause large latencies and buffer | 
|---|
| 149 | overflows.  If this is a problem, then the catchup member can be | 
|---|
| 150 | called to process all outstanding requests. */ | 
|---|
| 151 | virtual void catchup(); | 
|---|
| 152 |  | 
|---|
| 153 | /// Prints out information about the object. | 
|---|
| 154 | virtual void print(std::ostream &o = ExEnv::out0()) const; | 
|---|
| 155 |  | 
|---|
| 156 | /** Create a file group.  This routine looks for a -filegrp | 
|---|
| 157 | argument, then the environmental variable FILEGRP, and, finally, | 
|---|
| 158 | the default MessageGrp object to decide which specialization of | 
|---|
| 159 | FileGrp would be appropriate.  The argument to -integralgrp should | 
|---|
| 160 | be either string for a ParsedKeyVal constructor or a classname. | 
|---|
| 161 | The default ThreadGrp and MessageGrp objects should be initialized | 
|---|
| 162 | before this is called. */ | 
|---|
| 163 | static FileGrp* initial_filegrp(int &argc, char** argv); | 
|---|
| 164 | static FileGrp* initial_filegrp(); | 
|---|
| 165 | /** The default file group contains the primary file group to | 
|---|
| 166 | be used by an application. */ | 
|---|
| 167 | static void set_default_filegrp(const Ref<FileGrp>&); | 
|---|
| 168 | /// Returns the default file group. | 
|---|
| 169 | static FileGrp* get_default_filegrp(); | 
|---|
| 170 | /// Clones the given FileGrp. The new FileGrp may need to be initialized additionally. | 
|---|
| 171 | virtual FileGrp* clone() =0; | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | #endif | 
|---|
| 177 |  | 
|---|
| 178 | // Local Variables: | 
|---|
| 179 | // mode: c++ | 
|---|
| 180 | // c-file-style: "CLJ" | 
|---|
| 181 | // End: | 
|---|