main_mpiio.cpp 5.16 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <time.h>
#include <mpi.h>

#include "tapioca.hpp"
#include "utils.h"

#define RED     "\x1b[31m"
#define GREEN   "\x1b[32m"
#define BLUE    "\x1b[34m"
#define RESET   "\x1b[0m"

int64_t particles (int64_t max, int rank) {
  int64_t n_part;

  srand(time(NULL) + rank);
  n_part = (rand () % max) + 10;

  return n_part;
}

int main (int argc, char * argv[])
{
  int world_numtasks, world_myrank, mycolor, mykey, sub_numtasks, sub_myrank, i, file_id;
  int64_t num_particles;
  int64_t sub_particles, tot_particles, particle_size, file_size, tot_size;
  int64_t scan_size = 0, offset, hdr = 0;
  double start_time, end_time, tot_time, max_time;
  double io_bw;
  MPI_Comm sub_comm;
  MPI_File file_handle;
  MPI_Info info;
  MPI_Status status;
  char output[100];
  int64_t chunkCount[1], chunkOffset[1];
  int chunkSize[1];
  Tapioca tp;
  
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &world_numtasks);
  MPI_Comm_rank(MPI_COMM_WORLD, &world_myrank);

  mycolor = tp.topology.BridgeNodeId ();
  //mycolor = 42;
  mykey = world_myrank;

  MPI_Comm_split (MPI_COMM_WORLD, mycolor, mykey, &sub_comm);
  MPI_Comm_size(sub_comm, &sub_numtasks);
  MPI_Comm_rank(sub_comm, &sub_myrank);

  if ( argv[2] != NULL )
    num_particles = particles (atoi ( argv[1] ), world_myrank);
  else
    num_particles = atoi ( argv[1] );

#ifdef BGQ
  snprintf (output, 100, "/projects/visualization/ftessier/debug/SimpleIO-%08d.dat", mycolor);
#elif XC40
  snprintf (output, 100, "/lus/theta-fs0/projects/Performance/ftessier/HACC/SimpleIO-%08d.dat", mycolor);
#else
  snprintf (output, 100, "./SimpleIO-%08d.dat", mycolor);
#endif


  /*****************/
  /*     WRITE     */
  /*****************/
  float *xx;
  xx = new float[num_particles];

  for (uint64_t i = 0; i< num_particles; i++)
    xx[i] = (float)i;
  
  MPI_Allreduce(&num_particles, &sub_particles, 1, MPI_LONG_LONG, MPI_SUM, sub_comm);
  MPI_Allreduce(&num_particles, &tot_particles, 1, MPI_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
  
  particle_size = sizeof(float);
  file_size     = particle_size * sub_particles;
  tot_size      = particle_size * tot_particles;

  if (sub_myrank == 0) {
    MPI_File_open(MPI_COMM_SELF, output,
		  MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &file_handle);
    MPI_File_set_size(file_handle, file_size);
    MPI_File_close (&file_handle);
  }

  MPI_Exscan (&num_particles, &scan_size, 1, MPI_LONG_LONG, MPI_SUM, sub_comm);
    
  if (0 == sub_myrank) {
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] TAPIOCA Simple I/O\n", mycolor);
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] Write output file\n", mycolor);
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] --> %lld particles per rank\n", mycolor, num_particles);
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] --> File size: %.2f MB (%lld particles)\n", 
	     mycolor, (double)file_size/(1024*1024), sub_particles);
  }

  MPI_Info_create ( &info );
  MPI_Info_set ( info, "cb_nodes", getenv("TAPIOCA_NBAGGR") );
  MPI_Info_set ( info, "bg_nodes_pset", getenv("TAPIOCA_NBAGGR") );

  MPI_File_open(sub_comm, output,
		MPI_MODE_WRONLY, info, &file_handle);

  MPIIOInfo ( file_handle );
  
  start_time = MPI_Wtime();

  offset = scan_size * particle_size;

  MPI_File_write_at_all (file_handle, offset, xx, num_particles, MPI_FLOAT, &status);
  
  MPI_File_close (&file_handle);

  end_time = MPI_Wtime();
  tot_time = end_time - start_time;
  MPI_Reduce (&tot_time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  
  if (0 == world_myrank) {
    io_bw = (double)tot_size / max_time / (1024 * 1024);
    fprintf (stdout, BLUE "[TIMING]" RESET " Write I/O bandwidth: %.2f MBps (%.2f MB in %.2f ms)\n",
	     io_bw, (double)tot_size/(1024*1024), max_time * 1000);
  }

  MPI_Barrier (MPI_COMM_WORLD);

  /*****************/
  /*     READ      */
  /*****************/
  float *xx_r;
  xx_r = new float[num_particles];

  MPI_File_open(sub_comm, output,
		MPI_MODE_RDONLY, MPI_INFO_NULL, &file_handle);

  if (0 == sub_myrank)
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] Read output file\n", mycolor);

  start_time = MPI_Wtime();

  offset = scan_size * particle_size;

  MPI_File_read_at_all (file_handle, offset, xx_r, num_particles, MPI_FLOAT, &status);
  
  MPI_File_close (&file_handle);

  end_time = MPI_Wtime();
  tot_time = end_time - start_time;
  MPI_Reduce (&tot_time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  
  if (0 == world_myrank) {
    io_bw = (double)tot_size / max_time / (1024 * 1024);
    fprintf (stdout, BLUE "[TIMING]" RESET " Read I/O bandwidth: %.2f MBps (%.2f MB in %.2f ms)\n",
	     io_bw, (double)tot_size/(1024*1024), max_time * 1000);
  }

  /*****************/
  /* VERIFICATION  */
  /*****************/
  for (uint64_t i = 0; i< num_particles; i++) {
    if (xx[i] != xx_r[i])
      {
	fprintf (stdout, RED "[ERROR]" RESET " Wrong value for particle %d\n", i);
	MPI_Abort (MPI_COMM_WORLD, -1);
      }
  }

  if (0 == sub_myrank)
    fprintf (stdout, GREEN "[INFO]" RESET " [%08d] Content verified and consistent\n", mycolor);

  /*****************/
  /*      FREE     */
  /*****************/
  delete [] xx;
  delete [] xx_r;
  
  MPI_Finalize ();
}