| 1 | //
|
|---|
| 2 | // transform_tbint.cc
|
|---|
| 3 | //
|
|---|
| 4 | // Copyright (C) 2004 Edward Valeev
|
|---|
| 5 | //
|
|---|
| 6 | // Author: Edward Valeev <edward.valeev@chemistry.gatech.edu>
|
|---|
| 7 | // Maintainer: EV
|
|---|
| 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 __GNUG__
|
|---|
| 29 | #pragma implementation
|
|---|
| 30 | #endif
|
|---|
| 31 |
|
|---|
| 32 | #include <stdexcept>
|
|---|
| 33 | #include <sstream>
|
|---|
| 34 |
|
|---|
| 35 | #include <util/misc/formio.h>
|
|---|
| 36 | #include <util/state/state_bin.h>
|
|---|
| 37 | #include <util/ref/ref.h>
|
|---|
| 38 | #include <math/scmat/local.h>
|
|---|
| 39 | #include <chemistry/qc/basis/integral.h>
|
|---|
| 40 | #include <chemistry/qc/basis/tbint.h>
|
|---|
| 41 | #include <chemistry/qc/mbptr12/transform_tbint.h>
|
|---|
| 42 |
|
|---|
| 43 | using namespace std;
|
|---|
| 44 | using namespace sc;
|
|---|
| 45 |
|
|---|
| 46 | inline int max(int a,int b) { return (a > b) ? a : b;}
|
|---|
| 47 |
|
|---|
| 48 | /*-----------
|
|---|
| 49 | TwoBodyMOIntsTransform
|
|---|
| 50 | -----------*/
|
|---|
| 51 | static ClassDesc TwoBodyMOIntsTransform_cd(
|
|---|
| 52 | typeid(TwoBodyMOIntsTransform),"TwoBodyMOIntsTransform",1,"virtual public SavableState",
|
|---|
| 53 | 0, 0, 0);
|
|---|
| 54 |
|
|---|
| 55 | TwoBodyMOIntsTransform::TwoBodyMOIntsTransform(const std::string& name, const Ref<MOIntsTransformFactory>& factory,
|
|---|
| 56 | const Ref<MOIndexSpace>& space1, const Ref<MOIndexSpace>& space2,
|
|---|
| 57 | const Ref<MOIndexSpace>& space3, const Ref<MOIndexSpace>& space4) :
|
|---|
| 58 | name_(name), factory_(factory), space1_(space1), space2_(space2), space3_(space3), space4_(space4)
|
|---|
| 59 | {
|
|---|
| 60 | mem_ = MemoryGrp::get_default_memorygrp();
|
|---|
| 61 | msg_ = MessageGrp::get_default_messagegrp();
|
|---|
| 62 | thr_ = ThreadGrp::get_default_threadgrp();
|
|---|
| 63 |
|
|---|
| 64 | // Default values
|
|---|
| 65 | num_te_types_ = 1;
|
|---|
| 66 | memory_ = factory_->memory();
|
|---|
| 67 | debug_ = factory_->debug();
|
|---|
| 68 | dynamic_ = factory_->dynamic();
|
|---|
| 69 | print_percent_ = factory_->print_percent();
|
|---|
| 70 | ints_method_ = factory_->ints_method();
|
|---|
| 71 | file_prefix_ = factory_->file_prefix();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | TwoBodyMOIntsTransform::TwoBodyMOIntsTransform(StateIn& si) : SavableState(si)
|
|---|
| 75 | {
|
|---|
| 76 | si.get(name_);
|
|---|
| 77 | factory_ << SavableState::restore_state(si);
|
|---|
| 78 | ints_acc_ << SavableState::restore_state(si);
|
|---|
| 79 |
|
|---|
| 80 | space1_ << SavableState::restore_state(si);
|
|---|
| 81 | space2_ << SavableState::restore_state(si);
|
|---|
| 82 | space3_ << SavableState::restore_state(si);
|
|---|
| 83 | space4_ << SavableState::restore_state(si);
|
|---|
| 84 |
|
|---|
| 85 | mem_ = MemoryGrp::get_default_memorygrp();
|
|---|
| 86 | msg_ = MessageGrp::get_default_messagegrp();
|
|---|
| 87 | thr_ = ThreadGrp::get_default_threadgrp();
|
|---|
| 88 |
|
|---|
| 89 | si.get(num_te_types_);
|
|---|
| 90 | double memory; si.get(memory); memory_ = (size_t) memory;
|
|---|
| 91 | si.get(debug_);
|
|---|
| 92 | int dynamic; si.get(dynamic); dynamic_ = (bool) dynamic;
|
|---|
| 93 | si.get(print_percent_);
|
|---|
| 94 | int ints_method; si.get(ints_method); ints_method_ = (MOIntsTransformFactory::StoreMethod) ints_method;
|
|---|
| 95 | si.get(file_prefix_);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | TwoBodyMOIntsTransform::~TwoBodyMOIntsTransform()
|
|---|
| 99 | {
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void
|
|---|
| 103 | TwoBodyMOIntsTransform::save_data_state(StateOut& so)
|
|---|
| 104 | {
|
|---|
| 105 | so.put(name_);
|
|---|
| 106 | SavableState::save_state(factory_.pointer(),so);
|
|---|
| 107 | SavableState::save_state(ints_acc_.pointer(),so);
|
|---|
| 108 |
|
|---|
| 109 | SavableState::save_state(space1_.pointer(),so);
|
|---|
| 110 | SavableState::save_state(space2_.pointer(),so);
|
|---|
| 111 | SavableState::save_state(space3_.pointer(),so);
|
|---|
| 112 | SavableState::save_state(space4_.pointer(),so);
|
|---|
| 113 |
|
|---|
| 114 | so.put(num_te_types_);
|
|---|
| 115 | so.put((double)memory_);
|
|---|
| 116 | so.put(debug_);
|
|---|
| 117 | so.put((int)dynamic_);
|
|---|
| 118 | so.put(print_percent_);
|
|---|
| 119 | so.put((int)ints_method_);
|
|---|
| 120 | so.put(file_prefix_);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void
|
|---|
| 124 | TwoBodyMOIntsTransform::set_num_te_types(const int num_te_types)
|
|---|
| 125 | {
|
|---|
| 126 | // need to figure out how to determine the number of te types supported by this TwoBodyInt
|
|---|
| 127 | if (num_te_types < 1 || num_te_types > TwoBodyInt::num_tbint_types)
|
|---|
| 128 | throw std::runtime_error("TwoBodyMOIntsTransform::set_num_te_types() -- ");
|
|---|
| 129 | num_te_types_ = num_te_types;
|
|---|
| 130 | init_vars();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void
|
|---|
| 134 | TwoBodyMOIntsTransform::set_memory(const size_t memory)
|
|---|
| 135 | {
|
|---|
| 136 | memory_ = memory;
|
|---|
| 137 | init_vars();
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | Ref<MemoryGrp>
|
|---|
| 141 | TwoBodyMOIntsTransform::mem() const {return mem_; }
|
|---|
| 142 |
|
|---|
| 143 | Ref<MessageGrp>
|
|---|
| 144 | TwoBodyMOIntsTransform::msg() const {return msg_; }
|
|---|
| 145 |
|
|---|
| 146 | Ref<R12IntsAcc>
|
|---|
| 147 | TwoBodyMOIntsTransform::ints_acc() const {return ints_acc_; }
|
|---|
| 148 |
|
|---|
| 149 | Ref<MOIndexSpace>
|
|---|
| 150 | TwoBodyMOIntsTransform::space1() const {return space1_;}
|
|---|
| 151 |
|
|---|
| 152 | Ref<MOIndexSpace>
|
|---|
| 153 | TwoBodyMOIntsTransform::space2() const {return space2_;}
|
|---|
| 154 |
|
|---|
| 155 | Ref<MOIndexSpace>
|
|---|
| 156 | TwoBodyMOIntsTransform::space3() const {return space3_;}
|
|---|
| 157 |
|
|---|
| 158 | Ref<MOIndexSpace>
|
|---|
| 159 | TwoBodyMOIntsTransform::space4() const {return space4_;}
|
|---|
| 160 |
|
|---|
| 161 | double
|
|---|
| 162 | TwoBodyMOIntsTransform::print_percent() const {return print_percent_; }
|
|---|
| 163 |
|
|---|
| 164 | int
|
|---|
| 165 | TwoBodyMOIntsTransform::batchsize() const {return batchsize_; }
|
|---|
| 166 |
|
|---|
| 167 | int
|
|---|
| 168 | TwoBodyMOIntsTransform::debug() const {return debug_; }
|
|---|
| 169 |
|
|---|
| 170 | bool
|
|---|
| 171 | TwoBodyMOIntsTransform::dynamic() const {return dynamic_; }
|
|---|
| 172 |
|
|---|
| 173 | int
|
|---|
| 174 | TwoBodyMOIntsTransform::num_te_types() const { return num_te_types_; }
|
|---|
| 175 |
|
|---|
| 176 | unsigned int
|
|---|
| 177 | TwoBodyMOIntsTransform::restart_orbital() const {
|
|---|
| 178 | return (ints_acc_.null() ? 0 : ints_acc_->next_orbital());
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | ///////////////////////////////////////////////////////
|
|---|
| 183 | // Compute the batchsize for the transformation
|
|---|
| 184 | //
|
|---|
| 185 | // Only arrays allocated before exiting the loop over
|
|---|
| 186 | // i-batches are included here - only these arrays
|
|---|
| 187 | // affect the batch size.
|
|---|
| 188 | ///////////////////////////////////////////////////////
|
|---|
| 189 | int
|
|---|
| 190 | TwoBodyMOIntsTransform::compute_transform_batchsize_(size_t mem_static, int rank_i)
|
|---|
| 191 | {
|
|---|
| 192 | // Check is have enough for even static objects
|
|---|
| 193 | size_t mem_dyn = 0;
|
|---|
| 194 | if (memory_ <= mem_static)
|
|---|
| 195 | return 0;
|
|---|
| 196 | else
|
|---|
| 197 | mem_dyn = memory_ - mem_static;
|
|---|
| 198 |
|
|---|
| 199 | // Determine if calculation is possible at all (i.e., if ni=1 possible)
|
|---|
| 200 | int ni = 1;
|
|---|
| 201 | distsize_t maxdyn = compute_transform_dynamic_memory_(ni);
|
|---|
| 202 | if (maxdyn > mem_dyn) {
|
|---|
| 203 | return 0;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | ni = 2;
|
|---|
| 207 | while (ni<=rank_i) {
|
|---|
| 208 | maxdyn = compute_transform_dynamic_memory_(ni);
|
|---|
| 209 | if (maxdyn >= mem_dyn) {
|
|---|
| 210 | ni--;
|
|---|
| 211 | break;
|
|---|
| 212 | }
|
|---|
| 213 | ni++;
|
|---|
| 214 | }
|
|---|
| 215 | if (ni > rank_i) ni = rank_i;
|
|---|
| 216 |
|
|---|
| 217 | return ni;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 | void
|
|---|
| 222 | TwoBodyMOIntsTransform::init_vars()
|
|---|
| 223 | {
|
|---|
| 224 | int me = msg_->me();
|
|---|
| 225 |
|
|---|
| 226 | int restart_orbital = ints_acc_.nonnull() ? ints_acc_->next_orbital() : 0;
|
|---|
| 227 | int rank_i = space1_->rank() - restart_orbital;
|
|---|
| 228 |
|
|---|
| 229 | mem_static_ = 0;
|
|---|
| 230 | if (me == 0) {
|
|---|
| 231 | // mem_static should include storage in MOIndexSpace
|
|---|
| 232 | mem_static_ = space1_->memory_in_use() +
|
|---|
| 233 | space2_->memory_in_use() +
|
|---|
| 234 | space3_->memory_in_use() +
|
|---|
| 235 | space4_->memory_in_use(); // scf vector
|
|---|
| 236 | int nthreads = thr_->nthread();
|
|---|
| 237 | // ... plus the integrals evaluators
|
|---|
| 238 | mem_static_ += nthreads * factory_->integral()->storage_required_grt(space1_->basis(),space2_->basis(),
|
|---|
| 239 | space3_->basis(),space4_->basis());
|
|---|
| 240 | batchsize_ = compute_transform_batchsize_(mem_static_,rank_i);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | // Send value of ni and mem_static to other nodes
|
|---|
| 244 | msg_->bcast(batchsize_);
|
|---|
| 245 | double mem_static_double = static_cast<double>(mem_static_);
|
|---|
| 246 | msg_->bcast(mem_static_double);
|
|---|
| 247 | mem_static_ = static_cast<size_t>(mem_static_double);
|
|---|
| 248 |
|
|---|
| 249 | if (batchsize_ == 0)
|
|---|
| 250 | throw std::runtime_error("TwoBodyMOIntsTransform::init_vars() -- batch size is 0: more memory or processors are needed");
|
|---|
| 251 |
|
|---|
| 252 | npass_ = 0;
|
|---|
| 253 | int rest = 0;
|
|---|
| 254 | if (batchsize_ == rank_i) {
|
|---|
| 255 | npass_ = 1;
|
|---|
| 256 | rest = 0;
|
|---|
| 257 | }
|
|---|
| 258 | else {
|
|---|
| 259 | rest = rank_i%batchsize_;
|
|---|
| 260 | npass_ = (rank_i - rest)/batchsize_ + 1;
|
|---|
| 261 | if (rest == 0) npass_--;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | void
|
|---|
| 266 | TwoBodyMOIntsTransform::reinit_acc()
|
|---|
| 267 | {
|
|---|
| 268 | if (ints_acc_.nonnull())
|
|---|
| 269 | ints_acc_ = 0;
|
|---|
| 270 | init_acc();
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | void
|
|---|
| 274 | TwoBodyMOIntsTransform::obsolete()
|
|---|
| 275 | {
|
|---|
| 276 | reinit_acc();
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | void
|
|---|
| 280 | TwoBodyMOIntsTransform::alloc_mem(const size_t localmem)
|
|---|
| 281 | {
|
|---|
| 282 | if (mem_.null())
|
|---|
| 283 | throw std::runtime_error("TwoBodyMOIntsTransform::alloc_mem() -- memory group not initialized");
|
|---|
| 284 | mem_->set_localsize(localmem);
|
|---|
| 285 | if (debug_ >= 1) {
|
|---|
| 286 | ExEnv::out0() << indent
|
|---|
| 287 | << "Size of global distributed array: "
|
|---|
| 288 | << mem_->totalsize()
|
|---|
| 289 | << " Bytes" << endl;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | void
|
|---|
| 294 | TwoBodyMOIntsTransform::dealloc_mem()
|
|---|
| 295 | {
|
|---|
| 296 | if (mem_.null())
|
|---|
| 297 | throw std::runtime_error("TwoBodyMOIntsTransform::dealloc_mem() -- memory group not initialized");
|
|---|
| 298 | mem_->set_localsize(0);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | int
|
|---|
| 302 | TwoBodyMOIntsTransform::compute_nij(const int rank_i, const int rank_j, const int nproc, const int me)
|
|---|
| 303 | {
|
|---|
| 304 | // compute nij as nij on node 0, since nij on node 0 is >= nij on other nodes
|
|---|
| 305 | int index = 0;
|
|---|
| 306 | int nij = 0;
|
|---|
| 307 | for (int i=0; i<rank_i; i++) {
|
|---|
| 308 | for (int j=0; j<rank_j; j++) {
|
|---|
| 309 | if (index++ % nproc == 0) nij++;
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | return nij;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | void
|
|---|
| 317 | TwoBodyMOIntsTransform::memory_report(std::ostream& os) const
|
|---|
| 318 | {
|
|---|
| 319 | size_t mem_dyn = distsize_to_size(compute_transform_dynamic_memory_(batchsize_));
|
|---|
| 320 | int restart_orbital = ints_acc_.nonnull() ? ints_acc_->next_orbital() : 0;
|
|---|
| 321 | int rank_i_restart = space1_->rank() - restart_orbital;
|
|---|
| 322 |
|
|---|
| 323 | os << indent
|
|---|
| 324 | << "Memory available per node: " << memory_ << " Bytes"
|
|---|
| 325 | << endl;
|
|---|
| 326 | os << indent
|
|---|
| 327 | << "Static memory used per node: " << mem_static_ << " Bytes"
|
|---|
| 328 | << endl;
|
|---|
| 329 | os << indent
|
|---|
| 330 | << "Total memory used per node: " << mem_dyn+mem_static_ << " Bytes"
|
|---|
| 331 | << endl;
|
|---|
| 332 | os << indent
|
|---|
| 333 | << "Memory required for one pass: "
|
|---|
| 334 | << compute_transform_dynamic_memory_(rank_i_restart)+mem_static_
|
|---|
| 335 | << " Bytes"
|
|---|
| 336 | << endl;
|
|---|
| 337 | os << indent
|
|---|
| 338 | << "Minimum memory required: "
|
|---|
| 339 | << compute_transform_dynamic_memory_(1)+mem_static_
|
|---|
| 340 | << " Bytes"
|
|---|
| 341 | << endl;
|
|---|
| 342 | os << indent
|
|---|
| 343 | << "Number of passes: " << (rank_i_restart+batchsize_-1)/batchsize_
|
|---|
| 344 | << endl;
|
|---|
| 345 | os << indent
|
|---|
| 346 | << "Batch size: " << batchsize_
|
|---|
| 347 | << endl;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | void
|
|---|
| 351 | TwoBodyMOIntsTransform::mospace_report(std::ostream& os) const
|
|---|
| 352 | {
|
|---|
| 353 | os << indent << "MO space 1" << endl << incindent;
|
|---|
| 354 | space1_->print_summary(os); os << decindent;
|
|---|
| 355 | os << indent << "MO space 2" << endl << incindent;
|
|---|
| 356 | space2_->print_summary(os); os << decindent;
|
|---|
| 357 | os << indent << "MO space 3" << endl << incindent;
|
|---|
| 358 | space3_->print_summary(os); os << decindent;
|
|---|
| 359 | os << indent << "MO space 4" << endl << incindent;
|
|---|
| 360 | space4_->print_summary(os); os << decindent;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | void
|
|---|
| 364 | TwoBodyMOIntsTransform::print_header(std::ostream& os) const
|
|---|
| 365 | {
|
|---|
| 366 | if (debug_ >= 0)
|
|---|
| 367 | os << indent << "Entered " << name_ << " integrals evaluator (transform type " << type() <<")" << endl;
|
|---|
| 368 | os << incindent;
|
|---|
| 369 |
|
|---|
| 370 | int nproc = msg_->n();
|
|---|
| 371 | if (debug_ >= 1)
|
|---|
| 372 | os << indent << scprintf("nproc = %i", nproc) << endl;
|
|---|
| 373 |
|
|---|
| 374 | if (restart_orbital() && debug_ >= 1) {
|
|---|
| 375 | os << indent
|
|---|
| 376 | << scprintf("Restarting at orbital %d",
|
|---|
| 377 | restart_orbital()) << endl;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | memory_report(os);
|
|---|
| 381 | if (dynamic_)
|
|---|
| 382 | os << indent << "Using dynamic load balancing." << endl;
|
|---|
| 383 | if (debug_ >= 1)
|
|---|
| 384 | mospace_report(os);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | void
|
|---|
| 388 | TwoBodyMOIntsTransform::print_footer(std::ostream& os) const
|
|---|
| 389 | {
|
|---|
| 390 | os << decindent;
|
|---|
| 391 | if (debug_ >= 0)
|
|---|
| 392 | os << indent << "Exited " << name_ << " integrals evaluator (transform type " << type() <<")" << endl;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 396 |
|
|---|
| 397 | // Local Variables:
|
|---|
| 398 | // mode: c++
|
|---|
| 399 | // c-file-style: "CLJ-CONDENSED"
|
|---|
| 400 | // End:
|
|---|