r102: fixed mpi bug
[ctsim.git] / src / phm2pj.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          phm2pj.cpp
5 **   Purpose:       Take projections of a phantom object
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: phm2pj.cpp,v 1.2 2000/06/19 17:58:13 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29 #include "timer.h"
30
31
32 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
33        O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
34
35 static struct option phm2pj_options[] = 
36 {
37   {"phantom", 1, 0, O_PHANTOM},
38   {"phmfile", 1, 0, O_PHMFILE},
39   {"desc", 1, 0, O_DESC},
40   {"nray", 1, 0, O_NRAY},
41   {"rotangle", 1, 0, O_ROTANGLE},
42   {"trace", 1, 0, O_TRACE},
43   {"verbose", 0, 0, O_VERBOSE},
44   {"help", 0, 0, O_HELP},
45   {"debug", 0, 0, O_DEBUG},
46   {"version", 0, 0, O_VERSION},
47   {0, 0, 0, 0}
48 };
49
50
51 void 
52 phm2pj_usage (const char *program)
53 {
54   cout << "usage: " << fileBasename(program) << " outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]" << endl;
55   cout << "Calculate (projections) through phantom object, either a predefined --phantom or a --phmfile" << endl;
56   cout << "" << endl;
57   cout << "     outfile      Name of output file for raysums" << endl;
58   cout << "     ndet         Number of detectors" << endl;
59   cout << "     nview        Number of rotated views" << endl;
60   cout << "     --phantom    Phantom to use for projection" << endl;
61   cout << "        herman    Herman head phantom" << endl;
62   cout << "        rowland   Rowland head phantom" << endl;
63   cout << "        browland  Bordered Rowland head phantom" << endl;
64   cout << "        unitpulse Unit pulse phantom" << endl;
65   cout << "     --phmfile    Get Phantom from phantom file" << endl;
66   cout << "     --desc       Description of raysum" << endl;
67   cout << "     --nray       Number of rays per detector (default = 1)" << endl;
68   cout << "     --rotangle   Degrees to rotate view through, multiple of PI (default = 1)" << endl;
69   cout << "     --trace      Trace level to use" << endl;
70   cout << "        none      No tracing (default)" << endl;
71   cout << "        text      Trace text level" << endl;
72   cout << "        phm       Trace phantom image" << endl;
73   cout << "        rays      Trace rays" << endl;
74   cout << "        plot      Trace plot" << endl;
75   cout << "        clipping  Trace clipping" << endl;
76   cout << "     --verbose    Verbose mode" << endl;
77   cout << "     --debug      Debug mode" << endl;
78   cout << "     --version    Print version" << endl;
79   cout << "     --help       Print this help message" << endl;
80 }
81
82 #ifdef HAVE_MPI
83 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug);
84 #endif
85
86 int 
87 phm2pj_main (int argc, char* argv[])
88 {
89   Phantom phm;
90   ScannerGeometry opt_geometry = DETECTOR_PARALLEL;
91   char *opt_outfile = NULL;
92   string opt_desc;
93   string opt_phmfilename;
94   int opt_ndet;
95   int opt_nview;
96   int opt_nray = 1;
97   int opt_trace = 0;
98   int optPhmNum = -1;
99   int opt_verbose = 0;
100   int opt_debug = 0;
101   double opt_rotangle = 1;
102   char* endptr = NULL;
103   char* endstr;
104
105 #ifdef HAVE_MPI
106   MPIWorld mpiWorld (argc, argv);
107 #endif
108
109   Timer timerProgram;
110
111 #ifdef HAVE_MPI
112   if (mpiWorld.getRank() == 0) {
113 #endif
114     while (1) {
115       int c = getopt_long(argc, argv, "", phm2pj_options, NULL);
116
117       if (c == -1)
118         break;
119       
120       switch (c) {
121       case O_PHANTOM:
122         if ((optPhmNum = opt_set_phantom (optarg)) < 0) {
123           phm2pj_usage(argv[0]);
124           return (1);
125         }
126         phm.create (optPhmNum);
127         break;
128       case O_PHMFILE:
129 #ifdef HAVE_MPI
130         if (mpiWorld.getRank() == 0)
131           cerr << "Can not read phantom from file in MPI mode" << endl;
132         return (1);
133 #endif
134         opt_phmfilename = optarg;
135         phm.createFromFile (opt_phmfilename.c_str());
136         break;
137       case O_VERBOSE:
138         opt_verbose = 1;
139         break;
140       case O_DEBUG:
141         opt_debug = 1;
142         break;
143         break;
144       case O_TRACE:
145         if ((opt_trace = opt_set_trace(optarg)) < 0) {
146           phm2pj_usage(argv[0]);
147           return (1);
148         }
149         break;
150       case O_DESC:
151         opt_desc = optarg;
152         break;
153       case O_ROTANGLE:
154         opt_rotangle = strtod(optarg, &endptr);
155         endstr = optarg + strlen(optarg);
156         if (endptr != endstr) {
157           cerr << "Error setting --rotangle to " << optarg << endl;
158           phm2pj_usage(argv[0]);
159           return (1);
160         }
161         break;
162       case O_NRAY:
163         opt_nray = strtol(optarg, &endptr, 10);
164         endstr = optarg + strlen(optarg);
165         if (endptr != endstr) {
166           cerr << "Error setting --nray to %s" << optarg << endl;
167           phm2pj_usage(argv[0]);
168           return (1);
169         }
170         break;
171         case O_VERSION:
172 #ifdef VERSION
173           cout << "Version: " << VERSION << endl;
174 #else
175           cout << "Unknown version number" << endl;
176 #endif
177           return (0);
178       case O_HELP:
179       case '?':
180         phm2pj_usage(argv[0]);
181         return (0);
182       default:
183         phm2pj_usage(argv[0]);
184         return (1);
185       }
186     }
187   
188     if (phm.nPElem() == 0) {
189       cerr << "No phantom defined" << endl;
190       phm2pj_usage(argv[0]);
191       return (1);
192     }
193     if (optind + 3 != argc) {
194       phm2pj_usage(argv[0]);
195       return (1);
196     }
197
198     opt_outfile = argv[optind];
199     opt_ndet = strtol(argv[optind+1], &endptr, 10);
200     endstr = argv[optind+1] + strlen(argv[optind+1]);
201     if (endptr != endstr) {
202       cerr << "Error setting --ndet to " << argv[optind+1] << endl;
203       phm2pj_usage(argv[0]);
204       return (1);
205     }
206     opt_nview = strtol(argv[optind+2], &endptr, 10);
207     endstr = argv[optind+2] + strlen(argv[optind+2]);
208     if (endptr != endstr) {
209       cerr << "Error setting --nview to " << argv[optind+2] << endl;
210       phm2pj_usage(argv[0]);
211       return (1);
212     }
213
214     ostringstream desc;
215     desc << "Raysum_Collect: NDet=" << opt_ndet << ", Nview=" << opt_nview << ", NRay=" << opt_nray << ", RotAngle=" << opt_rotangle << ", ";
216     if (opt_phmfilename.length()) {
217       desc << "PhantomFile=" << opt_phmfilename;
218     } else if (optPhmNum != -1) {
219       desc << "Phantom=" << name_of_phantom(optPhmNum);
220     }
221     if (opt_desc.length()) {
222       desc << ": " << opt_desc;
223     }
224     opt_desc = desc.str();
225 #ifdef HAVE_MPI
226   }
227 #endif
228
229 #ifdef HAVE_MPI
230   TimerCollectiveMPI timerBcast(mpiWorld.getComm());
231   mpiWorld.getComm().Bcast (&opt_rotangle, 1, MPI::DOUBLE, 0);
232   mpiWorld.getComm().Bcast (&opt_nview, 1, MPI::INT, 0);
233   mpiWorld.getComm().Bcast (&opt_ndet, 1, MPI::INT, 0);
234   mpiWorld.getComm().Bcast (&opt_nray, 1, MPI::INT, 0);
235   mpiWorld.getComm().Bcast (&optPhmNum, 1, MPI::INT, 0);
236   mpiWorld.getComm().Bcast (&opt_verbose, 1, MPI::INT, 0);
237   mpiWorld.getComm().Bcast (&opt_debug, 1, MPI::INT, 0);
238   mpiWorld.getComm().Bcast (&opt_trace, 1, MPI::INT, 0);
239   timerBcast.timerEndAndReport ("Time to broadcast variables");
240
241   if (mpiWorld.getRank() > 0 && optPhmNum >= 0)
242     phm.create (optPhmNum);
243 #endif
244
245   opt_rotangle *= PI;
246   Scanner scanner (phm, opt_geometry, opt_ndet, opt_nview, opt_nray, opt_rotangle);
247
248 #ifdef HAVE_MPI
249   mpiWorld.setTotalWorkUnits (opt_nview);
250
251   Projections pjGlobal;
252   if (mpiWorld.getRank() == 0)
253     pjGlobal.initFromScanner (scanner);
254   
255   if (opt_verbose)
256     pjGlobal.printScanInfo();
257
258   Projections pjLocal (scanner);
259   pjLocal.setNView (mpiWorld.getMyLocalWorkUnits());
260
261   if (opt_debug)
262     cout << "pjLocal->nview = " << pjLocal.nView() << " (process " << mpiWorld.getRank() << ")" << endl;;
263
264   TimerCollectiveMPI timerProject (mpiWorld.getComm());
265   scanner.collectProjections (pjLocal, phm, mpiWorld.getMyStartWorkUnit(), opt_trace);
266   if (opt_verbose)
267     timerProject.timerEndAndReport ("Time to collect projections");
268
269   TimerCollectiveMPI timerGather (mpiWorld.getComm());
270   GatherProjectionsMPI (mpiWorld, pjGlobal, pjLocal, opt_debug);
271   if (opt_verbose) 
272      timerGather.timerEndAndReport ("Time to gather projections");
273
274 #else
275   Projections pjGlobal (scanner);
276   scanner.collectProjections (pjGlobal, phm, 0, opt_trace);
277 #endif
278   
279 #ifdef HAVE_MPI
280   if (mpiWorld.getRank() == 0) 
281 #endif
282     {
283       pjGlobal.setCalcTime (timerProgram.timerEnd());
284       pjGlobal.setRemark (opt_desc);
285       pjGlobal.write (opt_outfile);
286       if (opt_verbose) {
287         phm.print();
288         cout << endl;
289         pjGlobal.printScanInfo();
290         cout << endl;
291         cout << "  Remark: " << pjGlobal.remark() << endl;
292         cout << "Run time: " << pjGlobal.calcTime() << " seconds" << endl;
293       }
294     }
295
296   return (0);
297 }
298
299
300 /* FUNCTION
301  *    GatherProjectionsMPI
302  *
303  * SYNOPSIS
304  *    Gather's raysums from all processes in pjLocal in pjGlobal in process 0
305  */
306
307 #ifdef HAVE_MPI
308 void GatherProjectionsMPI (MPIWorld& mpiWorld, Projections& pjGlobal, Projections& pjLocal, const int opt_debug)
309 {
310   for (int iw = 0; iw < mpiWorld.getMyLocalWorkUnits(); iw++) {
311     DetectorArray& detArray = pjLocal.getDetectorArray(iw);
312     double viewAngle = detArray.viewAngle();
313     int nDet = detArray.nDet();
314     DetectorValue* detval = detArray.detValues();
315
316     mpiWorld.getComm().Send(&viewAngle, 1, MPI::DOUBLE, 0, 0);
317     mpiWorld.getComm().Send(&nDet, 1, MPI::INT, 0, 0);
318     mpiWorld.getComm().Send(detval, nDet, MPI::FLOAT, 0, 0);
319   }
320
321   if (mpiWorld.getRank() == 0) {
322     for (int iProc = 0; iProc < mpiWorld.getNumProcessors(); iProc++) {
323       for (int iw = mpiWorld.getStartWorkUnit(iProc); iw <= mpiWorld.getEndWorkUnit(iProc); iw++) {
324         MPI::Status status;
325         double viewAngle;
326         int nDet;
327         DetectorArray& detArray = pjGlobal.getDetectorArray(iw);
328         DetectorValue* detval = detArray.detValues();
329
330         mpiWorld.getComm().Recv(&viewAngle, 1, MPI::DOUBLE, iProc, 0, status);
331         mpiWorld.getComm().Recv(&nDet, 1, MPI::INT, iProc, 0, status);
332         mpiWorld.getComm().Recv(detval, nDet, MPI::FLOAT, iProc, 0, status);
333         detArray.setViewAngle (viewAngle);
334       }
335     }
336   }
337 }
338 #endif
339
340
341 #ifndef NO_MAIN
342 int 
343 main (int argc, char* argv[])
344 {
345   int retval = 1;
346
347   try {
348     retval = phm2pj_main(argc, argv);
349   } catch (exception e) {
350     cerr << "Exception: " << e.what() << endl;
351   } catch (...) {
352     cerr << "Unknown exception" << endl;
353   }
354
355   return (retval);
356 }
357 #endif
358