1 | //
|
---|
2 | // r12ia_mpiiofile.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 <stdlib.h>
|
---|
34 | #include <util/misc/string.h>
|
---|
35 | #include <util/misc/formio.h>
|
---|
36 | #include <util/misc/exenv.h>
|
---|
37 | #include <chemistry/qc/mbptr12/r12ia_mpiiofile.h>
|
---|
38 |
|
---|
39 | using namespace std;
|
---|
40 | using namespace sc;
|
---|
41 |
|
---|
42 | ///////////////////////////////////////////////////////////////
|
---|
43 |
|
---|
44 | static ClassDesc R12IntsAcc_MPIIOFile_cd(
|
---|
45 | typeid(R12IntsAcc_MPIIOFile),"R12IntsAcc_MPIIOFile",1,"public R12IntsAcc",
|
---|
46 | 0, 0, 0);
|
---|
47 |
|
---|
48 | R12IntsAcc_MPIIOFile::R12IntsAcc_MPIIOFile(Ref<MemoryGrp>& mem, const char* filename, int nte_types,
|
---|
49 | int ni, int nj, int nx, int ny) :
|
---|
50 | R12IntsAcc(nte_types, ni, nj, nx, ny), datafile_(MPI_FILE_NULL)
|
---|
51 | {
|
---|
52 | mem_ = mem;
|
---|
53 | filename_ = strdup(filename);
|
---|
54 |
|
---|
55 | init(false);
|
---|
56 | }
|
---|
57 |
|
---|
58 | R12IntsAcc_MPIIOFile::R12IntsAcc_MPIIOFile(StateIn& si) :
|
---|
59 | SavableState(si), R12IntsAcc(si)
|
---|
60 | {
|
---|
61 | mem_ = MemoryGrp::get_default_memorygrp();
|
---|
62 | si.getstring(filename_);
|
---|
63 |
|
---|
64 | init(true);
|
---|
65 | }
|
---|
66 |
|
---|
67 | R12IntsAcc_MPIIOFile::~R12IntsAcc_MPIIOFile()
|
---|
68 | {
|
---|
69 | for(int i=0;i<ni_;i++)
|
---|
70 | for(int j=0;j<nj_;j++) {
|
---|
71 | int ij = ij_index(i,j);
|
---|
72 | for(int oper_type=0; oper_type<num_te_types(); oper_type++)
|
---|
73 | if (pairblk_[ij].ints_[oper_type] != NULL) {
|
---|
74 | ExEnv::outn() << indent << mem_->me() << ": i = " << i << " j = " << j << " oper_type = " << oper_type << endl;
|
---|
75 | throw std::runtime_error("Logic error: R12IntsAcc_MPIIOFile::~ : some nonlocal blocks have not been released!");
|
---|
76 | }
|
---|
77 | }
|
---|
78 | delete[] pairblk_;
|
---|
79 | free(filename_);
|
---|
80 | }
|
---|
81 |
|
---|
82 | void
|
---|
83 | R12IntsAcc_MPIIOFile::save_data_state(StateOut& so)
|
---|
84 | {
|
---|
85 | R12IntsAcc::save_data_state(so);
|
---|
86 | so.putstring(filename_);
|
---|
87 | }
|
---|
88 |
|
---|
89 | void
|
---|
90 | R12IntsAcc_MPIIOFile::init(bool restart)
|
---|
91 | {
|
---|
92 | int errcod;
|
---|
93 | errcod = MPI_Comm_size(MPI_COMM_WORLD, &nproc_);
|
---|
94 | nints_per_block_ = nxy_*num_te_types();
|
---|
95 |
|
---|
96 | pairblk_ = new struct PairBlkInfo[ni_*nj_];
|
---|
97 | int i, j, ij;
|
---|
98 | for(i=0,ij=0;i<ni_;i++)
|
---|
99 | for(j=0;j<nj_;j++,ij++) {
|
---|
100 | pairblk_[ij].ints_[eri] = NULL;
|
---|
101 | pairblk_[ij].ints_[r12] = NULL;
|
---|
102 | pairblk_[ij].ints_[r12t1] = NULL;
|
---|
103 | pairblk_[ij].ints_[r12t2] = NULL;
|
---|
104 | pairblk_[ij].refcount_[eri] = 0;
|
---|
105 | pairblk_[ij].refcount_[r12] = 0;
|
---|
106 | pairblk_[ij].refcount_[r12t1] = 0;
|
---|
107 | pairblk_[ij].refcount_[r12t2] = 0;
|
---|
108 | pairblk_[ij].offset_ = (MPI_Offset)ij*blocksize_;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // Try opening/creating the file
|
---|
112 | int amode;
|
---|
113 | if (!restart)
|
---|
114 | amode = MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_WRONLY;
|
---|
115 | else
|
---|
116 | amode = MPI_MODE_RDONLY;
|
---|
117 |
|
---|
118 | errcod = MPI_File_open(MPI_COMM_WORLD, filename_, amode,
|
---|
119 | MPI_INFO_NULL, &datafile_);
|
---|
120 | check_error_code_(errcod);
|
---|
121 | errcod = MPI_File_close(&datafile_);
|
---|
122 | check_error_code_(errcod);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void
|
---|
126 | R12IntsAcc_MPIIOFile::check_error_code_(int errcod) const
|
---|
127 | {
|
---|
128 |
|
---|
129 | if (errcod != MPI_SUCCESS) {
|
---|
130 |
|
---|
131 | int errclass;
|
---|
132 | MPI_Error_class(errcod, &errclass);
|
---|
133 |
|
---|
134 | switch (errclass) {
|
---|
135 | default:
|
---|
136 | char* errstr = new char[MPI_MAX_ERROR_STRING];
|
---|
137 | int errstrlen;
|
---|
138 | MPI_Error_string(errcod, errstr, &errstrlen);
|
---|
139 | ExEnv::out0() << "R12IntsAcc_MPIIOFile::R12IntsAcc_MPIIOFile -- MPI-I/O error: " << errstr
|
---|
140 | << " on file " << filename_ << endl;
|
---|
141 | delete[] errstr;
|
---|
142 | throw std::runtime_error("R12IntsAcc_MPIIOFile::R12IntsAcc_MPIIOFile -- MPI-I/O error");
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | void
|
---|
149 | R12IntsAcc_MPIIOFile::store_pair_block(int i, int j, double *ints)
|
---|
150 | {
|
---|
151 | ExEnv::err0() << "R12IntsAcc_MPIIOFile::store_pair_block() called: error" << endl;
|
---|
152 | abort();
|
---|
153 | }
|
---|
154 |
|
---|
155 | void
|
---|
156 | R12IntsAcc_MPIIOFile::commit()
|
---|
157 | {
|
---|
158 | mem_->set_localsize(0);
|
---|
159 | mem_->sync();
|
---|
160 | mem_->deactivate();
|
---|
161 | R12IntsAcc::commit();
|
---|
162 | }
|
---|
163 |
|
---|
164 | void
|
---|
165 | R12IntsAcc_MPIIOFile::activate()
|
---|
166 | {
|
---|
167 | R12IntsAcc::activate();
|
---|
168 | int errcod = MPI_File_open(MPI_COMM_WORLD, filename_, MPI_MODE_RDONLY | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &datafile_);
|
---|
169 | check_error_code_(errcod);
|
---|
170 | }
|
---|
171 |
|
---|
172 | void
|
---|
173 | R12IntsAcc_MPIIOFile::deactivate()
|
---|
174 | {
|
---|
175 | mem_->activate();
|
---|
176 | int errcod = MPI_File_close(&datafile_);
|
---|
177 | check_error_code_(errcod);
|
---|
178 | R12IntsAcc::deactivate();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void
|
---|
182 | R12IntsAcc_MPIIOFile::release_pair_block(int i, int j, tbint_type oper_type)
|
---|
183 | {
|
---|
184 | int ij = ij_index(i,j);
|
---|
185 | struct PairBlkInfo *pb = &pairblk_[ij];
|
---|
186 | if (pb->refcount_[oper_type] <= 0) {
|
---|
187 | ExEnv::outn() << indent << mem_->me() << ":refcount=0: i = " << i << " j = " << j << " tbint_type = " << oper_type << endl;
|
---|
188 | throw std::runtime_error("Logic error: R12IntsAcc_MPIIOFile::release_pair_block: refcount is already zero!");
|
---|
189 | }
|
---|
190 | if (pb->ints_[oper_type] != NULL && pb->refcount_[oper_type] == 1) {
|
---|
191 | delete[] pb->ints_[oper_type];
|
---|
192 | pb->ints_[oper_type] = NULL;
|
---|
193 | }
|
---|
194 | pb->refcount_[oper_type] -= 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | ///////////////////////////////////////////////////////////////
|
---|
198 |
|
---|
199 | static ClassDesc R12IntsAcc_MPIIOFile_Ind_cd(
|
---|
200 | typeid(R12IntsAcc_MPIIOFile_Ind),"R12IntsAcc_MPIIOFile_Ind",1,"public R12IntsAcc",
|
---|
201 | 0, 0, create<R12IntsAcc_MPIIOFile_Ind>);
|
---|
202 |
|
---|
203 | R12IntsAcc_MPIIOFile_Ind::R12IntsAcc_MPIIOFile_Ind(StateIn& si) :
|
---|
204 | SavableState(si), R12IntsAcc_MPIIOFile(si)
|
---|
205 | {
|
---|
206 | }
|
---|
207 |
|
---|
208 | R12IntsAcc_MPIIOFile_Ind::R12IntsAcc_MPIIOFile_Ind(Ref<MemoryGrp>& mem, const char* filename, int num_te_types,
|
---|
209 | int ni, int nj, int nx, int ny) :
|
---|
210 | R12IntsAcc_MPIIOFile(mem,filename,num_te_types,ni,nj,nx,ny)
|
---|
211 | {
|
---|
212 | }
|
---|
213 |
|
---|
214 | R12IntsAcc_MPIIOFile_Ind::~R12IntsAcc_MPIIOFile_Ind()
|
---|
215 | {
|
---|
216 | }
|
---|
217 |
|
---|
218 | void
|
---|
219 | R12IntsAcc_MPIIOFile_Ind::save_data_state(StateOut&so)
|
---|
220 | {
|
---|
221 | R12IntsAcc_MPIIOFile::save_data_state(so);
|
---|
222 | }
|
---|
223 |
|
---|
224 | void
|
---|
225 | R12IntsAcc_MPIIOFile_Ind::store_memorygrp(Ref<MemoryGrp>& mem, int ni, const size_t blksize)
|
---|
226 | {
|
---|
227 | if (committed_) {
|
---|
228 | ExEnv::out0() << "R12IntsAcc_MPIIOFile_Ind::store_memorygrp(mem,ni) called after all data has been committed" << endl;
|
---|
229 | abort();
|
---|
230 | }
|
---|
231 | // mem must be the same as mem_
|
---|
232 | else if (mem_ != mem) {
|
---|
233 | ExEnv::out0() << "R12IntsAcc_MemoryGrp::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
---|
234 | "mem != R12IntsAcc_MemoryGrp::mem_" << endl;
|
---|
235 | abort();
|
---|
236 | }
|
---|
237 | else if (ni > ni_) {
|
---|
238 | ExEnv::out0() << "R12IntsAcc_MPIIOFile_Ind::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
---|
239 | "ni > R12IntsAcc_MPIIOFile_Ind::ni_" << endl;
|
---|
240 | abort();
|
---|
241 | }
|
---|
242 | else if (next_orbital() + ni > ni_) {
|
---|
243 | ExEnv::out0() << "R12IntsAcc_MPIIOFile_Ind::store_memorygrp(mem,ni) called with invalid argument:" << endl <<
|
---|
244 | "ni+next_orbital() > R12IntsAcc_MPIIOFile_Ind::ni_" << endl;
|
---|
245 | abort();
|
---|
246 | }
|
---|
247 | else {
|
---|
248 | size_t blksize_memgrp = blksize;
|
---|
249 | if (blksize_memgrp == 0)
|
---|
250 | blksize_memgrp = blksize_;
|
---|
251 |
|
---|
252 | // Now do some extra work to figure layout of data in MemoryGrp
|
---|
253 | // Compute global offsets to each processor's data
|
---|
254 | int i,j,ij;
|
---|
255 | int me = mem->me();
|
---|
256 | int nproc = mem->n();
|
---|
257 |
|
---|
258 | // Append the data to the file
|
---|
259 | int errcod = MPI_File_open(MPI_COMM_WORLD, filename_, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY, MPI_INFO_NULL, &datafile_);
|
---|
260 | check_error_code_(errcod);
|
---|
261 |
|
---|
262 | for (int i=0; i<ni; i++)
|
---|
263 | for (int j=0; j<nj_; j++) {
|
---|
264 | int ij = ij_index(i,j);
|
---|
265 | int proc = ij%nproc;
|
---|
266 | if (proc != me) continue;
|
---|
267 | int local_ij_index = ij/nproc;
|
---|
268 | double *data = (double *) ((size_t)mem->localdata() + blksize_memgrp*num_te_types()*local_ij_index);
|
---|
269 | for(int te_type=0; te_type < num_te_types(); te_type++) {
|
---|
270 | int IJ = ij_index(i+next_orbital(),j);
|
---|
271 | int errcod = MPI_File_seek(datafile_, pairblk_[IJ].offset_+(MPI_Offset)te_type*blksize_, MPI_SEEK_SET);
|
---|
272 | check_error_code_(errcod);
|
---|
273 | MPI_Status status;
|
---|
274 | errcod = MPI_File_write(datafile_, (void *)data, nxy_, MPI_DOUBLE, &status);
|
---|
275 | check_error_code_(errcod);
|
---|
276 | data = (double*) ((size_t) data + blksize_memgrp);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | // Close the file and update the i counter
|
---|
280 | errcod = MPI_File_close(&datafile_);
|
---|
281 | check_error_code_(errcod);
|
---|
282 | }
|
---|
283 |
|
---|
284 | inc_next_orbital(ni);
|
---|
285 | }
|
---|
286 |
|
---|
287 | double *
|
---|
288 | R12IntsAcc_MPIIOFile_Ind::retrieve_pair_block(int i, int j, tbint_type oper_type)
|
---|
289 | {
|
---|
290 | int ij = ij_index(i,j);
|
---|
291 | struct PairBlkInfo *pb = &pairblk_[ij];
|
---|
292 | // Always first check if it's already in memory
|
---|
293 | if (pb->ints_[oper_type] == 0) {
|
---|
294 | MPI_Offset offset = pb->offset_ + (MPI_Offset)oper_type*blksize_;
|
---|
295 | int errcod = MPI_File_seek(datafile_, offset, MPI_SEEK_SET);
|
---|
296 | check_error_code_(errcod);
|
---|
297 | double *buffer = new double[nxy_];
|
---|
298 | MPI_Status status;
|
---|
299 | errcod = MPI_File_read(datafile_, (void *)buffer, nxy_, MPI_DOUBLE, &status);
|
---|
300 | check_error_code_(errcod);
|
---|
301 | pb->ints_[oper_type] = buffer;
|
---|
302 | }
|
---|
303 | pb->refcount_[oper_type] += 1;
|
---|
304 | return pb->ints_[oper_type];
|
---|
305 | }
|
---|
306 |
|
---|
307 | // Local Variables:
|
---|
308 | // mode: c++
|
---|
309 | // c-file-style: "CLJ"
|
---|
310 | // End:
|
---|