parUtils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. @file parUtils.h
  3. @brief A set of parallel utilities.
  4. @author Rahul S. Sampath, rahul.sampath@gmail.com
  5. @author Hari Sundar, hsundar@gmail.com
  6. @author Shravan Veerapaneni, shravan@seas.upenn.edu
  7. @author Santi Swaroop Adavani, santis@gmail.com
  8. @author Dhairya Malhotra, dhairya.malhotra@gmail.com
  9. */
  10. #ifndef __PVFMM_PAR_UTILS_H_
  11. #define __PVFMM_PAR_UTILS_H_
  12. #include "mpi.h"
  13. #include <vector>
  14. #include <vector.hpp>
  15. /**
  16. @namespace par
  17. @author Rahul Sampath
  18. @author Hari Sundar
  19. @brief Collection of Generic Parallel Functions: Sorting, Partitioning, Searching,...
  20. */
  21. namespace pvfmm{
  22. namespace par{
  23. /**
  24. @author Rahul S. Sampath
  25. */
  26. template <typename T>
  27. int Mpi_Alltoallv_sparse(T* sendbuf, int* sendcnts, int* sdispls,
  28. T* recvbuf, int* recvcnts, int* rdispls, const MPI_Comm& comm);
  29. /**
  30. @author Rahul S. Sampath
  31. */
  32. template <typename T>
  33. int Mpi_Alltoallv_dense(T* sendbuf, int* sendcnts, int* sdispls,
  34. T* recvbuf, int* recvcnts, int* rdispls, const MPI_Comm& comm);
  35. template<typename T>
  36. unsigned int defaultWeight(const T *a);
  37. /**
  38. @brief A parallel weighted partitioning function. In our implementation, we
  39. do not pose any restriction on the input or the number of processors. This
  40. function can be used with an odd number of processors as well. Some
  41. processors can pass an empty vector as input. The relative ordering of the
  42. elements is preserved.
  43. @author Hari Sundar
  44. @author Rahul Sampath
  45. @param[in,out] vec the input vector
  46. @param[in] getWeight function pointer to compute the weight of each
  47. element. If you pass NULL, then every element will get a weight equal to 1.
  48. @param[in] comm the communicator
  49. */
  50. template<typename T>
  51. int partitionW(Vector<T>& vec,
  52. unsigned int (*getWeight)(const T *), const MPI_Comm& comm);
  53. template<typename T>
  54. int partitionW(std::vector<T>& vec,
  55. unsigned int (*getWeight)(const T *), const MPI_Comm& comm);
  56. /**
  57. @brief A parallel hyper quick sort implementation.
  58. @author Dhairya Malhotra
  59. @param[in] in the input vector
  60. @param[out] out the output vector
  61. @param[in] comm the communicator
  62. */
  63. template<typename T>
  64. int HyperQuickSort(const Vector<T>& in, Vector<T> & out, const MPI_Comm& comm);
  65. template<typename T>
  66. int HyperQuickSort(const std::vector<T>& in, std::vector<T> & out, const MPI_Comm& comm);
  67. template<typename A, typename B>
  68. struct SortPair{
  69. int operator<(const SortPair<A,B>& p1) const{ return key<p1.key;}
  70. A key;
  71. B data;
  72. };
  73. /**
  74. @brief Returns the scatter mapping which will sort the keys.
  75. @author Dhairya Malhotra
  76. @param[in] key the input keys to sort
  77. @param[out] scatter_index the output index vector for the scatter mapping
  78. @param[in] comm the MPI communicator
  79. @param[in] split_key for partitioning of sorted array, optional
  80. */
  81. template<typename T>
  82. int SortScatterIndex(const Vector<T>& key, Vector<size_t>& scatter_index,
  83. const MPI_Comm& comm, const T* split_key=NULL);
  84. /**
  85. @brief Forward scatter data based on scatter index.
  86. @author Dhairya Malhotra
  87. @param[in,out] data the data to scatter
  88. @param[in] scatter_index the index vector for the scatter mapping
  89. @param[in] comm the MPI communicator
  90. */
  91. template<typename T>
  92. int ScatterForward(Vector<T>& data, const Vector<size_t>& scatter_index,
  93. const MPI_Comm& comm);
  94. /**
  95. @brief Reverse scatter data based on scatter index.
  96. @author Dhairya Malhotra
  97. @param[in,out] data the data to scatter
  98. @param[in] scatter_index the index vector for the scatter mapping
  99. @param[in] comm the MPI communicator
  100. @param[in] loc_size the local array size after scatter, optional
  101. */
  102. template<typename T>
  103. int ScatterReverse(Vector<T>& data, const Vector<size_t>& scatter_index,
  104. const MPI_Comm& comm, size_t loc_size=0);
  105. }//end namespace
  106. }//end namespace
  107. #include "parUtils.txx"
  108. #endif //__PVFMM_PAR_UTILS_H_