TIMPI
Loading...
Searching...
No Matches
test
message_tag_unit.C
Go to the documentation of this file.
1
#include <
timpi/communicator.h
>
2
#include <
timpi/message_tag.h
>
3
#include <
timpi/timpi_init.h
>
4
5
#define TIMPI_UNIT_ASSERT(expr) \
6
if (!(expr)) \
7
timpi_error();
8
9
using namespace
TIMPI
;
10
11
Communicator
*
TestCommWorld
;
12
13
void
testGetUniqueTagAuto
()
14
{
15
// We need to duplicate the communicator first, because the
16
// original might already have tags used by other unit tests
17
18
TIMPI::Communicator
newcomm
;
19
20
TestCommWorld
->
duplicate
(
newcomm
);
21
22
const
int
n_vals
= 5;
23
const
int
n_vals_in_scope
= 3;
24
std::vector<int>
vals
(
n_vals
);
25
26
{
27
std::vector<TIMPI::MessageTag>
tags
(
n_vals_in_scope
);
28
for
(
int
i
=0;
i
!=
n_vals_in_scope
; ++
i
)
29
{
30
tags
[
i
] =
newcomm
.get_unique_tag();
31
vals
[
i
] =
tags
[
i
].value();
32
for
(
int
j
=0;
j
!=
i
; ++
j
)
33
{
34
TIMPI_UNIT_ASSERT
(
vals
[
i
] !=
vals
[
j
]);
35
}
36
}
37
}
38
39
// Even after we go out of scope those values should be used up
40
for
(
int
i
=
n_vals_in_scope
;
i
!=
n_vals
; ++
i
)
41
{
42
TIMPI::MessageTag
another_tag
=
newcomm
.get_unique_tag();
43
vals
[
i
] =
another_tag
.
value
();
44
for
(
int
j
=0;
j
!=
i
; ++
j
)
45
{
46
TIMPI_UNIT_ASSERT
(
vals
[
i
] !=
vals
[
j
]);
47
}
48
}
49
}
50
51
52
53
void
testGetUniqueTagManual
()
54
{
55
// Here we'll use the standard communicator, because even if it
56
// used these tags in other contexts it should have freed them for
57
// reuse later.
58
59
const
int
requests
[] = {2, 4, 6, 8, 8, 6, 8, 123, 3141, 3142};
60
61
for
(
const
int
i
:
requests
)
62
{
63
TIMPI::MessageTag
manual_tag
=
64
TestCommWorld
->
get_unique_tag
(
i
);
65
TIMPI_UNIT_ASSERT
(
i
==
manual_tag
.value());
66
67
TIMPI::MessageTag
tag_copy
=
manual_tag
;
68
TIMPI_UNIT_ASSERT
(
i
==
tag_copy
.value());
69
70
TIMPI::MessageTag
tag_move
= std::move(
tag_copy
);
71
TIMPI_UNIT_ASSERT
(
i
==
tag_move
.value());
72
73
TIMPI::MessageTag
stupidly_manual_tag
(
i
);
74
TIMPI_UNIT_ASSERT
(
i
==
stupidly_manual_tag
.value());
75
}
76
}
77
78
79
void
testGetUniqueTagOverlap
()
80
{
81
// Try to request the same tag twice, make sure we don't get it
82
// the second time.
83
84
const
int
requests
[] = {2, 4, 6, 8, 8, 6, 8, 123, 3141, 3142};
85
86
for
(
const
int
i
:
requests
)
87
{
88
TIMPI::MessageTag
manual_tag
=
89
TestCommWorld
->
get_unique_tag
(
i
);
90
TIMPI_UNIT_ASSERT
(
i
==
manual_tag
.value());
91
92
TIMPI::MessageTag
dup_manual_tag
=
93
TestCommWorld
->
get_unique_tag
(
i
);
94
TIMPI_UNIT_ASSERT
(
i
!=
dup_manual_tag
.value());
95
96
TIMPI::MessageTag
tag_copy
=
manual_tag
;
97
TIMPI_UNIT_ASSERT
(
i
==
tag_copy
.value());
98
99
TIMPI::MessageTag
tag_move
= std::move(
tag_copy
);
100
TIMPI_UNIT_ASSERT
(
i
==
tag_move
.value());
101
}
102
}
103
104
int
main
(
int
argc
,
const
char
*
const
*
argv
)
105
{
106
TIMPI::TIMPIInit
init
(
argc
,
argv
);
107
TestCommWorld
= &
init
.comm();
108
109
testGetUniqueTagAuto
();
110
testGetUniqueTagManual
();
111
testGetUniqueTagOverlap
();
112
113
return
0;
114
}
TIMPI::Communicator
Encapsulates the MPI_Comm object.
Definition
communicator.h:109
TIMPI::Communicator::get_unique_tag
MessageTag get_unique_tag(int tagvalue=MessageTag::invalid_tag) const
Get a tag that is unique to this Communicator.
Definition
communicator.C:251
TIMPI::Communicator::duplicate
void duplicate(const Communicator &comm)
Definition
communicator.C:137
TIMPI::MessageTag
Encapsulates the MPI tag integers.
Definition
message_tag.h:47
TIMPI::MessageTag::value
int value() const
Definition
message_tag.h:92
TIMPI::TIMPIInit
The TIMPIInit class, when constructed, initializes any dependent libraries (e.g.
Definition
timpi_init.h:58
communicator.h
message_tag.h
TestCommWorld
Communicator * TestCommWorld
Definition
message_tag_unit.C:11
testGetUniqueTagManual
void testGetUniqueTagManual()
Definition
message_tag_unit.C:53
testGetUniqueTagAuto
void testGetUniqueTagAuto()
Definition
message_tag_unit.C:13
testGetUniqueTagOverlap
void testGetUniqueTagOverlap()
Definition
message_tag_unit.C:79
TIMPI
Definition
parallel_sync.h:36
TIMPI::cast_int
Tnew cast_int(Told oldvar)
Definition
timpi_assert.h:297
timpi_init.h
main
int main()
Definition
version.C:20
Generated on Fri Aug 21 2026 13:32:13 for TIMPI by
1.9.8