AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
AuxiliarMethods.hpp
Go to the documentation of this file.
1/*
2 * This file is part of AQUAgpusph, a free CFD program based on SPH.
3 * Copyright (C) 2012 Jose Luis Cercos Pita <jl.cercos@upm.es>
4 *
5 * AQUAgpusph is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * AQUAgpusph is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with AQUAgpusph. If not, see <http://www.gnu.org/licenses/>.
17 */
18
22
23#ifndef AUXILIARMETHODS_H_INCLUDED
24#define AUXILIARMETHODS_H_INCLUDED
25
26#include "sphPrerequisites.hpp"
27#include <string>
28#include <vector>
29#include <tuple>
30#include <deque>
31#include <limits>
32#include <filesystem>
33#include "aquagpusph/ext/boost/numeric/conversion/cast.hpp"
34
35#ifdef HAVE_MPI
36#include <mpi.h>
37#endif
38
39namespace Aqua {
40
47bool
48hasPrefix(const std::string& str, const std::string& prefix);
49
52inline bool
53startswith(const std::string& str, const std::string& prefix)
54{
55 return hasPrefix(str, prefix);
56}
57
64bool
65hasSuffix(const std::string& str, const std::string& suffix);
66
69inline bool
70endswith(const std::string& str, const std::string& prefix)
71{
72 return hasSuffix(str, prefix);
73}
74
81void
82replaceAll(std::string& str,
83 const std::string& search,
84 const std::string& replace);
85
93std::string
94replaceAllCopy(std::string str, std::string search, std::string replace);
95
101void
102ltrim(std::string& s);
103
109void
110rtrim(std::string& s);
111
117void
118trim(std::string& s);
119
126std::string
127ltrimCopy(std::string s);
128
135std::string
136rtrimCopy(std::string s);
137
144std::string
145trimCopy(std::string s);
146
158std::string
159xxd2string(unsigned char* arr, unsigned int len);
160
163void
164toLower(std::string& str);
165
169std::string
170toLowerCopy(std::string str);
171
180void
181setStrConstants(std::string& str);
182
192std::string
193setStrConstantsCopy(std::string str);
194
200std::vector<std::string>
201split(std::string str, char chr);
202
209inline std::vector<std::string>
210split(const std::string& s)
211{
212 return split(s, ' ');
213}
214
224std::vector<std::string>
225split_formulae(std::string str);
226
247std::string
248newFilePath(const std::string& basename,
249 unsigned int& i,
250 unsigned int digits = 5);
251
253
258template <typename T>
259inline bool
260isPowerOf2(T x) { return !(x & (x - 1)); }
261
263
270template <typename T>
271inline T
273{
274 if (n && isPowerOf2(n))
275 return n;
276
277 T p = 1;
278 while (p < n) {
279 p <<= 1;
280 }
281 return p;
282}
283
284
286
293template<typename T=unsigned int>
294T
295roundUp(T x, T divisor)
296{
297 T rest = x % divisor;
298 if (rest) {
299 x -= rest;
300 x += divisor;
301 }
302 return x;
303}
304
309template<typename Tout=int, typename Tin>
310inline Tout
311round(Tin n) { return (Tout)(n + ((n >= 0) ? 0.5 : -0.5)); }
312
316const std::string
317getExePath();
318
328const std::string
330
332
336const std::string
337getFolderFromFilePath(const std::string file_path);
338
340
344const std::string
345getFileNameFromFilePath(const std::string file_path);
346
348
353const std::string
354getExtensionFromFilePath(const std::string file_path);
355
361bool
362isFile(const std::string file_name);
363
369bool
370isRelativePath(const std::string path);
371
373
378size_t
379getLocalWorkSize(cl_command_queue queue);
380
382
389size_t
390getGlobalWorkSize(size_t n, size_t local_work_size);
391
393
399inline float
400clamp(float x, float a, float b)
401{
402 return x < a ? a : (x > b ? b : x);
403}
404
412template<class Tdst, class Torg>
413inline Tdst
415{
416 Tdst r;
417 try
418 {
419 r = boost::numeric_cast<Torg>(v);
420 }
421 catch(boost::numeric::bad_numeric_cast& e) {
422 throw std::out_of_range("narrow_cast<>() failed");
423 throw std::out_of_range(e.what());
424 }
425 return r;
426}
427
429
433unsigned int
434numberOfDigits(unsigned int number);
435
436#ifdef HAVE_MPI
437
438namespace MPI {
439
444std::string error_str(int errorcode);
445
451DECLDIR void init(int *argc, char ***argv);
452
456DECLDIR void finalize();
457
463int rank(MPI_Comm comm);
464
470int size(MPI_Comm comm);
471
476DECLDIR void barrier(MPI_Comm comm);
477
487void send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag,
488 MPI_Comm comm);
489
500MPI_Request isend(const void *buf, int count, MPI_Datatype datatype, int dest,
501 int tag, MPI_Comm comm);
502
513MPI_Status recv(void *buf, int count, MPI_Datatype datatype, int source,
514 int tag, MPI_Comm comm);
515
526MPI_Request irecv(void *buf, int count, MPI_Datatype datatype,
527 int source, int tag, MPI_Comm comm);
528
534MPI_Status wait(MPI_Request *request);
535
536} // ::MPI
537
538#endif
539
540} // ::Aqua
541
542#endif // AUXILIARMETHODS_H_INCLUDED
#define T
Definition Sort.hcl.in:83
__kernel void count(__global const int *imove, __global unsigned int *ibuffer, usize N)
Identify the buffer particles.
Definition SetBuffer.cl:37
void finalize()
Wrapper for MPI_Finalize()
Definition AuxiliarMethods.cpp:409
MPI_Request isend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
Wrapper for MPI_Isend()
Definition AuxiliarMethods.cpp:461
MPI_Status recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm)
Wrapper for MPI_Recv()
Definition AuxiliarMethods.cpp:474
MPI_Status wait(MPI_Request *request)
Wrapper for MPI_Wait()
Definition AuxiliarMethods.cpp:500
int size(MPI_Comm comm)
Wrapper for MPI_Comm_size()
Definition AuxiliarMethods.cpp:429
void barrier(MPI_Comm comm)
Wrapper for MPI_Barrier()
Definition AuxiliarMethods.cpp:440
void send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
Wrapper for MPI_Send()
Definition AuxiliarMethods.cpp:450
int rank(MPI_Comm comm)
Wrapper for MPI_Comm_rank()
Definition AuxiliarMethods.cpp:418
std::string error_str(int errorcode)
Wrapper for MPI_Error_string()
Definition AuxiliarMethods.cpp:377
MPI_Request irecv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm)
Wrapper for MPI_Irecv()
Definition AuxiliarMethods.cpp:487
void init(int *argc, char ***argv)
Wrapper for MPI_Init()
Definition AuxiliarMethods.cpp:388
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
bool startswith(const std::string &str, const std::string &prefix)
Alias for hasPrefix()
Definition AuxiliarMethods.hpp:53
void toLower(std::string &str)
Convert a string to lower case.
Definition AuxiliarMethods.cpp:148
const std::string getFolderFromFilePath(const std::string file_path)
Gets the folder path which contains the file <strong class=.
Definition AuxiliarMethods.cpp:303
Tdst narrow_cast(Torg v)
Cast a value, checking that it is not overflowing.
Definition AuxiliarMethods.hpp:414
std::string setStrConstantsCopy(std::string str)
Set several constants into a string.
Definition AuxiliarMethods.cpp:175
std::string toLowerCopy(std::string str)
Convert a string to lower case.
Definition AuxiliarMethods.cpp:154
std::string newFilePath(const std::string &basename, unsigned int &i, unsigned int digits)
Look for a file path which is not already taken.
Definition AuxiliarMethods.cpp:215
const std::string getFileNameFromFilePath(const std::string file_path)
Gets the file name of the path <strong class=.
Definition AuxiliarMethods.cpp:310
bool endswith(const std::string &str, const std::string &prefix)
Alias for hasSuffix()
Definition AuxiliarMethods.hpp:70
std::string xxd2string(unsigned char *arr, unsigned int len)
Transform a xxd exported file into a C++ string.
Definition AuxiliarMethods.cpp:132
std::string ltrimCopy(std::string s)
Remove all the blank spaces (including line breaks, tabulators...) string suffix.
Definition AuxiliarMethods.cpp:111
size_t getGlobalWorkSize(size_t n, size_t local_work_size)
Compute the global work size needed to compute <strong class= threads.
Definition AuxiliarMethods.cpp:358
T roundUp(T x, T divisor)
Rounded up value which is divisible by <strong class=.
Definition AuxiliarMethods.hpp:295
std::string trimCopy(std::string s)
Remove all the blank spaces (including line breaks, tabulators...) string prefix and suffix.
Definition AuxiliarMethods.cpp:125
void ltrim(std::string &s)
Remove all the blank spaces (including line breaks, tabulators...) string prefix.
Definition AuxiliarMethods.cpp:87
unsigned int isPowerOf2(unsigned int n)
Definition AuxiliarMethods.cpp:272
void trim(std::string &s)
Remove all the blank spaces (including line breaks, tabulators...) string prefix and suffix.
Definition AuxiliarMethods.cpp:104
unsigned int numberOfDigits(unsigned int number)
Get the number of digits of an integer decimal text representation.
Definition AuxiliarMethods.cpp:364
float clamp(float x, float a, float b)
Clamps a value between the bounds.
Definition AuxiliarMethods.hpp:400
std::string rtrimCopy(std::string s)
Remove all the blank spaces (including line breaks, tabulators...) string suffix.
Definition AuxiliarMethods.cpp:118
std::vector< std::string > split(std::string str, char chr)
Split a string by a character.
Definition AuxiliarMethods.cpp:182
void rtrim(std::string &s)
Remove all the blank spaces (including line breaks, tabulators...) string suffix.
Definition AuxiliarMethods.cpp:95
std::vector< std::string > split_formulae(std::string str)
Split a list of split_formulae.
Definition AuxiliarMethods.cpp:194
const std::string getExtensionFromFilePath(const std::string file_path)
Gets the file extension.
Definition AuxiliarMethods.cpp:317
size_t getLocalWorkSize(cl_command_queue queue)
Compute the maximum local work size allowed by a device.
Definition AuxiliarMethods.cpp:339
void setStrConstants(std::string &str)
Set several constants into a string.
Definition AuxiliarMethods.cpp:161
const std::string getRootPath()
Get the root path.
Definition AuxiliarMethods.cpp:293
std::string replaceAllCopy(std::string str, std::string search, std::string replace)
Replace all substring occurrences by another substring.
Definition AuxiliarMethods.cpp:80
Tout round(Tin n)
Round an float value to an integer one.
Definition AuxiliarMethods.hpp:311
bool isFile(const std::string file_name)
Check if the file <strong class= exist on the system.
Definition AuxiliarMethods.cpp:324
void replaceAll(std::string &str, const std::string &search, const std::string &replace)
Replace all substring occurrences by another substring.
Definition AuxiliarMethods.cpp:68
bool hasPrefix(const std::string &str, const std::string &prefix)
Check if a string starts with an specific prefix.
Definition AuxiliarMethods.cpp:54
bool isRelativePath(const std::string path)
Check if the path <strong class= is a relative or an absolute one.
Definition AuxiliarMethods.cpp:331
unsigned int nextPowerOf2(unsigned int n)
Definition AuxiliarMethods.cpp:259
bool hasSuffix(const std::string &str, const std::string &suffix)
Check if a string ends with an specific suffix.
Definition AuxiliarMethods.cpp:61
const std::string getExePath()
Get the executable path.
Definition AuxiliarMethods.cpp:278
Set of definitions and macros related with the implementation.
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition sphPrerequisites.hpp:65