parUtils.h 4.0 KB

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