Line data Source code
1 : //
2 : // Class Status
3 : // A communication status handle for non-blocking communication.
4 : //
5 : #ifndef IPPL_MPI_STATUS_H
6 : #define IPPL_MPI_STATUS_H
7 :
8 : #include <mpi.h>
9 : #include <optional>
10 :
11 : namespace ippl {
12 : namespace mpi {
13 :
14 : class Status {
15 : public:
16 0 : Status()
17 0 : : status_m()
18 0 : , count_m(-1){};
19 :
20 : Status(const Status&) = default;
21 :
22 : Status& operator=(Status& other) = default;
23 :
24 : int source() const noexcept { return status_m.MPI_SOURCE; }
25 :
26 : int tag() const noexcept { return status_m.MPI_TAG; }
27 :
28 : int error() const noexcept { return status_m.MPI_ERROR; }
29 :
30 : template <typename T>
31 : std::optional<int> count();
32 :
33 : // https://en.cppreference.com/w/cpp/language/cast_operator
34 0 : operator MPI_Status*() noexcept { return &status_m; }
35 :
36 : operator const MPI_Status*() const noexcept { return &status_m; }
37 :
38 : private:
39 : MPI_Status status_m;
40 : int count_m;
41 : };
42 :
43 : template <typename T>
44 : std::optional<int> Status::count() {
45 : if (count_m != -1) {
46 : return count_m;
47 : }
48 :
49 : int count = MPI_UNDEFINED;
50 : MPI_Datatype datatype = get_mpi_datatype<T>(T());
51 : MPI_Get_count(&status_m, datatype, &count);
52 :
53 : if (count == MPI_UNDEFINED) {
54 : return std::optional<int>();
55 : }
56 : count_m = count;
57 : return count_m;
58 : }
59 : } // namespace mpi
60 : } // namespace ippl
61 :
62 : #endif
|