r90: Convert MPI structure to C++ class
[ctsim.git] / src / phm2if.cpp
index 9b2b33b040798ad2a4647180aaa65616d33a2a4f..66ceb470c4a3af84386d79e66f9069af0d51627a 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: phm2if.cpp,v 1.5 2000/06/08 16:43:10 kevin Exp $
+**  $Id: phm2if.cpp,v 1.6 2000/06/09 01:35:33 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -103,11 +103,11 @@ phm2sdf_usage (const char *program)
 }
 
 #ifdef HAVE_MPI
-void mpi_gather_image (ImageFile* im_global, ImageFile* im_local, const int opt_debug);
+void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_local, const int opt_debug);
 #endif
 
 int 
-phm2sdf_main (const int argc, char *const argv[])
+phm2sdf_main (int argc, char* argv[])
 {
   ImageFile* im_global = NULL;
   PHANTOM *phm = NULL;
@@ -127,20 +127,9 @@ phm2sdf_main (const int argc, char *const argv[])
   int opt_verbose = 0;
   double time_start=0, time_end=0;
 #ifdef HAVE_MPI
-  ImageFile* im_local = NULL;
-  int mpi_argc = argc;
-  char **mpi_argv = (char **) argv;
   double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
-
-  MPI::Init (mpi_argc, mpi_argv);
-  mpi_ct.comm = MPI::COMM_WORLD.Dup ();
-  mpi_ct.nproc = mpi_ct.comm.Get_size();
-  mpi_ct.my_rank = mpi_ct.comm.Get_rank();
-
-  if (mpi_ct.nproc > CT_MPI_MAX_PROCESS) {
-    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
-             mpi_ct.nproc, CT_MPI_MAX_PROCESS);
-  }
+  ImageFile* im_local = NULL;
+  MPIWorld mpiWorld (argc, argv);
 #endif
 
 #ifdef HAVE_MPI
@@ -150,7 +139,7 @@ phm2sdf_main (const int argc, char *const argv[])
 #endif
   
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
 #endif
 
     strcpy(opt_desc, "");
@@ -174,7 +163,7 @@ phm2sdf_main (const int argc, char *const argv[])
        strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
        phm = phm_create_from_file(opt_phmfilename);
 #ifdef HAVE_MPI
-       if (mpi_ct.my_rank == 0) 
+       if (mpiWorld.getRank() == 0) 
          fprintf(stderr, "Can't use phantom from file in MPI mode\n");
        return (1);
 #endif
@@ -304,29 +293,29 @@ phm2sdf_main (const int argc, char *const argv[])
 
 #ifdef HAVE_MPI
   mpi_t1 = MPI::Wtime();
