r90: Convert MPI structure to C++ class
[ctsim.git] / src / phm2rs.cpp
index ed9c9e034a564bfb907811734666d924cab8be31..ac30c59f90112e989986a2136170d8bc3ae9385f 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: phm2rs.cpp,v 1.3 2000/06/08 16:43:10 kevin Exp $
+**  $Id: phm2rs.cpp,v 1.4 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
@@ -50,10 +50,6 @@ static struct option phm2rs_options[] =
 void 
 phm2rs_usage (const char *program)
 {
-#ifdef HAVE_MPI
-if (mpi_ct.my_rank == 0)
-  {
-#endif  
   fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
   fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
   fprintf(stdout,"a predefined --phantom or a --phmfile\n");
@@ -81,18 +77,14 @@ if (mpi_ct.my_rank == 0)
   fprintf(stdout,"     --debug      Debug mode\n");
   fprintf(stdout,"     --version    Print version\n");
   fprintf(stdout,"     --help       Print this help message\n");
-#ifdef HAVE_MPI
-  }
-  mpi_ct.comm.Abort(1);
-#endif
 }
 
 #ifdef HAVE_MPI
-void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
+void mpi_gather_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
 #endif
 
 int 
-phm2rs_main (const int argc, char *const argv[])
+phm2rs_main (int argc, char* argv[])
 {
   DETECTOR *det;
   PHANTOM *phm = NULL;
@@ -108,29 +100,19 @@ phm2rs_main (const int argc, char *const argv[])
   double opt_rotangle = 1;
   double time_start = 0, time_end = 0;
 
-#ifndef HAVE_MPI
-  time_start = td_current_sec();
-#else
-  RAYSUM *rs_local;
-  int mpi_argc = argc;
-  char **mpi_argv = (char **) argv;
+#ifdef HAVE_MPI
   double mpi_t1 = 0, mpi_t2 = 0, 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);
-  }
+  RAYSUM *rs_local;
+  MPIWorld mpiWorld (argc, argv);
 
   time_start = MPI::Wtime();
+#else
+  time_start = td_current_sec();
 #endif
   
+
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
 #endif
     strcpy(opt_desc, "");
     strcpy(opt_phmfilename, "");
@@ -152,7 +134,7 @@ phm2rs_main (const int argc, char *const argv[])
        break;
       case O_PHMFILE:
 #ifdef HAVE_MPI
-       if (mpi_ct.my_rank == 0)
+       if (mpiWorld.getRank() == 0)
          fprintf(stderr, "Can not read phantom from file in MPI mode\n");
        return (1);
 #endif
@@ -256,17 +238,17 @@ phm2rs_main (const int argc, char *const argv[])
 #endif
 
 #ifdef HAVE_MPI
-  mpi_ct.comm.Barrier ();
-  mpi_ct.comm.Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&opt_nview, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_ndet, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_nray, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_phmnum, 1, MPI::INT, 0);
-  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);
-
-  if (mpi_ct.my_rank > 0 && opt_phmnum >= 0)
+  mpiWorld.getComm().Barrier ();
+  mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_phmnum, 1, MPI::INT, 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);
+
+  if (mpiWorld.getRank() > 0 && opt_phmnum >= 0)
     phm = phm_create (opt_phmnum);
 #endif
 
@@ -274,36 +256,39 @@ phm2rs_main (const int argc, char *const argv[])
   det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
 
 #ifdef HAVE_MPI
-  mpi_ct_calc_work_units(opt_nview);
-  if (mpi_ct.my_rank == 0) {
+  mpiWorld.setTotalWorkUnits (opt_nview);
+
+  if (mpiWorld.getRank() == 0) {
     rs_global = raysum_create_from_det (opt_outfile, det);
     raysum_alloc_views(rs_global);
   }
   
   rs_local = raysum_create_from_det (NULL, det);
-  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
+  rs_local->nview = mpiWorld.getMyLocalWorkUnits();
   if (opt_debug)
-    printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
+    printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpiWorld.getRank());
 
   if (opt_verbose)
     mpi_t1 = MPI::Wtime();
-  raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
+  raysum_collect (rs_local, det, phm, mpiWorld.getMyStartWorkUnit(), opt_trace, FALSE);
   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 collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
   }
 
   if (opt_verbose)
     mpi_t1 = MPI::Wtime();
-  mpi_gather_rs(rs_global, rs_local, opt_debug);
+
+  mpi_gather_rs (mpiWorld, rs_global, rs_local, opt_debug);
+
   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 gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
   }
 #else
@@ -315,7 +300,7 @@ phm2rs_main (const int argc, char *const argv[])
   time_end = td_current_sec();
 #else
   time_end = MPI::Wtime();
-  if (mpi_ct.my_rank == 0) 
+  if (mpiWorld.getRank() == 0) 
 #endif
     {
       rs_global->calctime = time_end - time_start;
@@ -327,7 +312,7 @@ phm2rs_main (const int argc, char *const argv[])
     }
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) 
+  if (mpiWorld.getRank() == 0) 
 #endif
     {
       raysum_write (rs_global);
@@ -349,34 +334,22 @@ phm2rs_main (const int argc, char *const argv[])
  */
 
 #ifdef HAVE_MPI
-void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
+void mpi_gather_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
 {
-  int iproc;
-  int end_work_unit;
-  int iw;
-
-  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
-  for (iw = 0; iw <= end_work_unit; iw++) {
-    mpi_ct.comm.Send(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0);
-    mpi_ct.comm.Send(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0);
-    mpi_ct.comm.Send(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0);
+  for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
+    mpiWorld.getComm().Send(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0);
+    mpiWorld.getComm().Send(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0);
+    mpiWorld.getComm().Send(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0);
   }
 
-  if (mpi_ct.my_rank == 0) {
-    for (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\n", iproc);
-
-      for (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(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iproc, 0, status);
-       mpi_ct.comm.Recv(&rs_global->view[iw]->ndet, 1, MPI::INT, iproc, 0, status);
-       mpi_ct.comm.Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iproc, 0, status);
+       mpiWorld.getComm().Recv(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0, status);
+       mpiWorld.getComm().Recv(&rs_global->view[iw]->ndet, 1, MPI::INT, iProc, 0, status);
+       mpiWorld.getComm().Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iProc, 0, status);
       }
     }
   }
@@ -387,7 +360,7 @@ void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
 
 #ifndef NO_MAIN
 int 
-main (const int argc, char *const argv[])
+main (int argc, char* argv[])
 {
   return (phm2rs_main(argc, argv));
 }