Convert gcell to use boost::threads instead of omnithread.
[debian/gnuradio] / gcell / apps / benchmark_nop.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #if defined(HAVE_CONFIG_H)
23 #include <config.h>
24 #endif
25 #include <gcell/gc_job_manager.h>
26 #include <boost/date_time/posix_time/posix_time_types.hpp>
27 #include <getopt.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 // handle to embedded SPU executable that contains benchmark routines
32 // (The name of the variable (benchmark_procs) is the name of the spu executable.)
33 extern spe_program_handle_t benchmark_procs;
34
35 static gc_proc_id_t gcp_benchmark_udelay = GCP_UNKNOWN_PROC;
36
37 static void
38 init_jd(gc_job_desc *jd, unsigned int usecs)
39 {
40   jd->proc_id = gcp_benchmark_udelay;
41   jd->input.nargs = 1;
42   jd->input.arg[0].u32 = usecs;
43   jd->output.nargs = 0;
44   jd->eaa.nargs = 0;
45 }
46
47 static void
48 run_test(unsigned int nspes, unsigned int usecs, int njobs)
49 {
50   using namespace boost::posix_time;
51
52   static const int NJDS = 64;
53   int nsubmitted = 0;
54   int ncompleted = 0;
55   gc_job_desc *all_jds[NJDS];
56   gc_job_desc *jds[2][NJDS];
57   unsigned int njds[2];
58   unsigned int ci;              // current index
59   bool done[NJDS];
60   
61   gc_jm_options opts;
62   opts.program_handle = gc_program_handle_from_address(&benchmark_procs);
63   opts.nspes = nspes;
64   opts.gang_schedule = true;
65   gc_job_manager_sptr mgr = gc_make_job_manager(&opts);
66
67   if ((gcp_benchmark_udelay = mgr->lookup_proc("benchmark_udelay")) == GCP_UNKNOWN_PROC){
68     fprintf(stderr, "lookup_proc: failed to find \"benchmark_udelay\"\n");
69     return;
70   }
71
72   // allocate and init all job descriptors
73   for (int i = 0; i < NJDS; i++){
74     all_jds[i] = mgr->alloc_job_desc();
75     init_jd(all_jds[i], usecs);
76   }
77
78   ptime t_start(microsec_clock::universal_time());
79
80   ci = 0;
81   njds[0] = 0;
82   njds[1] = 0;
83   
84   // submit the first batch
85   for (int i = 0; i < NJDS; i++){
86     if (mgr->submit_job(all_jds[i])){
87       jds[ci][njds[ci]++] = all_jds[i];
88       nsubmitted++;
89     }
90     else {
91       printf("submit_job(jds[%d]) failed, status = %d\n",
92              i, all_jds[i]->status);
93     }
94   }
95   
96   while (ncompleted < njobs){
97     njds[ci^1] = 0;
98     int n = mgr->wait_jobs(njds[ci], jds[ci], done, GC_WAIT_ANY);
99     // printf("%2d\n", n);
100     if (n < 0){
101       fprintf(stderr, "mgr->wait_jobs failed\n");
102       break;
103     }
104     for (unsigned int i = 0; i < njds[ci]; i++){
105       if (!done[i]){    // remember for next iteration
106         jds[ci^1][njds[ci^1]++] = jds[ci][i];
107       }
108       else {
109         ncompleted++;
110         // printf("ncompleted = %7d\n", ncompleted);
111         if (nsubmitted < njobs){                    // submit another one
112           if (mgr->submit_job(jds[ci][i])){
113             jds[ci^1][njds[ci^1]++] = jds[ci][i];  // remember for next iter
114             nsubmitted++;
115           }
116           else {
117             printf("submit_job(jds[%d]) failed, status = %d\n",
118                    i, jds[ci][i]->status);
119           }
120         }
121       }
122     }
123     ci ^= 1;    // toggle current
124   }
125
126   // stop timing
127   ptime t_stop(microsec_clock::universal_time());
128   double delta = (t_stop - t_start).total_microseconds() * 1e-6;
129   printf("nspes: %2d  udelay: %4d  elapsed_time: %7.3f  njobs: %g  speedup: %6.3f\n",
130          mgr->nspes(), usecs, delta, (double) njobs,
131          njobs * usecs * 1e-6 / delta);
132 }
133
134 int
135 main(int argc, char **argv)
136 {
137   unsigned int nspes = 0;
138   unsigned int usecs = 0;
139   int njobs = 500000;
140   int ch;
141
142   while ((ch = getopt(argc, argv, "n:u:N:")) != EOF){
143     switch(ch){
144     case 'n':
145       nspes = strtol(optarg, 0, 0);
146       break;
147
148     case 'u':
149       usecs = strtol(optarg, 0, 0);
150       break;
151
152     case 'N':
153       njobs = strtol(optarg, 0, 0);
154       break;
155
156     case '?':
157     default:
158       fprintf(stderr, "usage: benchmark_nop [-n <nspes>] [-u <udelay>] [-N <njobs>]\n");
159       return 1;
160     }
161   }
162
163   run_test(nspes, usecs, njobs);
164   return 0;
165 }