3 * Copyright 2007,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
22 #ifndef INCLUDED_GC_JOB_MANAGER_H
23 #define INCLUDED_GC_JOB_MANAGER_H
25 #include <boost/utility.hpp>
26 #include <boost/shared_ptr.hpp>
31 #include "gc_job_desc.h"
34 typedef boost::shared_ptr<gc_job_manager> gc_job_manager_sptr;
35 typedef boost::shared_ptr<spe_program_handle_t> spe_program_handle_sptr;
38 * \brief Return a boost::shared_ptr to an spe_program_handle_t
40 * \param filename is the name of the SPE ELF executable to open.
42 * Calls spe_image_open to open the file. If successful returns a
43 * boost::shared_ptr that will call spe_image_close when it's time to
46 * Returns the equivalent of the NULL pointer if the file cannot be
47 * opened, or if it's not an SPE ELF object file.
49 * \sa gc_program_handle_from_address
51 spe_program_handle_sptr
52 gc_program_handle_from_filename(const std::string &filename);
55 * \brief Return a boost::shared_ptr to an spe_program_handle_t
57 * \param handle is a non-zero pointer to an embedded SPE image.
59 * If successful returns a boost::shared_ptr that does nothing when
60 * it's time to free the object.
62 * \sa gc_program_handle_from_filename
64 spe_program_handle_sptr
65 gc_program_handle_from_address(spe_program_handle_t *handle);
68 * \brief map gc_job_status_t into a string
71 gc_job_status_string(gc_job_status_t status);
74 * \brief Options that configure the job_manager.
75 * The default values are reasonable.
77 struct gc_jm_options {
78 unsigned int max_jobs; // max # of job descriptors in system
79 unsigned int max_client_threads; // max # of client threads of job manager
80 unsigned int nspes; // how many SPEs shall we use? 0 -> all of them
81 bool gang_schedule; // shall we gang schedule?
82 bool use_affinity; // shall we try for affinity (FIXME not implmented)
83 bool enable_logging; // shall we log SPE events?
84 uint32_t log2_nlog_entries; // log2 of number of log entries (default is 12 == 4k)
85 spe_program_handle_sptr program_handle; // program to load into SPEs
88 max_jobs(0), max_client_threads(0), nspes(0),
89 gang_schedule(true), use_affinity(false),
90 enable_logging(false), log2_nlog_entries(12)
103 class gc_exception : public std::runtime_error
106 gc_exception(const std::string &msg);
109 class gc_unknown_proc : public gc_exception
112 gc_unknown_proc(const std::string &msg);
115 class gc_bad_alloc : public gc_exception
118 gc_bad_alloc(const std::string &msg);
121 class gc_bad_align : public gc_exception
124 gc_bad_align(const std::string &msg);
127 class gc_bad_submit : public gc_exception
130 gc_bad_submit(const std::string &name, gc_job_status_t status);
134 * \brief Create an instance of the job manager
137 gc_make_job_manager(const gc_jm_options *options = 0);
141 * \brief Abstract class that manages SPE jobs.
143 * There is typically a single instance derived from this class.
144 * It is safe to call its methods from any thread.
146 class gc_job_manager : boost::noncopyable
149 gc_job_manager(const gc_jm_options *options = 0);
151 virtual ~gc_job_manager();
154 * Stop accepting new jobs. Wait for existing jobs to complete.
155 * Return all managed SPE's to the system.
157 virtual bool shutdown() = 0;
160 * \brief Return number of SPE's currently allocated to job manager.
162 virtual int nspes() const = 0;
165 * \brief Return a pointer to a properly aligned job descriptor,
166 * or throws gc_bad_alloc if there are none available.
168 virtual gc_job_desc *alloc_job_desc() = 0;
171 *! Free a job descriptor previously allocated with alloc_job_desc()
173 * \param[in] jd pointer to job descriptor to free.
175 virtual void free_job_desc(gc_job_desc *jd) = 0;
178 * \brief Submit a job for asynchronous processing on an SPE.
180 * \param[in] jd pointer to job description
182 * The caller must not read or write the job description
183 * or any of the memory associated with any indirect arguments
184 * until after calling wait_job.
186 * \returns true iff the job was successfully enqueued.
187 * If submit_job returns false, check jd->status for additional info.
189 virtual bool submit_job(gc_job_desc *jd) = 0;
192 * \brief Wait for job to complete.
194 * A thread may only wait for jobs which it submitted.
196 * \returns true if sucessful, else false.
199 wait_job(gc_job_desc *jd) = 0;
202 * \brief wait for 1 or more jobs to complete.
204 * \param[input] njobs is the length of arrays \p jd and \p done.
205 * \param[input] jd are the jobs that are to be waited for.
206 * \param[output] done indicates whether the corresponding job is complete.
207 * \param[input] mode indicates whether to wait for ALL or ANY of the jobs
208 * in \p jd to complete.
210 * A thread may only wait for jobs which it submitted.
212 * \returns number of jobs completed, or -1 if error.
213 * The caller must examine the status field of each job to confirm
214 * successful completion of the job.
217 wait_jobs(unsigned int njobs,
218 gc_job_desc *jd[], bool done[], gc_wait_mode mode) = 0;
221 * Return the maximum number of bytes of EA arguments that may be
222 * copied to or from the SPE in a single job. The limit applies
223 * independently to the "get" and "put" args.
224 * \sa gc_job_desc_t, gc_job_ea_args_t
226 virtual int ea_args_maxsize() = 0;
229 * Return gc_proc_id_t associated with spu procedure \p proc_name if one
230 * exists, otherwise throws gc_unknown_proc.
232 virtual gc_proc_id_t lookup_proc(const std::string &proc_name) = 0;
235 * Return a vector of all known spu procedure names.
237 virtual std::vector<std::string> proc_names() = 0;
240 * \brief Set the singleton gc_job_manager instance.
241 * \param mgr is the job manager instance.
243 * The singleton is weakly held, thus the caller must maintain
244 * a reference to the mgr for the duration. (If we held the
245 * manager strongly, the destructor would never be called, and the
246 * resources (SPEs) would not be returned.) Bottom line: the
247 * caller is responsible for life-time management.
249 static void set_singleton(gc_job_manager_sptr mgr);
252 * \brief Retrieve the singleton gc_job_manager instance.
254 * Returns the singleton gc_job_manager instance or raises
255 * boost::bad_weak_ptr if the singleton is empty.
257 static gc_job_manager_sptr singleton();
260 virtual void set_debug(int debug);
265 #endif /* INCLUDED_GC_JOB_MANAGER_H */