r31: Code cleanup
[ctsim.git] / src / ctrec.c
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: ctrec.c,v 1.7 2000/05/03 08:49:50 kevin Exp $
6 **  $Log: ctrec.c,v $
7 **  Revision 1.7  2000/05/03 08:49:50  kevin
8 **  Code cleanup
9 **
10 **  Revision 1.6  2000/05/02 15:31:47  kevin
11 **  code cleaning
12 **
13 **  Revision 1.5  2000/04/30 11:41:06  kevin
14 **  Cleaned up debugging code
15 **
16 **  Revision 1.4  2000/04/30 10:13:27  kevin
17 **  Fixed MPI bugs
18 **
19 **  Revision 1.3  2000/04/30 04:06:13  kevin
20 **  Update Raysum i/o routines
21 **  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
22 **
23 **  Revision 1.2  2000/04/29 23:24:56  kevin
24 **  *** empty log message ***
25 **
26 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
27 **  Initial CVS import for first public release
28 **
29 **
30 **
31 **  This program is free software; you can redistribute it and/or modify
32 **  it under the terms of the GNU General Public License (version 2) as
33 **  published by the Free Software Foundation.
34 **
35 **  This program is distributed in the hope that it will be useful,
36 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
37 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38 **  GNU General Public License for more details.
39 **
40 **  You should have received a copy of the GNU General Public License
41 **  along with this program; if not, write to the Free Software
42 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
43 ******************************************************************************/
44
45 /* FILE
46  *   ctrec.c            Reconstruct an image from raysums
47  *
48  * DATE
49  *   Aug 84
50  *   Jul 99  -- Converted to ANSI C
51  *              Added MPI parallel processing
52  */
53
54 #include "ct.h"
55
56 #define O_INTERP       1
57 #define O_FILTER       2
58 #define O_FILTER_PARAM 3
59 #define O_BACKPROJ     4
60 #define O_VERBOSE      5
61 #define O_TRACE        6
62 #define O_HELP         7
63 #define O_DEBUG        8
64 #define O_VERSION      9
65
66 static struct option my_options[] =
67 {
68   {"interp", 1, 0, O_INTERP},
69   {"filter", 1, 0, O_FILTER},
70   {"filter-param", 1, 0, O_FILTER_PARAM},
71   {"backproj", 1, 0, O_BACKPROJ},
72   {"trace", 1, 0, O_TRACE},
73   {"debug", 0, 0, O_DEBUG},
74   {"verbose", 0, 0, O_VERBOSE},
75   {"help", 0, 0, O_HELP},
76   {"version", 0, 0, O_VERSION},
77   {0, 0, 0, 0}
78 };
79
80 void 
81 usage (const char *program)
82 {
83   fprintf(stdout,"usage: %s raysum-file image-file nx-image ny-image [OPTIONS]\n", kbasename(program));
84   fprintf(stdout,"Image reconstruction from raysum projections\n");
85   fprintf(stdout,"\n");
86   fprintf(stdout,"   raysum-file     Input raysum file\n");
87   fprintf(stdout,"   image-file      Output image file in SDF2D format\n");
88   fprintf(stdout,"   nx-image        Number of columns in output image\n");
89   fprintf(stdout,"   ny-image        Number of rows in output image\n");
90   fprintf(stdout,"   --interp        Interpolation method during backprojection\n");
91   fprintf(stdout,"       nearest     Nearest neighbor interpolation\n");
92   fprintf(stdout,"       linear      Linear interpolation\n");
93   fprintf(stdout,"       bspline     B-spline interpolation\n");
94   fprintf(stdout,"    --filter       Filter name\n");
95   fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting (default)\n");
96   fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
97   fprintf(stdout,"       abs_cos       Abs * Cosine\n");
98   fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
99   fprintf(stdout,"       shepp         Shepp-Logan\n");
100   fprintf(stdout,"       bandlimit     Bandlimiting\n");
101   fprintf(stdout,"       sinc          Sinc\n");
102   fprintf(stdout,"       cos           Cosine\n");
103   fprintf(stdout,"       triangle      Triangle\n");
104   fprintf(stdout,"       hamming       Hamming\n");
105   fprintf(stdout,"    --backproj     Backprojection Method\n");
106   fprintf(stdout,"       trig        Trigometric functions at every point\n");
107   fprintf(stdout,"       table       Trigometric functions with precalculated table\n");
108   fprintf(stdout,"       diff        Difference method\n");
109   fprintf(stdout,"       diff2       Optimized difference method (default)\n");
110   fprintf(stdout,"       idiff2      Optimized difference method with integer math\n");
111   fprintf(stdout,"    --filter-param Alpha level for Hamming filter\n");
112   fprintf(stdout,"    --trace        Set tracing to level\n");
113   fprintf(stdout,"         none      No tracing (default)\n");
114   fprintf(stdout,"         status    Status tracing\n");
115   fprintf(stdout,"         phm       Trace phantom\n");
116   fprintf(stdout,"         rays      Trace allrays\n");
117   fprintf(stdout,"         plot      Trace plotting\n");
118   fprintf(stdout,"         clipping  Trace clipping\n");
119   fprintf(stdout,"    --verbose      Turn on verbose mode\n");
120   fprintf(stdout,"    --debug        Turn on debug mode\n");
121   fprintf(stdout,"    --version      Print version\n");
122   fprintf(stdout,"    --help         Print this help message\n");
123   exit(1);  
124 }
125
126
127 #ifdef MPI_CT
128 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
129 #endif
130 static void print_raysum_info(const RAYSUM *rs);
131
132 int 
133 main (const int argc, char *const argv[])
134 {
135   IMAGE *im_global = NULL;
136   RAYSUM *rs_global = NULL;
137   char *rs_name, *im_filename = NULL;
138   char remark[MAXREMARK];
139   char filt_name[80];
140   int nx, ny;
141   double time_start = 0, time_end = 0;
142   char *endptr;
143   int opt_verbose = 0;
144   int opt_debug = 0;
145   int opt_trace = TRACE_NONE;
146   double opt_filter_param = 1;
147   int opt_filter = W_A_BANDLIMIT;
148   int opt_interp = I_LINEAR;
149   int opt_interp_param = 1;
150   int opt_backproj = O_BPROJ_DIFF2;
151 #ifdef MPI_CT
152   IMAGE *im_local;
153   RAYSUM *rs_local;
154   int mpi_argc = argc;
155   char **mpi_argv = (char **) argv;
156   int mpi_nview, mpi_ndet;
157   double mpi_detinc, mpi_rotinc, mpi_phmlen;
158   double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
159   int ix;
160
161   MPI_Init(&mpi_argc, &mpi_argv);
162   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
163   MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
164   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
165
166   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
167     sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
168               mpi_ct.nproc, MPI_MAX_PROCESS);
169     exit(1);
170   }
171 #endif
172
173 #ifdef MPI_CT
174   time_start = MPI_Wtime();
175 #else
176   time_start = td_current_sec();
177 #endif
178
179 #ifdef MPI_CT
180   if (mpi_ct.my_rank == 0) {
181 #endif
182     while (1) {
183       int c = getopt_long(argc, argv, "", my_options, NULL);
184       char *endptr = NULL;
185       
186       if (c == -1)
187         break;
188       
189       switch (c)
190         {
191         case O_INTERP:
192           opt_interp = opt_set_interpolation(optarg, argv[0]);
193           break;
194         case O_FILTER:
195           opt_filter = opt_set_filter(optarg, argv[0]);
196           break;
197         case O_BACKPROJ:
198           opt_backproj = opt_set_backproj(optarg, argv[0]);
199           break;
200         case O_FILTER_PARAM:
201           opt_filter_param = strtod(optarg, &endptr);
202           if (endptr != optarg + strlen(optarg)) {
203             usage(argv[0]);
204             exit(1);
205           }
206           break;
207         case O_VERBOSE:
208           opt_verbose = 1;
209           break;
210         case O_DEBUG:
211           opt_debug = 1;
212           break;
213         case O_TRACE:
214           opt_trace = opt_set_trace(optarg, argv[0]);
215           break;
216         case O_VERSION:
217 #ifdef VERSION
218           fprintf(stdout, "Version %s\n", VERSION);
219 #else
220           fprintf(stderr, "Unknown version number");
221 #endif
222           exit(0);
223         case O_HELP:
224         case '?':
225           usage(argv[0]);
226           exit(0);
227         default:
228           usage(argv[0]);
229           exit(1);
230         }
231     }
232   
233     if (optind + 4 != argc) {
234       usage(argv[0]);
235       exit(1);
236     }
237
238     rs_name = argv[optind];
239   
240     im_filename = argv[optind + 1];
241   
242     nx = strtol(argv[optind + 2], &endptr, 10);
243     ny = strtol(argv[optind + 3], &endptr, 10);
244   
245     if (opt_filter == W_G_HAMMING || opt_filter == W_AG_HAMMING)
246       sprintf (filt_name, "%s: alpha = %.2f",
247                name_of_filter (opt_filter), opt_filter_param); 
248     else
249       sprintf (filt_name, "%s", name_of_filter (opt_filter));
250   
251     sprintf (remark,
252              "Reconstruct: %dx%d, %s, %s, %s",
253              nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
254   
255     if (opt_verbose)
256       fprintf (stdout, "%s\n", remark);
257 #ifdef MPI_CT
258   }
259 #endif
260
261 #ifdef MPI_CT
262   if (mpi_ct.my_rank == 0) {
263     rs_global = raysum_open (rs_name);
264     raysum_read (rs_global);
265     if (opt_verbose)
266       print_raysum_info(rs_global);
267
268     mpi_ndet = rs_global->ndet;
269     mpi_nview = rs_global->nview;
270     mpi_detinc = rs_global->det_inc;
271     mpi_phmlen = rs_global->phmlen;
272     mpi_rotinc = rs_global->rot_inc;
273   }
274
275   mpi_t1 = MPI_Wtime();
276   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
277   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
278   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
279   MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
280   MPI_Bcast(&opt_interp, 1, MPI_INT, 0, mpi_ct.comm);
281   MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
282   MPI_Bcast(&opt_interp_param, 1, MPI_INT, 0, mpi_ct.comm);
283   MPI_Bcast(&opt_backproj, 1, MPI_INT, 0, mpi_ct.comm);
284   MPI_Bcast(&mpi_ndet, 1, MPI_INT, 0, mpi_ct.comm);
285   MPI_Bcast(&mpi_nview, 1, MPI_INT, 0, mpi_ct.comm);
286   MPI_Bcast(&mpi_detinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
287   MPI_Bcast(&mpi_phmlen, 1, MPI_DOUBLE, 0, mpi_ct.comm);
288   MPI_Bcast(&mpi_rotinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
289   MPI_Bcast(&nx, 1, MPI_INT, 0, mpi_ct.comm);
290   MPI_Bcast(&ny, 1, MPI_INT, 0, mpi_ct.comm);
291   if (opt_verbose) {
292     mpi_t2 = MPI_Wtime();
293     mpi_t = mpi_t2 - mpi_t1;
294     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
295     if (mpi_ct.my_rank == 0)
296       printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
297   }
298
299   mpi_ct_calc_work_units(mpi_nview);
300
301   rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
302
303   rs_local->ndet = mpi_ndet;
304   rs_local->nview = mpi_nview;
305   rs_local->det_inc = mpi_detinc;
306   rs_local->phmlen = mpi_phmlen;
307   rs_local->rot_inc = mpi_rotinc;
308
309   if (opt_verbose)
310     mpi_t1 = MPI_Wtime();
311   mpi_scatter_rs(rs_global, rs_local, opt_debug);
312   if (opt_verbose) {
313     mpi_t2 = MPI_Wtime();
314     mpi_t = mpi_t2 - mpi_t1;
315     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
316     if (mpi_ct.my_rank == 0)
317       printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
318   }
319
320   if (mpi_ct.my_rank == 0) {
321     im_global = image_create (im_filename, nx, ny);
322     sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
323     sdf_add_empty_label (im_global->dfp_2d->dfp);
324   }
325   im_local = image_create (NULL, nx, ny);
326
327 #else
328   rs_global = raysum_open (rs_name);
329   raysum_read (rs_global);
330   if (opt_verbose)
331     print_raysum_info(rs_global);
332
333   im_global = image_create (im_filename, nx, ny);
334   sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
335   sdf_add_empty_label (im_global->dfp_2d->dfp);
336 #endif
337
338 #ifdef MPI_CT
339   mpi_t1 = MPI_Wtime();
340   image_reconst (im_local, rs_local, opt_filter, opt_filter_param, 
341                  opt_interp, opt_interp_param, opt_backproj, opt_trace);
342   mpi_t2 = MPI_Wtime();
343   mpi_t = mpi_t2 - mpi_t1;
344   MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
345   if (mpi_ct.my_rank == 0 && opt_verbose)
346     printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
347 #else
348   image_reconst (im_global, rs_global, opt_filter, opt_filter_param, 
349                  opt_interp, opt_interp_param, opt_backproj, opt_trace);
350 #endif
351
352 #ifdef MPI_CT
353   if (mpi_ct.my_rank == 0) {
354     raysum_close (rs_global);
355   }
356
357   if (opt_verbose)
358     mpi_t1 = MPI_Wtime();
359
360   for (ix = 0; ix < im_local->nx; ix++) {
361     void *recvbuf = NULL;
362     if (mpi_ct.my_rank == 0)
363       recvbuf = im_global->v[ix];
364
365     MPI_Reduce(im_local->v[ix], recvbuf, im_local->ny, MPI_FLOAT, MPI_SUM, 0, mpi_ct.comm);
366   }
367
368   if (opt_verbose) {
369     mpi_t2 = MPI_Wtime();
370     mpi_t = mpi_t2 - mpi_t1;
371     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
372     if (mpi_ct.my_rank == 0)
373       printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
374   }
375   if (mpi_ct.my_rank == 0) {
376     strncpy (im_global->remark, remark, MAXREMARK);
377     time_end = MPI_Wtime();
378     im_global->calctime = time_end - time_start;
379     image_save (im_global);
380     if (opt_verbose)
381       fprintf (stdout, "Time active = %.2f\n", im_global->calctime);
382     }
383 #else  
384   raysum_close (rs_global);
385   strncpy (im_global->remark, remark, MAXREMARK);
386   time_end = td_current_sec();
387   im_global->calctime = time_end - time_start;
388   image_save (im_global);
389   if (opt_verbose)
390     fprintf (stdout, "Time active = %.2f secs\n", im_global->calctime);
391 #endif
392
393 #ifdef MPI_CT
394         MPI_Finalize();
395 #endif
396
397         return (0);
398 }
399
400
401 #ifdef MPI_CT
402 static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
403 {
404   int iproc;
405   int end_work_unit;
406   int iw = 0;
407
408   if (mpi_ct.my_rank == 0) {
409     for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
410       end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc]  - 1;
411
412       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
413         MPI_Send(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm);
414         MPI_Send(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm);
415         MPI_Send(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm);
416       }
417     }
418   }
419
420   if (opt_debug)
421     fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
422
423   end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
424   for (iw = 0; iw <= end_work_unit; iw++) {
425     MPI_Status status;
426
427     MPI_Recv(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm, &status);
428     MPI_Recv(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm, &status);
429     MPI_Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm, &status);
430   }
431   rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
432   if (opt_debug) {
433     MPI_Barrier(MPI_COMM_WORLD);
434     fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
435   }
436 }
437
438 #endif
439
440 static void print_raysum_info(const RAYSUM *rs)
441 {
442   printf ("Number of detectors: %d\n", rs->ndet);
443   printf ("    Number of views: %d\n", rs->nview);
444   printf ("             Remark: %s\n", rs->remark);
445   printf ("             phmlen: %f\n", rs->phmlen);
446   printf ("          det_start: %f\n", rs->det_start);
447   printf ("            det_inc: %f\n", rs->det_inc);
448   printf ("          rot_start: %f\n", rs->rot_start);
449   printf ("            rot_inc: %f\n", rs->rot_inc);
450 }