[0b990d] | 1 | //
|
---|
| 2 | // file.cc
|
---|
| 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 implementation
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #include <sstream>
|
---|
| 33 | #include <util/misc/string.h>
|
---|
| 34 |
|
---|
| 35 | #ifdef HAVE_CONFIG_H
|
---|
| 36 | #include <scconfig.h>
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | #include <util/misc/formio.h>
|
---|
| 41 | #include <util/class/scexception.h>
|
---|
| 42 | #include <util/group/file.h>
|
---|
| 43 |
|
---|
| 44 | #include <util/group/fileproc.h>
|
---|
| 45 | #if defined(HAVE_MPI)
|
---|
| 46 | # include <util/group/messmpi.h>
|
---|
| 47 | //# include <util/group/filemtmpi.h>
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 | using namespace std;
|
---|
| 51 | using namespace sc;
|
---|
| 52 |
|
---|
| 53 | //////////////////////////////////////////////////////////////////////
|
---|
| 54 | // FileGrp members
|
---|
| 55 |
|
---|
| 56 | static ClassDesc FileGrp_cd(
|
---|
| 57 | typeid(FileGrp),"FileGrp",1,"public DescribedClass",
|
---|
| 58 | 0, 0, 0);
|
---|
| 59 |
|
---|
| 60 | FileGrp::FileGrp()
|
---|
| 61 | {
|
---|
| 62 | debug_ = 0;
|
---|
| 63 |
|
---|
| 64 | datafile_ = 0;
|
---|
| 65 | filename_ = 0;
|
---|
| 66 | offsets_ = 0;
|
---|
| 67 |
|
---|
| 68 | init_locks();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | FileGrp::FileGrp(const Ref<KeyVal>& keyval)
|
---|
| 72 | {
|
---|
| 73 | debug_ = keyval->intvalue("debug");
|
---|
| 74 |
|
---|
| 75 | datafile_ = 0;
|
---|
| 76 | filename_ = 0;
|
---|
| 77 | offsets_ = 0;
|
---|
| 78 |
|
---|
| 79 | init_locks();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | FileGrp::~FileGrp()
|
---|
| 83 | {
|
---|
| 84 | delete[] offsets_;
|
---|
| 85 | delete[] locks_;
|
---|
| 86 | datafile_ = 0;
|
---|
| 87 | filename_ = 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void
|
---|
| 91 | FileGrp::open()
|
---|
| 92 | {
|
---|
| 93 | if (filename_) {
|
---|
| 94 | datafile_ = open(filename_, O_RDWR);
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
| 97 | throw ProgrammingError("open() called but filename has not been set",
|
---|
| 98 | __FILE__, __LINE__, class_desc());
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void
|
---|
| 103 | FileGrp::close()
|
---|
| 104 | {
|
---|
| 105 | close(datafile_);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void
|
---|
| 109 | FileGrp::set_filename(char *name)
|
---|
| 110 | {
|
---|
| 111 | if (filename_) {
|
---|
| 112 | throw ProgrammingError("set_filename() called but filename has been set already",
|
---|
| 113 | __FILE__, __LINE__, class_desc());
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | filename_ = strdup(name);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | const char*
|
---|
| 120 | FileGrp::get_filename() const
|
---|
| 121 | {
|
---|
| 122 | return filename_;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void
|
---|
| 126 | FileGrp::init_locks()
|
---|
| 127 | {
|
---|
| 128 | Ref<ThreadGrp> thgrp = ThreadGrp::get_default_threadgrp();
|
---|
| 129 | nlock_ = 2 * thgrp->nthread();
|
---|
| 130 | locks_ = new Ref<ThreadLock>[nlock_];
|
---|
| 131 | for (int i=0; i<nlock_; i++) locks_[i] = thgrp->new_lock();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | FileGrp *
|
---|
| 135 | FileGrp::initial_filegrp()
|
---|
| 136 | {
|
---|
| 137 | int argc = 0;
|
---|
| 138 | return initial_filegrp(argc,0);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | FileGrp *
|
---|
| 142 | FileGrp::initial_filegrp(int &argc, char *argv[])
|
---|
| 143 | {
|
---|
| 144 | FileGrp *grp = 0;
|
---|
| 145 |
|
---|
| 146 | char *keyval_string = 0;
|
---|
| 147 |
|
---|
| 148 | // see if a file group is given on the command line
|
---|
| 149 | if (argc && argv) {
|
---|
| 150 | for (int i=0; i<argc; i++) {
|
---|
| 151 | if (argv[i] && !strcmp(argv[i], "-filegrp")) {
|
---|
| 152 | char *filegrp_string = argv[i];
|
---|
| 153 | i++;
|
---|
| 154 | if (i >= argc) {
|
---|
| 155 | ExEnv::errn() << "-filegrp must be following by an argument"
|
---|
| 156 | << endl;
|
---|
| 157 | abort();
|
---|
| 158 | }
|
---|
| 159 | keyval_string = argv[i];
|
---|
| 160 | // move the filegrp arguments to the end of argv
|
---|
| 161 | int j;
|
---|
| 162 | for (j=i+1; j<argc; j++) {
|
---|
| 163 | argv[j-2] = argv[j];
|
---|
| 164 | }
|
---|
| 165 | argv[j++] = filegrp_string;
|
---|
| 166 | argv[j++] = keyval_string;
|
---|
| 167 | // decrement argc to hide the last two arguments
|
---|
| 168 | argc -= 2;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (!keyval_string) {
|
---|
| 175 | // find out if the environment gives the containing file group
|
---|
| 176 | keyval_string = getenv("FILEGRP");
|
---|
| 177 | if (keyval_string) {
|
---|
| 178 | if (!strncmp("FILEGRP=", keyval_string, 11)) {
|
---|
| 179 | keyval_string = strchr(keyval_string, '=');
|
---|
| 180 | }
|
---|
| 181 | if (*keyval_string == '=') keyval_string++;
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // if keyval input for a file group was found, then
|
---|
| 186 | // create it.
|
---|
| 187 | if (keyval_string) {
|
---|
| 188 | //ExEnv::outn() << "Creating FileGrp from \"" << keyval_string << "\"" << endl;
|
---|
| 189 | Ref<ParsedKeyVal> strkv = new ParsedKeyVal();
|
---|
| 190 | strkv->parse_string(keyval_string);
|
---|
| 191 | Ref<DescribedClass> dc = strkv->describedclassvalue();
|
---|
| 192 | grp = dynamic_cast<FileGrp*>(dc.pointer());
|
---|
| 193 | if (dc.null()) {
|
---|
| 194 | ExEnv::errn() << "initial_filegrp: couldn't find a FileGrp in "
|
---|
| 195 | << keyval_string << endl;
|
---|
| 196 | abort();
|
---|
| 197 | }
|
---|
| 198 | else if (!grp) {
|
---|
| 199 | ExEnv::errn() << "initial_filegrp: wanted FileGrp but got "
|
---|
| 200 | << dc->class_name() << endl;
|
---|
| 201 | abort();
|
---|
| 202 | }
|
---|
| 203 | // prevent an accidental delete
|
---|
| 204 | grp->reference();
|
---|
| 205 | strkv = 0;
|
---|
| 206 | dc = 0;
|
---|
| 207 | // accidental delete not a problem anymore since all smart pointers
|
---|
| 208 | // to grp are dead
|
---|
| 209 | grp->dereference();
|
---|
| 210 | return grp;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | return grp;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | void
|
---|
| 217 | FileGrp::activate()
|
---|
| 218 | {
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | void
|
---|
| 222 | FileGrp::deactivate()
|
---|
| 223 | {
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void
|
---|
| 227 | FileGrp::print(ostream&o) const
|
---|
| 228 | {
|
---|
| 229 | o << scprintf("FileGrp (node %d):\n", me());
|
---|
| 230 | o << scprintf("%d: n = %d\n", me(), n());
|
---|
| 231 | for (int i=0; i<=n_; i++) {
|
---|
| 232 | o << scprintf("%d: offset[%d] = %5d\n", me(), i, offsets_[i]);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | void
|
---|
| 237 | FileGrp::sum_reduction(double *data, distsize_t doffset, int dlength)
|
---|
| 238 | {
|
---|
| 239 | distsize_t offset = doffset * sizeof(double);
|
---|
| 240 | int length = dlength * sizeof(double);
|
---|
| 241 |
|
---|
| 242 | if (offset + length > totalsize()) {
|
---|
| 243 | ExEnv::errn() << scprintf("FileGrp::sum_reduction: arg out of range\n");
|
---|
| 244 | abort();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | double *source_data = (double*) obtain_readwrite(offset, length);
|
---|
| 248 |
|
---|
| 249 | for (int i=0; i<dlength; i++) {
|
---|
| 250 | source_data[i] += data[i];
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | release_readwrite((void*) source_data, offset, length);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void
|
---|
| 257 | FileGrp::sum_reduction_on_node(double *data, size_t doffset, int dlength,
|
---|
| 258 | int node)
|
---|
| 259 | {
|
---|
| 260 | if (node == -1) node = me();
|
---|
| 261 |
|
---|
| 262 | sum_reduction(data, doffset + offset(node)/sizeof(double),
|
---|
| 263 | dlength);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | void
|
---|
| 267 | FileGrp::catchup()
|
---|
| 268 | {
|
---|
| 269 | return;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | void
|
---|
| 273 | FileGrp::obtain_local_lock(size_t start, size_t fence)
|
---|
| 274 | {
|
---|
| 275 | distsize_t locked_region_size = 1 + localsize()/nlock_;
|
---|
| 276 | int lstart = start/locked_region_size;
|
---|
| 277 | int llast = fence/locked_region_size;
|
---|
| 278 | for (int i=lstart; i<=llast; i++) {
|
---|
| 279 | locks_[i]->lock();
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | void
|
---|
| 284 | FileGrp::release_local_lock(size_t start, size_t fence)
|
---|
| 285 | {
|
---|
| 286 | distsize_t locked_region_size = 1 + localsize()/nlock_;
|
---|
| 287 | int lstart = start/locked_region_size;
|
---|
| 288 | int llast = fence/locked_region_size;
|
---|
| 289 | for (int i=lstart; i<=llast; i++) {
|
---|
| 290 | locks_[i]->unlock();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | static Ref<FileGrp> default_filegrp;
|
---|
| 295 |
|
---|
| 296 | void
|
---|
| 297 | FileGrp::set_default_filegrp(const Ref<FileGrp>& grp)
|
---|
| 298 | {
|
---|
| 299 | default_filegrp = grp;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | FileGrp*
|
---|
| 303 | FileGrp::get_default_filegrp()
|
---|
| 304 | {
|
---|
| 305 | if (default_filegrp.nonnull()) return default_filegrp.pointer();
|
---|
| 306 |
|
---|
| 307 | Ref<MessageGrp> msg = MessageGrp::get_default_messagegrp();
|
---|
| 308 |
|
---|
| 309 | #if defined(HAVE_MPI) && defined(DEFAULT_MTMPI)
|
---|
| 310 | Ref<ThreadGrp> thr = ThreadGrp::get_default_threadgrp();
|
---|
| 311 | // default_filegrp = new MTMPIFileGrp(msg,thr);
|
---|
| 312 | return default_filegrp.pointer();
|
---|
| 313 | #endif
|
---|
| 314 |
|
---|
| 315 |
|
---|
| 316 | if (msg.null()) {
|
---|
| 317 | ExEnv::errn() << scprintf("FileGrp::get_default_filegrp: requires default MessageGrp if default behavior not configured\n");
|
---|
| 318 | abort();
|
---|
| 319 | }
|
---|
| 320 | #if defined(HAVE_MPI)
|
---|
| 321 | else if (msg->class_desc() == ::class_desc<MPIMessageGrp>()) {
|
---|
| 322 | Ref<ThreadGrp> thr = ThreadGrp::get_default_threadgrp();
|
---|
| 323 | // default_filegrp = new MTMPIFileGrp(msg,thr);
|
---|
| 324 | return default_filegrp.pointer();
|
---|
| 325 | }
|
---|
| 326 | #endif
|
---|
| 327 | else if (msg->n() == 1) {
|
---|
| 328 | default_filegrp = new ProcFileGrp();
|
---|
| 329 | return default_filegrp.pointer();
|
---|
| 330 | }
|
---|
| 331 | else {
|
---|
| 332 | ExEnv::errn() << scprintf("FileGrp::get_default_filegrp: cannot create "
|
---|
| 333 | "default for \"%s\"\n.", msg->class_name());
|
---|
| 334 | abort();
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | if (default_filegrp.null()) {
|
---|
| 338 | ExEnv::err0() << scprintf("WARNING: FileGrp::get_default_filegrp(): failed\n");
|
---|
| 339 | default_filegrp = new ProcFileGrp;
|
---|
| 340 | }
|
---|
| 341 | return default_filegrp.pointer();
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 345 |
|
---|
| 346 | // Local Variables:
|
---|
| 347 | // mode: c++
|
---|
| 348 | // c-file-style: "CLJ"
|
---|
| 349 | // End:
|
---|