tp_memory.cpp 4.65 KB
Newer Older
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 54 55 56 57 58 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#include "tp_memory.hpp"

Memory::Memory ()
{
  this->request_ = NULL;
  this->mmapAllocatorRank_ = 0;
}


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 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_ );
      // Preallocate the file
      break;
    default:
      printMsg ( ERROR, "Unable to allocate memory (mem = %s)\n", this->memName () );
      MPI_Abort ( MPI_COMM_WORLD, -1 );
    }
}


void Memory::memFree  ( ) {
  int rank, err;
  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 HDD:
      if ( this->masterRank_ ) {
	printMsg ( DEBUG, "Close file %s on HDD (%s:%d)\n", this->fileName_, __FILE__, __LINE__ );
      }
      MPI_File_close ( &this->fileHandle_ );
      break;
    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:
      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:
      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 ( ) {
  int err, rank;
  MPI_Status status;

  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

  switch ( this->mem_ )
    {
    case DDR:
      MPI_Win_fence ( 0, this->RMAWin_ );
      break;
    case HDD:
      if ( this->request_ != NULL )
	MPI_Wait ( &this->request_, &status );
      break;
    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 HDD:
      return "HDD";
      break;
    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 ( "HDD", name ) ) return HDD;

  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;
}