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