StatMech
Loading...
Searching...
No Matches
Fluc_randHOp.cpp File Reference

Classes

struct  statm_t
 

Functions

void read_off_memory_status (statm_t &result)
 
int main (int argc, char **argv)
 

Variables

std::string const USAGE_commom = "Usage: 1.This 2.n_max 3.n_min 4.num_h 5.num_op 6.division No. 7.Output dir "
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
122 {
123 if(argc != N_ARGS) {
124 std::cerr << USAGE_commom << USAGE_sp << std::endl;
125 std::exit(EX_USAGE);
126 }
127
128 Integer_t const MAX = 100000; // 1つのファイルに書き込むデータ数の上限
129 Integer_t const dim_loc = 2; // サイト上のヒルベルト空間の次元
130 Integer_t const div = (Integer_t)std::atoll(argv[5]);
131 Integer_t const num_h = (Integer_t)std::atoll(argv[3]); // 局所ハミルトニアンに含まれるサイト数
132 Integer_t const num_op = (Integer_t)std::atoll(argv[4]); // 局所演算子に含まれるサイト数
133 Integer_t const n_max = (Integer_t)std::atoll(argv[1]); // 全スピンの数
134 Integer_t const n_min = (Integer_t)std::atoll(argv[2]);
135 Integer_t const dloc_h = pow(dim_loc, num_h); // 局所ハミルトニアンの空間の次元
136 Integer_t const dloc_op = pow(dim_loc, num_op); // 局所演算子の空間の次元
137 Integer_t const seed = 0;
138 Integer_t const repMin = MAX * div;
139 Integer_t const repMax = repMin + MAX - 1;
140 std::cout << "# dloc_h=" << dloc_h << ", dloc_op=" << dloc_op << "\n";
141
142 Integer_t dim_sub; //全ヒルベルト空間の次元
143 Integer_t ndata, info, failed = 0; // カウンタ
144 double gE, EnergyRange, OpRange, OpMin;
145 double Fluc, MaxFluc, MaxPos; // ETHの指標
146 double const cutoff = 0.45;
147 double const dE = 0.02; // ミクロカノニカル平均を計算するエネルギーシェルの幅
148 double const Emin = 0 + cutoff;
149 double const Emax = 1 - cutoff;
150
151 std::stringstream buff;
152
153 if(n_min > n_max) {
154 std::cerr << "Error: n_min(" << n_min << ") > n_max(" << n_max << ")." << std::endl;
155 std::exit(EX_USAGE);
156 }
157 if(!Initialize(argc, argv)) {
158 std::cerr << "Error: Initialization failed." << std::endl;
159 std::exit(EX_USAGE);
160 }
161#ifndef NDEBUG
163 std::cerr << "# Successfully initialized." << std::endl;
164#endif
165
166 //******************** Check for the directory structure ********************
167#ifndef NDEBUG
168 std::cerr << "# Checking for the directory structure." << std::endl;
169#endif
170 std::string d_str1(argv[6]);
171 d_str1 += "/LocalRandomMatrix/";
172 d_str1 += QUOTE(ENSEMBLE);
173 std::string d_str2(d_str1);
174 buff.str("");
175 buff.clear(std::stringstream::goodbit);
176 buff << "/" << TYPE << "randHOp" << PRECISION << "_"
177 << "Local" << num_h << "_Cut" << (Integer_t)(100 * cutoff) << "_Seed" << seed << "_Min"
178 << n_min << "_Max" << n_max;
179 d_str1 += buff.str();
180 filesystem::create_directories(d_str1);
181
182 if(div == 0) {
183 buff.str("");
184 buff.clear(std::stringstream::goodbit);
185 buff << "/" << TYPE << "Structure" << PRECISION << "_"
186 << "Local" << num_h << "_Seed" << seed << "_Min" << n_min << "_Max" << n_max;
187 d_str2 += buff.str();
188 filesystem::create_directories(d_str2);
189 }
190
191 std::ofstream OutMeasure[n_max + 1];
192 for(Integer_t n = n_min; n <= n_max; ++n) {
193 buff.str("");
194 buff.clear(std::stringstream::goodbit);
195 buff << "/MeasureOfETH_N" << n << "_" << div << ".txt";
196 std::string filename(d_str1);
197 filename += buff.str();
198 OutMeasure[n].open(filename);
199 if(OutMeasure[n].fail()) {
200 std::cerr << "Can't open a file" << filename << "." << std::endl;
201 std::exit(EX_CANTCREAT);
202 }
203 OutMeasure[n] << std::right;
204 }
205
206 std::ofstream OutSample;
207 //******************** (END) Check for the directory structure ********************
208
209 //***************************************************************************
210 //******************** Allocation & Initialization **************************
211 //***************************************************************************
212#ifdef GPU
213 magma_init();
214 magma_queue_t queue = NULL;
215 magma_int_t dev = 0;
216 magma_getdevice(&dev);
217 magma_queue_create(dev, &queue);
218#else
219 void* GPUconf = nullptr;
220#endif
221
222 //******************** Translation invariance ********************
223#ifndef NDEBUG
224 std::cerr << "# Calculating translation-invariant sectors." << std::endl;
225#endif
226 Integer_t const wave_number = 0;
227 std::vector<TransSector> Sector(n_max + 1);
228 for(Integer_t n = n_max; n >= n_min; --n) {
229 Sector[n].initialize(n, wave_number, dim_loc);
230#ifdef GPU
231 Sector[n].copyToGPU(queue);
232#endif
233 std::cout << "(n=" << n << ", k=" << wave_number << ") num_EqClass = " << Sector[n].dim()
234 << ", 2^n/n =" << pow(dim_loc, n) / n << "\n";
235 OutMeasure[n] << "# (n=" << n << ") dim=" << Sector[n].dim() << "\n"
236 << "# 1.(Sample No.) 2.(Fluc_2) 3.(Fluc_infty) 4.(Pos_max_flux) 5.(ndata)\n"
237 << std::endl
238 << std::scientific;
239 }
240 //******************** (END)Translation invariance ********************
241
242 //********** Allocate CPU memories **********//
243#if !defined(NDEBUG) && defined(MAGMA)
244 std::cerr << "# Allocating CPU memories." << std::endl;
245#endif
246 Integer_t const dim_max = Sector[n_max].dim();
247 std::vector<Integer_t> NdataInShell(dim_max);
248 std::vector<double> eigenEnergy(dim_max);
249 std::vector<double> EXPvalue(dim_max);
250 std::vector<double> MCAverage(dim_max);
251 matrix<Complex_t> h(dloc_h, dloc_h);
252 matrix<Complex_t> loc(dloc_op, dloc_op);
253#ifndef GPU
254 matrix<Complex_t> h_tot(dim_max, dim_max);
255 matrix<Complex_t> loc_tot(dim_max, dim_max);
256 #define dh_tot h_tot
257 #define dloc_tot loc_tot
258#endif
259 //********** (END) Allocate CPU memories **********//
260
261#ifdef GPU
262 //********** Allocate GPU memories **********//
263 #ifndef NDEBUG
264 std::cerr << "# Allocating GPU memories." << std::endl;
265 #endif
266 constexpr Integer_t GPU_UNIT = 32;
267 Integer_t const LDT = magma_roundup(dim_max, GPU_UNIT);
268 matrix_gpu<Complex_t> dh(dloc_h, dloc_h);
269 matrix_gpu<Complex_t> dloc(dloc_op, dloc_op);
270 matrix_gpu<Complex_t> dh_tot(LDT, dim_max);
271 matrix_gpu<Complex_t> dloc_tot(LDT, dim_max);
272 //********** (END) Allocate GPU memories **********//
273
274 //********** Determine GPU configuration **********//
275 #ifndef NDEBUG
276 std::cerr << "# Determining GPU configuration." << std::endl;
277 #endif
278 GlobFuncPtr = MatrixElementsInSector;
279 struct cudaFuncAttributes attr;
280 cudaFuncGetAttributes(&attr, GlobFuncPtr);
281 Integer_t nThread = (Integer_t)sqrt(attr.maxThreadsPerBlock);
282 Integer_t nBlock = (Integer_t)dim_max / nThread;
283 if(dim_max > nBlock * nThread) nBlock += 1;
284 GPUconfig conf(dim3(nBlock, nBlock, 1), dim3(nThread, nThread, 1), 0, queue);
285
286 std::cout << "# constSizeBytes = " << attr.constSizeBytes << "\n"
287 << "# maxThreadsPerBlock = " << attr.maxThreadsPerBlock << "\n"
288 << "# (dim_max=" << dim_max << ") nBlock=" << nBlock << ", nThread= " << nThread
289 << "*" << nThread << " =" << nThread * nThread << std::endl;
290 magma_queue_sync(queue);
291 //********** (END) Determine GPU configuration **********//
292#endif // #ifdef GPU
293
294 double start, t_int, end, temp_t;
295 double T_diag = 0, T_post = 0, T_pre = 0;
296 init_genrand(seed);
297 start = getETtime();
298 for(Integer_t repetition = 0; repetition < repMin; ++repetition) {
299 generateLocal_h(h, dloc_h, -1);
300 generateLocal_op(loc, dloc_op, -1);
301 }
302 end = getETtime();
303 std::cout << "(init_genrand): time=" << std::fixed << (end - start) << std::endl;
304 //***************************************************************************
305 //******************** (END) Allocation & Initialization ********************
306 //***************************************************************************
307
308 start = getETtime();
309 end = start;
310 for(Integer_t repetition = repMin; repetition <= repMax; ++repetition) {
311 // 局所ハミルトニアンと局所物理量をランダムにとる ******************************//
312#ifndef NDEBUG
314 std::cerr << "# (GPU, rep= " << std::setw(6) << repetition << ") size=" << Usage.size
315 << ", resident=" << Usage.resident << ", share=" << Usage.share
316 << ", text=" << Usage.text << ", lib=" << Usage.lib << ", data=" << Usage.data
317 << ", dt=" << Usage.dt << std::endl;
318#endif
319 generateLocal_h(h, dloc_h, -1);
320 generateLocal_op(loc, dloc_op, -1);
321#ifdef GPU
322 magma_setmatrix(dloc_h, dloc_h, sizeof(Complex_t), &*h.begin(), dloc_h, dh.ptr(), dloc_h,
323 queue);
324 magma_setmatrix(dloc_op, dloc_op, sizeof(Complex_t), &*loc.begin(), dloc_op, dloc.ptr(),
325 dloc_op, queue);
326#endif
327
328 if(repetition < 10000) {
329 //****************************** 局所演算子に関する性質を保存するファイルを用意 ******************************//
330 buff.str("");
331 buff.clear(std::stringstream::goodbit);
332 buff << "/Sample_No" << repetition << ".txt";
333 std::string filename(d_str2);
334 filename += "/RawData";
335 filesystem::create_directory(filename);
336 filename += buff.str();
337 OutSample.open(filename);
338 if(OutSample.fail()) {
339 std::cerr << "Can't open a file" << filename;
340 std::exit(EX_CANTCREAT);
341 }
342 OutSample << "# dim_loc=" << dim_loc << ", num_h=" << num_h << ", num_op=" << num_op
343 << "\n"
344 << "# wave_number=" << wave_number << "\n"
345 << "# 1.(System size) 2.(Energy density) 3.(Relative Pos) 4.(Exp. value)\n"
346 << std::endl
347 << std::scientific;
348 //****************************** (END) 局所演算子に関する性質を保存するファイルを用意 ******************************//
349 }
350 for(Integer_t n = n_max; n >= n_min; --n) {
351#ifndef NDEBUG
352 std::cout << "# (rep,n)=(" << repetition << "," << n << ")" << std::endl;
353 std::cerr << "# Constructing global matrices in the sector." << std::endl;
354#endif
355 dim_sub = Sector[n].dim();
356 temp_t = getETtime();
357#ifdef GPU
358 constructGlobal_h(dh_tot, dh, num_h, Sector.at(n), conf);
359 constructGlobal_op(dloc_tot, dloc, num_op, Sector.at(n), conf);
360 magma_queue_sync(conf.queue());
361#else
362 constructGlobal_h(h_tot, h, num_h, n, dim_loc);
363 constructGlobal_h(loc_tot, loc, num_op, n, dim_loc);
364#endif
365 T_pre += getETtime() - temp_t;
366
367#ifndef NDEBUG
368 std::cerr << "# Calculating eigenstate expectation values." << std::endl;
369#endif
370 temp_t = getETtime();
371 info = EigenExpValue(EXPvalue, eigenEnergy, dim_sub, dh_tot, dloc_tot, conf.queue());
372 if(info != 0) {
373 failed += 1;
374 buff.str("");
375 buff.clear(std::stringstream::goodbit);
376 buff << "/Failed_N" << n << "_No" << repetition << ".txt";
377 std::string filename(d_str1);
378 filename += buff.str();
379 std::ofstream ErrFp;
380 ErrFp.open(filename);
381 if(!ErrFp.is_open()) {
382 std::cerr << "# Warning: Can't open a file " << filename << ". Continue..."
383 << std::endl;
384 }
385 else {
386 ErrFp << "# info=" << info
387 << "\n\n"
388 "# seed="
389 << seed << ", div=" << div << ", N=" << n << ", repetition=" << repetition
390 << "\n"
391 "# Local Hamiltonian\n";
392 h.print(ErrFp, dloc_h, dloc_h);
393 ErrFp << "\n";
394 ErrFp << "# Local operator\n";
395 loc.print(ErrFp, dloc_op, dloc_op);
396 ErrFp << "\n";
397 if(Sector[n].dim() < 110) {
398 ErrFp << "# Total Hamiltonian\n";
399 dh_tot.print(ErrFp, dim_sub, dim_sub, queue);
400 ErrFp << "# Total operator\n";
401 dloc_tot.print(ErrFp, dim_sub, dim_sub, queue);
402 }
403 ErrFp.close();
404 }
405 continue;
406 }
407#ifndef NDEBUG
408 std::cerr << "# Calculating the spectral range of the observable." << std::endl;
409#endif
410 OpRange = SpectralRange(OpMin, dim_sub, dloc_tot);
411 T_diag += getETtime() - temp_t;
412
413#ifndef NDEBUG
414 std::cerr << "# Calculating measures of the ETH." << std::endl;
415#endif
416 temp_t = getETtime();
417 for(Integer_t i = 0; i < dim_sub; ++i) EXPvalue[i] /= OpRange;
418 EnergyRange = eigenEnergy[dim_sub - 1] - eigenEnergy[0];
419 gE = eigenEnergy[0];
420 for(Integer_t i = 0; i < dim_sub; ++i)
421 eigenEnergy[i] = (eigenEnergy[i] - gE) / EnergyRange;
422
423 MCAverage.resize(dim);
424 NdataInShell.resize(dim);
425 std::fill(MCAverage.begin(), MCAverage.end(), 0.0);
426 std::fill(NdataInShell.begin(), NdataInShell.end(), 0.0);
427 for(Integer_t i = 0; i < dim; ++i) {
428 MCAverage[i] = MicroCanonicalAverage(NdataInShell[i], eigenEnergy[i], dE, dim_sub,
429 eigenEnergy, EXPvalue, i);
430 }
431
432 ndata = MeasureOfETH(Fluc, MaxFluc, MaxPos, Emin, Emax, dim_sub, eigenEnergy, EXPvalue,
433 MCAverage);
434 T_post += getETtime() - temp_t;
435
436 OutMeasure[n] << std::setw(6) << repetition << " " << std::showpos << std::setw(13)
437 << Fluc << " " << std::setw(13) << MaxFluc << " " << std::setw(13)
438 << MaxPos << " " << std::noshowpos << ndata << "\n";
439 if(repetition < 10000) {
440 for(Integer_t i = 0; i < dim_sub; ++i) {
441 OutSample << std::setw(2) << n << " " << std::showpos << std::setw(13)
442 << eigenEnergy[i] * EnergyRange + gE << " " << std::setw(13)
443 << eigenEnergy[i] << " " << std::setw(13) << EXPvalue[i] << "\n"
444 << std::noshowpos;
445 }
446 }
447 }
448 if(repetition < 10000) OutSample.close();
449 if(repetition % 10 == 9) {
450 t_int = end;
451 end = getETtime();
452 std::cerr << "(total=" << std::setw(6) << repetition + 1
453 << "): timeINT=" << std::setprecision(6) << std::setw(8) << (end - t_int)
454 << ", timeTOT=" << std::setprecision(6) << std::setw(8) << (end - start)
455 << ", T_construct=" << std::setprecision(6) << std::setw(10) << T_pre << "("
456 << std::setprecision(1) << 100 * T_pre / (end - start) << "%)"
457 << ", T_diag=" << std::setprecision(6) << std::setw(8) << T_diag << "("
458 << std::setprecision(1) << 100 * T_diag / (end - start) << "%)"
459 << ", T_process=" << std::setprecision(6) << std::setw(8) << T_post << "("
460 << std::setprecision(1) << 100 * T_post / (end - start) << "%)" << std::endl;
461 for(Integer_t n = n_min; n <= n_max; ++n) OutMeasure[n].flush();
462 }
463 }
464
465 Finalize(argc, argv);
466 return 0;
467}
__global__ void MatrixElementsInSector(void)
Definition generateRM.cuh:61
double getETtime()
Definition EnergySpectrum.c:14
std::ofstream OutMeasure[n_max+1]
Definition MeasureOfETH_CreateOutputFiles.cpp:1
statm_t Usage
Definition Fluc_randHOp.cpp:55
void read_off_memory_status(statm_t &result)
Definition Fluc_randHOp.cpp:98
std::string const USAGE_commom
Definition Fluc_randHOp.cpp:91
int const N_ARGS
Definition generateRM.hpp:16
std::string const USAGE_sp
Definition generateRM.hpp:17
std::vector< TransSector > Sector(n_max+1)
constexpr Integer_t wave_number
Definition TranslationInvariantSectors.cpp:1
Definition mytypes.hpp:147
Definition mytypes.hpp:272
Calculate the microcanonical averages with respect to a given sorted vector 'eigVal'.
Definition MicroCanonicalAverage.hpp:25
struct cudaFuncAttributes attr
Definition getAttributesOfMatrixElementsInSector.cpp:2
Integer_t const nBlock
Definition getAttributesOfMatrixElementsInSector.cpp:5
Integer_t const nThread
Definition getAttributesOfMatrixElementsInSector.cpp:4
GPUconfig GPUconf(dim3(nBlock, nBlock, 1), dim3(nThread, nThread, 1), 0, queue)
MKL_INT Integer_t
Definition mytypes.hpp:359
constexpr Integer_t dim_loc
Definition setVariablesForEnsemble.cpp:36
Integer_t const num_op
Definition setVariablesForEnsemble.cpp:39
Integer_t const num_h
Definition setVariablesForEnsemble.cpp:38
Integer_t const repMin
Definition setVariablesForEnsemble.cpp:31
Integer_t const n_min
Definition setVariablesForEnsemble.cpp:28
Integer_t const repMax
Definition setVariablesForEnsemble.cpp:32
Integer_t const div
Definition setVariablesForEnsemble.cpp:29
Integer_t const dloc_op
Definition setVariablesForEnsemble.cpp:41
Integer_t const dloc_h
Definition setVariablesForEnsemble.cpp:40
Integer_t const n_max
Definition setVariablesForEnsemble.cpp:27
constexpr double Emin
Definition setVariablesForMCAverage.cpp:4
constexpr double cutoff
Definition setVariablesForMCAverage.cpp:3
constexpr double Emax
Definition setVariablesForMCAverage.cpp:5
double const dE
Definition setVariablesForMCAverage.cpp:2
Integer_t EigenExpValue(std::vector< double > &EXPvalue, std::vector< double > &Eigenvalue, Integer_t const dim, matrix< Complex_t< double > > &Hamiltonian, const matrix< Complex_t< double > > &Operator, void *GPUconf)
Definition statmech.cpp:120
Integer_t MeasureOfETH(double &Fluc_2, double &Fluc_inf, double &MaxPos, double const Emin, double const Emax, Integer_t const dim, const std::vector< double > &eigenEnergy, const std::vector< double > &EXPvalue, const std::vector< double > &MCAverage)
Definition statmech.cpp:244
double SpectralRange(double &OpMin, Integer_t const dim, matrix< Complex_t< double > > &Operator)
Definition statmech.cpp:49
Definition Fluc_randHOp.cpp:94
unsigned long dt
Definition Fluc_randHOp.cpp:95
unsigned long lib
Definition Fluc_randHOp.cpp:95
unsigned long text
Definition Fluc_randHOp.cpp:95
unsigned long resident
Definition Fluc_randHOp.cpp:95
unsigned long share
Definition Fluc_randHOp.cpp:95
unsigned long size
Definition Fluc_randHOp.cpp:95
unsigned long data
Definition Fluc_randHOp.cpp:95
std::stringstream buff("")

◆ read_off_memory_status()

void read_off_memory_status ( statm_t result)
98 {
99 const char* statm_path = "/proc/self/statm";
100
101 FILE* f = fopen(statm_path, "r");
102 if(!f) {
103 perror(statm_path);
104 abort();
105 }
106 if(7
107 != fscanf(f, "%ld %ld %ld %ld %ld %ld %ld", &result.size, &result.resident, &result.share,
108 &result.text, &result.lib, &result.data, &result.dt)) {
109 perror(statm_path);
110 abort();
111 }
112 fclose(f);
113}

Variable Documentation

◆ USAGE_commom

std::string const USAGE_commom = "Usage: 1.This 2.n_max 3.n_min 4.num_h 5.num_op 6.division No. 7.Output dir "