[14de469] | 1 | /** \file helpers.cpp
|
---|
| 2 | *
|
---|
[6ac7ee] | 3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on
|
---|
[14de469] | 4 | */
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | #include "helpers.hpp"
|
---|
| 8 |
|
---|
[6cd79d] | 9 | #include "memoryusageobserver.hpp"
|
---|
| 10 |
|
---|
[14de469] | 11 | /********************************************** helpful functions *********************************/
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | /** Asks for a double value and checks input
|
---|
| 15 | * \param *text question
|
---|
| 16 | */
|
---|
| 17 | double ask_value(const char *text)
|
---|
| 18 | {
|
---|
[042f82] | 19 | double test = 0.1439851348959832147598734598273456723948652983045928346598365;
|
---|
| 20 | do {
|
---|
| 21 | cout << Verbose(0) << text;
|
---|
| 22 | cin >> test;
|
---|
| 23 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
|
---|
| 24 | return test;
|
---|
[14de469] | 25 | };
|
---|
| 26 |
|
---|
[d3a46d] | 27 | /** Output of a debug message to stderr.
|
---|
| 28 | * \param *P Problem at hand, points to ParallelSimulationData#me
|
---|
| 29 | * \param output output string
|
---|
| 30 | */
|
---|
| 31 | #ifdef HAVE_DEBUG
|
---|
| 32 | void debug_in(const char *output, const char *file, const int line) {
|
---|
[042f82] | 33 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
[d3a46d] | 34 | }
|
---|
| 35 | #else
|
---|
[042f82] | 36 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
[d3a46d] | 37 | #endif
|
---|
[14de469] | 38 |
|
---|
| 39 | /** modulo operator for doubles.
|
---|
| 40 | * \param *b pointer to double
|
---|
| 41 | * \param lower_bound lower bound
|
---|
| 42 | * \param upper_bound upper bound
|
---|
| 43 | */
|
---|
| 44 | void bound(double *b, double lower_bound, double upper_bound)
|
---|
| 45 | {
|
---|
[042f82] | 46 | double step = (upper_bound - lower_bound);
|
---|
| 47 | while (*b >= upper_bound)
|
---|
| 48 | *b -= step;
|
---|
| 49 | while (*b < lower_bound)
|
---|
| 50 | *b += step;
|
---|
[6ac7ee] | 51 | };
|
---|
[14de469] | 52 |
|
---|
| 53 | /** Flips two doubles.
|
---|
| 54 | * \param *x pointer to first double
|
---|
| 55 | * \param *y pointer to second double
|
---|
| 56 | */
|
---|
| 57 | void flip(double *x, double *y)
|
---|
| 58 | {
|
---|
[042f82] | 59 | double tmp;
|
---|
| 60 | tmp = *x;
|
---|
| 61 | *x = *y;
|
---|
| 62 | *y = tmp;
|
---|
[14de469] | 63 | };
|
---|
| 64 |
|
---|
| 65 | /** Returns the power of \a n with respect to \a base.
|
---|
| 66 | * \param base basis
|
---|
| 67 | * \param n power
|
---|
| 68 | * \return \f$base^n\f$
|
---|
| 69 | */
|
---|
| 70 | int pot(int base, int n)
|
---|
| 71 | {
|
---|
[042f82] | 72 | int res = 1;
|
---|
| 73 | int j;
|
---|
| 74 | for (j=n;j--;)
|
---|
| 75 | res *= base;
|
---|
| 76 | return res;
|
---|
[14de469] | 77 | };
|
---|
| 78 |
|
---|
[5034e1] | 79 | /** Counts lines in file.
|
---|
| 80 | * Note we are scanning lines from current position, not from beginning.
|
---|
| 81 | * \param InputFile file to be scanned.
|
---|
| 82 | */
|
---|
| 83 | int CountLinesinFile(ifstream &InputFile)
|
---|
| 84 | {
|
---|
| 85 | char *buffer = Malloc<char>(MAXSTRINGSIZE, "CountLinesinFile: *buffer");
|
---|
| 86 | int lines=0;
|
---|
| 87 |
|
---|
| 88 | int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
|
---|
| 89 | // count the number of lines, i.e. the number of fragments
|
---|
| 90 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
|
---|
| 91 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 92 | while(!InputFile.eof()) {
|
---|
| 93 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 94 | lines++;
|
---|
| 95 | }
|
---|
| 96 | InputFile.seekg(PositionMarker, ios::beg);
|
---|
| 97 | Free(&buffer);
|
---|
| 98 | return lines;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
[14de469] | 101 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
| 102 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
| 103 | * \param digits number to create with 0 prefixed
|
---|
| 104 | * \return allocated(!) char array with number in digits, ten base.
|
---|
| 105 | */
|
---|
| 106 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
| 107 | {
|
---|
[042f82] | 108 | char *returnstring;
|
---|
| 109 | int number = FragmentNumber;
|
---|
| 110 | int order = 0;
|
---|
| 111 | while (number != 0) { // determine number of digits needed
|
---|
| 112 | number = (int)floor(((double)number / 10.));
|
---|
| 113 | order++;
|
---|
| 114 | //cout << "Number is " << number << ", order is " << order << "." << endl;
|
---|
| 115 | }
|
---|
| 116 | // allocate string
|
---|
[29812d] | 117 | returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring");
|
---|
[042f82] | 118 | // terminate and fill string array from end backward
|
---|
| 119 | returnstring[order] = '\0';
|
---|
| 120 | number = digits;
|
---|
| 121 | for (int i=order;i--;) {
|
---|
| 122 | returnstring[i] = '0' + (char)(number % 10);
|
---|
| 123 | number = (int)floor(((double)number / 10.));
|
---|
| 124 | }
|
---|
| 125 | //cout << returnstring << endl;
|
---|
| 126 | return returnstring;
|
---|
[14de469] | 127 | };
|
---|
| 128 |
|
---|
[e198c7] | 129 | /** Tests whether a given string contains a valid number or not.
|
---|
| 130 | * \param *string
|
---|
| 131 | * \return true - is a number, false - is not a valid number
|
---|
| 132 | */
|
---|
[6ac7ee] | 133 | bool IsValidNumber( const char *string)
|
---|
[e198c7] | 134 | {
|
---|
[042f82] | 135 | int ptr = 0;
|
---|
| 136 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot
|
---|
| 137 | ptr++;
|
---|
| 138 | if ((string[ptr] >= '0') && (string[ptr] <= '9'))
|
---|
| 139 | return true;
|
---|
| 140 | return false;
|
---|
[e198c7] | 141 | };
|
---|
| 142 |
|
---|
[f66195] | 143 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
|
---|
| 144 | * \param *symm 6-dim array of unique symmetric matrix components
|
---|
| 145 | * \return allocated NDIM*NDIM array with the symmetric matrix
|
---|
| 146 | */
|
---|
| 147 | double * ReturnFullMatrixforSymmetric(double *symm)
|
---|
| 148 | {
|
---|
| 149 | double *matrix = Malloc<double>(NDIM * NDIM, "molecule::ReturnFullMatrixforSymmetric: *matrix");
|
---|
| 150 | matrix[0] = symm[0];
|
---|
| 151 | matrix[1] = symm[1];
|
---|
| 152 | matrix[2] = symm[3];
|
---|
| 153 | matrix[3] = symm[1];
|
---|
| 154 | matrix[4] = symm[2];
|
---|
| 155 | matrix[5] = symm[4];
|
---|
| 156 | matrix[6] = symm[3];
|
---|
| 157 | matrix[7] = symm[4];
|
---|
| 158 | matrix[8] = symm[5];
|
---|
| 159 | return matrix;
|
---|
| 160 | };
|
---|
| 161 |
|
---|
| 162 | /** Comparison function for GSL heapsort on distances in two molecules.
|
---|
| 163 | * \param *a
|
---|
| 164 | * \param *b
|
---|
| 165 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
|
---|
| 166 | */
|
---|
| 167 | int CompareDoubles (const void * a, const void * b)
|
---|
| 168 | {
|
---|
| 169 | if (*(double *)a > *(double *)b)
|
---|
| 170 | return -1;
|
---|
| 171 | else if (*(double *)a < *(double *)b)
|
---|
| 172 | return 1;
|
---|
| 173 | else
|
---|
| 174 | return 0;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | /** Allocates a memory range using malloc().
|
---|
[29812d] | 179 | * Prints the provided error message in case of a failure.
|
---|
| 180 | *
|
---|
| 181 | * \param number of memory slices of type X to allocate
|
---|
| 182 | * \param failure message which is printed if the allocation fails
|
---|
| 183 | * \return pointer to the allocated memory range, will be NULL if a failure occurred
|
---|
| 184 | */
|
---|
| 185 | template <> char* Malloc<char>(size_t size, const char* output)
|
---|
| 186 | {
|
---|
| 187 | char* buffer = NULL;
|
---|
| 188 | buffer = (char*) malloc(sizeof(char) * (size + 1));
|
---|
| 189 | for (size_t i = size; i--;)
|
---|
| 190 | buffer[i] = (i % 2 == 0) ? 'p': 'c';
|
---|
| 191 | buffer[size] = '\0';
|
---|
| 192 |
|
---|
[c30180] | 193 | if (buffer != NULL) {
|
---|
| 194 | MemoryUsageObserver::getInstance()->addMemory(buffer, size);
|
---|
| 195 | } else {
|
---|
[29812d] | 196 | cout << Verbose(0) << "Malloc for datatype " << typeid(char).name()
|
---|
| 197 | << " failed - pointer is NULL: " << output << endl;
|
---|
[c30180] | 198 | }
|
---|
[29812d] | 199 |
|
---|
| 200 | return buffer;
|
---|
| 201 | };
|
---|
| 202 |
|
---|
[21b9c3] | 203 | /**
|
---|
| 204 | * Frees all memory registered by the memory observer and calls exit(225) afterwards.
|
---|
| 205 | */
|
---|
[6cd79d] | 206 | void performCriticalExit() {
|
---|
[21b9c3] | 207 | map<void*, size_t> pointers = MemoryUsageObserver::getInstance()->getPointersToAllocatedMemory();
|
---|
| 208 | for (map<void*, size_t>::iterator runner = pointers.begin(); runner != pointers.end(); runner++) {
|
---|
| 209 | Free(((void**) &runner->first));
|
---|
| 210 | }
|
---|
[e198c7] | 211 |
|
---|
[21b9c3] | 212 | exit(255);
|
---|
| 213 | }
|
---|