Grid 0.7.0
SharedMemory.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/communicator/SharedMemory.cc
6
7 Copyright (C) 2015
8
9Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26*************************************************************************************/
27/* END LEGAL */
28#pragma once
29
30#include <Grid/GridCore.h>
31
32#if defined (GRID_COMMS_MPI3)
33#include <mpi.h>
34#endif
35#include <semaphore.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <limits.h>
39#include <sys/types.h>
40#include <sys/ipc.h>
41#include <sys/shm.h>
42#include <sys/mman.h>
43#include <zlib.h>
44
46
47#if defined (GRID_COMMS_MPI3)
48typedef MPI_Comm Grid_MPI_Comm;
49typedef MPI_Request MpiCommsRequest_t;
50#ifdef ACCELERATOR_AWARE_MPI
51typedef MPI_Request CommsRequest_t;
52#else
53/*
54 * Enable state transitions as each packet flows.
55 */
56enum PacketType_t {
57 FaceGather,
58 InterNodeXmit,
59 InterNodeRecv,
60 IntraNodeXmit,
61 IntraNodeRecv,
62 InterNodeXmitISend,
63 InterNodeReceiveHtoD
64};
65/*
66 *Package arguments needed for various actions along packet flow
67 */
68typedef struct {
69 PacketType_t PacketType;
70 void *host_buf;
71 void *device_buf;
72 int dest;
73 int tag;
74 int commdir;
75 unsigned long bytes;
79#endif
80
81#else
83typedef int CommsRequest_t;
84typedef int Grid_MPI_Comm;
85#endif
86
88private:
89 static const int MAXLOG2RANKSPERNODE = 16;
90
91
92 // Init once lock on the buffer allocation
93 static int _ShmSetup;
94 static int _ShmAlloc;
95 static uint64_t _ShmAllocBytes;
96
97public:
99 // HPE 8600 hypercube optimisation
101 static int HPEhypercube;
102
103 static int ShmSetup(void) { return _ShmSetup; }
104 static int ShmAlloc(void) { return _ShmAlloc; }
105 static uint64_t ShmAllocBytes(void) { return _ShmAllocBytes; }
106 static uint64_t MAX_MPI_SHM_BYTES;
107 static int Hugepages;
108
109 static std::vector<void *> WorldShmCommBufs;
110#ifndef ACCELERATOR_AWARE_MPI
111 static void *HostCommBuf;
112#endif
114 static int WorldRank;
115 static int WorldSize;
116
118 static int WorldShmRank;
119 static int WorldShmSize;
120
121 static int WorldNodes;
122 static int WorldNode;
123
124 static std::vector<int> WorldShmRanks;
125
127 // Create an optimal reordered communicator that makes MPI_Cart_create get it right
129 static void Init(Grid_MPI_Comm comm); // Typically MPI_COMM_WORLD
130 // Turns MPI_COMM_WORLD into right layout for Cartesian
131 static void OptimalCommunicator (const Coordinate &processors,Grid_MPI_Comm & optimal_comm,Coordinate &ShmDims);
132 static void OptimalCommunicatorHypercube (const Coordinate &processors,Grid_MPI_Comm & optimal_comm,Coordinate &ShmDims);
133 static void OptimalCommunicatorSharedMemory(const Coordinate &processors,Grid_MPI_Comm & optimal_comm,Coordinate &ShmDims);
134 static void GetShmDims(const Coordinate &WorldDims,Coordinate &ShmDims);
136 // Provide shared memory facilities off comm world
138 static void SharedMemoryAllocate(uint64_t bytes, int flags);
139 static void SharedMemoryFree(void);
140 // static void SharedMemoryCopy(void *dest,void *src,size_t bytes);
141 static void SharedMemoryZero(void *dest,size_t bytes);
142
143};
144
146// one per communicator
149{
150private:
151 static const int MAXLOG2RANKSPERNODE = 16;
152
153 size_t heap_top;
155 size_t heap_size;
156
157#ifndef ACCELERATOR_AWARE_MPI
158 size_t host_heap_top; // set in free all
159 size_t host_heap_bytes;// set in free all
160 void *HostCommBuf; // set in SetCommunicator
161 size_t host_heap_size; // set in SetCommunicator
162#endif
163
164protected:
165
166 Grid_MPI_Comm ShmComm; // for barriers
169 std::vector<void *> ShmCommBufs;
170 std::vector<int> ShmRanks;// Mapping comm ranks to Shm ranks
171
172public:
176 // set the buffers & sizes
179
181 // For this instance ; disjoint buffer sets between splits if split grid
183 void ShmBarrier(void);
184
186 // Call on any instance
188 void SharedMemoryTest(void);
189
190 void *ShmBufferSelf(void);
191 void *ShmBuffer (int rank);
192 void *ShmBufferTranslate(int rank,void * local_p);
193 void *ShmBufferMalloc(size_t bytes);
194 void ShmBufferFreeAll(void) ;
195#ifndef ACCELERATOR_AWARE_MPI
196 void *HostBufferMalloc(size_t bytes);
197 void HostBufferFreeAll(void);
198#endif
200 // Make info on Nodes & ranks and Shared memory available
204
205};
206
208
int acceleratorEvent_t
AcceleratorVector< int, MaxDims > Coordinate
Definition Coordinate.h:95
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
int Grid_MPI_Comm
int CommsRequest_t
int MpiCommsRequest_t
static void SharedMemoryAllocate(uint64_t bytes, int flags)
static const int MAXLOG2RANKSPERNODE
static void SharedMemoryFree(void)
static int WorldShmSize
static void OptimalCommunicatorHypercube(const Coordinate &processors, Grid_MPI_Comm &optimal_comm, Coordinate &ShmDims)
static Grid_MPI_Comm WorldComm
static uint64_t _ShmAllocBytes
static std::vector< int > WorldShmRanks
static uint64_t ShmAllocBytes(void)
static void Init(Grid_MPI_Comm comm)
static void GetShmDims(const Coordinate &WorldDims, Coordinate &ShmDims)
static int _ShmAlloc
static void SharedMemoryZero(void *dest, size_t bytes)
static uint64_t MAX_MPI_SHM_BYTES
static int _ShmSetup
static int ShmSetup(void)
static int HPEhypercube
static int WorldNodes
static int WorldShmRank
static void OptimalCommunicatorSharedMemory(const Coordinate &processors, Grid_MPI_Comm &optimal_comm, Coordinate &ShmDims)
static std::vector< void * > WorldShmCommBufs
static int ShmAlloc(void)
static Grid_MPI_Comm WorldShmComm
static void OptimalCommunicator(const Coordinate &processors, Grid_MPI_Comm &optimal_comm, Coordinate &ShmDims)
void * ShmBufferTranslate(int rank, void *local_p)
std::vector< int > ShmRanks
void ShmBarrier(void)
void * ShmBufferSelf(void)
void * ShmBufferMalloc(size_t bytes)
size_t heap_bytes
int RankCount(void)
void * ShmBuffer(int rank)
void ShmBufferFreeAll(void)
int NodeCount(void)
void SharedMemoryTest(void)
void SetCommunicator(Grid_MPI_Comm comm)
std::vector< void * > ShmCommBufs
static const int MAXLOG2RANKSPERNODE
Grid_MPI_Comm ShmComm