envision-benchmark.c

Go to the documentation of this file.
00001 /*!@file Envision/envision-benchmark.c Benchmark envision */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Envision/envision-benchmark.c $
00035 // $Id: envision-benchmark.c 9096 2007-12-18 21:27:33Z rjpeters $
00036 //
00037 
00038 #ifndef ENVISION_APP_ENVISION_C_DEFINED
00039 #define ENVISION_APP_ENVISION_C_DEFINED
00040 
00041 #include "Envision/env_alloc.h"
00042 #include "Envision/env_c_math_ops.h"
00043 #include "Envision/env_image.h"
00044 #include "Envision/env_image_ops.h"
00045 #include "Envision/env_mt_visual_cortex.h"
00046 #include "Envision/env_params.h"
00047 #include "Envision/env_pthread_interface.h"
00048 #include "Envision/env_visual_cortex.h"
00049 
00050 #include <ctype.h>
00051 #include <stdarg.h>
00052 #include <stdio.h>
00053 #include <stdlib.h> // for atoi(), malloc(), free()
00054 #include <string.h>
00055 #include <sys/resource.h>
00056 #include <sys/time.h>
00057 
00058 // ######################################################################
00059 // Thunk to convert from env_size_t to size_t
00060 static void* malloc_thunk(env_size_t n)
00061 {
00062         return malloc(n);
00063 }
00064 
00065 // ######################################################################
00066 static void assert_handler(const char* what, int custom_msg,
00067                            const char* where, int line_no)
00068         __attribute__((noreturn));
00069 
00070 static void assert_handler(const char* what, int custom_msg,
00071                            const char* where, int line_no)
00072 {
00073         if (custom_msg)
00074                 fprintf(stderr, "Assertion failed (%s:%d):\n\t%s\n\n",
00075                         where, line_no, what);
00076         else
00077                 fprintf(stderr, "Assertion failed (%s:%d):\n\texpected '%s'\n\n",
00078                         where, line_no, what);
00079         abort();
00080 }
00081 
00082 // ######################################################################
00083 int main(int argc, const char** argv)
00084 {
00085         if (argc != 1 && argc != 2 && argc != 4 && argc != 5)
00086         {
00087                 fprintf(stderr,
00088                         "usage: %s [numframes=100] [width=512] [height=512] [multithreaded?]\n",
00089                         argv[0]);
00090                 return 1;
00091         }
00092 
00093         const int nframes = argc > 1 ? atoi(argv[1]) : 100;
00094         const int w = argc > 2 ? atoi(argv[2]) : 512;
00095         const int h = argc > 3 ? atoi(argv[3]) : 512;
00096         const int multithreaded = argc > 4 ? atoi(argv[4]) : 0;
00097         const struct env_dims indims = { w, h };
00098         const env_size_t insize = w * h;
00099 
00100         // Instantiate our various ModelComponents:
00101         struct env_params envp;
00102         env_params_set_defaults(&envp);
00103 
00104         envp.maxnorm_type = ENV_VCXNORM_MAXNORM;
00105         envp.scale_bits = 16;
00106 
00107         env_assert_set_handler(&assert_handler);
00108         if (multithreaded)
00109         {
00110                 env_init_pthread_alloc();
00111                 env_init_pthread_job_server();
00112         }
00113         env_allocation_init(&malloc_thunk, &free);
00114 
00115         if (nframes > 0)
00116         {
00117                 // allocate two images with different random
00118                 // (uninitialized) content (so that we excite the
00119                 // dynamic channels with non-static inputs, which may
00120                 // some day make a difference in execute time):
00121                 struct env_rgb_pixel *in1 = (struct env_rgb_pixel*)
00122                         env_allocate(insize * sizeof(struct env_rgb_pixel));
00123                 struct env_rgb_pixel *in2 = (struct env_rgb_pixel*)
00124                         env_allocate(insize * sizeof(struct env_rgb_pixel));
00125 
00126                 struct env_visual_cortex ivc;
00127                 env_visual_cortex_init(&ivc, &envp);
00128 
00129                 fprintf(stderr, "%s: START: %d frames %dx%d... ",
00130                         argv[0], nframes, w, h);
00131                 fflush(stderr);
00132 
00133                 struct timeval real1, real2;
00134                 gettimeofday(&real1, /* timezone */ 0);
00135 
00136                 struct rusage ru1, ru2;
00137                 getrusage(RUSAGE_SELF, &ru1);
00138 
00139                 for (int c = 0; c < nframes; ++c)
00140                 {
00141                         struct env_image ivcout = env_img_initializer;
00142                         struct env_image intens = env_img_initializer;
00143                         struct env_image color = env_img_initializer;
00144                         struct env_image ori = env_img_initializer;
00145 #                       ifdef ENV_WITH_DYNAMIC_CHANNELS
00146                         struct env_image flicker = env_img_initializer;
00147                         struct env_image motion = env_img_initializer;
00148 #                       endif
00149 
00150                         env_mt_visual_cortex_input(multithreaded,
00151                                                 &ivc, &envp, "visualcortex",
00152                                                 c & 1 ? in2 : in1, 0,
00153                                                 indims,
00154                                                 0,
00155                                                 0,
00156                                                 &ivcout,
00157                                                 &intens, &color, &ori
00158 #                                               ifdef ENV_WITH_DYNAMIC_CHANNELS
00159                                                 , &flicker, &motion
00160 #                                               endif
00161                                 );
00162 
00163                         env_visual_cortex_rescale_ranges(
00164                                 &ivcout, &intens, &color, &ori
00165 #                               ifdef ENV_WITH_DYNAMIC_CHANNELS
00166                                 , &flicker, &motion
00167 #                               endif
00168                                 );
00169 
00170                         env_img_make_empty(&ivcout);
00171                         env_img_make_empty(&intens);
00172                         env_img_make_empty(&color);
00173                         env_img_make_empty(&ori);
00174 #                       ifdef ENV_WITH_DYNAMIC_CHANNELS
00175                         env_img_make_empty(&flicker);
00176                         env_img_make_empty(&motion);
00177 #                       endif
00178                 }
00179 
00180                 getrusage(RUSAGE_SELF, &ru2);
00181 
00182                 gettimeofday(&real2, 0);
00183 
00184                 const double real_secs =
00185                         (real2.tv_sec - real1.tv_sec)
00186                         + (real2.tv_usec - real1.tv_usec)
00187                         / 1000000.0;
00188 
00189                 const double user_secs =
00190                         (ru2.ru_utime.tv_sec - ru1.ru_utime.tv_sec)
00191                         + (ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec)
00192                         / 1000000.0;
00193 
00194                 const double sys_secs =
00195                         (ru2.ru_stime.tv_sec - ru1.ru_stime.tv_sec)
00196                         + (ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec)
00197                         / 1000000.0;
00198 
00199                 const double frame_rate = nframes / real_secs;
00200                 const double msec_per_frame = (1000.0*real_secs) / nframes;
00201 
00202                 fprintf(stderr, "DONE.\n");
00203                 fprintf(stderr, "%s: real %.3fs; user %.3fs; "
00204                         "sys %.3fs\n", argv[0], real_secs,
00205                         user_secs, sys_secs);
00206                 fprintf(stderr, "%s: %.3ffps; %.3fmsec/frame\n",
00207                         argv[0], frame_rate, msec_per_frame);
00208                 env_visual_cortex_destroy(&ivc);
00209                 env_deallocate(in1);
00210                 env_deallocate(in2);
00211         }
00212         env_allocation_cleanup();
00213 
00214         return 0;
00215 }
00216 
00217 // ######################################################################
00218 /* So things look consistent in everyone's emacs... */
00219 /* Local Variables: */
00220 /* indent-tabs-mode: nil */
00221 /* c-file-style: "linux" */
00222 /* End: */
00223 
00224 #endif // ENVISION_APP_ENVISION_C_DEFINED
Generated on Sun May 8 08:40:38 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3