SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
helper_methods.h
Go to the documentation of this file.
1 
9 #ifndef _HELPER_METHODS_H
10 #define _HELPER_METHODS_H
11 
12 #include <string>
13 #include <algorithm>
14 #include <fstream>
15 #include <sstream>
16 
17 namespace skepu {
18 
22 const std::string trimSpaces(const std::string& pString, const std::string& pWhitespace = " \t")
23 {
24  const size_t beginStr = pString.find_first_not_of(pWhitespace);
25  if (beginStr == std::string::npos)
26  {
27  // no content
28  return "";
29  }
30 
31  const size_t endStr = pString.find_last_not_of(pWhitespace);
32  const size_t range = endStr - beginStr + 1;
33 
34  return pString.substr(beginStr, range);
35 }
36 
37 template <class T> const T& max ( const T& a, const T& b ) {
38  return (b<a)?a:b; // or: return comp(b,a)?a:b; for the comp version
39 }
40 
41 
45 template <typename T>
46 inline T get_random_number(T min, T max)
47 {
48  return (T)( rand() % (int)(max-min+1) + min); //(T)( rand() % (int)max + min );
49 }
50 
51 
55 std::string read_file_into_string(const std::string &filename)
56 {
57  std::string content = "";
58  std::ifstream ifs(filename.c_str());
59  if(ifs.is_open())
60  {
61  content.assign( (std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()) );
62  }
63  return content;
64 }
65 
69 void toLowerCase(std::string &str)
70 {
71  std::transform(str.begin(), str.end(),str.begin(), ::tolower);
72 }
73 
77 void toUpperCase(std::string &str)
78 {
79  std::transform(str.begin(), str.end(),str.begin(), ::toupper);
80 }
81 
82 
86 bool startsWith(const std::string& main, const std::string& prefix)
87 {
88  return (main.substr(0, prefix.size()) == prefix);
89 }
90 
91 
95 template <typename T>
96 T* allocateHostMemory(unsigned int n)
97 {
98  T* p=0;
99 
100  #if defined(SKEPU_CUDA) && defined(USE_PINNED_MEMORY)
101  cudaError_t err;
102  DEBUG_TEXT_LEVEL1("Pinned memory allocation ******\n")
103 // err = cudaMallocHost((void **)&p, n * sizeof(T)); // Works before CUDA 4.0
104  err = cudaHostAlloc((void **)&p, n * sizeof(T), cudaHostAllocPortable); // For CUDA 4.0
105 // err = cudaHostAlloc((void **)&p, n * sizeof(T)); // For CUDA 4.0
106 
107  if(err)
108  {
109  std::cerr<<"*** Error Malloc: "<< cudaGetErrorString(err)<<"\n";
110  throw std::bad_alloc();
111  }
112  #else
113  p = new T[n];
114  #endif
115 
116  if (!p)
117  {
118  std::cerr<<"*** Error Malloc **************\n";
119  throw std::bad_alloc();
120  }
121  return p;
122 }
123 
124 
128 template <typename T>
130 {
131  #if defined(SKEPU_CUDA) && defined(USE_PINNED_MEMORY)
132 
133  cudaError_t err;
134  err = cudaFreeHost(p);
135  if(err)
136  {
137  std::cerr<<"*** Error De-Alloc: "<< cudaGetErrorString(err) <<"\n";
138  throw std::bad_alloc();
139  }
140  else
141  {
142  DEBUG_TEXT_LEVEL1("Pinned memory de-allocation Successful ******\n")
143  }
144 
145  #else
146  if(p) delete[] p;
147  #endif
148 }
149 
150 
151 }
152 
153 
154 #endif
155 
bool startsWith(const std::string &main, const std::string &prefix)
Definition: helper_methods.h:86
void toUpperCase(std::string &str)
Definition: helper_methods.h:77
void deallocateHostMemory(T *p)
Definition: helper_methods.h:129
const std::string trimSpaces(const std::string &pString, const std::string &pWhitespace=" \t")
Definition: helper_methods.h:22
T get_random_number(T min, T max)
Definition: helper_methods.h:46
T * allocateHostMemory(unsigned int n)
Definition: helper_methods.h:96
void toLowerCase(std::string &str)
Definition: helper_methods.h:69
std::string read_file_into_string(const std::string &filename)
Definition: helper_methods.h:55