| 1 | //
|
|---|
| 2 | // r12ia_node0file.cc
|
|---|
| 3 | //
|
|---|
| 4 | // Copyright (C) 2002 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 __GNUC__
|
|---|
| 29 | #pragma implementation
|
|---|
| 30 | #endif
|
|---|
| 31 |
|
|---|
| 32 | #include <stdexcept>
|
|---|
| 33 | #include <util/class/scexception.h>
|
|---|
| 34 | #include <sys/types.h>
|
|---|
| 35 | #include <sys/stat.h>
|
|---|
| 36 | #include <fcntl.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <util/misc/string.h>
|
|---|
| 40 | #include <util/misc/formio.h>
|
|---|
| 41 | #include <util/misc/exenv.h>
|
|---|
| 42 | #include <chemistry/qc/mbptr12/r12ia_node0file.h>
|
|---|
| 43 |
|
|---|
| 44 | #define CREATE_FILE_ON_NODE0_ONLY 1
|
|---|
| 45 |
|
|---|
| 46 | using namespace std;
|
|---|
| 47 | using namespace sc;
|
|---|
| 48 |
|
|---|
| 49 | ///////////////////////////////////////////////////////////////
|
|---|
| 50 |
|
|---|
| 51 | static ClassDesc R12IntsAcc_Node0File_cd(
|
|---|
| 52 | typeid(R12IntsAcc_Node0File),"R12IntsAcc_Node0File",1,"public R12IntsAcc",
|
|---|
| 53 | 0, 0, create<R12IntsAcc_Node0File>);
|
|---|
| 54 |
|
|---|
| 55 | R12IntsAcc_Node0File::R12IntsAcc_Node0File(Ref<MemoryGrp>& mem, const char* filename, int num_te_types,
|
|---|
| 56 | int ni, int nj, int nx, int ny) :
|
|---|
| 57 | R12IntsAcc(num_te_types, ni, nj, nx, ny)
|
|---|
| 58 | {
|
|---|
| 59 | mem_ = mem;
|
|---|
| 60 | filename_ = strdup(filename);
|
|---|
| 61 |
|
|---|
| 62 | init(false);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | R12IntsAcc_Node0File::R12IntsAcc_Node0File(StateIn& si) :
|
|---|
| 66 | R12IntsAcc(si)
|
|---|
| 67 | {
|
|---|
| 68 | mem_ = MemoryGrp::get_default_memorygrp();
|
|---|
| 69 | si.getstring(filename_);
|
|---|
| 70 |
|
|---|
| 71 | init(true);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | R12IntsAcc_Node0File::~R12IntsAcc_Node0File()
|
|---|
| 75 | {
|
|---|
| 76 | for(int i=0;i<ni_;i++)
|
|---|
| 77 | for(int j=0;j<nj_;j++) {
|
|---|
| 78 | if (!is_avail(i,j)) {
|
|---|
| 79 | int ij = ij_index(i,j);
|
|---|
| 80 | for(int oper_type=0; oper_type<num_te_types(); oper_type++)
|
|---|
| 81 | if (pairblk_[ij].ints_[oper_type] != NULL) {
|
|---|
| 82 | ExEnv::outn() << indent << taskid() << ": i = " << i << " j = " << j << " oper_type = " << oper_type << endl;
|
|---|
| 83 | throw std::runtime_error("Logic error: R12IntsAcc_Node0File::~ : some nonlocal blocks have not been released!");
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | delete[] pairblk_;
|
|---|
| 88 |
|
|---|
| 89 | // Destroy the file
|
|---|
| 90 | #if CREATE_FILE_ON_NODE0_ONLY
|
|---|
| 91 | if (taskid() == 0)
|
|---|
| 92 | #endif
|
|---|
| 93 | unlink(filename_);
|
|---|
| 94 | free(filename_);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void
|
|---|
| 98 | R12IntsAcc_Node0File::save_data_state(StateOut& so)
|
|---|
| 99 | {
|
|---|
| 100 | R12IntsAcc::save_data_state(so);
|
|---|
| 101 | so.putstring(filename_);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void
|
|---|
| 105 | R12IntsAcc_Node0File::init(bool restart)
|
|---|
| 106 | {
|
|---|
| 107 | pairblk_ = new struct PairBlkInfo[ni_*nj_];
|
|---|
| 108 | int i, j, ij;
|
|---|
| 109 | for(i=0,ij=0;i<ni_;i++)
|
|---|
| 110 | for(j=0;j<nj_;j++,ij++) {
|
|---|
| 111 | pairblk_[ij].ints_[eri] = NULL;
|
|---|
| 112 | pairblk_[ij].ints_[r12] = NULL;
|
|---|
| 113 | pairblk_[ij].ints_[r12t1] = NULL;
|
|---|
| 114 | pairblk_[ij].ints_[r12t2] = NULL;
|
|---|
| 115 | pairblk_[ij].refcount_[eri] = 0;
|
|---|
| 116 | pairblk_[ij].refcount_[r12] = 0;
|
|---|
| 117 | pairblk_[ij].refcount_[r12t1] = 0;
|
|---|
| 118 | pairblk_[ij].refcount_[r12t2] = 0;
|
|---|
| 119 | pairblk_[ij].offset_ = (off_t)ij*blocksize_;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // node 0 will have the file
|
|---|
| 123 | #if CREATE_FILE_ON_NODE0_ONLY
|
|---|
| 124 | if (taskid() != 0)
|
|---|
| 125 | return;
|
|---|
| 126 | #endif
|
|---|
| 127 |
|
|---|
| 128 | // See if can open/create the file
|
|---|
| 129 | if (restart) {
|
|---|
| 130 | datafile_ = open(filename_,O_WRONLY|O_APPEND,0644);
|
|---|
| 131 | }
|
|---|
| 132 | else {
|
|---|
| 133 | datafile_ = creat(filename_,0644);
|
|---|
| 134 | }
|
|---|
| 135 | // Check if the file was opened correctly
|
|---|
| 136 | check_filedescr_();
|
|---|
| 137 | // If everything is fine close it and proceed
|
|---|
| 138 | close(datafile_);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void
|
|---|
| 142 | R12IntsAcc_Node0File::check_filedescr_()
|
|---|
| 143 | {
|
|---|
| 144 | // If the file was not opened correctly - throw an exception
|
|---|
| 145 | if (datafile_ == -1) {
|
|---|
| 146 | switch (errno) {
|
|---|
| 147 | case EACCES:
|
|---|
| 148 | throw std::runtime_error("R12IntsAcc_Node0File::R12IntsAcc_Node0File -- access to the requested file is not allowed");
|
|---|
| 149 | case ENOSPC:
|
|---|
| 150 | throw std::runtime_error("R12IntsAcc_Node0File::R12IntsAcc_Node0File -- no space left in the filesystem");
|
|---|
| 151 | case ENOENT:
|
|---|
| 152 | throw std::runtime_error("R12IntsAcc_Node0File::R12IntsAcc_Node0File -- file does not exist");
|
|---|
| 153 | case ENOTDIR:
|
|---|
| 154 | throw std::runtime_error("R12IntsAcc_Node0File::R12IntsAcc_Node0File -- not a directory");
|
|---|
| 155 | default:
|
|---|
| 156 | perror(0);
|
|---|
| 157 | throw std::runtime_error("R12IntsAcc_Node0File::R12IntsAcc_Node0File -- failed to open POSIX file on node 0");
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void
|
|---|
| 163 | R12IntsAcc_Node0File::store_memorygrp(Ref<MemoryGrp>& mem, int ni, const size_t blksize)
|
|---|
| 164 | {
|
|---|
| 165 | if (committed_) {
|
|---|
| 166 | ExEnv::out0() << "R12IntsAcc_Node0File::store_memorygrp(mem,ni) called after all data has been committed" << endl;
|
|---|
| 167 | abort();
|
|---|
| 168 | }
|
|---|
| 169 | // mem must be the same as mem_
|
|---|
| 170 | else if (mem_ != mem) {
|
|---|
| 171 | ExEnv::out0() << "R12IntsAcc_MemoryGrp::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
|---|
| 172 | "mem != R12IntsAcc_MemoryGrp::mem_" << endl;
|
|---|
| 173 | abort();
|
|---|
| 174 | }
|
|---|
| 175 | // Will store integrals on node 0
|
|---|
| 176 | else if (taskid() != 0)
|
|---|
| 177 | return;
|
|---|
| 178 | else if (ni > ni_) {
|
|---|
| 179 | ExEnv::out0() << "R12IntsAcc_Node0File::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
|---|
| 180 | "ni > R12IntsAcc_Node0File::ni_" << endl;
|
|---|
| 181 | abort();
|
|---|
| 182 | }
|
|---|
| 183 | else if (next_orbital() + ni > ni_) {
|
|---|
| 184 | ExEnv::out0() << "R12IntsAcc_Node0File::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
|---|
| 185 | "ni+next_orbital() > R12IntsAcc_Node0File::ni_" << endl;
|
|---|
| 186 | abort();
|
|---|
| 187 | }
|
|---|
| 188 | else {
|
|---|
| 189 | size_t blksize_memgrp = blksize;
|
|---|
| 190 | if (blksize_memgrp == 0)
|
|---|
| 191 | blksize_memgrp = blksize_;
|
|---|
| 192 |
|
|---|
| 193 | // Now do some extra work to figure layout of data in MemoryGrp
|
|---|
| 194 | // Compute global offsets to each processor's data
|
|---|
| 195 | int i,j,ij;
|
|---|
| 196 | int me = taskid();
|
|---|
| 197 | int nproc = mem->n();
|
|---|
| 198 |
|
|---|
| 199 | // Append the data to the file
|
|---|
| 200 | datafile_ = open(filename_,O_WRONLY|O_APPEND,0644);
|
|---|
| 201 | for (int i=0; i<ni; i++)
|
|---|
| 202 | for (int j=0; j<nj_; j++) {
|
|---|
| 203 | double *data;
|
|---|
| 204 | int ij = ij_index(i,j);
|
|---|
| 205 | int proc = ij%nproc;
|
|---|
| 206 | int local_ij_index = ij/nproc;
|
|---|
| 207 | if (proc != me) {
|
|---|
| 208 | distsize_t moffset = (distsize_t)local_ij_index*blksize_memgrp*num_te_types() + mem->offset(proc);
|
|---|
| 209 | for(int te_type=0; te_type < num_te_types(); te_type++) {
|
|---|
| 210 | data = (double *) mem->obtain_readonly(moffset, blksize_);
|
|---|
| 211 | ssize_t wrote_this_much = write(datafile_, data, blksize_);
|
|---|
| 212 | if (wrote_this_much != blksize_)
|
|---|
| 213 | throw FileOperationFailed("R12IntsAcc_Node0File::store_memorygrp() failed",
|
|---|
| 214 | __FILE__,
|
|---|
| 215 | __LINE__,
|
|---|
| 216 | filename_,
|
|---|
| 217 | FileOperationFailed::Write);
|
|---|
| 218 | mem->release_readonly(data, moffset, blksize_);
|
|---|
| 219 | moffset += blksize_memgrp;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | else {
|
|---|
| 223 | data = (double *) ((size_t)mem->localdata() + blksize_memgrp*num_te_types()*local_ij_index);
|
|---|
| 224 | for(int te_type=0; te_type < num_te_types(); te_type++) {
|
|---|
| 225 | ssize_t wrote_this_much = write(datafile_, data, blksize_);
|
|---|
| 226 | if (wrote_this_much != blksize_)
|
|---|
| 227 | throw FileOperationFailed("R12IntsAcc_Node0File::store_memorygrp() failed",
|
|---|
| 228 | __FILE__,
|
|---|
| 229 | __LINE__,
|
|---|
| 230 | filename_,
|
|---|
| 231 | FileOperationFailed::Write);
|
|---|
| 232 | data = (double*) ((size_t) data + blksize_memgrp);
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 | // Close the file and update the i counter
|
|---|
| 237 | close(datafile_);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | inc_next_orbital(ni);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | void
|
|---|
| 244 | R12IntsAcc_Node0File::store_pair_block(int i, int j, double *ints)
|
|---|
| 245 | {
|
|---|
| 246 | ExEnv::err0() << "R12IntsAcc_Node0File::store_pair_block() called: error" << endl;
|
|---|
| 247 | abort();
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | void
|
|---|
| 251 | R12IntsAcc_Node0File::commit()
|
|---|
| 252 | {
|
|---|
| 253 | mem_->set_localsize(0);
|
|---|
| 254 | mem_->sync();
|
|---|
| 255 | mem_->deactivate();
|
|---|
| 256 | R12IntsAcc::commit();
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | void
|
|---|
| 260 | R12IntsAcc_Node0File::activate()
|
|---|
| 261 | {
|
|---|
| 262 | R12IntsAcc::activate();
|
|---|
| 263 | #if CREATE_FILE_ON_NODE0_ONLY
|
|---|
| 264 | if (taskid() == 0)
|
|---|
| 265 | #endif
|
|---|
| 266 | datafile_ = open(filename_, O_RDONLY);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | void
|
|---|
| 270 | R12IntsAcc_Node0File::deactivate()
|
|---|
| 271 | {
|
|---|
| 272 | mem_->activate();
|
|---|
| 273 | #if CREATE_FILE_ON_NODE0_ONLY
|
|---|
| 274 | if (taskid() == 0)
|
|---|
| 275 | #endif
|
|---|
| 276 | close(datafile_);
|
|---|
| 277 | R12IntsAcc::deactivate();
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | double *
|
|---|
| 281 | R12IntsAcc_Node0File::retrieve_pair_block(int i, int j, tbint_type oper_type)
|
|---|
| 282 | {
|
|---|
| 283 | // Can retrieve blocks on node 0 only
|
|---|
| 284 | if (is_avail(i,j)) {
|
|---|
| 285 | int ij = ij_index(i,j);
|
|---|
| 286 | struct PairBlkInfo *pb = &pairblk_[ij];
|
|---|
| 287 | // Always first check if it's already in memory
|
|---|
| 288 | if (pb->ints_[oper_type] == 0) {
|
|---|
| 289 | off_t offset = pb->offset_ + (off_t)oper_type*blksize_;
|
|---|
| 290 | off_t result_offset = lseek(datafile_,offset,SEEK_SET);
|
|---|
| 291 | if (offset == (off_t)-1 || result_offset != offset)
|
|---|
| 292 | throw FileOperationFailed("R12IntsAcc_Node0File::retrieve_pair_block() failed",
|
|---|
| 293 | __FILE__,
|
|---|
| 294 | __LINE__,
|
|---|
| 295 | filename_,
|
|---|
| 296 | FileOperationFailed::Other);
|
|---|
| 297 | pb->ints_[oper_type] = new double[nxy_];
|
|---|
| 298 | ssize_t read_this_much = read(datafile_,pb->ints_[oper_type],blksize_);
|
|---|
| 299 | if (read_this_much != blksize_)
|
|---|
| 300 | throw FileOperationFailed("R12IntsAcc_Node0File::retrieve_pair_block() failed",
|
|---|
| 301 | __FILE__,
|
|---|
| 302 | __LINE__,
|
|---|
| 303 | filename_,
|
|---|
| 304 | FileOperationFailed::Read);
|
|---|
| 305 | }
|
|---|
| 306 | pb->refcount_[oper_type] += 1;
|
|---|
| 307 | return pb->ints_[oper_type];
|
|---|
| 308 | }
|
|---|
| 309 | else
|
|---|
| 310 | throw std::runtime_error("R12IntsAcc_Node0File::retrieve_pair_block() called on node other than 0");
|
|---|
| 311 |
|
|---|
| 312 | return 0;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | void
|
|---|
| 316 | R12IntsAcc_Node0File::release_pair_block(int i, int j, tbint_type oper_type)
|
|---|
| 317 | {
|
|---|
| 318 | if (is_avail(i,j)) {
|
|---|
| 319 | int ij = ij_index(i,j);
|
|---|
| 320 | struct PairBlkInfo *pb = &pairblk_[ij];
|
|---|
| 321 | if (pb->refcount_[oper_type] <= 0) {
|
|---|
| 322 | ExEnv::outn() << indent << taskid() << ":refcount=0: i = " << i << " j = " << j << " tbint_type = " << oper_type << endl;
|
|---|
| 323 | throw std::runtime_error("Logic error: R12IntsAcc_Node0File::release_pair_block: refcount is already zero!");
|
|---|
| 324 | }
|
|---|
| 325 | if (pb->ints_[oper_type] != NULL && pb->refcount_[oper_type] == 1) {
|
|---|
| 326 | delete[] pb->ints_[oper_type];
|
|---|
| 327 | pb->ints_[oper_type] = NULL;
|
|---|
| 328 | }
|
|---|
| 329 | pb->refcount_[oper_type] -= 1;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 | // Local Variables:
|
|---|
| 335 | // mode: c++
|
|---|
| 336 | // c-file-style: "CLJ"
|
|---|
| 337 | // End:
|
|---|