一个混合 MPI_Init() 和 gtk_init() 的实例序
int main (int argc, char *argv[])
{
int i, n;
pthread_t draw_thread;
#ifdef USEMPI
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
// parse cmdline args
while((i = getopt(argc, argv, "d:r:w:n:shx:y:")) != EOF) {
switch(i) {
case 'd':
debug = atoi(optarg);
break;
case 'x':
initpos[0] = atoi(optarg);
break;
case 'y':
initpos[1] = atoi(optarg);
break;
case 's':
showgui = 0;
break;
case 'n':
samples = atoi(optarg);
break;
case 'r':
n = strlen(optarg);
if (sequence!=NULL && rank==0) {
printf("Can't use both -r and -w simultaneously!!!\n");
exit(1);
}
sequence = calloc(n+1, sizeof(char));
strcpy(sequence, optarg);
pt_mode = PT_READ;
break;
case 'w':
n = strlen(optarg);
if (sequence!=NULL && rank==0) {
printf("Can't use both -r and -w simultaneously!!!\n");
exit(1);
}
sequence = calloc(n+1, sizeof(char));
strcpy(sequence, optarg);
pt_mode = PT_WRITE;
break;
case 'h':
default:
if (rank==0) {
printf("Usage: partracker [options]\n"\
"\n"\
" [-d <level>]\n"\
" Displays <level> debug information\n"\
"\n"\
" [-r <basename]\n"\
" Reads the frames from a sequence of jpg files named\n"\
" <basename>*.jpg instead of from a video4linux camera.\n"\
" For example:\n"\
" partracker -r test\n"\
" would read all files named test*.jpg in sorted order\n"\
"\n"\
" [-w <basename>]\n"\
" Writes the raw frames (before processing them)\n"\
" to a sequence of jpg files starting at <basename>0000.jpg\n"\
"\n"\
" [-x <xpos>]\n"\
" Sets the initial x-position of the tracker to <xpos>\n"\
"\n"\
" [-y <ypos>]\n"\
" Sets the initial y-position of the tracker to <ypos>\n"\
" Default initial position is (30,30)\n"\
"\n"\
" [-n <samples]\n"\
" Sets the number of samples per iteration of the\n"\
" condensation filter to <samples> (default: 1000)\n"\
"\n"\
" [-s]\n"\
" Suppresses GUI display for faster processing\n");
}
exit(0);
}
}
if (rank==0) {
printf("%d/%d: partracker v0.01: using d=%d, n=%d, %s %s, %s, (%d,%d)\n",
rank, num_nodes, debug, samples,
(sequence==NULL ? "" : (pt_mode==PT_READ ? "reading from" : "writing to")),
(sequence==NULL? "<live feed>" : sequence),
(showgui ? "GUI" : "no GUI"), initpos[0], initpos[1]);
}
#ifdef NOCAM
if (pt_mode != PT_READ) {
printf("No capture device available!!!\nPlease specify a read sequence. Try partracker -h for usage information.\nQuitting...\n");
exit(1);
}
#endif
g_thread_init(NULL);
#ifdef GUI
if (rank==0) {
gtk_set_locale();
gtk_init(&argc, &argv);
gdk_rgb_init();
}
#endif
// Initialize the storage structures
memset(&gCamera,0,sizeof(struct CaptureData));
gCamera.current_display = SMALL_IMAGE;
gCamera.desired.capture_size = ECS_VGA;
pthread_mutex_init(&gCamera.desired.lock, NULL );
pthread_cond_init(&gCamera.desired.cond, NULL );
pthread_mutex_init(&gCamera.image.lock, NULL );
PartrackerInit();
open_camera( &gCamera );
set_camera_info( &gCamera );
get_camera_info( &gCamera );
#ifdef GUI
if (rank==0 && showgui) {
mainwindow = create_mainwindow();
gCamera.displayarea[SMALL_IMAGE].drawing_area = lookup_widget( mainwindow, "drawingarea" );
g_assert( gCamera.displayarea[SMALL_IMAGE].drawing_area );
init_capture_widgets(&gCamera);
gtk_widget_show (mainwindow);
}
#endif
if ( setup_memory_mapping( &gCamera ))
exit(1);
gCamera.timer = g_timer_new();
g_timer_start( gCamera.timer );
#ifdef GUI
pthread_create(&draw_thread, NULL, (void *)&draw_image, (void *)NULL);
gdk_threads_enter();
gtk_main ();
gdk_threads_leave();
gQuitFlag = TRUE;
pthread_join( draw_thread, NULL );
#else
draw_image();
#endif
// clean up
close_camera( &gCamera );
if (sequence!=NULL)
free(sequence);
PartrackerCleanup();
#ifdef GUI
for ( i = 0 ; i < 2 ; i++ ) {
if ( gCamera.displayarea[i].pixmap ) {
gdk_pixmap_unref(gCamera.displayarea[i].pixmap);
gCamera.displayarea[i].pixmap = NULL;
}
}
#endif
#ifdef USEMPI
MPI_Finalize();
#endif
g_timer_stop( gCamera.timer );
g_timer_destroy(gCamera.timer);
pthread_mutex_destroy( &gCamera.desired.lock );
pthread_cond_destroy( &gCamera.desired.cond );
pthread_mutex_destroy( &gCamera.image.lock );
return 0;
}
{
int i, n;
pthread_t draw_thread;
#ifdef USEMPI
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
// parse cmdline args
while((i = getopt(argc, argv, "d:r:w:n:shx:y:")) != EOF) {
switch(i) {
case 'd':
debug = atoi(optarg);
break;
case 'x':
initpos[0] = atoi(optarg);
break;
case 'y':
initpos[1] = atoi(optarg);
break;
case 's':
showgui = 0;
break;
case 'n':
samples = atoi(optarg);
break;
case 'r':
n = strlen(optarg);
if (sequence!=NULL && rank==0) {
printf("Can't use both -r and -w simultaneously!!!\n");
exit(1);
}
sequence = calloc(n+1, sizeof(char));
strcpy(sequence, optarg);
pt_mode = PT_READ;
break;
case 'w':
n = strlen(optarg);
if (sequence!=NULL && rank==0) {
printf("Can't use both -r and -w simultaneously!!!\n");
exit(1);
}
sequence = calloc(n+1, sizeof(char));
strcpy(sequence, optarg);
pt_mode = PT_WRITE;
break;
case 'h':
default:
if (rank==0) {
printf("Usage: partracker [options]\n"\
"\n"\
" [-d <level>]\n"\
" Displays <level> debug information\n"\
"\n"\
" [-r <basename]\n"\
" Reads the frames from a sequence of jpg files named\n"\
" <basename>*.jpg instead of from a video4linux camera.\n"\
" For example:\n"\
" partracker -r test\n"\
" would read all files named test*.jpg in sorted order\n"\
"\n"\
" [-w <basename>]\n"\
" Writes the raw frames (before processing them)\n"\
" to a sequence of jpg files starting at <basename>0000.jpg\n"\
"\n"\
" [-x <xpos>]\n"\
" Sets the initial x-position of the tracker to <xpos>\n"\
"\n"\
" [-y <ypos>]\n"\
" Sets the initial y-position of the tracker to <ypos>\n"\
" Default initial position is (30,30)\n"\
"\n"\
" [-n <samples]\n"\
" Sets the number of samples per iteration of the\n"\
" condensation filter to <samples> (default: 1000)\n"\
"\n"\
" [-s]\n"\
" Suppresses GUI display for faster processing\n");
}
exit(0);
}
}
if (rank==0) {
printf("%d/%d: partracker v0.01: using d=%d, n=%d, %s %s, %s, (%d,%d)\n",
rank, num_nodes, debug, samples,
(sequence==NULL ? "" : (pt_mode==PT_READ ? "reading from" : "writing to")),
(sequence==NULL? "<live feed>" : sequence),
(showgui ? "GUI" : "no GUI"), initpos[0], initpos[1]);
}
#ifdef NOCAM
if (pt_mode != PT_READ) {
printf("No capture device available!!!\nPlease specify a read sequence. Try partracker -h for usage information.\nQuitting...\n");
exit(1);
}
#endif
g_thread_init(NULL);
#ifdef GUI
if (rank==0) {
gtk_set_locale();
gtk_init(&argc, &argv);
gdk_rgb_init();
}
#endif
// Initialize the storage structures
memset(&gCamera,0,sizeof(struct CaptureData));
gCamera.current_display = SMALL_IMAGE;
gCamera.desired.capture_size = ECS_VGA;
pthread_mutex_init(&gCamera.desired.lock, NULL );
pthread_cond_init(&gCamera.desired.cond, NULL );
pthread_mutex_init(&gCamera.image.lock, NULL );
PartrackerInit();
open_camera( &gCamera );
set_camera_info( &gCamera );
get_camera_info( &gCamera );
#ifdef GUI
if (rank==0 && showgui) {
mainwindow = create_mainwindow();
gCamera.displayarea[SMALL_IMAGE].drawing_area = lookup_widget( mainwindow, "drawingarea" );
g_assert( gCamera.displayarea[SMALL_IMAGE].drawing_area );
init_capture_widgets(&gCamera);
gtk_widget_show (mainwindow);
}
#endif
if ( setup_memory_mapping( &gCamera ))
exit(1);
gCamera.timer = g_timer_new();
g_timer_start( gCamera.timer );
#ifdef GUI
pthread_create(&draw_thread, NULL, (void *)&draw_image, (void *)NULL);
gdk_threads_enter();
gtk_main ();
gdk_threads_leave();
gQuitFlag = TRUE;
pthread_join( draw_thread, NULL );
#else
draw_image();
#endif
// clean up
close_camera( &gCamera );
if (sequence!=NULL)
free(sequence);
PartrackerCleanup();
#ifdef GUI
for ( i = 0 ; i < 2 ; i++ ) {
if ( gCamera.displayarea[i].pixmap ) {
gdk_pixmap_unref(gCamera.displayarea[i].pixmap);
gCamera.displayarea[i].pixmap = NULL;
}
}
#endif
#ifdef USEMPI
MPI_Finalize();
#endif
g_timer_stop( gCamera.timer );
g_timer_destroy(gCamera.timer);
pthread_mutex_destroy( &gCamera.desired.lock );
pthread_cond_destroy( &gCamera.desired.cond );
pthread_mutex_destroy( &gCamera.image.lock );
return 0;
}