X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=src%2Fphm2rs.cpp;h=04593493f8d7ab8f9d477dc405922037f2b703f5;hb=b5857e74e5735455b5ef11cbea5044ae7b2e8a0d;hp=ac30c59f90112e989986a2136170d8bc3ae9385f;hpb=2451ac413848718a1dd666ce6f6464e974680f47;p=ctsim.git diff --git a/src/phm2rs.cpp b/src/phm2rs.cpp index ac30c59..0459349 100644 --- a/src/phm2rs.cpp +++ b/src/phm2rs.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: phm2rs.cpp,v 1.4 2000/06/09 01:35:33 kevin Exp $ +** $Id: phm2rs.cpp,v 1.6 2000/06/15 19:07:10 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 @@ -25,8 +25,9 @@ ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ - #include "ct.h" +#include "timer.h" + enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE, O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION }; @@ -50,72 +51,67 @@ static struct option phm2rs_options[] = void phm2rs_usage (const char *program) { - 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"); - fprintf(stdout,"\n"); - fprintf(stdout," outfile Name of output file for raysums\n"); - fprintf(stdout," ndet Number of detectors\n"); - fprintf(stdout," nview Number of rotated views\n"); - fprintf(stdout," --phantom Phantom to use for projection\n"); - fprintf(stdout," herman Herman head phantom\n"); - fprintf(stdout," rowland Rowland head phantom\n"); - fprintf(stdout," browland Bordered Rowland head phantom\n"); - fprintf(stdout," unitpulse Unit pulse phantom\n"); - fprintf(stdout," --phmfile Get Phantom from phantom file\n"); - fprintf(stdout," --desc Description of raysum\n"); - fprintf(stdout," --nray Number of rays per detector (default = 1)\n"); - fprintf(stdout," --rotangle Degrees to rotate view through, multiple of PI (default = 1)\n"); - fprintf(stdout," --trace Trace level to use\n"); - fprintf(stdout," none No tracing (default)\n"); - fprintf(stdout," text Trace text level\n"); - fprintf(stdout," phm Trace phantom image\n"); - fprintf(stdout," rays Trace rays\n"); - fprintf(stdout," plot Trace plot\n"); - fprintf(stdout," clipping Trace clipping\n"); - fprintf(stdout," --verbose Verbose mode\n"); - fprintf(stdout," --debug Debug mode\n"); - fprintf(stdout," --version Print version\n"); - fprintf(stdout," --help Print this help message\n"); + cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]" << endl; + cout << "Calculate raysums (projections) through phantom object, either" << endl; + cout << "a predefined --phantom or a --phmfile" << endl; + cout << "" << endl; + cout << " outfile Name of output file for raysums" << endl; + cout << " ndet Number of detectors" << endl; + cout << " nview Number of rotated views" << endl; + cout << " --phantom Phantom to use for projection" << endl; + cout << " herman Herman head phantom" << endl; + cout << " rowland Rowland head phantom" << endl; + cout << " browland Bordered Rowland head phantom" << endl; + cout << " unitpulse Unit pulse phantom" << endl; + cout << " --phmfile Get Phantom from phantom file" << endl; + cout << " --desc Description of raysum" << endl; + cout << " --nray Number of rays per detector (default = 1)" << endl; + cout << " --rotangle Degrees to rotate view through, multiple of PI (default = 1)" << endl; + cout << " --trace Trace level to use" << endl; + cout << " none No tracing (default)" << endl; + cout << " text Trace text level" << endl; + cout << " phm Trace phantom image" << endl; + cout << " rays Trace rays" << endl; + cout << " plot Trace plot" << endl; + cout << " clipping Trace clipping" << endl; + cout << " --verbose Verbose mode" << endl; + cout << " --debug Debug mode" << endl; + cout << " --version Print version" << endl; + cout << " --help Print this help message" << endl; } #ifdef HAVE_MPI -void mpi_gather_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug); +void GatherProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug); #endif int phm2rs_main (int argc, char* argv[]) { - DETECTOR *det; - PHANTOM *phm = NULL; - RAYSUM *rs_global = NULL; - char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1]; - char opt_phmfilename[256]; + Detector *det; + Phantom phm; + RAYSUM *rsGlobal = NULL; + char *opt_outfile = NULL; + string opt_desc; + string opt_phmfilename; char *endptr, *endstr; int opt_ndet, opt_nview; int opt_nray = 1; - int opt_trace = 0, opt_phmnum = -1; + int opt_trace = 0; + int opt_phmnum = -1; int opt_verbose = 0; int opt_debug = 0; double opt_rotangle = 1; - double time_start = 0, time_end = 0; #ifdef HAVE_MPI - double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g; - RAYSUM *rs_local; + RAYSUM *rsLocal; MPIWorld mpiWorld (argc, argv); - - time_start = MPI::Wtime(); -#else - time_start = td_current_sec(); #endif - + + Timer timerProgram; #ifdef HAVE_MPI if (mpiWorld.getRank() == 0) { #endif - strcpy(opt_desc, ""); - strcpy(opt_phmfilename, ""); while (1) { int c = getopt_long(argc, argv, "", phm2rs_options, NULL); char *endptr = NULL; @@ -130,16 +126,16 @@ phm2rs_main (int argc, char* argv[]) phm2rs_usage(argv[0]); return (1); } - phm = phm_create (opt_phmnum); + phm.create (opt_phmnum); break; case O_PHMFILE: #ifdef HAVE_MPI if (mpiWorld.getRank() == 0) - fprintf(stderr, "Can not read phantom from file in MPI mode\n"); + cerr << "Can not read phantom from file in MPI mode" << endl; return (1); #endif - strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename)); - phm = phm_create_from_file(opt_phmfilename); + opt_phmfilename = optarg; + phm.createFromFile (opt_phmfilename.c_str()); break; case O_VERBOSE: opt_verbose = 1; @@ -155,13 +151,13 @@ phm2rs_main (int argc, char* argv[]) } break; case O_DESC: - strncpy(opt_desc, optarg, sizeof(opt_desc)); + opt_desc = optarg; break; case O_ROTANGLE: opt_rotangle = strtod(optarg, &endptr); endstr = optarg + strlen(optarg); if (endptr != endstr) { - fprintf(stderr,"Error setting --rotangle to %s\n", optarg); + cerr << "Error setting --rotangle to " << optarg << endl; phm2rs_usage(argv[0]); return (1); } @@ -170,16 +166,16 @@ phm2rs_main (int argc, char* argv[]) opt_nray = strtol(optarg, &endptr, 10); endstr = optarg + strlen(optarg); if (endptr != endstr) { - fprintf(stderr,"Error setting --nray to %s\n", optarg); + cerr << "Error setting --nray to %s" << optarg << endl; phm2rs_usage(argv[0]); return (1); } break; case O_VERSION: #ifdef VERSION - fprintf(stdout, "Version %s\n", VERSION); + cout << "Version: " << VERSION << endl; #else - fprintf(stderr, "Unknown version number"); + cout << "Unknown version number" << endl; #endif exit(0); case O_HELP: @@ -192,8 +188,8 @@ phm2rs_main (int argc, char* argv[]) } } - if (phm == NULL) { - fprintf(stderr, "No phantom defined\n"); + if (phm.nPElem() == 0) { + cerr << "No phantom defined" << endl; phm2rs_usage(argv[0]); return (1); } @@ -206,39 +202,34 @@ phm2rs_main (int argc, char* argv[]) opt_ndet = strtol(argv[optind+1], &endptr, 10); endstr = argv[optind+1] + strlen(argv[optind+1]); if (endptr != endstr) { - fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]); + cerr << "Error setting --ndet to " << argv[optind+1] << endl; phm2rs_usage(argv[0]); return (1); } opt_nview = strtol(argv[optind+2], &endptr, 10); endstr = argv[optind+2] + strlen(argv[optind+2]); if (endptr != endstr) { - fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]); + cerr << "Error setting --nview to " << argv[optind+2] << endl; phm2rs_usage(argv[0]); return (1); } - snprintf(str, sizeof(str), - "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ", - opt_ndet, opt_nview, opt_nray, opt_rotangle); - if (opt_phmfilename[0]) { - strncat(str, "Phantom=", sizeof(str)); - strncat(str, opt_phmfilename, sizeof(str)); + ostringstream desc; + desc << "Raysum_Collect: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", "; + if (opt_phmfilename.length()) { + desc << "PhantomFile=" << opt_phmfilename; } else if (opt_phmnum != -1) { - strncat(str, "Phantom=", sizeof(str)); - strncat(str, name_of_phantom(opt_phmnum), sizeof(str)); + desc << "Phantom=" << name_of_phantom(opt_phmnum); } - if (opt_desc[0]) { - strncat(str, ": ", sizeof(str)); - strncat(str, opt_desc, sizeof(str)); + if (opt_desc.length()) { + desc << ": " << opt_desc; } - strncpy(opt_desc, str, sizeof(opt_desc)); + opt_desc = desc.str(); #ifdef HAVE_MPI } #endif #ifdef HAVE_MPI - 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); @@ -249,97 +240,73 @@ phm2rs_main (int argc, char* argv[]) mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0); if (mpiWorld.getRank() > 0 && opt_phmnum >= 0) - phm = phm_create (opt_phmnum); + phm.create (opt_phmnum); #endif opt_rotangle *= PI; - det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle); + det = new Detector (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle); #ifdef HAVE_MPI mpiWorld.setTotalWorkUnits (opt_nview); if (mpiWorld.getRank() == 0) { - rs_global = raysum_create_from_det (opt_outfile, det); - raysum_alloc_views(rs_global); + rsGlobal = raysum_create_from_det (opt_outfile, *det); + raysum_alloc_views(rsGlobal); } - rs_local = raysum_create_from_det (NULL, det); - rs_local->nview = mpiWorld.getMyLocalWorkUnits(); + rsLocal = raysum_create_from_det (NULL, *det); + rsLocal->nview = mpiWorld.getMyLocalWorkUnits(); if (opt_debug) - printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpiWorld.getRank()); + cout << "rsLocal->nview = " << rsLocal->nview << " (process " << mpiWorld.getRank() << ")" << endl;; + TimerMPI timerProject (mpiWorld.getComm()); + raysum_collect (rsLocal, *det, phm, mpiWorld.getMyStartWorkUnit(), opt_trace, FALSE); if (opt_verbose) - mpi_t1 = MPI::Wtime(); - raysum_collect (rs_local, det, phm, mpiWorld.getMyStartWorkUnit(), opt_trace, FALSE); - if (opt_verbose) { - mpi_t2 = MPI::Wtime(); - mpi_t = mpi_t2 - mpi_t1; - 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); - } + timerProject.timerEndAndReport ("Time to collect projections"); + TimerMPI timerGather (mpiWorld.getComm()); + GatherProjectionsMPI (mpiWorld, rsGlobal, rsLocal, opt_debug); if (opt_verbose) - mpi_t1 = MPI::Wtime(); - - mpi_gather_rs (mpiWorld, rs_global, rs_local, opt_debug); - - if (opt_verbose) { - mpi_t2 = MPI::Wtime(); - mpi_t = mpi_t2 - mpi_t1; - 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); - } + timerGather.timerEndAndReport ("Time to gather projections"); #else - rs_global = raysum_create_from_det (opt_outfile, det); - raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE); + rsGlobal = raysum_create_from_det (opt_outfile, *det); + raysum_collect (rsGlobal, *det, phm, 0, opt_trace, FALSE); #endif -#ifndef HAVE_MPI - time_end = td_current_sec(); -#else - time_end = MPI::Wtime(); +#ifdef HAVE_MPI if (mpiWorld.getRank() == 0) #endif { - rs_global->calctime = time_end - time_start; - strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark)); + rsGlobal->calctime = timerProgram.timerEnd(); + rsGlobal->remark = opt_desc; + raysum_write (rsGlobal); + raysum_close (rsGlobal); if (opt_verbose) { - fprintf(stdout, "Remark: %s\n", rs_global->remark); - fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime); + cout << "Remark: " << rsGlobal->remark << endl; + cout << "Run time: " << rsGlobal->calctime << " seconds" << endl; } } -#ifdef HAVE_MPI - if (mpiWorld.getRank() == 0) -#endif - { - raysum_write (rs_global); - raysum_close (rs_global); - } - - phm_free (phm); - detector_free (det); + delete det; return (0); } /* FUNCTION - * mpi_gather_rs + * GatherProjectionsMPI * * SYNOPSIS - * Gather's raysums from all processes in rs_local in rs_global in process 0 + * Gather's raysums from all processes in rsLocal in rsGlobal in process 0 */ #ifdef HAVE_MPI -void mpi_gather_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug) +void GatherProjectionsMPI (MPIWorld& mpiWorld, RAYSUM *rsGlobal, RAYSUM *rsLocal, const int opt_debug) { 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); + mpiWorld.getComm().Send(&rsLocal->view[iw]->view_angle, 1, MPI::DOUBLE, 0, 0); + mpiWorld.getComm().Send(&rsLocal->view[iw]->ndet, 1, MPI::INT, 0, 0); + mpiWorld.getComm().Send(rsLocal->view[iw]->detval, rsLocal->ndet, MPI::FLOAT, 0, 0); } if (mpiWorld.getRank() == 0) { @@ -347,9 +314,9 @@ void mpi_gather_rs (MPIWorld& mpiWorld, RAYSUM *rs_global, RAYSUM *rs_local, con for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) { MPI::Status 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); + mpiWorld.getComm().Recv(&rsGlobal->view[iw]->view_angle, 1, MPI::DOUBLE, iProc, 0, status); + mpiWorld.getComm().Recv(&rsGlobal->view[iw]->ndet, 1, MPI::INT, iProc, 0, status); + mpiWorld.getComm().Recv(rsGlobal->view[iw]->detval, rsGlobal->ndet, MPI::FLOAT, iProc, 0, status); } } }