-
Notifications
You must be signed in to change notification settings - Fork 3
/
SymmetricMemoryRegion.hpp
53 lines (45 loc) · 1.52 KB
/
SymmetricMemoryRegion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include "Verbs.hpp"
/// This class is a wrapper around the Verbs memory region
/// registration API for regions that start at the same address on
/// each core. The constructor should be called on all cores.
class SymmetricMemoryRegion {
private:
Verbs & v;
std::vector< uint32_t > rkeys;
ibv_mr * mr;
public:
SymmetricMemoryRegion( Verbs & v, void * base, size_t size )
: v(v)
, rkeys( v.m.size )
, mr( nullptr )
{
mr = v.register_memory_region( base, size );
if( !mr ) {
std::cerr << "Memory registration failed at " << base
<< " of size " << size
<< std::endl;
exit(1);
}
// TODO: maybe exchange base addresses?
// exchange rkeys
MPI_CHECK( MPI_Allgather( &mr->rkey, 1, MPI_UINT32_T,
&rkeys[0], 1, MPI_UINT32_T,
v.m.main_communicator_ ) );
// ensure exchange is complete before anybody uses the rkeys
v.m.barrier();
}
~SymmetricMemoryRegion() {
int retval = ibv_dereg_mr( mr );
if( retval != 0 ) {
perror( "Memory deregistration failed" );
exit(1);
}
}
inline void * base() const { return mr->addr; }
inline void * addr() const { return mr->addr; } // synonym of base()
inline size_t size() const { return mr->length; }
inline size_t length() const { return mr->length; } // synonym of size()
inline uint32_t lkey() const { return mr->lkey; }
inline uint32_t rkey( int remote_rank ) const { return rkeys[remote_rank]; }
};