tp_memory.cpp 7.74 KB
Newer Older
1 2 3 4 5
#include "tp_memory.hpp"

Memory::Memory ()
{
  this->request_ = NULL;
6
  this->mmapAllocatorRank_ = 0;
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 54 55 56 57 58
}


Memory::~Memory ()
{
}


/**********************/
/*  |-- Allocation    */
/**********************/
void Memory::memAlloc ( int64_t buffSize, mem_t mem, bool masterRank, char* fileName, MPI_Comm comm ) {
  int rank, err;
    
  this->mem_        = mem;
  this->masterRank_ = masterRank;
  MPI_Comm_dup( comm, &this->comm_ );
  this->buffSize_   = buffSize;
    
  MPI_Comm_rank ( this->comm_, &rank );

  switch ( this->mem_ )
    {
    case DDR:
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Allocate memory on DDR (%s:%d)\n", __FILE__, __LINE__ );
	this->buffer_ = malloc ( this->buffSize_ );
	MPI_Win_create ( this->buffer_, this->buffSize_, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );
      }
      else
	MPI_Win_create ( NULL, 0, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );
	
      MPI_Win_fence (0, this->RMAWin_ );
      break;
    case HBM:
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Allocate memory on HBM (%s:%d)\n", __FILE__, __LINE__ );
	this->buffer_ = hbw_malloc ( this->buffSize_ );
	MPI_Win_create ( this->buffer_, this->buffSize_, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );
      }
      else
	MPI_Win_create ( NULL, 0, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );
	
      MPI_Win_fence (0, this->RMAWin_ );
      break;
    case HDD:
      strcpy ( this->fileName_, fileName );
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Open file %s on HDD (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
      }
      err = MPI_File_open( this->comm_, this->fileName_, MPI_MODE_RDWR | MPI_MODE_CREATE,
			   MPI_INFO_NULL, &this->fileHandle_ );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      // Preallocate the file
      break;
    case NVR:
      strcpy ( this->fileName_, fileName );
      if ( this->mmapAllocatorRank_ == rank ) {
	printMsg ( DEBUG, "Map file %s in DRAM (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
	// Add test for memory capacity
	this->fd_ = open(this->fileName_, O_RDWR | O_CREAT, S_IRWXU);
	
	if ( this->fd_ == -1 ) {
	  printMsg ( ERROR, "Error while opening the file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
	  MPI_Abort ( MPI_COMM_WORLD, -1 );	  
	}

	if ( ftruncate ( this->fd_, this->buffSize_ ) == -1 ) {
	  printMsg ( ERROR, "Error while truncating the file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
	  MPI_Abort ( MPI_COMM_WORLD, -1 );	  
	}
	
	this->ptrMap_ = mmap(0, this->buffSize_, PROT_READ | PROT_WRITE, MAP_SHARED, this->fd_, 0);
	if (this->ptrMap_ == MAP_FAILED) {
	  printMsg ( ERROR, "Mmap of file %s has failed (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
          MPI_Abort ( MPI_COMM_WORLD, -1 );
	}
	
	MPI_Win_create ( this->ptrMap_, this->buffSize_, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );
      }
      else
	MPI_Win_create ( NULL, 0, 1, MPI_INFO_NULL, this->comm_, &this->RMAWin_ );

      MPI_Win_fence (0, this->RMAWin_ );
90 91 92 93 94 95 96 97 98
      break;
    default:
      printMsg ( ERROR, "Unable to allocate memory (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }
}


void Memory::memFree  ( ) {
99
  int rank, err;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
    
  switch ( this->mem_ )
    {
    case DDR:
      MPI_Win_free ( &this->RMAWin_ );
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Free memory on DDR (%s:%d)\n", __FILE__, __LINE__ );
	free ( this->buffer_ );
      }
      break;
    case HBM:
      MPI_Win_free ( &this->RMAWin_ );
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Free memory on HBM (%s:%d)\n", __FILE__, __LINE__ );
	hbw_free ( this->buffer_ );
      }
      break;
    case HDD:
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Close file %s on HDD (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
      }
      MPI_File_close ( &this->fileHandle_ );
      break;
124 125 126 127 128 129 130 131 132 133 134 135 136 137
    case NVR:
      this->memFlush ();
      MPI_Win_free ( &this->RMAWin_ );
      
      if ( this->mmapAllocatorRank_ == rank ) {
	printMsg ( DEBUG, "Free memory on NVRAM and unmap file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
	err = munmap ( this->ptrMap_, this->buffSize_ );
	if ( err == -1 ) {
	  printMsg ( ERROR, "Error while unmaping the file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
          MPI_Abort ( MPI_COMM_WORLD, -1 );
	}
	close ( this->fd_ );
      }
      break;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    default:
      printMsg ( ERROR, "Unable to free memory (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }
}


/**********************/
/*  |-- I/O           */
/**********************/
int Memory::memWrite ( void* srcBuffer, int64_t srcSize, int64_t offset, int destRank ) {
  int err;
  MPI_Status status;

  switch ( this->mem_ )
    {
    case DDR:
    case HBM:
156
    case NVR:
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      err = MPI_Put ( srcBuffer, srcSize, MPI_BYTE, destRank, offset, srcSize, MPI_BYTE, this->RMAWin_ );
      break;
    case HDD:
      err = MPI_File_iwrite_at ( this->fileHandle_, offset, srcBuffer, srcSize, MPI_BYTE, &this->request_ );
      break;
    default:
      printMsg ( ERROR, "Error while writing data (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }

  return err;
}


int Memory::memRead  ( void* srcBuffer, int64_t srcSize, int64_t offset, int destRank ) {
  int err;
  MPI_Status status;

  switch ( this->mem_ )
    {
    case DDR:
    case HBM:
179
    case NVR:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      err = MPI_Get ( srcBuffer, srcSize, MPI_BYTE, destRank, offset, srcSize, MPI_BYTE, this->RMAWin_ );
      break;
    case HDD:
      err = MPI_File_iread_at ( this->fileHandle_, offset, srcBuffer, srcSize, MPI_BYTE, &this->request_ );
      break;
    default:
      printMsg ( ERROR, "Error while reading data (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }

  return err;
}

 
int Memory::memFlush ( ) {
195
  int err, rank;
196 197
  MPI_Status status;

198 199
  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

200 201 202 203 204 205 206 207 208 209
  switch ( this->mem_ )
    {
    case DDR:
    case HBM:
      MPI_Win_fence ( 0, this->RMAWin_ );
      break;
    case HDD:
      if ( this->request_ != NULL )
	MPI_Wait ( &this->request_, &status );
      break;
210 211 212 213 214 215 216 217 218 219 220
    case NVR:
      if ( this->mmapAllocatorRank_ == rank ) {
	printMsg ( DEBUG, "Sync memory and file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
	err = msync( this->ptrMap_, this->buffSize_, MS_SYNC );
	if ( err == -1 ) {
	  printMsg ( ERROR, "Error while syncing memory and file %s (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
          MPI_Abort ( MPI_COMM_WORLD, -1 );
	}
      }
      //MPI_Win_fence ( 0, this->RMAWin_ );
      break;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    default:
      printMsg ( ERROR, "Error while flushing data (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }

  return err;
}


int Memory::memUncache  ( ) {
  return 0;
}


/**********************/
/*  |-- Utils         */
/**********************/
char* Memory::memName  ( ) {
  switch ( this->mem_ )
    {
    case DDR:
      return "DDR";
      break;
    case HBM:
      return "HBM";
      break;
    case SSD:
      return "SSD";
      break;
    case HDD:
      return "HDD";
      break;
253 254 255
    case NVR:
      return "NVR";
      break;
256 257 258 259 260 261 262 263 264 265 266 267
    default:
      printMsg ( ERROR, "Wrong memory type!\n" );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }
}


mem_t Memory::memType  ( char* name ) {
  if ( ! strcmp ( "DDR", name ) ) return DDR;
  if ( ! strcmp ( "HBM", name ) ) return HBM;
  if ( ! strcmp ( "SSD", name ) ) return SSD;
  if ( ! strcmp ( "HDD", name ) ) return HDD;
268
  if ( ! strcmp ( "NVR", name ) ) return NVR;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

  printMsg ( ERROR, "Wrong memory name!\n" );
  MPI_Abort ( MPI_COMM_WORLD, -1 );
}


/************************/
/*  |-- Characteristics */
/************************/
int64_t Memory::memBandwidth ( ) {
  return 0;
}


int64_t Memory::memLatency   ( ) {
  return 0;
}


int64_t Memory::memCapacity  ( ) {
  return 0;
}


bool Memory::memPersistency  ( ) {
  return false;
}