Grid 0.7.0
SharedMemoryNone.cc
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
29#include <Grid/GridCore.h>
30
32#define header "SharedMemoryNone: "
33
34/*Construct from an MPI communicator*/
36{
37 assert(_ShmSetup==0);
38 WorldComm = 0;
39 WorldRank = 0;
40 WorldSize = 1;
41 WorldShmComm = 0 ;
42 WorldShmRank = 0 ;
43 WorldShmSize = 1 ;
44 WorldNodes = 1 ;
45 WorldNode = 0 ;
47 WorldShmCommBufs.resize(1);
48 _ShmSetup=1;
49}
50
51void GlobalSharedMemory::OptimalCommunicator(const Coordinate &processors,Grid_MPI_Comm & optimal_comm,Coordinate &SHM)
52{
53 optimal_comm = WorldComm;
54 SHM = Coordinate(processors.size(),1);
55}
56
58// Hugetlbfs mapping intended, use anonymous mmap
60#if 1
61void GlobalSharedMemory::SharedMemoryAllocate(uint64_t bytes, int flags)
62{
63 std::cout << header "SharedMemoryAllocate "<< bytes<< " GPU implementation "<<std::endl;
64 void * ShmCommBuf ;
65 assert(_ShmSetup==1);
66 assert(_ShmAlloc==0);
67
69 // Each MPI rank should allocate our own buffer
71 ShmCommBuf = acceleratorAllocDevice(bytes);
72
73 if (ShmCommBuf == (void *)NULL ) {
74 std::cerr << " SharedMemoryNone.cc acceleratorAllocDevice failed NULL pointer for " << bytes<<" bytes " << std::endl;
75 exit(EXIT_FAILURE);
76 }
77 if ( WorldRank == 0 ){
78 std::cout << WorldRank << header " SharedMemoryNone.cc acceleratorAllocDevice "<< bytes
79 << "bytes at "<< std::hex<< ShmCommBuf <<std::dec<<" for comms buffers " <<std::endl;
80 }
81 SharedMemoryZero(ShmCommBuf,bytes);
82
84 // Loop over ranks/gpu's on our node
86 WorldShmCommBufs[0] = ShmCommBuf;
87
88 _ShmAllocBytes=bytes;
89 _ShmAlloc=1;
90}
91#else
92void GlobalSharedMemory::SharedMemoryAllocate(uint64_t bytes, int flags)
93{
94 void * ShmCommBuf ;
95 assert(_ShmSetup==1);
96 assert(_ShmAlloc==0);
97 int mmap_flag =0;
98#ifdef MAP_ANONYMOUS
99 mmap_flag = mmap_flag| MAP_SHARED | MAP_ANONYMOUS;
100#endif
101#ifdef MAP_ANON
102 mmap_flag = mmap_flag| MAP_SHARED | MAP_ANON;
103#endif
104#ifdef MAP_HUGETLB
105 if ( flags ) mmap_flag |= MAP_HUGETLB;
106#endif
107 ShmCommBuf =(void *) mmap(NULL, bytes, PROT_READ | PROT_WRITE, mmap_flag, -1, 0);
108 if (ShmCommBuf == (void *)MAP_FAILED) {
109 perror("mmap failed ");
110 exit(EXIT_FAILURE);
111 }
112#ifdef MADV_HUGEPAGE
113 if (!Hugepages ) madvise(ShmCommBuf,bytes,MADV_HUGEPAGE);
114#endif
115 bzero(ShmCommBuf,bytes);
116 WorldShmCommBufs[0] = ShmCommBuf;
117 _ShmAllocBytes=bytes;
118 _ShmAlloc=1;
119};
120#endif
121void GlobalSharedMemory::SharedMemoryZero(void *dest,size_t bytes)
122{
123 acceleratorMemSet(dest,0,bytes);
124}
125//void GlobalSharedMemory::SharedMemoryCopy(void *dest,void *src,size_t bytes)
126//{
127// acceleratorCopyToDevice(src,dest,bytes);
128//}
130// Global shared functionality finished
131// Now move to per communicator functionality
134{
135 assert(GlobalSharedMemory::ShmAlloc()==1);
136 ShmRanks.resize(1);
137 ShmCommBufs.resize(1);
138 ShmRanks[0] = 0;
139 ShmRank = 0;
140 ShmSize = 1;
142 // Map ShmRank to WorldShmRank and use the right buffer
147 return;
148}
150// On node barrier
152void SharedMemory::ShmBarrier(void){ return ; }
153
155// Test the shared memory is working
157void SharedMemory::SharedMemoryTest(void) { return; }
158
159void *SharedMemory::ShmBuffer(int rank)
160{
161 return NULL;
162}
163void *SharedMemory::ShmBufferTranslate(int rank,void * local_p)
164{
165 return NULL;
166}
168{};
169
171
void * acceleratorAllocDevice(size_t bytes)
void acceleratorMemSet(void *base, int value, size_t bytes)
AcceleratorVector< int, MaxDims > Coordinate
Definition Coordinate.h:95
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
#define header
int Grid_MPI_Comm
accelerator_inline size_type size(void) const
Definition Coordinate.h:52
static void SharedMemoryAllocate(uint64_t bytes, int flags)
static int WorldShmSize
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 int _ShmAlloc
static void SharedMemoryZero(void *dest, size_t bytes)
static int _ShmSetup
static int WorldNodes
static int WorldShmRank
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 * ShmBuffer(int rank)
void ShmBufferFreeAll(void)
void SharedMemoryTest(void)
void SetCommunicator(Grid_MPI_Comm comm)
std::vector< void * > ShmCommBufs