59 unsigned char b1, b2, b3, b4;
64 return ((
int)b1<<24) + ((
int)b2<<16) + ((
int)b3<<8) + b4;
73 unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
82 return ((
long long)b1<<56) + ((
long long)b2<<48) + ((
long long)b3<<40) + ((
long long)b4<<32) + ((
long long)b5<<24) + ((
long long)b6<<16) + ((
long long)b7<<8) + b8;
97 dat2.b[0] = dat1.b[3];
98 dat2.b[1] = dat1.b[2];
99 dat2.b[2] = dat1.b[1];
100 dat2.b[3] = dat1.b[0];
117 dat2.b[0] = dat1.b[7];
118 dat2.b[1] = dat1.b[6];
119 dat2.b[2] = dat1.b[5];
120 dat2.b[3] = dat1.b[4];
121 dat2.b[4] = dat1.b[3];
122 dat2.b[5] = dat1.b[2];
123 dat2.b[6] = dat1.b[1];
124 dat2.b[7] = dat1.b[0];
135 int exp_base10 = round(log10(num));
136 double man_base10 = num*pow(10., -exp_base10);
137 double factor = pow(10., -ndigits+1);
138 double truncated_man_base10 = man_base10-fmod(man_base10, factor);
139 double rounded_remainder = fmod(man_base10, factor)/factor;
141 rounded_remainder = rounded_remainder > 0.5 ? 1.0*factor : 0.0;
143 return (truncated_man_base10+rounded_remainder)*pow(10.0, exp_base10);
152 const double tenth = pow(10., ndigits);
153 return floor(num*tenth)/tenth;
163 string Err =
"Error in opening the input file";
164 if (file!=
"NULL") Err +=
": " + file;
165 ErrorCBL(Err,
"checkIO",
"Kernel.cpp", ExitCode::_IO_);
176 string Err =
"Error in opening the output file";
177 if (file!=
"NULL") Err +=
": " + file;
178 ErrorCBL(Err,
"checkIO",
"Kernel.cpp", ExitCode::_IO_);
188 for (
size_t vv=0; vv<
Var.size(); vv++)
198 string COM =
"if [ $"+
Var+
" ]; then touch tmp; fi";
199 if (system (COM.c_str())) {};
200 ifstream fin_check(
"tmp");
202 WarningMsgCBL(
"the variable " +
Var +
" has not been defined!",
"check_EnvVar",
"Kenrel.cpp");
203 fin_check.clear(); fin_check.close();
204 if (system(
"rm -f tmp")) {};
217 if (type==1) mem =
"VmRSS:";
218 else if (type==2) mem =
"VmSize:";
219 else ErrorCBL(
"the input value of type is not allowed!",
"used_memory",
"Kernel.cpp");
221 string file =
"/proc/self/status";
222 ifstream fin(file.c_str());
checkIO(fin, file);
225 while (getline(fin, line)) {
226 stringstream ss(line);
228 while (ss>>aa) val.push_back(aa);
229 if (val.size()==3 && val[0]==mem) {
230 memory = atoi(val[1].c_str());
235 fin.clear(); fin.close();
252 int cbl::check_memory (
const double frac,
const bool exit,
const std::string func,
const int type)
255 struct sysinfo memInfo;
258 long long freePhysMem = memInfo.freeram;
259 freePhysMem *= memInfo.mem_unit;
262 if (used > freePhysMem*0.001*frac) {
263 string Err =
"possible memory problem";
264 Err += (func.empty()) ?
"!\n" :
" in "+func+
" !\n";
265 Err +=
"freePhysMem = "+
conv((
double)freePhysMem*1.e-9,
par::fDP3)+
" GB\n";
266 Err +=
"memory used by the process: = "+
conv((
double)used*1.e-6,
par::fDP3)+
" GB\n";
267 if (exit)
ErrorCBL(Err,
"check_memory",
"Kernel.cpp");
268 else {
WarningMsgCBL(Err,
"check_memory",
"Kernel.cpp");
return 0; }
273 (void)frac; (void)exit; (void)func; (void)type;
286 sort(vv.begin(),vv.end());
287 vector<int>::iterator it;
288 it = unique(vv.begin(),vv.end());
289 vv.resize(it-vv.begin());
298 sort(vv.begin(),vv.end());
299 vector<double>::iterator it;
300 it = unique(vv.begin(),vv.end());
301 vv.resize(it-vv.begin());
310 sort(vv.begin(),vv.end());
311 vector<string>::iterator it;
312 it = unique(vv.begin(),vv.end());
313 vv.resize(it-vv.begin());
320 bool cbl::glob::operator<(
const cbl::glob::CL &c1,
const cbl::glob::CL &c2) {
return c1.VV[0] < c2.VV[0];}
323 void cbl::sort_2vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2,
const int dim)
326 vector<cbl::glob::CL> ccc;
327 for (
int i=0; i<dim; i++) {
328 vector<double> vect = {*p1, *p2};
329 cbl::glob::CL cl(vect);
331 if (i+1<dim) {*p1 ++; *p2 ++; temp ++;}
333 sort (ccc.begin(),ccc.end());
334 p1 -= temp; p2 -= temp;
335 for (
int i=0; i<dim; i++) {
336 *p1 = ccc[i].VV[0]; *p2 = ccc[i].VV[1];
337 if (i+1<dim) {*p1 ++; *p2 ++;}
341 void cbl::sort_3vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2, std::vector<double>::iterator p3,
const int dim)
344 vector<cbl::glob::CL> ccc;
345 for (
int i=0; i<dim; i++) {
346 vector<double> vect = {*p1, *p2, *p3};
347 cbl::glob::CL cl(vect);
349 if (i+1<dim) {*p1 ++; *p2 ++; *p3 ++; temp ++;}
351 sort (ccc.begin(),ccc.end());
352 p1 -= temp; p2 -= temp; p3 -= temp;
353 for (
int i=0; i<dim; i++) {
354 *p1 = ccc[i].VV[0]; *p2 = ccc[i].VV[1]; *p3 = ccc[i].VV[2];
355 if (i+1<dim) {*p1 ++; *p2 ++; *p3 ++;}
359 void cbl::sort_4vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2, std::vector<double>::iterator p3, std::vector<double>::iterator p4,
const int dim)
362 vector<cbl::glob::CL> ccc;
363 for (
int i=0; i<dim; i++) {
364 vector<double> vect = {*p1, *p2, *p3, *p4};
365 cbl::glob::CL cl(vect);
367 if (i+1<dim) {*p1 ++; *p2 ++; *p3 ++; *p4 ++; temp ++;}
369 sort (ccc.begin(),ccc.end());
370 p1 -= temp; p2 -= temp; p3 -= temp; p4 -= temp;
371 for (
int i=0; i<dim; i++) {
372 *p1 = ccc[i].VV[0]; *p2 = ccc[i].VV[1]; *p3 = ccc[i].VV[2]; *p4 = ccc[i].VV[3];
373 if (i+1<dim) {*p1 ++; *p2 ++; *p3 ++; *p4 ++;}
381 int cbl::makeDir (std::string path,
const std::string rootPath,
const mode_t mode,
const bool verbose)
385 for (string::iterator iter=path.begin(); iter!=path.end();) {
386 string::iterator newIter = find(iter, path.end(),
'/');
387 string newPath = rootPath+
"/"+string(path.begin(), newIter);
389 if (stat(newPath.c_str(), &st)!=0) {
390 if (mkdir(newPath.c_str(), mode)!=0 && errno!=EEXIST)
391 return ErrorCBL(
"cannot create the directory "+newPath+strerror(errno),
"makeDir",
"Kernel.cpp");
394 if (!S_ISDIR(st.st_mode)) {
396 return ErrorCBL(newPath+
" is not a directory",
"makeDir",
"Kernel.cpp");
399 WarningMsgCBL(newPath+
" already exists",
"makeDir",
"Kernel.cpp");
402 if (newIter!=path.end()) ++iter;
410 std::vector<std::vector<double>>
cbl::read_file (
const std::string file_name,
const std::string path_name,
const std::vector<int> column_data,
const int skip_nlines)
412 const string input_file (path_name+file_name);
413 const int cl_max = column_data.size();
415 ifstream fin(input_file.c_str());
checkIO(fin, input_file);
419 unsigned int n_lines = 0;
420 while(getline(fin, line)) n_lines++;
421 n_lines-=skip_nlines;
423 fin.clear(); fin.close();
426 vector<vector<double>> final_data(cl_max, vector<double>(n_lines, 0));
428 #pragma omp parallel num_threads(cl_max>omp_get_max_threads() ? omp_get_max_threads() : cl_max)
431 #pragma omp for schedule(dynamic)
432 for (
int cl=0; cl<cl_max; ++cl) {
435 ifstream Fin (input_file);
439 for (
int i=0; i<skip_nlines; ++i)
444 for (
unsigned int nn=0; nn<n_lines; ++nn) {
447 stringstream ss(Line);
448 vector<double> num;
double NUM;
449 while (ss>>NUM) num.emplace_back(NUM);
452 final_data[cl][nn] = num[column_data[cl]-1];
455 Fin.clear(); Fin.close();
464 std::vector<std::vector<double>>
cbl::read_file (
const std::string file_name,
const std::string path_name,
const std::vector<int> column_data,
const std::string delimiter,
const char comment)
466 std::ifstream file_input(path_name+file_name.c_str(),std::ios::in);
checkIO(file_input, path_name+file_name);
469 while (!file_input.eof())
471 std::getline(file_input,line);
475 file_input.clear(); file_input.seekg(0, std::ios::beg);
477 std::vector<std::vector<double>> read_data(column_data.size(), std::vector<double>());
478 std::vector<std::string> temp(1000);
479 for (
int i=0; i<line_count; i++){
480 getline(file_input,line);
481 if (line.at(0) != comment){
484 while (line.find(delimiter) != std::string::npos){
485 line_pos = line.find(delimiter);
486 temp[vector_ind] = line.substr(0, line_pos);
487 line.erase(0, line_pos + delimiter.length());
491 temp[vector_ind] = line.substr(0, line_pos);
492 line.erase(0, line_pos + delimiter.length());
494 for (
size_t i=0; i<read_data.size(); i++){
495 read_data[i].emplace_back(std::stod(temp[column_data[i]]));
Useful generic functions.
static const char fDP3[]
conversion symbol for: double -> std::string
Var
the catalogue variables
The global namespace of the CosmoBolognaLib
std::string conv(const T val, const char *fact)
convert a number to a std::string
std::vector< std::vector< double > > read_file(const std::string file_name, const std::string path_name, const std::vector< int > column_data, const int skip_nlines=0)
read a data from a file ASCII
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
void sort_4vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, std::vector< double >::iterator p3, std::vector< double >::iterator p4, const int dim)
sort the elements of a std::vectors, and the elements of three other std::vectors according to the fi...
int used_memory(const int type)
get the memory used by current process in kB
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
void unique_unsorted(std::vector< int > &vv)
erase all the equal elements of the input std::vector
void set_EnvVar(const std::vector< std::string > Var)
set evironment variables
short ShortSwap(const short s)
endian conversion of a short variable
double round_to_digits(const double num, const int ndigits)
reduce the digit figures of an input double
double DoubleSwap(const double d)
endian conversion of a double variable
void sort_2vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, const int dim)
sort the elements of a std::vectors, and the elements of a second std::vector according to the first ...
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
long long LongSwap(const long long i)
endian conversion of a long integer variable
int check_memory(const double frac, const bool exit=true, const std::string func="", const int type=1)
check if the memory used by current process is larger than a given fraction of the available memory
void check_EnvVar(const std::string Var)
check if an environment variable exists
float FloatSwap(const float f)
endian conversion of a float variable
double round_to_precision(const double num, const int ndigits)
reduce the precision of an input double
int makeDir(std::string path, const std::string rootPath=".", const mode_t mode=0777, const bool verbose=false)
function to create multiple directories
void sort_3vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, std::vector< double >::iterator p3, const int dim)
sort the elements of a std::vectors, and the elements of two other std::vectors according to the firs...
int IntSwap(const int i)
endian conversion of an integer variable