vector.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifdef __INTEL_OFFLOAD
  23. __attribute__ ((target(mic)))
  24. #endif
  25. Device{
  26. Device& operator=(Vector& V){
  27. dim=V.Dim();
  28. dev_ptr=(uintptr_t)&V[0];
  29. return *this;
  30. }
  31. inline T& operator[](size_t j) const{
  32. return ((T*)dev_ptr)[j];
  33. }
  34. size_t dim;
  35. uintptr_t dev_ptr;
  36. };
  37. Vector();
  38. Vector(size_t dim_, T* data_=NULL, bool own_data_=true);
  39. Vector(const Vector& V);
  40. Vector(const std::vector<T>& V);
  41. ~Vector();
  42. void Swap(Vector<T>& v1);
  43. void ReInit(size_t dim_, T* data_=NULL, bool own_data_=true);
  44. Device& AllocDevice(bool copy);
  45. void Device2Host();
  46. void FreeDevice(bool copy);
  47. void Write(const char* fname);
  48. size_t Dim() const;
  49. size_t Capacity() const;
  50. void Resize(size_t dim_, bool fit_size=false);
  51. void SetZero();
  52. Vector& operator=(const Vector& V);
  53. Vector& operator=(const std::vector<T>& V);
  54. T& operator[](size_t j) const;
  55. private:
  56. size_t dim;
  57. size_t capacity;
  58. T* data_ptr;
  59. bool own_data;
  60. Device dev;
  61. };
  62. }//end namespace
  63. #include <vector.txx>
  64. #ifdef __INTEL_OFFLOAD
  65. #pragma offload_attribute(pop)
  66. #endif
  67. #endif //_PVFMM_VECTOR_HPP_