Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #ifndef PC_DEVICE_PARALLEL_SCATTERED_VECTOR_HPP
  2. #define PC_DEVICE_PARALLEL_SCATTERED_VECTOR_HPP
  3.  
  4. #include "pc/defines.hpp"
  5.  
  6. #include <iostream>
  7.  
  8. #include <boost/shared_ptr.hpp>
  9.  
  10. #include "pc/misc/vector_traits.hpp"
  11. #include "pc/device/all_devices.hpp"
  12.  
  13. using namespace pc::misc;
  14. using namespace pc::device;
  15.  
  16. namespace pc { namespace device { namespace parallel
  17. {
  18. template <class VectorType>
  19. struct GPUProxyVector
  20. {
  21. typedef VectorType vector_type;
  22. typedef boost::shared_ptr<vector_type> vector_ptr_type;
  23. typedef pc::misc::VectorTraits<vector_type> vector_traits_type;
  24. typedef typename vector_traits_type::value_type value_type;
  25. typedef pc::device::WorkerManager<GPU> workers_type;
  26. typedef boost::shared_ptr<workers_type> workers_ptr_type;
  27.  
  28. protected:
  29.  
  30.  
  31.  
  32. private:
  33.  
  34. workers_ptr_type workers_;
  35. DEVICE_MEMOREIS(value_type, GPU) values_;
  36. };
  37.  
  38. template <class HType, class DType>
  39. struct ScatteredBase
  40. {
  41. typedef HType h_type;
  42. typedef boost::shared_ptr<h_type> h_ptr_type;
  43. typedef DType d_type;
  44. typedef boost::shared_ptr<d_type> d_ptr_type;
  45. typedef pc::device::WorkerManager<GPU> workers_type;
  46. typedef boost::shared_ptr<workers_type> workers_ptr_type;
  47.  
  48. ScatteredBase()
  49. : hDirty_(false)
  50. , dDirty_(false)
  51. {
  52. }
  53.  
  54. ScatteredBase(const h_ptr_type & h)
  55. : h_(h)
  56. , hDirty_(true)
  57. , dDirty_(false)
  58. {
  59. }
  60.  
  61. const h_type & constGet() const
  62. {
  63. #ifdef PARALLEL_DEBUG
  64. std::cout << ">> constGet()" << std::endl;
  65. #endif
  66. return *h_;
  67. }
  68.  
  69. h_type & get()
  70. {
  71. #ifdef PARALLEL_DEBUG
  72. std::cout << ">> get()" << std::endl;
  73. #endif
  74. return *h_;
  75. }
  76.  
  77. const d_type & constGetDeviceProxy() const
  78. {
  79. #ifdef PARALLEL_DEBUG
  80. std::cout << ">> constGetDeviceProxy()" << std::endl;
  81. #endif
  82. return *d_;
  83. }
  84.  
  85. d_type & getDeviceProxy()
  86. {
  87. #ifdef PARALLEL_DEBUG
  88. std::cout << ">> getDeviceProxy()" << std::endl;
  89. #endif
  90. return *d_;
  91. }
  92.  
  93. protected:
  94.  
  95. void setH(const h_ptr_type & h)
  96. {
  97. h_ = h;
  98. hDirty_ = true;
  99. }
  100.  
  101. void sync(bool isConstAccess)
  102. {
  103. ASSERT(!(hDirty_ && dDirty_), "both is dirty, lost!");
  104.  
  105. if (!hDirty_ && !dDirty_) return;
  106.  
  107. if (hDirty_)
  108. {
  109. ASSERT(h_, "invalid h, but h is set to dirty");
  110. // send to workers...
  111. hDirty_ = !isConstAccess;
  112.  
  113. void sync(bool isConstAccess)
  114. {
  115. #ifdef HAVE_GPU
  116.  
  117. // user WorkerManager<GPU> to copy to and from device
  118. // workers->scatter
  119. ASSERT(!(hDirty_ && dDirty_), "error");
  120.  
  121. if(!hDirty_ && !dDirty_) return;
  122.  
  123. if(hDirty)
  124. {
  125. workers->recv...;
  126. hDirty_ = !isConstAccess;
  127. }
  128.  
  129. if(dDirty_)
  130. {
  131. workers->send...;
  132. dDirty_ = !isConstAccess;
  133. }
  134.  
  135. #endif
  136. }
  137. private:
  138.  
  139. bool hDirty_;
  140. bool dDirty_;
  141. h_ptr_type h_;
  142. d_ptr_type d_;
  143. workers_ptr_type workers_;
  144. };
  145.  
  146. template <class HType>
  147. struct ScatteredVector : public ScatteredBase<HType>
  148. {
  149. typedef HType vector_type;
  150. typedef boost::shared_ptr<vector_type> vector_ptr_type;
  151. typedef pc::misc::VectorTraits<vector_type> vector_traits_type;
  152. typedef typename vector_traits_type::value_type value_type;
  153.  
  154. /*!
  155. @brief C'tor for host only
  156. */
  157. ScatteredVector(unsigned int length)
  158. {
  159. set_h(traits_type::makePtr(length));
  160. }
  161.  
  162. /*!
  163. @brief C'tor for host only
  164. */
  165. ScatteredVector(const vector_ptr_type & v)
  166. : ScatteredBase(v)
  167. {
  168. }
  169.  
  170. };
  171.  
  172. }}} // namespaces
  173.  
  174. #endif
Add Comment
Please, Sign In to add comment