r90: Convert MPI structure to C++ class
[ctsim.git] / src / ctrec.cpp
index fed86ee0504ebe45c5fa537ed842488133bbc1db..68b9ff669606260806bb0d8067519a05c24cd53a 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: ctrec.cpp,v 1.4 2000/06/08 16:43:10 kevin Exp $
+**  $Id: ctrec.cpp,v 1.5 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
@@ -92,13 +92,13 @@ ctrec_usage (const char *program)
 
 
 #ifdef HAVE_MPI
-static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
+static void mpi_scatter_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
 #endif
 
 static void print_raysum_info(const RAYSUM *rs);
 
 int 
-ctrec_main (const int argc, char *const argv[])
+ctrec_main (int argc, char * argv[])
 {
   ImageFile *im_global = NULL;
   RAYSUM *rs_global = NULL;
@@ -119,22 +119,10 @@ ctrec_main (const int argc, char *const argv[])
 #ifdef HAVE_MPI
   ImageFile *im_local;
   RAYSUM *rs_local;
-  int mpi_argc = argc;
-  char **mpi_argv = (char **) argv;
   int mpi_nview, mpi_ndet;
   double mpi_detinc, mpi_rotinc, mpi_phmlen;
   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);
-    exit(1);
-  }
+  MPIWorld mpiWorld (argc, argv);
 #endif
 
 #ifdef HAVE_MPI
@@ -144,7 +132,7 @@ ctrec_main (const int argc, char *const argv[])
 #endif
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
 #endif
     while (1) {
       int c = getopt_long(argc, argv, "", my_options, NULL);
@@ -236,7 +224,7 @@ ctrec_main (const int argc, char *const argv[])
 #endif
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
     rs_global = raysum_open (rs_name);
     raysum_read (rs_global);
     if (opt_verbose)
@@ -250,32 +238,32 @@ ctrec_main (const int argc, char *const argv[])
   }
 
   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_filter, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_interp, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&opt_interp_param, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&opt_backproj, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_ndet, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_nview, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
-  mpi_ct.comm.Bcast (&nx, 1, MPI::INT, 0);
-  mpi_ct.comm.Bcast (&ny, 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);
+  mpiWorld.getComm().Bcast (&opt_filter, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_interp, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_filter_param, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&opt_interp_param, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&opt_backproj, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_ndet, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_nview, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&mpi_detinc, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&mpi_phmlen, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&mpi_rotinc, 1, MPI::DOUBLE, 0);
+  mpiWorld.getComm().Bcast (&nx, 1, MPI::INT, 0);
+  mpiWorld.getComm().Bcast (&ny, 1, MPI::INT, 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(mpi_nview);
+  mpiWorld.setTotalWorkUnits (mpi_nview);
 
-  rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
+  rs_local = raysum_create (NULL, mpiWorld.getMyLocalWorkUnits(), mpi_ndet);
 
   rs_local->ndet = mpi_ndet;
   rs_local->nview = mpi_nview;
@@ -285,16 +273,16 @@ ctrec_main (const int argc, char *const argv[])
 
   if (opt_verbose)
     mpi_t1 = MPI::Wtime();
-  mpi_scatter_rs(rs_global, rs_local, opt_debug);
+  mpi_scatter_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 scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
   }
 
-  if (mpi_ct.my_rank == 0) {
+  if (mpiWorld.getRank() == 0) {
     im_global = new ImageFile (im_filename, nx, ny);
     im_global->adf.fileCreate();
   }
@@ -317,8 +305,8 @@ ctrec_main (const int argc, char *const argv[])
 
   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 && opt_verbose)
+  mpiWorld.getComm().Reduce(&mpi_t, &mpi_t_g, 1, MPI::DOUBLE, MPI::MAX, 0);
+  if (mpiWorld.getRank() == 0 && opt_verbose)
     printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
 #else
   proj_reconst (*im_global, rs_global, opt_filter, opt_filter_param, 
@@ -333,26 +321,26 @@ ctrec_main (const int argc, char *const argv[])
   int nyLocal = im_local->adf.ny();
   ImageFileArray vLocal = im_local->getArray();
   ImageFileArray vGlobal = NULL;
-  if (mpi_ct.my_rank == 0)
+  if (mpiWorld.getRank() == 0)
     vGlobal = im_global->getArray();
 
   for (int ix = 0; ix < nxLocal; ix++) {
     void *recvbuf = NULL;
-    if (mpi_ct.my_rank == 0)
+    if (mpiWorld.getRank() == 0)
       recvbuf = vGlobal[ix];
 
-    mpi_ct.comm.Reduce(vLocal[ix], recvbuf, nyLocal, im_local->getMPIDataType(), MPI::SUM, 0);
+    mpiWorld.getComm().Reduce(vLocal[ix], recvbuf, nyLocal, im_local->getMPIDataType(), MPI::SUM, 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 reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
   }
 
-  if (mpi_ct.my_rank == 0)
+  if (mpiWorld.getRank() == 0)
       time_end = MPI::Wtime();
 #else
   time_end = td_current_sec();
@@ -360,7 +348,7 @@ ctrec_main (const int argc, char *const argv[])
 
 
 #ifdef HAVE_MPI
-  if (mpi_ct.my_rank == 0)
+  if (mpiWorld.getRank() == 0)
 #endif
     {
       raysum_close (rs_global);
@@ -382,40 +370,26 @@ ctrec_main (const int argc, char *const argv[])
 
 
 #ifdef HAVE_MPI
-static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
+static void mpi_scatter_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
 {
-  int iproc;
-  int end_work_unit;
-  int iw = 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;
-
-      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
-       mpi_ct.comm.Send(&rs_global->view[iw]->ndet, 1, MPI::INT, iproc, 0);
-       mpi_ct.comm.Send(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iproc, 0);
-       mpi_ct.comm.Send(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iproc, 0);
+  if (mpiWorld.getRank() == 0) {
+    for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
+      for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
+       mpiWorld.getComm().Send(&rs_global->view[iw]->ndet, 1, MPI::INT, iProc, 0);
+       mpiWorld.getComm().Send(&rs_global->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0);
+       mpiWorld.getComm().Send(rs_global->view[iw]->detval, rs_global->ndet, MPI::FLOAT, iProc, 0);
       }
     }
   }
 
-  if (opt_debug)
-    fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
-
-  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
-  for (iw = 0; iw <= end_work_unit; iw++) {
+  for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
     MPI::Status status;
 
-    mpi_ct.comm.Recv(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
-    mpi_ct.comm.Recv(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
-    mpi_ct.comm.Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0, status);
-  }
-  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
-  if (opt_debug) {
-    mpi_ct.comm.Barrier();
-    fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
+    mpiWorld.getComm().Recv(&rs_local->view[iw]->ndet, 1, MPI::INT, 0, 0, status);
+    mpiWorld.getComm().Recv(&rs_local->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0, status);
+    mpiWorld.getComm().Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI::FLOAT, 0, 0, status);
   }
+  rs_local->nview = mpiWorld.getMyLocalWorkUnits();
 }
 
 #endif
@@ -434,7 +408,7 @@ static void print_raysum_info(const RAYSUM *rs)
 
 #ifndef NO_MAIN
 int 
-main (const int argc, char *const argv[])
+main (int argc, char* argv[])
 {
   return (ctrec_main(argc, argv));
 }