4dbf09080b4dac963cbce5a9f8f038fdf396bbdb
[ctsim.git] / src / phm2rs.c
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: phm2rs.c,v 1.9 2000/05/08 20:02:32 kevin Exp $
6 **  $Log: phm2rs.c,v $
7 **  Revision 1.9  2000/05/08 20:02:32  kevin
8 **  ANSI C changes
9 **
10 **  Revision 1.8  2000/05/04 18:16:34  kevin
11 **  renamed filter definitions
12 **
13 **  Revision 1.7  2000/05/03 08:49:50  kevin
14 **  Code cleanup
15 **
16 **  Revision 1.6  2000/04/30 18:23:53  kevin
17 **  Code cleaning
18 **
19 **  Revision 1.5  2000/04/30 10:13:27  kevin
20 **  Fixed MPI bugs
21 **
22 **  Revision 1.4  2000/04/30 04:06:13  kevin
23 **  Update Raysum i/o routines
24 **  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
25 **
26 **  Revision 1.3  2000/04/29 23:24:56  kevin
27 **  *** empty log message ***
28 **
29 **  Revision 1.2  2000/04/28 13:50:45  kevin
30 **  Removed Makefile Makefile.in that are automatically generated by autoconf
31 **
32 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
33 **  Initial CVS import for first public release
34 **
35 **
36 **
37 **  This program is free software; you can redistribute it and/or modify
38 **  it under the terms of the GNU General Public License (version 2) as
39 **  published by the Free Software Foundation.
40 **
41 **  This program is distributed in the hope that it will be useful,
42 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
43 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44 **  GNU General Public License for more details.
45 **
46 **  You should have received a copy of the GNU General Public License
47 **  along with this program; if not, write to the Free Software
48 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
49 ******************************************************************************/
50 /* FILE
51  *   phm2rs.c           Generate raysum projections from phantom object
52  *
53  * HISTORY
54  *   1984 -- Final DOS version  
55  *   1999 -- First UNIX version
56  */
57
58 #include "ct.h"
59
60 enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_TRACE, O_VERBOSE, O_HELP, O_PHMFILE, O_DEBUG, O_VERSION };
61
62 static struct option my_options[] = 
63 {
64   {"phantom", 1, 0, O_PHANTOM},
65   {"phmfile", 1, 0, O_PHMFILE},
66   {"desc", 1, 0, O_DESC},
67   {"nray", 1, 0, O_NRAY},
68   {"rotangle", 1, 0, O_ROTANGLE},
69   {"trace", 1, 0, O_TRACE},
70   {"verbose", 0, 0, O_VERBOSE},
71   {"help", 0, 0, O_HELP},
72   {"debug", 0, 0, O_DEBUG},
73   {"version", 0, 0, O_VERSION},
74   {0, 0, 0, 0}
75 };
76
77 void 
78 usage (const char *program)
79 {
80 #ifdef MPI_CT
81 if (mpi_ct.my_rank == 0)
82   {
83 #endif  
84   fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
85   fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
86   fprintf(stdout,"a predefined --phantom or a --phmfile\n");
87   fprintf(stdout,"\n");
88   fprintf(stdout,"     outfile      Name of output file for raysums\n");
89   fprintf(stdout,"     ndet         Number of detectors\n");
90   fprintf(stdout,"     nview        Number of rotated views\n");
91   fprintf(stdout,"     --phantom    Phantom to use for projection\n");
92   fprintf(stdout,"        herman    Herman head phantom\n");
93   fprintf(stdout,"        rowland   Rowland head phantom\n");
94   fprintf(stdout,"        browland  Bordered Rowland head phantom\n");
95   fprintf(stdout,"        unitpulse Unit pulse phantom\n");
96   fprintf(stdout,"     --phmfile    Get Phantom from phantom file\n");
97   fprintf(stdout,"     --desc       Description of raysum\n");
98   fprintf(stdout,"     --nray       Number of rays per detector (default = 1)\n");
99   fprintf(stdout,"     --rotangle   Degrees to rotate view through, multiple of PI (default = 1)\n");
100   fprintf(stdout,"     --trace      Trace level to use\n");
101   fprintf(stdout,"        none      No tracing (default)\n");
102   fprintf(stdout,"        text      Trace text level\n");
103   fprintf(stdout,"        phm       Trace PHANTOM\n");
104   fprintf(stdout,"        rays      Trace rays\n");
105   fprintf(stdout,"        plot      Trace plot\n");
106   fprintf(stdout,"        clipping  Trace clipping\n");
107   fprintf(stdout,"     --verbose    Verbose mode\n");
108   fprintf(stdout,"     --debug      Debug mode\n");
109   fprintf(stdout,"     --version    Print version\n");
110   fprintf(stdout,"     --help       Print this help message\n");
111 #ifdef MPI_CT
112   }
113   MPI_Abort(mpi_ct.comm, 1);
114 #endif
115   exit(1);
116 }
117
118
119 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
120
121 int 
122 main (const int argc, char *const argv[])
123 {
124   DETECTOR *det;
125   PHANTOM *phm = NULL;
126   RAYSUM *rs_global = NULL;
127   char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1];
128   char opt_phmfilename[256];
129   char *endptr, *endstr;
130   int opt_ndet, opt_nview;
131   int opt_nray = 1;
132   int opt_trace = 0, opt_phmnum = -1;
133   int opt_verbose = 0;
134   int opt_debug = 0;
135   double opt_rotangle = 1;
136   double time_start = 0, time_end = 0;
137 #ifdef MPI_CT
138   RAYSUM *rs_local;
139   int mpi_argc = argc;
140   char **mpi_argv = (char **) argv;
141   double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
142
143   MPI_Init(&mpi_argc, &mpi_argv);
144   MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
145   MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
146   MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
147
148   if (mpi_ct.nproc > MPI_MAX_PROCESS) {
149     sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
150               mpi_ct.nproc, MPI_MAX_PROCESS);
151     exit(1);
152   }
153   time_start = MPI_Wtime();
154 #else
155   time_start = td_current_sec();
156 #endif
157   
158 #ifdef MPI_CT
159   if (mpi_ct.my_rank == 0) {
160 #endif
161     strcpy(opt_desc, "");
162     strcpy(opt_phmfilename, "");
163     while (1) {
164       int c = getopt_long(argc, argv, "", my_options, NULL);
165       char *endptr = NULL;
166       char *endstr;
167       
168       if (c == -1)
169         break;
170       
171       switch (c) {
172       case O_PHANTOM:
173         opt_phmnum = opt_set_phantom (optarg, argv[0]);
174         phm = phm_create (opt_phmnum);
175         break;
176       case O_PHMFILE:
177 #ifdef MPI_CT
178         if (mpi_ct.my_rank == 0)
179           fprintf(stderr, "Can not read phantom from file in MPI mode\n");
180         exit(1);
181 #endif
182         strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
183         phm = phm_create_from_file(opt_phmfilename);
184         break;
185       case O_VERBOSE:
186         opt_verbose = 1;
187         break;
188       case O_DEBUG:
189         opt_debug = 1;
190         break;
191         break;
192       case O_TRACE:
193         opt_trace = opt_set_trace(optarg, argv[0]);
194         break;
195       case O_DESC:
196         strncpy(opt_desc, optarg, sizeof(opt_desc));
197         break;
198       case O_ROTANGLE:
199         opt_rotangle = strtod(optarg, &endptr);
200         endstr = optarg + strlen(optarg);
201         if (endptr != endstr) {
202           fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
203           usage(argv[0]);
204           exit(1);
205         }
206         break;
207       case O_NRAY:
208         opt_nray = strtol(optarg, &endptr, 10);
209         endstr = optarg + strlen(optarg);
210         if (endptr != endstr) {
211           fprintf(stderr,"Error setting --nray to %s\n", optarg);
212           usage(argv[0]);
213           exit(1);
214         }
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 (phm == NULL) {
234       fprintf(stderr, "No phantom defined\n");
235       usage(argv[0]);
236       exit(1);
237     }
238     if (optind + 3 != argc) {
239       usage(argv[0]);
240       exit(1);
241     }
242     opt_outfile = argv[optind];
243     opt_ndet = strtol(argv[optind+1], &endptr, 10);
244     endstr = argv[optind+1] + strlen(argv[optind+1]);
245     if (endptr != endstr) {
246       fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]);
247       usage(argv[0]);
248       exit(1);
249     }
250     opt_nview = strtol(argv[optind+2], &endptr, 10);
251     endstr = argv[optind+2] + strlen(argv[optind+2]);
252     if (endptr != endstr) {
253       fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]);
254       usage(argv[0]);
255       exit(1);
256     }
257
258     snprintf(str, sizeof(str), 
259              "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ", 
260              opt_ndet, opt_nview, opt_nray, opt_rotangle);
261     if (opt_phmfilename[0]) {
262       strncat(str, "Phantom=", sizeof(str));
263       strncat(str, opt_phmfilename, sizeof(str));
264     } else if (opt_phmnum != -1) {
265       strncat(str, "Phantom=", sizeof(str));
266       strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
267     }
268     if (opt_desc[0]) {
269       strncat(str, ": ", sizeof(str));
270       strncat(str, opt_desc, sizeof(str));
271     }
272     strncpy(opt_desc, str, sizeof(opt_desc));
273 #ifdef MPI_CT
274   }
275 #endif
276
277 #ifdef MPI_CT
278   MPI_Barrier(mpi_ct.comm);
279   MPI_Bcast(&opt_rotangle, 1, MPI_DOUBLE, 0, mpi_ct.comm);
280   MPI_Bcast(&opt_nview, 1, MPI_INT, 0, mpi_ct.comm);
281   MPI_Bcast(&opt_ndet, 1, MPI_INT, 0, mpi_ct.comm);
282   MPI_Bcast(&opt_nray, 1, MPI_INT, 0, mpi_ct.comm);
283   MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
284   MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
285   MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
286   MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
287
288   if (mpi_ct.my_rank > 0 && opt_phmnum > 0)
289     phm = phm_create (opt_phmnum);
290 #endif
291
292   opt_rotangle *= PI;
293   det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
294
295 #ifdef MPI_CT
296   mpi_ct_calc_work_units(opt_nview);
297   if (mpi_ct.my_rank == 0) {
298     rs_global = raysum_create_from_det (opt_outfile, det);
299     raysum_alloc_views(rs_global);
300   }
301   
302   rs_local = raysum_create_from_det (NULL, det);
303   rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
304   if (opt_debug)
305     printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
306
307   if (opt_verbose)
308     mpi_t1 = MPI_Wtime();
309   raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
310   if (opt_verbose) {
311     mpi_t2 = MPI_Wtime();
312     mpi_t = mpi_t2 - mpi_t1;
313     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
314     if (mpi_ct.my_rank == 0)
315       printf("Time to collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
316   }
317
318   if (opt_verbose)
319     mpi_t1 = MPI_Wtime();
320   mpi_gather_rs(rs_global, rs_local, opt_debug);
321   if (opt_verbose) {
322     mpi_t2 = MPI_Wtime();
323     mpi_t = mpi_t2 - mpi_t1;
324     MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
325     if (mpi_ct.my_rank == 0)
326       printf("Time to gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
327   }
328 #else
329   rs_global = raysum_create_from_det (opt_outfile, det);
330   raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
331 #endif
332   
333 #ifdef MPI_CT
334   time_end = MPI_Wtime();
335   if (mpi_ct.my_rank == 0) {
336     rs_global->calctime = time_end - time_start;
337     strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
338     if (opt_verbose) {
339       fprintf(stdout, "Remark: %s\n", rs_global->remark);
340       fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
341     }
342   }
343 #else
344   time_end = td_current_sec();
345   rs_global->calctime = time_end - time_start;
346   strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
347   if (opt_verbose) {
348     fprintf(stdout, "Remark: %s\n", rs_global->remark);
349     fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
350   }
351 #endif
352
353 #ifdef MPI_CT
354   if (mpi_ct.my_rank == 0) {
355     raysum_write (rs_global);
356     raysum_close (rs_global);
357   }
358 #else  
359   raysum_write (rs_global);
360   raysum_close (rs_global);
361 #endif
362
363   detector_free (det);
364
365   return (0);
366 }
367
368
369 #ifdef MPI_CT
370 void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
371 {
372   int iproc;
373   int end_work_unit;
374   int iw;
375
376   end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
377   for (iw = 0; iw <= end_work_unit; iw++) {
378     MPI_Send(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm);
379     MPI_Send(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm);
380     MPI_Send(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm);
381   }
382
383   if (mpi_ct.my_rank == 0) {
384     for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
385       end_work_unit = mpi_ct.start_work_unit[iproc] 
386         + mpi_ct.local_work_units[iproc] 
387         - 1;
388
389       if (opt_debug)
390         fprintf(stdout, "Recv rs data from process %d\n", iproc);
391
392       for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
393         MPI_Status status;
394
395         MPI_Recv(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm, &status);
396         MPI_Recv(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm, &status);
397         MPI_Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
398       }
399     }
400   }
401
402 }
403 #endif