parUtils.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  36. @brief A parallel weighted partitioning function. In our implementation, we
  37. do not pose any restriction on the input or the number of processors. This
  38. function can be used with an odd number of processors as well. Some
  39. processors can pass an empty vector as input. The relative ordering of the
  40. elements is preserved.
  41. @author Hari Sundar
  42. @author Rahul Sampath
  43. @param[in,out] vec the input vector
  44. @param[in] getWeight function pointer to compute the weight of each
  45. element. If you pass NULL, then every element will get a weight equal to 1.
  46. @param[in] comm the communicator
  47. */
  48. template<typename T>
  49. int partitionW(Vector<T>& vec,
  50. long long* wts, const MPI_Comm& comm);
  51. template<typename T>
  52. int partitionW(std::vector<T>& vec,
  53. long long* wts, const MPI_Comm& comm);
  54. /**
  55. @brief A parallel hyper quick sort implementation.
  56. @author Dhairya Malhotra
  57. @param[in] in the input vector
  58. @param[out] out the output vector
  59. @param[in] comm the communicator
  60. */
  61. template<typename T>
  62. int HyperQuickSort(const Vector<T>& in, Vector<T> & out, const MPI_Comm& comm);
  63. template<typename T>
  64. int HyperQuickSort(const std::vector<T>& in, std::vector<T> & out, const MPI_Comm& comm);
  65. template<typename A, typename B>
  66. struct SortPair{
  67. int operator<(const SortPair<A,B>& p1) const{ return key<p1.key;}
  68. A key;
  69. B data;
  70. };
  71. /**
  72. @brief Returns the scatter mapping which will sort the keys.
  73. @author Dhairya Malhotra
  74. @param[in] key the input keys to sort
  75. @param[out] scatter_index the output index vector for the scatter mapping
  76. @param[in] comm the MPI communicator
  77. @param[in] split_key for partitioning of sorted array, optional
  78. */
  79. template<typename T>
  80. int SortScatterIndex(const Vector<T>& key, Vector<size_t>& scatter_index,
  81. const MPI_Comm& comm, const T* split_key=NULL);
  82. /**
  83. @brief Forward scatter data based on scatter index.
  84. @author Dhairya Malhotra
  85. @param[in,out] data the data to scatter
  86. @param[in] scatter_index the index vector for the scatter mapping
  87. @param[in] comm the MPI communicator
  88. */
  89. template<typename T>
  90. int ScatterForward(Vector<T>& data, const Vector<size_t>& scatter_index,
  91. const MPI_Comm& comm);
  92. /**
  93. @brief Reverse scatter data based on scatter index.
  94. @author Dhairya Malhotra
  95. @param[in,out] data the data to scatter
  96. @param[in] scatter_index the index vector for the scatter mapping
  97. @param[in] comm the MPI communicator
  98. @param[in] loc_size the local array size after scatter, optional
  99. */
  100. template<typename T>
  101. int ScatterReverse(Vector<T>& data, const Vector<size_t>& scatter_index,
  102. const MPI_Comm& comm, size_t loc_size=0);
  103. }//end namespace
  104. }//end namespace
  105. #include "parUtils.txx"
  106. #endif //__PVFMM_PAR_UTILS_H_