mem_utils.txx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <fft_wrapper.hpp>
  12. #include <profile.hpp>
  13. namespace pvfmm{
  14. namespace mem{
  15. #ifdef __INTEL_OFFLOAD
  16. #pragma offload_attribute(push,target(mic))
  17. #endif
  18. // For fundamental data types.
  19. template <class T>
  20. T* aligned_malloc_f(size_t size_, size_t alignment){
  21. assert(alignment <= 0x8000);
  22. size_t size=size_*sizeof(T);
  23. uintptr_t r = (uintptr_t)malloc(size + --alignment + 2);
  24. assert(r!=0);
  25. uintptr_t o = (uintptr_t)(r + 2 + alignment) & ~(uintptr_t)alignment;
  26. if (!r) return NULL;
  27. ((uint16_t*)o)[-1] = (uint16_t)(o-r);
  28. return (T*)o;
  29. //return (T*)fftw_malloc(size);
  30. }
  31. template <class T>
  32. void aligned_free_f(T* p_){
  33. void* p=(void*)p_;
  34. if (!p) return;
  35. free((void*)((uintptr_t)p-((uint16_t*)p)[-1]));
  36. //fftw_free(p);
  37. }
  38. template <class T>
  39. T* aligned_malloc(size_t size_, size_t alignment){
  40. //void* p=aligned_malloc_f<T>(size_,alignment);
  41. //return new(p) T[size_];
  42. T* A=new T[size_];
  43. assert(A!=NULL);
  44. return A;
  45. }
  46. template <>
  47. inline double* aligned_malloc<double>(size_t size_, size_t alignment){
  48. return aligned_malloc_f<double>(size_,alignment);
  49. }
  50. template <>
  51. inline float* aligned_malloc<float>(size_t size_, size_t alignment){
  52. return aligned_malloc_f<float>(size_,alignment);
  53. }
  54. template <>
  55. inline char* aligned_malloc<char>(size_t size_, size_t alignment){
  56. return aligned_malloc_f<char>(size_,alignment);
  57. }
  58. template <class T>
  59. void aligned_free(T* p_){
  60. delete[] p_;
  61. }
  62. template <>
  63. inline void aligned_free<double>(double* p_){
  64. aligned_free_f<double>(p_);
  65. }
  66. template <>
  67. inline void aligned_free<float>(float* p_){
  68. aligned_free_f<float>(p_);
  69. }
  70. template <>
  71. inline void aligned_free<char>(char* p_){
  72. aligned_free_f<char>(p_);
  73. }
  74. inline void * memcopy ( void * destination, const void * source, size_t num ){
  75. if(destination==source || num==0) return destination;
  76. return memcpy ( destination, source, num );
  77. }
  78. #ifdef __INTEL_OFFLOAD
  79. #pragma offload_attribute(pop)
  80. #endif
  81. }//end namespace
  82. }//end namespace