PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bridge/cpp/bh/bh.hpp

https://bitbucket.org/cruise/bohrium-i-o
C++ Header | 327 lines | 192 code | 80 blank | 55 comment | 1 complexity | d8584e74cade6facecedb65e68ab4d57 MD5 | raw file
  1. /*
  2. This file is part of cphVB and copyright (c) 2012 the cphVB team:
  3. http://bohrium.bitbucket.org
  4. Bohrium is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation, either version 3
  7. of the License, or (at your option) any later version.
  8. Bohrium is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the
  13. GNU Lesser General Public License along with Bohrium.
  14. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __BOHRIUM_BRIDGE_CPP
  17. #define __BOHRIUM_BRIDGE_CPP
  18. #include "bh.h"
  19. #include <complex>
  20. #define BH_CPP_QUEUE_MAX 1000
  21. #include "iterator.hpp"
  22. #include <stdexcept>
  23. #ifdef DEBUG
  24. #define DEBUG_PRINT(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( false )
  25. #else
  26. #define DEBUG_PRINT(...) do{ } while ( false )
  27. #endif
  28. namespace bh {
  29. const double PI_D = 3.141592653589793238462;
  30. const float PI_F = 3.14159265358979f;
  31. const float PI = 3.14159265358979f;
  32. template <typename T> // Forward declaration
  33. class multi_array;
  34. //
  35. // Extensions
  36. //
  37. static bh_intp reduce_id;
  38. static bh_intp random_id;
  39. enum reducible {
  40. ADD = BH_ADD,
  41. MULTIPLY = BH_MULTIPLY,
  42. MIN = BH_MINIMUM,
  43. MAX = BH_MAXIMUM,
  44. LOGICAL_AND = BH_LOGICAL_AND,
  45. LOGICAL_OR = BH_LOGICAL_OR,
  46. BITWISE_AND = BH_BITWISE_AND,
  47. BITWISE_OR = BH_BITWISE_OR
  48. };
  49. //
  50. // Slicing
  51. //
  52. class slice_range {
  53. public:
  54. slice_range();
  55. slice_range(int begin, int end, unsigned int stride);
  56. int begin, end;
  57. unsigned int stride;
  58. };
  59. template <typename T>
  60. class slice {
  61. public:
  62. slice(multi_array<T>& op);
  63. slice& operator[](int rhs);
  64. slice& operator[](slice_range& rhs);
  65. // Create a actual view of the slice
  66. bh::multi_array<T>& view();
  67. private:
  68. multi_array<T>* op; // The op getting sliced
  69. int dims; // The amount of dims covered by the slice
  70. slice_range ranges[BH_MAXDIM]; // The ranges...
  71. };
  72. //
  73. // The Abstraction
  74. //
  75. template <typename T>
  76. class multi_array {
  77. public:
  78. // Constructors:
  79. multi_array();
  80. multi_array( unsigned int n );
  81. multi_array( unsigned int m, unsigned int n );
  82. multi_array( unsigned int d2, unsigned int d1, unsigned int d0 );
  83. multi_array( multi_array<T> const& operand );
  84. // Deconstructor:
  85. ~multi_array();
  86. // Types:
  87. typedef multi_array_iter<T> iterator;
  88. // Iterator
  89. iterator begin();
  90. iterator end();
  91. size_t len();
  92. //
  93. // Operators:
  94. //
  95. // =, [], (), -> must be "internal" (nonstatic member functions) and thus declared here.
  96. //
  97. // Definitions are provided in:
  98. //
  99. // - multi_array.hpp for those implemented by hand ([], ++, --, ostream<< ).
  100. // - slicing.hpp: Auxilary behavior of the [] operator.
  101. // - operators.hpp: defined code-generator.
  102. //
  103. // Slicing / explicit view
  104. slice<T>& operator[](int rhs); // Select a single element / dimension
  105. slice<T>& operator[](slice_range& rhs); // Select a range (begin, end, stride)
  106. multi_array& operator()(const T& n); // Shaping / reshaping
  107. multi_array& operator()(const T& m, const T& n); // Shaping / reshaping
  108. multi_array& operator()(const T& d2, const T& d1, const T& d0); // Shaping / reshaping
  109. multi_array& operator=(const T& rhs); // Initialization / assignment.
  110. multi_array& operator=(multi_array<T>& rhs); // Initialization / assignment.
  111. template <typename In>
  112. multi_array<T>& operator=(multi_array<In>& rhs); // Initialization / assignment.
  113. multi_array& operator=(slice<T>& rhs ); // Initialization / assignment.
  114. multi_array& operator+=(const T& rhs); // Compound assignment / increment
  115. multi_array& operator+=(multi_array& rhs);
  116. multi_array& operator-=(const T& rhs);
  117. multi_array& operator-=(multi_array& rhs);
  118. multi_array& operator*=(const T& rhs);
  119. multi_array& operator*=(multi_array& rhs);
  120. multi_array& operator/=(const T& rhs);
  121. multi_array& operator/=(multi_array &rhs);
  122. multi_array& operator%=(const T& rhs);
  123. multi_array& operator%=(multi_array &rhs);
  124. multi_array& operator>>=(const T& rhs);
  125. multi_array& operator>>=(multi_array& rhs);
  126. multi_array& operator<<=(const T& rhs);
  127. multi_array& operator<<=(multi_array& rhs);
  128. multi_array& operator&=(const T& rhs);
  129. multi_array& operator&=(multi_array& rhs);
  130. multi_array& operator^=(const T& rhs);
  131. multi_array& operator^=(multi_array& rhs);
  132. multi_array& operator|=(const T& rhs);
  133. multi_array& operator|=(multi_array& rhs);
  134. multi_array& operator++(); // Increment all elements in container
  135. multi_array& operator++(int);
  136. multi_array& operator--(); // Decrement all elements in container
  137. multi_array& operator--(int);
  138. multi_array<T>& copy(); // Explicity create a copy of array
  139. multi_array<T>& flatten(); // Create a flat copy of the array
  140. template <typename Ret> // Typecast, creates a copy.
  141. multi_array<Ret>& as();
  142. // Extensions
  143. multi_array<T>& reduce(reducible op, unsigned int axis);
  144. // This stuff should not be used by the regular user...
  145. unsigned int unlink();
  146. unsigned int getKey() const;
  147. unsigned long getRank() const;
  148. bool getTemp() const;
  149. void setTemp(bool temp);
  150. protected:
  151. unsigned int key;
  152. bool temp;
  153. bool linked;
  154. private:
  155. void init();
  156. };
  157. /**
  158. * Encapsulation of communication with Bohrium runtime.
  159. * Implemented as a singleton.
  160. *
  161. * Note: not thread-safe.
  162. */
  163. class Runtime {
  164. public:
  165. static Runtime* instance();
  166. ~Runtime();
  167. // Input and output are of the same type
  168. template <typename T> // SYS: FREE, SYNC, DISCARD;
  169. void enqueue(bh_opcode opcode, multi_array<T>& op0);
  170. template <typename T> // x = y + z
  171. void enqueue(bh_opcode opcode, multi_array<T>& op0, multi_array<T> & op1, multi_array<T> & op2);
  172. template <typename T> // x = y + 1;
  173. void enqueue(bh_opcode opcode, multi_array<T>& op0, multi_array<T> & op1, const T& op2);
  174. template <typename T> // x = 1 + y;
  175. void enqueue(bh_opcode opcode, multi_array<T>& op0, const T& op1, multi_array<T> & op2);
  176. template <typename T> // x = y;
  177. void enqueue(bh_opcode opcode, multi_array<T>& op0, multi_array<T> & op1);
  178. template <typename T> // x = 1.0;
  179. void enqueue(bh_opcode opcode, multi_array<T>& op0, const T& op1);
  180. // Same input but different output type
  181. template <typename Ret, typename In> // x = y;
  182. void enqueue(bh_opcode opcode, multi_array<Ret>& op0, multi_array<In>& op1);
  183. template <typename Ret, typename In> // x = y < z
  184. void enqueue(bh_opcode opcode, multi_array<Ret>& op0, multi_array<In>& op1, multi_array<In>& op2);
  185. template <typename Ret, typename In> // x = y < 1;
  186. void enqueue(bh_opcode opcode, multi_array<Ret>& op0, multi_array<In>& op1, const In& op2);
  187. template <typename Ret, typename In> // x = 1 < y;
  188. void enqueue(bh_opcode opcode, multi_array<Ret>& op0, const In& op1, multi_array<In>& op2);
  189. template <typename T> // Userfunc / extensions
  190. void enqueue(bh_userfunc* rinstr);
  191. bh_intp flush();
  192. bh_intp get_queue_size();
  193. template <typename T>
  194. multi_array<T>& op();
  195. template <typename T>
  196. multi_array<T>& temp();
  197. template <typename T>
  198. multi_array<T>& temp(multi_array<T>& input);
  199. template <typename T>
  200. multi_array<T>& view(multi_array<T>& base);
  201. template <typename T>
  202. multi_array<T>& temp_view(multi_array<T>& base);
  203. bh_intp guard();
  204. private:
  205. static Runtime* pInstance; // Singleton instance pointer.
  206. bh_instruction queue[BH_CPP_QUEUE_MAX]; // Bytecode queue
  207. bh_userfunc *ext_queue[BH_CPP_QUEUE_MAX];
  208. bh_intp ext_in_queue;
  209. bh_intp queue_size;
  210. bh_init vem_init; // Bohrium interface
  211. bh_execute vem_execute;
  212. bh_shutdown vem_shutdown;
  213. bh_reg_func vem_reg_func;
  214. bh_component **components, // Bohrium component setup
  215. *self_component,
  216. *vem_component;
  217. bh_intp children_count;
  218. Runtime(); // Ensure no external instantiation.
  219. };
  220. template <typename T> // These should be "generators"...
  221. multi_array<T>& empty();
  222. template <typename T>
  223. multi_array<T>& zeros();
  224. template <typename T>
  225. multi_array<T>& ones();
  226. template <typename T>
  227. multi_array<T>& arange();
  228. template <typename T>
  229. multi_array<T>& random();
  230. template <typename T>
  231. multi_array<T>& random(int n);
  232. template <typename T>
  233. void pprint(multi_array<T>& op);
  234. }
  235. #include "multi_array.hpp" // Operand definition.
  236. #include "broadcast.hpp" // Operand manipulations.
  237. #include "slicing.hpp" // Operand slicing / explicit views / aliases
  238. #include "runtime.hpp" // Communication with Bohrium runtime
  239. #include "extensions.hpp" // Communication with Bohrium runtime
  240. #include "operators.hpp" // DSEL Operations via operator-overloads.
  241. #include "functions.hpp" // DSEL Operations via functions.
  242. #include "sugar.hpp" // DSEL Additional sugar...
  243. #endif