2d3611fd9a113be94c327849471e209cedd2d7b9
[debian/gnuradio] / gcell / src / apps / benchmark_nop.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 "gc_job_manager.h"
26 #include "mb_time.h"
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   static const int NJDS = 64;
51   int nsubmitted = 0;
52   int ncompleted = 0;
53   gc_job_desc *all_jds[NJDS];
54   gc_job_desc *jds[2][NJDS];
55   unsigned int njds[2];
56   unsigned int ci;              // current index
57   bool done[NJDS];
58   
59   gc_jm_options opts;
60   opts.program_handle = &benchmark_procs;
61   opts.nspes = nspes;
62   opts.gang_schedule = true;
63   gc_job_manager *mgr = gc_make_job_manager(&opts);
64
65   if ((gcp_benchmark_udelay = mgr->lookup_proc("benchmark_udelay")) == GCP_UNKNOWN_PROC){
66     fprintf(stderr, "lookup_proc: failed to find \"benchmark_udelay\"\n");
67     return;
68   }
69
70   // allocate and init all job descriptors
71   for (int i = 0; i < NJDS; i++){
72     all_jds[i] = mgr->alloc_job_desc();
73     init_jd(all_jds[i], usecs);
74   }
75
76   mb_time t_start = mb_time::time();
77
78   ci = 0;
79   njds[0] = 0;
80   njds[1] = 0;
81   
82   // submit the first batch
83   for (int i = 0; i < NJDS; i++){
84     if (mgr->submit_job(all_jds[i])){
85       jds[ci][njds[ci]++] = all_jds[i];
86       nsubmitted++;
87     }
88     else {
89       printf("submit_job(jds[%d]) failed, status = %d\n",
90              i, all_jds[i]->status);
91     }
92   }
93   
94   while (ncompleted < njobs){
95     njds[ci^1] = 0;
96     int n = mgr->wait_jobs(njds[ci], jds[ci], done, GC_WAIT_ANY);
97     // printf("%2d\n", n);
98     if (n < 0){
99       fprintf(stderr, "mgr->wait_jobs failed\n");
100       break;
101     }
102     for (unsigned int i = 0; i < njds[ci]; i++){
103       if (!done[i]){    // remember for next iteration
104         jds[ci^1][njds[ci^1]++] = jds[ci][i];
105       }
106       else {
107         ncompleted++;
108         // printf("ncompleted = %7d\n", ncompleted);
109         if (nsubmitted < njobs){                    // submit another one
110           if (mgr->submit_job(jds[ci][i])){
111             jds[ci^1][njds[ci^1]++] = jds[ci][i];  // remember for next iter
112             nsubmitted++;
113           }
114           else {
115             printf("submit_job(jds[%d]) failed, status = %d\n",
116                    i, jds[ci][i]->status);
117           }
118         }
119       }
120     }
121     ci ^= 1;    // toggle current
122   }
123
124   // stop timing
125   mb_time t_stop = mb_time::time();
126   double delta = (t_stop - t_start).double_time();
127   printf("nspes: %2d  udelay: %4d  elapsed_time: %7.3f  njobs: %g  speedup: %6.3f\n",
128          mgr->nspes(), usecs, delta, (double) njobs,
129          njobs * usecs * 1e-6 / delta);
130
131   delete mgr;
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 }