mem_utils.txx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * \file mat_utils.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 11-5-2013
  5. * \brief This file contains implementation of mem_utils.hpp.
  6. */
  7. #include <omp.h>
  8. #include <cassert>
  9. #include <cstring>
  10. #include <stdint.h>
  11. #include <profile.hpp>
  12. #ifdef __INTEL_OFFLOAD
  13. #pragma offload_attribute(push,target(mic))
  14. #endif
  15. namespace pvfmm{
  16. namespace mem{
  17. // For fundamental data types.
  18. template <class T>
  19. T* aligned_malloc_f(size_t size_, size_t alignment){
  20. assert(alignment <= 0x8000);
  21. size_t size=size_*sizeof(T);
  22. uintptr_t r = (uintptr_t)malloc(size + --alignment + 2);
  23. //if (!r) return NULL;
  24. ASSERT_WITH_MSG(r!=0, "malloc failed.");
  25. uintptr_t o = (uintptr_t)(r + 2 + alignment) & ~(uintptr_t)alignment;
  26. ((uint16_t*)o)[-1] = (uint16_t)(o-r);
  27. return (T*)o;
  28. //return (T*)fftw_malloc(size);
  29. }
  30. template <class T>
  31. void aligned_free_f(T* p_){
  32. void* p=(void*)p_;
  33. if (!p) return;
  34. free((void*)((uintptr_t)p-((uint16_t*)p)[-1]));
  35. //fftw_free(p);
  36. }
  37. template <class T>
  38. T* aligned_malloc(size_t size_, size_t alignment){
  39. //void* p=aligned_malloc_f<T>(size_,alignment);
  40. //return new(p) T[size_];
  41. T* A=new T[size_];
  42. assert(A!=NULL);
  43. return A;
  44. }
  45. template <>
  46. inline double* aligned_malloc<double>(size_t size_, size_t alignment){
  47. return aligned_malloc_f<double>(size_,alignment);
  48. }
  49. template <>
  50. inline float* aligned_malloc<float>(size_t size_, size_t alignment){
  51. return aligned_malloc_f<float>(size_,alignment);
  52. }
  53. template <>
  54. inline char* aligned_malloc<char>(size_t size_, size_t alignment){
  55. return aligned_malloc_f<char>(size_,alignment);
  56. }
  57. template <class T>
  58. void aligned_free(T* p_){
  59. delete[] p_;
  60. }
  61. template <>
  62. inline void aligned_free<double>(double* p_){
  63. aligned_free_f<double>(p_);
  64. }
  65. template <>
  66. inline void aligned_free<float>(float* p_){
  67. aligned_free_f<float>(p_);
  68. }
  69. template <>
  70. inline void aligned_free<char>(char* p_){
  71. aligned_free_f<char>(p_);
  72. }
  73. inline void * memcopy ( void * destination, const void * source, size_t num ){
  74. if(destination==source || num==0) return destination;
  75. return memcpy ( destination, source, num );
  76. }
  77. }//end namespace
  78. }//end namespace
  79. #ifdef __INTEL_OFFLOAD
  80. #pragma offload_attribute(pop)
  81. #endif