vector.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * \file vector.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 2-11-2011
  5. * \brief This file contains definition of the class Vector.
  6. */
  7. #ifndef _PVFMM_VECTOR_HPP_
  8. #define _PVFMM_VECTOR_HPP_
  9. #include <vector>
  10. #include <iostream>
  11. #include <stdint.h>
  12. #ifdef __INTEL_OFFLOAD
  13. #pragma offload_attribute(push,target(mic))
  14. #endif
  15. namespace pvfmm{
  16. template <class T>
  17. class Vector{
  18. template <class Y>
  19. friend std::ostream& operator<<(std::ostream& output, const Vector<Y>& V);
  20. public:
  21. struct
  22. Device{
  23. Device& operator=(Vector& V){
  24. dim=V.Dim();
  25. dev_ptr=(uintptr_t)&V[0];
  26. return *this;
  27. }
  28. inline T& operator[](size_t j) const{
  29. return ((T*)dev_ptr)[j];
  30. }
  31. size_t dim;
  32. uintptr_t dev_ptr;
  33. };
  34. Vector();
  35. Vector(size_t dim_, T* data_=NULL, bool own_data_=true);
  36. Vector(const Vector& V);
  37. Vector(const std::vector<T>& V);
  38. ~Vector();
  39. void Swap(Vector<T>& v1);
  40. void ReInit(size_t dim_, T* data_=NULL, bool own_data_=true);
  41. Device& AllocDevice(bool copy);
  42. void Device2Host();
  43. void FreeDevice(bool copy);
  44. void Write(const char* fname);
  45. size_t Dim() const;
  46. size_t Capacity() const;
  47. void Resize(size_t dim_, bool fit_size=false);
  48. void SetZero();
  49. Vector& operator=(const Vector& V);
  50. Vector& operator=(const std::vector<T>& V);
  51. T& operator[](size_t j) const;
  52. private:
  53. size_t dim;
  54. size_t capacity;
  55. T* data_ptr;
  56. bool own_data;
  57. Device dev;
  58. };
  59. }//end namespace
  60. #ifdef __INTEL_OFFLOAD
  61. #pragma offload_attribute(pop)
  62. #endif
  63. #include <vector.txx>
  64. #endif //_PVFMM_VECTOR_HPP_