LatticeYangMills
inputconf.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (c) 2018 Giovanni Pederiva
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 ******************************************************************************/
25 
34 #include "Utils/clusterspecifier.h"
35 #ifndef LACONIA
36 #include <mpi/mpi.h>
37 #else
38 #include <mpi.h>
39 #endif
40 #include <cstdio>
41 #include <algorithm>
42 #include<boost/filesystem.hpp>
43 #include "InputOutput/inputconf.h"
44 #include "Math/lattice.h"
45 #include "ParallelTools/parallel.h"
46 #include "Utils/clusterspecifier.h"
47 
48 namespace LatticeIO {
49 
54  double ReverseFloat( const double inFloat )
55  {
56  double retVal;
57  char *floatToConvert = ( char* ) & inFloat;
58  char *returnFloat = ( char* ) & retVal;
59 
60  // swap the bytes into a temporary buffer
61  returnFloat[0] = floatToConvert[7];
62  returnFloat[1] = floatToConvert[6];
63  returnFloat[2] = floatToConvert[5];
64  returnFloat[3] = floatToConvert[4];
65  returnFloat[4] = floatToConvert[3];
66  returnFloat[5] = floatToConvert[2];
67  returnFloat[6] = floatToConvert[1];
68  returnFloat[7] = floatToConvert[0];
69  return retVal;
70  }
75  double ReverseDouble( const double inDouble ){
76  double retVal;
77  char *doubleToConvert = ( char* ) & inDouble;
78  char *returnDouble = ( char* ) & retVal;
79 
80  // swap the bytes into a temporary buffer
81  returnDouble[0] = doubleToConvert[7];
82  returnDouble[1] = doubleToConvert[6];
83  returnDouble[2] = doubleToConvert[5];
84  returnDouble[3] = doubleToConvert[4];
85  returnDouble[4] = doubleToConvert[3];
86  returnDouble[5] = doubleToConvert[2];
87  returnDouble[6] = doubleToConvert[1];
88  returnDouble[7] = doubleToConvert[0];
89  return retVal;
90 
91  }
92 
93 
94  int InputConf::m_linkSize = 18 * sizeof(double);
95  std::string InputConf::m_inputDir;
96 
101  void InputConf::setInputDir(std::string inputDir){
102  m_inputDir = inputDir;
103  }
104 
109  void InputConf::getInputList(std::vector<std::string>& inputConfList){
110  boost::filesystem::path inputPath = m_inputDir;
111  for (boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(inputPath))
112  if(x.path().extension() == ".bin")
113  inputConfList.push_back(x.path().string());
114  std::sort(inputConfList.begin(), inputConfList.end());
115  }
116 
117  // // Read a single file for every sublattice (for testing)
118  // void InputConf::readSubLattice(GluonField& lattice, int confNum){
119  // char fileName [1024];
120  // sprintf(fileName, "%s/r%03du%03d", OUT_PREFIX, Parallel::rank(), confNum);
121  // FILE* input = fopen(fileName, "rb");
122  // for(int t = 0; t < Parallel::latticeSubSize()[3]; t++){
123  // for(int z = 0; z < Parallel::latticeSubSize()[2]; z++){
124  // for(int y = 0; y < Parallel::latticeSubSize()[1]; y++){
125  // for(int x = 0; x < Parallel::latticeSubSize()[0]; x++){
126  // //fread(lattice(x,y,z,t).m_links, sizeof(double), 72, input);
127  // }}}}
128  // fclose(input);
129  // }
130 
136  void InputConf::readConf(GluonField& lattice, int confNum){
137  char fileName[1024];
138  sprintf(fileName, "%s/conf%04d.bin", OUT_PREFIX, confNum);
139  readConf(lattice, fileName);
140  }
141 
147  void InputConf::readConf(GluonField& lattice, const char* inputFile){
148  MPI_File input;
149  MPI_Offset startPointT, startPointZ, startPointY, startPointX;
150  int volumeX = 1;
151  int volumeY = Parallel::latticeFullSize()[0] * volumeX;
152  int volumeZ = Parallel::latticeFullSize()[1] * volumeY;
153  int volumeT = Parallel::latticeFullSize()[2] * volumeZ;
154 
155  // Open file
156  Parallel::openFile(input, inputFile);
157 
158  for(int t = 0; t < Parallel::latticeSubSize()[3]; t++){
159  startPointT = volumeT * ( Parallel::rankCoord()[3]*Parallel::latticeSubSize()[3] + t);
160  for(int z = 0; z < Parallel::latticeSubSize()[2]; z++){
161  startPointZ = startPointT + volumeZ * ( Parallel::rankCoord()[2]*Parallel::latticeSubSize()[2] + z);
162  for(int y = 0; y < Parallel::latticeSubSize()[1]; y++){
163  startPointY = startPointZ + volumeY * ( Parallel::rankCoord()[1]*Parallel::latticeSubSize()[1] + y);
164  for(int x = 0; x < Parallel::latticeSubSize()[0]; x++){
165  startPointX = startPointY + volumeX * ( Parallel::rankCoord()[0]*Parallel::latticeSubSize()[0] + x);
166  startPointX *= 4*m_linkSize;
167 
168  // FOR CHROMA
169 // for(int mu = 0; mu < 4; mu++){
170 // for(int i = 0; i < 18; i++){
171 // double a;
172 // MPI_File_read_at(input, startPointX, &a, 1, MPI_DOUBLE, MPI_STATUS_IGNORE);
173 // lattice[mu].at(x,y,z,t).mat[i] = ReverseDouble(a);
174 // startPointX += sizeof(double);
175 // }
176 // }
177 
178  // FOR REASONABLE ENDIAN
179  for(int mu = 0; mu < 4; mu++){
180  MPI_File_read_at(input, startPointX, lattice[mu].at(x,y,z,t).mat.data(), 18, MPI_DOUBLE, MPI_STATUS_IGNORE);
181  startPointX += m_linkSize;
182  }
183  }
184  }
185  }
186  }
187  Parallel::closeFile(input);
188  }
189 
190 } // end LatticeIO
Contains the definition of the Lattice class.
static std::string m_inputDir
The path of the input directory.
Definition: inputconf.h:67
Contains classes for reading lattices from binary files.
Class that represents the QCD field as an array of 4 SU3 lattices.
Definition: field.h:53
double ReverseDouble(const double inDouble)
changes the endianess of a double variable
Definition: inputconf.cpp:75
static void readConf(GluonField &lattice, int confNum)
Reads a binary file from a int marker for the name.
Definition: inputconf.cpp:136
Utilities for parallelization.
static int m_linkSize
Contains the size in bytes of a 4 links on a lattice site.
Definition: inputconf.h:65
static void getInputList(std::vector< std::string > &inputConfList)
Returns a list of all valid input files in a the input directory.
Definition: inputconf.cpp:109
static void closeFile(MPI_File &file)
closes a file with MPI
Definition: parallel.cpp:156
double ReverseFloat(const double inFloat)
changes the endianess of a float variable
Definition: inputconf.cpp:54
static void openFile(MPI_File &file, const char *fileName)
opens a file with MPI
Definition: parallel.cpp:148
static void setInputDir(std::string inputDir)
Sets the input directory.
Definition: inputconf.cpp:101