1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
6 ** Generate raysum projections from phantom object
9 ** 1984 -- Final DOS version
10 ** 1999 -- First UNIX version
12 ** $Id: phm2rs.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
13 ** $Log: phm2rs.cpp,v $
14 ** Revision 1.1 2000/06/07 02:29:05 kevin
15 ** Initial C++ versions
17 ** Revision 1.12 2000/05/24 22:50:04 kevin
18 ** Added support for new SGP library
20 ** Revision 1.11 2000/05/16 04:33:59 kevin
21 ** Improved option processing
23 ** Revision 1.9 2000/05/08 20:02:32 kevin
26 ** Revision 1.8 2000/05/04 18:16:34 kevin
27 ** renamed filter definitions
29 ** Revision 1.7 2000/05/03 08:49:50 kevin
32 ** Revision 1.6 2000/04/30 18:23:53 kevin
35 ** Revision 1.5 2000/04/30 10:13:27 kevin
38 ** Revision 1.4 2000/04/30 04:06:13 kevin
39 ** Update Raysum i/o routines
40 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
42 ** Revision 1.3 2000/04/29 23:24:56 kevin
43 ** *** empty log message ***
45 ** Revision 1.2 2000/04/28 13:50:45 kevin
46 ** Removed Makefile Makefile.in that are automatically generated by autoconf
48 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
49 ** Initial CVS import for first public release
53 ** This program is free software; you can redistribute it and/or modify
54 ** it under the terms of the GNU General Public License (version 2) as
55 ** published by the Free Software Foundation.
57 ** This program is distributed in the hope that it will be useful,
58 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
59 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 ** GNU General Public License for more details.
62 ** You should have received a copy of the GNU General Public License
63 ** along with this program; if not, write to the Free Software
64 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
65 ******************************************************************************/
70 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
71 O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
73 static struct option phm2rs_options[] =
75 {"phantom", 1, 0, O_PHANTOM},
76 {"phmfile", 1, 0, O_PHMFILE},
77 {"desc", 1, 0, O_DESC},
78 {"nray", 1, 0, O_NRAY},
79 {"rotangle", 1, 0, O_ROTANGLE},
80 {"trace", 1, 0, O_TRACE},
81 {"verbose", 0, 0, O_VERBOSE},
82 {"help", 0, 0, O_HELP},
83 {"debug", 0, 0, O_DEBUG},
84 {"version", 0, 0, O_VERSION},
90 phm2rs_usage (const char *program)
93 if (mpi_ct.my_rank == 0)
96 fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
97 fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
98 fprintf(stdout,"a predefined --phantom or a --phmfile\n");
100 fprintf(stdout," outfile Name of output file for raysums\n");
101 fprintf(stdout," ndet Number of detectors\n");
102 fprintf(stdout," nview Number of rotated views\n");
103 fprintf(stdout," --phantom Phantom to use for projection\n");
104 fprintf(stdout," herman Herman head phantom\n");
105 fprintf(stdout," rowland Rowland head phantom\n");
106 fprintf(stdout," browland Bordered Rowland head phantom\n");
107 fprintf(stdout," unitpulse Unit pulse phantom\n");
108 fprintf(stdout," --phmfile Get Phantom from phantom file\n");
109 fprintf(stdout," --desc Description of raysum\n");
110 fprintf(stdout," --nray Number of rays per detector (default = 1)\n");
111 fprintf(stdout," --rotangle Degrees to rotate view through, multiple of PI (default = 1)\n");
112 fprintf(stdout," --trace Trace level to use\n");
113 fprintf(stdout," none No tracing (default)\n");
114 fprintf(stdout," text Trace text level\n");
115 fprintf(stdout," phm Trace phantom image\n");
116 fprintf(stdout," rays Trace rays\n");
117 fprintf(stdout," plot Trace plot\n");
118 fprintf(stdout," clipping Trace clipping\n");
119 fprintf(stdout," --verbose Verbose mode\n");
120 fprintf(stdout," --debug Debug mode\n");
121 fprintf(stdout," --version Print version\n");
122 fprintf(stdout," --help Print this help message\n");
125 MPI_Abort(mpi_ct.comm, 1);
130 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
134 phm2rs_main (const int argc, char *const argv[])
138 RAYSUM *rs_global = NULL;
139 char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1];
140 char opt_phmfilename[256];
141 char *endptr, *endstr;
142 int opt_ndet, opt_nview;
144 int opt_trace = 0, opt_phmnum = -1;
147 double opt_rotangle = 1;
148 double time_start = 0, time_end = 0;
151 time_start = td_current_sec();
155 char **mpi_argv = (char **) argv;
156 double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
158 MPI_Init(&mpi_argc, &mpi_argv);
159 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
160 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
161 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
163 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
164 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)", mpi_ct.nproc, MPI_MAX_PROCESS);
167 time_start = MPI_Wtime();
171 if (mpi_ct.my_rank == 0) {
173 strcpy(opt_desc, "");
174 strcpy(opt_phmfilename, "");
176 int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
185 if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
186 phm2rs_usage(argv[0]);
189 phm = phm_create (opt_phmnum);
193 if (mpi_ct.my_rank == 0)
194 fprintf(stderr, "Can not read phantom from file in MPI mode\n");
197 strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
198 phm = phm_create_from_file(opt_phmfilename);
208 if ((opt_trace = opt_set_trace(optarg)) < 0) {
209 phm2rs_usage(argv[0]);
214 strncpy(opt_desc, optarg, sizeof(opt_desc));
217 opt_rotangle = strtod(optarg, &endptr);
218 endstr = optarg + strlen(optarg);
219 if (endptr != endstr) {
220 fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
221 phm2rs_usage(argv[0]);
226 opt_nray = strtol(optarg, &endptr, 10);
227 endstr = optarg + strlen(optarg);
228 if (endptr != endstr) {
229 fprintf(stderr,"Error setting --nray to %s\n", optarg);
230 phm2rs_usage(argv[0]);
236 fprintf(stdout, "Version %s\n", VERSION);
238 fprintf(stderr, "Unknown version number");
243 phm2rs_usage(argv[0]);
246 phm2rs_usage(argv[0]);
252 fprintf(stderr, "No phantom defined\n");
253 phm2rs_usage(argv[0]);
256 if (optind + 3 != argc) {
257 phm2rs_usage(argv[0]);
261 opt_outfile = argv[optind];
262 opt_ndet = strtol(argv[optind+1], &endptr, 10);
263 endstr = argv[optind+1] + strlen(argv[optind+1]);
264 if (endptr != endstr) {
265 fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]);
266 phm2rs_usage(argv[0]);
269 opt_nview = strtol(argv[optind+2], &endptr, 10);
270 endstr = argv[optind+2] + strlen(argv[optind+2]);
271 if (endptr != endstr) {
272 fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]);
273 phm2rs_usage(argv[0]);
277 snprintf(str, sizeof(str),
278 "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ",
279 opt_ndet, opt_nview, opt_nray, opt_rotangle);
280 if (opt_phmfilename[0]) {
281 strncat(str, "Phantom=", sizeof(str));
282 strncat(str, opt_phmfilename, sizeof(str));
283 } else if (opt_phmnum != -1) {
284 strncat(str, "Phantom=", sizeof(str));
285 strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
288 strncat(str, ": ", sizeof(str));
289 strncat(str, opt_desc, sizeof(str));
291 strncpy(opt_desc, str, sizeof(opt_desc));
297 MPI_Barrier(mpi_ct.comm);
298 MPI_Bcast(&opt_rotangle, 1, MPI_DOUBLE, 0, mpi_ct.comm);
299 MPI_Bcast(&opt_nview, 1, MPI_INT, 0, mpi_ct.comm);
300 MPI_Bcast(&opt_ndet, 1, MPI_INT, 0, mpi_ct.comm);
301 MPI_Bcast(&opt_nray, 1, MPI_INT, 0, mpi_ct.comm);
302 MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
303 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
304 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
305 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
307 if (mpi_ct.my_rank > 0 && opt_phmnum >= 0)
308 phm = phm_create (opt_phmnum);
312 det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
315 mpi_ct_calc_work_units(opt_nview);
316 if (mpi_ct.my_rank == 0) {
317 rs_global = raysum_create_from_det (opt_outfile, det);
318 raysum_alloc_views(rs_global);
321 rs_local = raysum_create_from_det (NULL, det);
322 rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
324 printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
327 mpi_t1 = MPI_Wtime();
328 raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
330 mpi_t2 = MPI_Wtime();
331 mpi_t = mpi_t2 - mpi_t1;
332 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
333 if (mpi_ct.my_rank == 0)
334 printf("Time to collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
338 mpi_t1 = MPI_Wtime();
339 mpi_gather_rs(rs_global, rs_local, opt_debug);
341 mpi_t2 = MPI_Wtime();
342 mpi_t = mpi_t2 - mpi_t1;
343 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
344 if (mpi_ct.my_rank == 0)
345 printf("Time to gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
348 rs_global = raysum_create_from_det (opt_outfile, det);
349 raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
353 time_end = td_current_sec();
355 time_end = MPI_Wtime();
356 if (mpi_ct.my_rank == 0)
359 rs_global->calctime = time_end - time_start;
360 strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
362 fprintf(stdout, "Remark: %s\n", rs_global->remark);
363 fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
368 if (mpi_ct.my_rank == 0)
371 raysum_write (rs_global);
372 raysum_close (rs_global);
386 * Gather's raysums from all processes in rs_local in rs_global in process 0
390 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
396 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
397 for (iw = 0; iw <= end_work_unit; iw++) {
398 MPI_Send(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm);
399 MPI_Send(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm);
400 MPI_Send(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm);
403 if (mpi_ct.my_rank == 0) {
404 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
405 end_work_unit = mpi_ct.start_work_unit[iproc]
406 + mpi_ct.local_work_units[iproc]
410 fprintf(stdout, "Recv rs data from process %d\n", iproc);
412 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
415 MPI_Recv(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm, &status);
416 MPI_Recv(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm, &status);
417 MPI_Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
428 main (const int argc, char *const argv[])
430 return (phm2rs_main(argc, argv));