vector.hpp 1.6 KB

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