1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: ctrec.c,v 1.13 2000/06/05 01:33:25 kevin Exp $
7 ** Revision 1.13 2000/06/05 01:33:25 kevin
10 ** Revision 1.12 2000/05/24 22:50:04 kevin
11 ** Added support for new SGP library
13 ** Revision 1.11 2000/05/16 04:33:59 kevin
14 ** Improved option processing
16 ** Revision 1.10 2000/05/11 01:06:30 kevin
17 ** Changed sprintf to snprintf
19 ** Revision 1.9 2000/05/08 20:02:32 kevin
22 ** Revision 1.8 2000/05/04 18:16:34 kevin
23 ** renamed filter definitions
25 ** Revision 1.7 2000/05/03 08:49:50 kevin
28 ** Revision 1.6 2000/05/02 15:31:47 kevin
31 ** Revision 1.5 2000/04/30 11:41:06 kevin
32 ** Cleaned up debugging code
34 ** Revision 1.4 2000/04/30 10:13:27 kevin
37 ** Revision 1.3 2000/04/30 04:06:13 kevin
38 ** Update Raysum i/o routines
39 ** Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
41 ** Revision 1.2 2000/04/29 23:24:56 kevin
42 ** *** empty log message ***
44 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
45 ** Initial CVS import for first public release
49 ** This program is free software; you can redistribute it and/or modify
50 ** it under the terms of the GNU General Public License (version 2) as
51 ** published by the Free Software Foundation.
53 ** This program is distributed in the hope that it will be useful,
54 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
55 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 ** GNU General Public License for more details.
58 ** You should have received a copy of the GNU General Public License
59 ** along with this program; if not, write to the Free Software
60 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
61 ******************************************************************************/
64 * ctrec.c Reconstruct an image from raysums
68 * Jul 99 -- Converted to ANSI C
69 * Added MPI parallel processing
74 enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
76 static struct option my_options[] =
78 {"interp", 1, 0, O_INTERP},
79 {"filter", 1, 0, O_FILTER},
80 {"filter-param", 1, 0, O_FILTER_PARAM},
81 {"backproj", 1, 0, O_BACKPROJ},
82 {"trace", 1, 0, O_TRACE},
83 {"debug", 0, 0, O_DEBUG},
84 {"verbose", 0, 0, O_VERBOSE},
85 {"help", 0, 0, O_HELP},
86 {"version", 0, 0, O_VERSION},
91 ctrec_usage (const char *program)
93 fprintf(stdout,"usage: %s raysum-file image-file nx-image ny-image [OPTIONS]\n", kbasename(program));
94 fprintf(stdout,"Image reconstruction from raysum projections\n");
96 fprintf(stdout," raysum-file Input raysum file\n");
97 fprintf(stdout," image-file Output image file in SDF2D format\n");
98 fprintf(stdout," nx-image Number of columns in output image\n");
99 fprintf(stdout," ny-image Number of rows in output image\n");
100 fprintf(stdout," --interp Interpolation method during backprojection\n");
101 fprintf(stdout," nearest Nearest neighbor interpolation\n");
102 fprintf(stdout," linear Linear interpolation\n");
103 #if HAVE_BSPLINE_INTERP
104 fprintf(stdout," bspline B-spline interpolation\n");
106 fprintf(stdout," --filter Filter name\n");
107 fprintf(stdout," abs_bandlimit Abs * Bandlimiting (default)\n");
108 fprintf(stdout," abs_sinc Abs * Sinc\n");
109 fprintf(stdout," abs_cos Abs * Cosine\n");
110 fprintf(stdout," abs_hamming Abs * Hamming\n");
111 fprintf(stdout," shepp Shepp-Logan\n");
112 fprintf(stdout," bandlimit Bandlimiting\n");
113 fprintf(stdout," sinc Sinc\n");
114 fprintf(stdout," cos Cosine\n");
115 fprintf(stdout," triangle Triangle\n");
116 fprintf(stdout," hamming Hamming\n");
117 fprintf(stdout," --backproj Backprojection Method\n");
118 fprintf(stdout," trig Trigometric functions at every point\n");
119 fprintf(stdout," table Trigometric functions with precalculated table\n");
120 fprintf(stdout," diff Difference method\n");
121 fprintf(stdout," diff2 Optimized difference method (default)\n");
122 fprintf(stdout," idiff2 Optimized difference method with integer math\n");
123 fprintf(stdout," --filter-param Alpha level for Hamming filter\n");
124 fprintf(stdout," --trace Set tracing to level\n");
125 fprintf(stdout," none No tracing (default)\n");
126 fprintf(stdout," text Text level tracing\n");
127 fprintf(stdout," phm Trace phantom\n");
128 fprintf(stdout," rays Trace allrays\n");
129 fprintf(stdout," plot Trace plotting\n");
130 fprintf(stdout," clipping Trace clipping\n");
131 fprintf(stdout," --verbose Turn on verbose mode\n");
132 fprintf(stdout," --debug Turn on debug mode\n");
133 fprintf(stdout," --version Print version\n");
134 fprintf(stdout," --help Print this help message\n");
139 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
142 static void print_raysum_info(const RAYSUM *rs);
145 ctrec_main (const int argc, char *const argv[])
147 IMAGE *im_global = NULL;
148 RAYSUM *rs_global = NULL;
149 char *rs_name, *im_filename = NULL;
150 char remark[MAXREMARK];
153 double time_start = 0, time_end = 0;
157 int opt_trace = TRACE_NONE;
158 double opt_filter_param = 1;
159 int opt_filter = FILTER_ABS_BANDLIMIT;
160 int opt_interp = I_LINEAR;
161 int opt_interp_param = 1;
162 BackprojType opt_backproj = O_BPROJ_DIFF2;
167 char **mpi_argv = (char **) argv;
168 int mpi_nview, mpi_ndet;
169 double mpi_detinc, mpi_rotinc, mpi_phmlen;
170 double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
173 MPI_Init(&mpi_argc, &mpi_argv);
174 MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
175 MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
176 MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
178 if (mpi_ct.nproc > MPI_MAX_PROCESS) {
179 sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
180 mpi_ct.nproc, MPI_MAX_PROCESS);
186 time_start = MPI_Wtime();
188 time_start = td_current_sec();
192 if (mpi_ct.my_rank == 0) {
195 int c = getopt_long(argc, argv, "", my_options, NULL);
204 if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
205 ctrec_usage(argv[0]);
210 if ((opt_filter = opt_set_filter(optarg)) < 0) {
211 ctrec_usage(argv[0]);
216 if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
217 ctrec_usage(argv[0]);
222 opt_filter_param = strtod(optarg, &endptr);
223 if (endptr != optarg + strlen(optarg)) {
224 ctrec_usage(argv[0]);
234 if ((opt_trace = opt_set_trace(optarg)) < 0) {
235 ctrec_usage(argv[0]);
241 fprintf(stdout, "Version %s\n", VERSION);
243 fprintf(stderr, "Unknown version number");
248 ctrec_usage(argv[0]);
251 ctrec_usage(argv[0]);
256 if (optind + 4 != argc) {
257 ctrec_usage(argv[0]);
261 rs_name = argv[optind];
263 im_filename = argv[optind + 1];
265 nx = strtol(argv[optind + 2], &endptr, 10);
266 ny = strtol(argv[optind + 3], &endptr, 10);
268 if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
269 snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
270 name_of_filter (opt_filter), opt_filter_param);
272 snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
274 snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
275 nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
278 fprintf (stdout, "%s\n", remark);
284 if (mpi_ct.my_rank == 0) {
285 rs_global = raysum_open (rs_name);
286 raysum_read (rs_global);
288 print_raysum_info(rs_global);
290 mpi_ndet = rs_global->ndet;
291 mpi_nview = rs_global->nview;
292 mpi_detinc = rs_global->det_inc;
293 mpi_phmlen = rs_global->phmlen;
294 mpi_rotinc = rs_global->rot_inc;
297 mpi_t1 = MPI_Wtime();
298 MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
299 MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
300 MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
301 MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
302 MPI_Bcast(&opt_interp, 1, MPI_INT, 0, mpi_ct.comm);
303 MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
304 MPI_Bcast(&opt_interp_param, 1, MPI_INT, 0, mpi_ct.comm);
305 MPI_Bcast(&opt_backproj, 1, MPI_INT, 0, mpi_ct.comm);
306 MPI_Bcast(&mpi_ndet, 1, MPI_INT, 0, mpi_ct.comm);
307 MPI_Bcast(&mpi_nview, 1, MPI_INT, 0, mpi_ct.comm);
308 MPI_Bcast(&mpi_detinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
309 MPI_Bcast(&mpi_phmlen, 1, MPI_DOUBLE, 0, mpi_ct.comm);
310 MPI_Bcast(&mpi_rotinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
311 MPI_Bcast(&nx, 1, MPI_INT, 0, mpi_ct.comm);
312 MPI_Bcast(&ny, 1, MPI_INT, 0, mpi_ct.comm);
314 mpi_t2 = MPI_Wtime();
315 mpi_t = mpi_t2 - mpi_t1;
316 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
317 if (mpi_ct.my_rank == 0)
318 printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
321 mpi_ct_calc_work_units(mpi_nview);
323 rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
325 rs_local->ndet = mpi_ndet;
326 rs_local->nview = mpi_nview;
327 rs_local->det_inc = mpi_detinc;
328 rs_local->phmlen = mpi_phmlen;
329 rs_local->rot_inc = mpi_rotinc;
332 mpi_t1 = MPI_Wtime();
333 mpi_scatter_rs(rs_global, rs_local, opt_debug);
335 mpi_t2 = MPI_Wtime();
336 mpi_t = mpi_t2 - mpi_t1;
337 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
338 if (mpi_ct.my_rank == 0)
339 printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
342 if (mpi_ct.my_rank == 0) {
343 im_global = image_create (im_filename, nx, ny);
344 sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
345 sdf_add_empty_label (im_global->dfp_2d->dfp);
347 im_local = image_create (NULL, nx, ny);
350 rs_global = raysum_open (rs_name);
351 raysum_read (rs_global);
353 print_raysum_info(rs_global);
355 im_global = image_create (im_filename, nx, ny);
356 sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
357 sdf_add_empty_label (im_global->dfp_2d->dfp);
361 mpi_t1 = MPI_Wtime();
362 image_reconst (im_local, rs_local, opt_filter, opt_filter_param,
363 opt_interp, opt_interp_param, opt_backproj, opt_trace);
364 mpi_t2 = MPI_Wtime();
365 mpi_t = mpi_t2 - mpi_t1;
366 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
367 if (mpi_ct.my_rank == 0 && opt_verbose)
368 printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
370 image_reconst (im_global, rs_global, opt_filter, opt_filter_param,
371 opt_interp, opt_interp_param, opt_backproj, opt_trace);
376 mpi_t1 = MPI_Wtime();
378 for (ix = 0; ix < im_local->nx; ix++) {
379 void *recvbuf = NULL;
380 if (mpi_ct.my_rank == 0)
381 recvbuf = im_global->v[ix];
383 MPI_Reduce(im_local->v[ix], recvbuf, im_local->ny, MPI_FLOAT, MPI_SUM, 0, mpi_ct.comm);
387 mpi_t2 = MPI_Wtime();
388 mpi_t = mpi_t2 - mpi_t1;
389 MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
390 if (mpi_ct.my_rank == 0)
391 printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
394 if (mpi_ct.my_rank == 0)
395 time_end = MPI_Wtime();
397 time_end = td_current_sec();
402 if (mpi_ct.my_rank == 0)
405 raysum_close (rs_global);
406 strncpy (im_global->remark, remark, sizeof(im_global->remark));
407 im_global->calctime = time_end - time_start;
408 image_save (im_global);
410 fprintf (stdout, "Time active = %.2f\n", im_global->calctime);
422 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
428 if (mpi_ct.my_rank == 0) {
429 for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
430 end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc] - 1;
432 for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
433 MPI_Send(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm);
434 MPI_Send(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm);
435 MPI_Send(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm);
441 fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
443 end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
444 for (iw = 0; iw <= end_work_unit; iw++) {
447 MPI_Recv(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm, &status);
448 MPI_Recv(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm, &status);
449 MPI_Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm, &status);
451 rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
453 MPI_Barrier(MPI_COMM_WORLD);
454 fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
460 static void print_raysum_info(const RAYSUM *rs)
462 printf ("Number of detectors: %d\n", rs->ndet);
463 printf (" Number of views: %d\n", rs->nview);
464 printf (" Remark: %s\n", rs->remark);
465 printf (" phmlen: %f\n", rs->phmlen);
466 printf (" det_start: %f\n", rs->det_start);
467 printf (" det_inc: %f\n", rs->det_inc);
468 printf (" rot_start: %f\n", rs->rot_start);
469 printf (" rot_inc: %f\n", rs->rot_inc);
474 main (const int argc, char *const argv[])
476 return (ctrec_main(argc, argv));