Commit a939059e authored by fabianw's avatar fabianw
Browse files

added working version (non MPI)

parent 0a03d44d
/*
* ArgumentParser.h
* Cubism
*
* This argument parser assumes that all arguments are optional ie, each of the argument names is preceded by a '-'
* all arguments are however NOT optional to avoid a mess with default values and returned values when not found!
*
* More converter could be required:
* add as needed
* TypeName as{TypeName}() in Value
*
* Created by Christian Conti on 6/7/10.
* Copyright 2010 ETH Zurich. All rights reserved.
*
*/
#pragma once
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace std;
class Value
{
private:
string content;
public:
Value() : content("") {}
Value(string content_) : content(content_) { /*printf("%s\n",content.c_str());*/ }
double asDouble(double def=0)
{
if (content == "")
{
ostringstream sbuf;
sbuf << def;
content = sbuf.str();
}
return (double) atof(content.c_str());
}
int asInt(int def=0)
{
if (content == "")
{
ostringstream sbuf;
sbuf << def;
content = sbuf.str();
}
return atoi(content.c_str());
}
bool asBool(bool def=false)
{
if (content == "")
{
if (def) content = "true";
else content = "false";
}
if (content == "0") return false;
if (content == "false") return false;
return true;
}
string asString(string def="")
{
if (content == "") content = def;
return content;
}
};
class CommandlineParser
{
private:
const int iArgC;
const char** vArgV;
bool bStrictMode, bVerbose;
protected:
map<string,Value> mapArguments;
public:
Value& operator()(const string arg)
{
if (bStrictMode)
{
map<string,Value>::const_iterator it = mapArguments.find(arg);
if (it == mapArguments.end())
{
printf("Runtime option NOT SPECIFIED! ABORTING! name: %s\n",arg.data());
abort();
}
}
if (bVerbose) printf("%s is %s\n", arg.data(), mapArguments[arg].asString().data());
return mapArguments[arg];
}
bool check(const string arg) const
{
return mapArguments.find(arg) != mapArguments.end();
}
CommandlineParser(const int argc, const char ** argv) : mapArguments(), iArgC(argc), vArgV(argv), bStrictMode(false), bVerbose(true)
{
for (int i=1; i<argc; i++)
if (argv[i][0] == '-')
{
string values = "";
int itemCount = 0;
for (int j=i+1; j<argc; j++)
{
const bool leadingDash = (argv[j][0] == '-');
const char c = argv[j][1];
const bool firstNumeric = ((c >= '0' && c <= '9') || c == 0) ? true : false;
if (leadingDash && !firstNumeric)
break;
else
{
if (strcmp(values.c_str(), ""))
values += ' ';
values += argv[j];
itemCount++;
}
}
if (itemCount == 0)
values = "true";
mapArguments[argv[i]] = Value(values);
i += itemCount;
}
mute();
//printf("found %ld arguments of %d\n",mapArguments.size(),argc);
}
int getargc() const { return iArgC; }
const char** getargv() const { return vArgV; }
void set_strict_mode()
{
bStrictMode = true;
}
void unset_strict_mode()
{
bStrictMode = false;
}
void mute()
{
bVerbose = false;
}
void loud()
{
bVerbose = true;
}
void save_options(string path=".")
{
string options;
for(map<string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
options+= it->first + " " + it->second.asString() + " ";
}
string filepath = (path + "/" + string("argumentparser.log"));
FILE * f = fopen(filepath.data(), "a");
if (f == NULL)
{
printf("impossible to write %s.\n", filepath.data());
return;
}
fprintf(f, "%s\n", options.data());
fclose(f);
}
void print_args()
{
for(map<string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
std::cout.width(50);
std::cout.fill('.');
std::cout << std::left << it->first;
std::cout << ": " << it->second.asString() << std::endl;
}
}
};
class ArgumentParser: public CommandlineParser
{
typedef std::map<std::string, Value> ArgMap;
typedef std::map<std::string, Value*> pArgMap;
typedef std::map<std::string, ArgMap* > FileMap;
const char commentStart;
// keep a reference form option origin
ArgMap from_commandline;
FileMap from_files;
pArgMap from_code;
// helper
void _ignoreComments(std::istream& stream, const char commentChar)
{
stream >> std::ws;
int nextchar = stream.peek();
while (nextchar == commentChar)
{
stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
stream >> std::ws;
nextchar = stream.peek();
}
}
bool _existKey(std::string& key) const
{
const std::string og_key = key;
bool bExist = true;
// look for both possible keys (i.e. with leading "-" and without)
ArgMap::const_iterator it = mapArguments.find(key);
if (it == mapArguments.end())
{
if (key[0] == '-') key = key.erase(0, 1);
else key = "-" + key;
it = mapArguments.find(key);
if (it == mapArguments.end())
{
key = og_key;
bExist = false;
}
}
return bExist;
}
inline std::string _stripKey(std::string key) const
{
if (key[0] == '-') key = key.erase(0, 1);
return key;
}
public:
ArgumentParser(const int _argc, const char ** _argv):
CommandlineParser(_argc, _argv), commentStart('#')
{
from_commandline = mapArguments;
}
virtual ~ArgumentParser()
{
for (FileMap::iterator it = from_files.begin(); it != from_files.end(); it++)
delete it->second;
}
void readFile(const std::string filepath)
{
from_files[filepath] = new ArgMap;
ArgMap& myFMap = *(from_files[filepath]);
std::ifstream confFile(filepath.c_str());
if (confFile.is_open())
{
// read (key value) pairs from input file, ignore comments
// beginning with "#"
_ignoreComments(confFile, commentStart);
while (!confFile.eof())
{
std::string line, key, val;
std::getline(confFile, line);
std::istringstream lineStream(line);
lineStream >> key;
lineStream >> val;
_ignoreComments(lineStream, commentStart);
while(!lineStream.eof())
{
std::string multiVal;
lineStream >> multiVal;
val += (" " + multiVal);
_ignoreComments(lineStream, commentStart);
}
if (_existKey(key)) continue;
std::pair<string, Value> item(key, Value(val));
mapArguments.insert(item); // add to parent container
myFMap.insert(item); // add to private container
_ignoreComments(confFile, commentStart);
}
}
}
Value& operator()(std::string key)
{
const bool bDefaultInCode = !_existKey(key);
Value& retval = CommandlineParser::operator()(key);
if (bDefaultInCode) from_code[key] = &retval;
return retval;
}
inline bool check(std::string key) const { return _existKey(key); }
inline bool exist(std::string key) const { return _existKey(key); }
void print_args()
{
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
std::cout << "* Summary:" << std::endl;
std::cout << "* Parameter read from command line: " << from_commandline.size() << std::endl;
size_t nFiles = 0;
size_t nFileParameter = 0;
for (FileMap::const_iterator it=from_files.begin(); it!=from_files.end(); ++it)
{
if (it->second->size() > 0)
{
++nFiles;
nFileParameter += it->second->size();
}
}
std::cout << "* Parameter read from " << std::setw(3) << std::right << nFiles << " file(s): " << nFileParameter << std::endl;
std::cout << "* Parameter read from defaults in code: " << from_code.size() << std::endl;
std::cout << "* Total number of parameter read from all sources: " << mapArguments.size() << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
// command line given arguments
if (!from_commandline.empty())
{
std::cout << "* Command Line:" << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
for(ArgMap::iterator it=from_commandline.begin(); it!=from_commandline.end(); it++)
{
std::cout.width(50);
std::cout.fill('.');
std::cout << std::left << _stripKey(it->first);
std::cout << ": " << it->second.asString() << std::endl;
}
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
}
// options read from input files
if (!from_files.empty())
{
for (FileMap::iterator itFile=from_files.begin(); itFile!=from_files.end(); itFile++)
{
if (!itFile->second->empty())
{
std::cout << "* File: " << itFile->first << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
ArgMap& fileArgs = *(itFile->second);
for(ArgMap::iterator it=fileArgs.begin(); it!=fileArgs.end(); it++)
{
std::cout.width(50);
std::cout.fill('.');
std::cout << std::left << _stripKey(it->first);
std::cout << ": " << it->second.asString() << std::endl;
}
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
}
}
}
// defaults defined in code
if (!from_code.empty())
{
std::cout << "* Defined in Code:" << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
for(pArgMap::iterator it=from_code.begin(); it!=from_code.end(); it++)
{
std::cout.width(50);
std::cout.fill('.');
std::cout << std::left << _stripKey(it->first);
std::cout << ": " << it->second->asString() << std::endl;
}
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
}
}
};
// File : Matrix3D.h
// Date : Wed Nov 23 14:45:22 2016
// Author : Fabian Wermelinger
// Description: 3D Matrix
// Copyright 2016 ETH Zurich. All Rights Reserved.
#ifndef MATRIX3D_H_L9MVKQGE
#define MATRIX3D_H_L9MVKQGE
#include <cassert>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <iostream>
template <typename T, int XS=0, int XE=0, int YS=0, int YE=0, int ZS=0, int ZE=0>
class Matrix3D
{
public:
Matrix3D() : m_Nx(0), m_Ny(0), m_Nz(0), m_allocated(false), m_data(nullptr) { }
Matrix3D(const int Nx, const int Ny, const int Nz, const T* const data = nullptr) :
m_Nx(0), m_Ny(0), m_Nz(0), m_allocated(false), m_data(nullptr)
{
alloc(Nx, Ny, Nz, data);
}
Matrix3D(const Matrix3D& rhs) :
m_Nx(0), m_Ny(0), m_Nz(0), m_allocated(false), m_data(nullptr)
{
if (rhs.m_allocated)
{
m_data = _alloc(rhs.m_Nx, rhs.m_Ny, rhs.m_Nz);
_copy(rhs.m_data);
}
}
Matrix3D(Matrix3D&& rhs) :
m_Nx(rhs.m_Nx), m_Ny(rhs.m_Ny), m_Nz(rhs.m_Nz),
m_XS(rhs.m_XS), m_XE(rhs.m_XE), m_Xpitch(rhs.m_Xpitch), m_Npitched(rhs.m_Npitched),
m_allocated(rhs.m_allocated), m_data(std::move(rhs.m_data))
{ }
~Matrix3D() { if (m_allocated) free(m_data); }
Matrix3D& operator=(const Matrix3D& rhs)
{
if (this != &rhs)
{
_reset();
if (rhs.m_allocated)
{
m_data = _alloc(rhs.m_Nx, rhs.m_Ny, rhs.m_Nz);
_copy(rhs.m_data);
}
}
return *this;
}
Matrix3D& operator=(Matrix3D&& rhs)
{
if (this != &rhs)
{
_reset();
if (rhs.m_allocated)
{
m_Nx = rhs.m_Nx;
m_Ny = rhs.m_Ny;
m_Nz = rhs.m_Nz;
m_XS = rhs.m_XS;
m_XE = rhs.m_XE;
m_Xpitch = rhs.m_Xpitch;
m_Npitched = rhs.m_Npitched;
m_allocated = rhs.m_allocated;
m_data = std::move(rhs.m_data);
rhs.m_allocated = false;
}
}
return *this;
}
T operator()(const int ix, const int iy, const int iz) const
{
assert(m_allocated);
assert(ix >= XS && ix < m_Nx+XE);
assert(iy >= YS && iy < m_Ny+YE);
assert(iz >= ZS && iz < m_Nz+ZE);
return m_data[(ix-XS) + m_Xpitch*(iy-YS) + m_Xpitch*(m_Ny+YE-YS)*(iz-ZS)];
}
T& operator()(const int ix, const int iy, const int iz)
{
assert(m_allocated);
assert(ix >= XS && ix < m_Nx+XE);
assert(iy >= YS && iy < m_Ny+YE);
assert(iz >= ZS && iz < m_Nz+ZE);
return m_data[(ix-XS) + m_Xpitch*(iy-YS) + m_Xpitch*(m_Ny+YE-YS)*(iz-ZS)];
}
inline void alloc(const int Nx, const int Ny, const int Nz, const T* const data = nullptr)
{
if (m_allocated) free(m_data);
m_data = _alloc(Nx, Ny, Nz);
if (data)
{
for (int iz = 0; iz < m_Nz; ++iz)
for (int iy = 0; iy < m_Ny; ++iy)
{
const T* const src = data + m_Nx*iy + m_Nx*m_Ny*iz;
T* dst = &(this->operator()(0,iy,iz));
memcpy(dst, src, m_Nx*sizeof(T));
}
}
}
inline int Nx() const { return m_Nx; }
inline int Ny() const { return m_Ny; }
inline int Nz() const { return m_Nz; }
private:
int m_Nx;
int m_Ny;
int m_Nz;
int m_XS, m_XE, m_Xpitch, m_Npitched;
bool m_allocated;
T* m_data;
inline T* _alloc(const int Nx, const int Ny, const int Nz)
{
m_Nx = Nx;
m_Ny = Ny;
m_Nz = Nz;
const int alignedElements = _ALIGN_/sizeof(T);
m_XS = alignedElements * (XS - (alignedElements-1))/alignedElements;
m_XE = alignedElements * (XE + (alignedElements-1))/alignedElements;
m_Xpitch = (m_Nx+m_XE) - m_XS;
m_Npitched = m_Xpitch * (m_Ny+YE-YS) * (m_Nz+ZE-ZS);
void* pmem;
if (posix_memalign(&pmem, _ALIGN_, m_Npitched*sizeof(T)))
{
std::cerr << "ERROR: Matrix3D: can not allocate memory" << std::endl;
abort();
}
memset(pmem, 0, m_Npitched*sizeof(T));
m_allocated = true;
return (T*)pmem;
}
inline void _copy(const T* const base)
{
const int Nslice = m_Xpitch * (m_Ny+YE-YS);
for (int iz = 0; iz < m_Nz+ZE-ZS; ++iz)
{
const T* const src = base + Nslice*iz;
T* dst = m_data + Nslice*iz;
memcpy(dst, src, Nslice*sizeof(T));
}
}
inline void _reset()
{
if (m_allocated) free(m_data);
m_Nx = m_Ny = m_Nz = m_XS = m_XE = m_Xpitch = m_Npitched = 0;
m_allocated = false;
m_data = nullptr;
}
};
#endif /* MATRIX3D_H_L9MVKQGE */
// File : common.h
// Date : Tue Nov 22 15:51:55 2016
// Author : Fabian Wermelinger
// Description: common stuff
// Copyright 2016 ETH Zurich. All Rights Reserved.
#ifndef COMMON_H_ZH0IVQBA
#define COMMON_H_ZH0IVQBA
#ifdef _FLOAT_PRECISION_
typedef float Real;
#else
typedef double Real;
#endif
#ifdef _USE_HDF_
#ifdef _FLOAT_PRECISION_
#define HDF_PRECISION H5T_NATIVE_FLOAT
#else
#define HDF_PRECISION H5T_NATIVE_DOUBLE
#endif
#endif /* _USE_HDF_ */
#ifndef _ALIGN_
#define _ALIGN_ 16
#endif /* _ALIGN_ */
#include "Matrix3D.h"
typedef Matrix3D<Real,-3,3,-3,3,-3,3> Matrix_t;
#endif /* COMMON_H_ZH0IVQBA */
// File : Interpolator.h
// Date : Tue Nov 22 15:37:00 2016
// Author : Fabian Wermelinger
// Description: Data interpolator type
// Copyright 2016 ETH Zurich. All Rights Reserved.
#ifndef INTERPOLATOR_H_QCK6H0CI
#define INTERPOLATOR_H_QCK6H0CI
#include <cassert>