tp_memory.hpp 3.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef TP_MEMORY_H
#define TP_MEMORY_H

#include <stdio.h>
#include <stdlib.h>

#include "tp_utils.hpp"
#include "tp_memory_interface.hpp"

#include <pmi.h>

class Memory: public iMemory {
public:
  /**********************/
  /*  |-- Allocation    */
  /**********************/
17
  void memAlloc ( int64_t buffSize, mem_t mem, bool masterRank, char* fileName, MPI_Comm comm ) {
18 19 20 21
    int rank;
    
    this->mem_        = mem;
    this->masterRank_ = masterRank;
22
    MPI_Comm_dup( comm, &this->comm_ );
23 24 25 26 27 28 29 30
    this->buffSize_   = buffSize;
    strcpy ( this->fileName_, fileName);
    
    MPI_Comm_rank ( this->comm_, &rank );

    switch ( this->mem_ )
      {
      case DDR:
31
	if ( this->masterRank_ ) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	  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;
      default:
	printMsg ( ERROR, "Unable to allocate memory (mem = %s)\n", this->memName () );
	MPI_Abort ( MPI_COMM_WORLD, -1 );
      }
  }

  void memFree  ( ) {
    int rank;
    MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
    
    switch ( this->mem_ )
      {
      case DDR:
	MPI_Win_free ( &this->RMAWin_ );
54
	if ( this->masterRank_ )
55 56 57 58 59 60 61 62 63 64 65
	  free ( this->buffer_ );
	break;
      default:
	printMsg ( ERROR, "Unable to free memory (mem = %s)\n", this->memName () );
	MPI_Abort ( MPI_COMM_WORLD, -1 );
      }
  }

  /**********************/
  /*  |-- I/O           */
  /**********************/
66
  int memWrite ( void* srcBuffer, int64_t srcSize, int64_t offset, int destRank ) {
67 68 69 70 71 72
    int err;
    MPI_Status status;

    switch ( this->mem_ )
      {
      case DDR:
73
	err = MPI_Put ( srcBuffer, srcSize, MPI_BYTE, destRank, offset, srcSize, MPI_BYTE, this->RMAWin_ );
74 75 76 77 78 79 80 81 82
	break;
      default:
	printMsg ( ERROR, "Error while writing data (mem = %s)\n", this->memName () );
	MPI_Abort ( MPI_COMM_WORLD, -1 );
      }

    return err;
  }

83
  int memRead  ( void* srcBuffer, int64_t srcSize, int64_t offset, int destRank ) {
84 85 86 87 88 89
    int err;
    MPI_Status status;

    switch ( this->mem_ )
      {
      case DDR:
90
	err = MPI_Get ( srcBuffer, srcSize, MPI_BYTE, destRank, offset, srcSize, MPI_BYTE, this->RMAWin_ );
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
	break;
      default:
	printMsg ( ERROR, "Error while reading data (mem = %s)\n", this->memName () );
	MPI_Abort ( MPI_COMM_WORLD, -1 );
      }

    return err;
  }
 
  int memFlush ( ) {
    int err;
    MPI_Status status;

    switch ( this->mem_ )
      {
      case DDR:
	MPI_Win_fence ( 0, this->RMAWin_ );
        break;
      default:
        printMsg ( ERROR, "Error while flushing data (mem = %s)\n", this->memName () );
        MPI_Abort ( MPI_COMM_WORLD, -1 );
      }

    return err;

  }

  /**********************/
  /*  |-- Utils         */
  /**********************/
  char* memName  ( ) {
    switch ( this->mem_ )
      {
      case DDR:
	return "DDR";
	break;
      case HBM:
	return "HBM";
	break;
      case SSD:
	return "SSD";
	break;
      case HDD:
	return "HDD";
	break;
      default:
	printMsg ( ERROR, "Wrong memory type!\n" );
	MPI_Abort ( MPI_COMM_WORLD, -1 );
      }
  }

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

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

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

  int64_t memLatency   ( ) {
    return 0;
  }

  int64_t memCapacity  ( ) {
    return 0;
  }
};

#endif // TP_MEMORY_H