-  mpi_ct.comm.Bcast (&opt_verbose, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_debug, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_trace, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_nx, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_ny, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_nsample, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_phmnum, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter_domain, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_nx, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_ny, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_nsample, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_filter_domain, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&opt_filter_bw, 1, MPI::DOUBLE, 0);
 
   if (opt_verbose) {
     mpi_t2 = MPI::Wtime();
     mpi_t = mpi_t2 - mpi_t1;
-    mpi_ct.comm.Reduce (&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-    if (mpi_ct.my_rank == 0)
+    mpiWorld.getComm().Reduce (&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
+    if (mpiWorld.getRank() == 0)
       printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
   }
 
-  mpi_ct_calc_work_units(opt_nx);
+  mpiWorld.setTotalWorkUnits (opt_nx);
 
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
     im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
     im_global->adf.fileCreate();
   }
@@ -341,7 +330,7 @@ phm2sdf_main (const int argc, char *const argv[])
 
 #ifdef HAVE_MPI
   else {
-    if (mpi_ct.my_rank == 0)
+    if (mpiWorld.getRank() == 0)
       fprintf(stderr, "phmnum < 0\n");
     exit(1);
   }
@@ -351,33 +340,33 @@ phm2sdf_main (const int argc, char *const argv[])
   ImageFileArray v;
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0)
+  if (mpiWorld.getRank() == 0)
     v = im_global->getArray ();
 
   if (phm->type == P_UNIT_PULSE) {
-    if (mpi_ct.my_rank == 0) {
+    if (mpiWorld.getRank() == 0) {
       v[opt_nx/2][opt_ny/2] = 1.;
       calctime = 0;
     }
   } else if (opt_filter != -1) {
-    if (mpi_ct.my_rank == 0) {
+    if (mpiWorld.getRank() == 0) {
       image_filter_response (*im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
       calctime = 0;
     }
   } else {
-    mpi_ct.comm.Barrier();
+    mpiWorld.getComm().Barrier();
 
     if (opt_verbose)
       mpi_t1 = MPI::Wtime();
-    phm_to_imagefile (phm, *im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],  mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
+    phm_to_imagefile (phm, *im_local, mpiWorld.getMyStartWorkUnit(),  mpiWorld.getMyLocalWorkUnits(), opt_nsample, opt_trace);
     if (opt_verbose) {
       mpi_t2 = MPI::Wtime();
       mpi_t = mpi_t2 - mpi_t1;
-      mpi_ct.comm.Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
-      if (mpi_ct.my_rank == 0)
+      mpiWorld.getComm().Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
+      if (mpiWorld.getRank() == 0)
        printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
     }
-    mpi_gather_image(im_global, im_local, opt_debug);
+    mpi_gather_image (mpiWorld, im_global, im_local, opt_debug);
   }
 #else
   v = im_global->getArray ();
@@ -403,7 +392,7 @@ phm2sdf_main (const int argc, char *const argv[])
 #endif
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) 
+  if (mpiWorld.getRank() == 0) 
 #endif
   {
     im_global->adf.arrayDataWrite ();
@@ -433,31 +422,23 @@ phm2sdf_main (const int argc, char *const argv[])
 
 
 #ifdef HAVE_MPI
-void mpi_gather_image (ImageFile* im_global, ImageFile* im_local, const int opt_debug)
+void mpi_gather_image (MPIWorld& mpiWorld, ImageFile* im_global, ImageFile* im_local, const int opt_debug)
 {
   ImageFileArray vLocal = im_local->getArray();
   ImageFileArray vGlobal = NULL;
-  if (mpi_ct.my_rank == 0)
-    vGlobal = im_global->getArray();
   int nyLocal = im_local->ny();
-  
-  int end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
-  for (int iw = 0; iw <= end_work_unit; iw++) {
-    mpi_ct.comm.Send(vLocal[iw], nyLocal, im_local->getMPIDataType(), 0, 0);
-  }
-
-  if (mpi_ct.my_rank == 0) {
-    for (int iproc = 0; iproc < mpi_ct.nproc; iproc++) {
-      end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc] - 1;
 
-      if (opt_debug) {
-       fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc, mpi_ct.start_work_unit[iproc], end_work_unit);
-       fflush(stdout);
-      }
+  if (mpiWorld.getRank() == 0)
+    vGlobal = im_global->getArray();
+  
+  for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++)
+    mpiWorld.getComm().Send(vLocal[iw], nyLocal, im_local->getMPIDataType(), 0, 0);
 
-      for (int iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+  if (mpiWorld.getRank() == 0) {
+    for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
+      for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
        MPI::Status status;
-       mpi_ct.comm.Recv(vGlobal[iw], nyLocal, im_local->getMPIDataType(), iproc, 0, status);
+       mpiWorld.getComm().Recv(vGlobal[iw], nyLocal, im_local->getMPIDataType(), iProc, 0, status);
       }
     }
   }
@@ -467,7 +448,7 @@ void mpi_gather_image (ImageFile* im_global, ImageFile* im_local, const int opt_
 
 #ifndef NO_MAIN
 int 
-main (const int argc, char *const argv[])
+main (int argc, char* argv[])
 {
   return (phm2sdf_main(argc, argv));
 }