SkePU  1.2
 All Classes Namespaces Files Functions Variables 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 {
19 
20 
21 template<typename T>
22 std::string convertToStr(T val)
23 {
24  std::ostringstream ss;
25  ss << val;
26  return ss.str();
27 }
28 
32 static const std::string trimSpaces(const std::string& pString, const std::string& pWhitespace = " \t")
33 {
34  const size_t beginStr = pString.find_first_not_of(pWhitespace);
35  if (beginStr == std::string::npos)
36  {
37  // no content
38  return "";
39  }
40 
41  const size_t endStr = pString.find_last_not_of(pWhitespace);
42  const size_t range = endStr - beginStr + 1;
43 
44  return pString.substr(beginStr, range);
45 }
46 
47 
51 template <typename T>
52 inline T get_random_number(T min, T max)
53 {
54  return (T)( rand() % (int)(max-min+1) + min); //(T)( rand() % (int)max + min );
55 // return min + (T)rand()/((T)RAND_MAX/(max-min)); //(T)( rand() % (int)max + min );
56 }
57 
58 
62 static std::string read_file_into_string(const std::string &filename)
63 {
64  std::string content = "";
65  std::ifstream ifs(filename.c_str());
66  if(ifs.is_open())
67  {
68  content.assign( (std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()) );
69  }
70  return content;
71 }
72 
76 static void toLowerCase(std::string &str)
77 {
78  std::transform(str.begin(), str.end(),str.begin(), ::tolower);
79 }
80 
84 static void toUpperCase(std::string &str)
85 {
86  std::transform(str.begin(), str.end(),str.begin(), ::toupper);
87 }
88 
89 
93 static bool startsWith(const std::string& main, const std::string& prefix)
94 {
95  return (main.substr(0, prefix.size()) == prefix);
96 }
97 
98 
99 
103 template<typename T>
104 void allocateHostMemory(T* &data, const size_t numElems)
105 {
106  #if defined(SKEPU_CUDA) && defined(USE_PINNED_MEMORY)
107  cudaError_t status = cudaMallocHost((void**)&data, numElems*sizeof(T));
108  if (status != cudaSuccess)
109  {
110  SKEPU_ERROR("Error allocating pinned host memory\n");
111  }
112  #else
113  data = new T[numElems];
114  if(!data)
115  SKEPU_ERROR("Memory allocation failed\n");
116  #endif
117 }
118 
122 template<typename T>
123 void deallocateHostMemory(T *data)
124 {
125  if(!data)
126  return;
127 
128  #if defined(SKEPU_CUDA) && defined(USE_PINNED_MEMORY)
129  cudaError_t status = cudaFreeHost(data);
130  if (status != cudaSuccess)
131  {
132  SKEPU_ERROR("Error de-allocating pinned host memory.\n");
133  }
134  #else
135  delete[] data;
136  #endif
137 }
138 
139 
140 
141 
142 
143 
144 }
145 
146 
147 #endif
148 
static void toLowerCase(std::string &str)
Definition: helper_methods.h:76
void allocateHostMemory(T *&data, const size_t numElems)
Definition: helper_methods.h:104
static std::string read_file_into_string(const std::string &filename)
Definition: helper_methods.h:62
static const std::string trimSpaces(const std::string &pString, const std::string &pWhitespace=" \t")
Definition: helper_methods.h:32
T min(T a, T b)
Definition: mapoverlap_convol_kernels.h:212
static void toUpperCase(std::string &str)
Definition: helper_methods.h:84
void deallocateHostMemory(T *data)
Definition: helper_methods.h:123
T max(T a, T b)
Definition: mapoverlap_convol_kernels.h:203
static bool startsWith(const std::string &main, const std::string &prefix)
Definition: helper_methods.h:93
T get_random_number(T min, T max)
Definition: helper_methods.h:52