node.h 15.1 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_MTAPI_NODE_H_
#define EMBB_MTAPI_NODE_H_

#include <embb/base/memory_allocation.h>
31
#include <embb/base/function.h>
32
#include <embb/mtapi/c/mtapi.h>
Marcus Winter committed
33
#include <embb/mtapi/execution_policy.h>
34 35
#include <embb/mtapi/status_exception.h>
#include <embb/mtapi/node_attributes.h>
36 37
#include <embb/mtapi/group.h>
#include <embb/mtapi/queue.h>
38 39 40
#include <embb/mtapi/task.h>
#include <embb/mtapi/task_attributes.h>
#include <embb/mtapi/job.h>
41
#include <embb/mtapi/action.h>
42 43 44 45 46 47
#include <embb/mtapi/task_context.h>

#ifdef GetJob
#undef GetJob
#endif

Marcus Winter committed
48 49 50 51
#ifdef MTAPI_CPP_AUTOMATIC_INITIALIZE
#define MTAPI_CPP_AUTOMATIC_DOMAIN_ID 1
#define MTAPI_CPP_AUTOMATIC_NODE_ID 1
#endif
52
#define EMBB_MTAPI_FUNCTION_JOB_ID 2
53 54 55 56 57 58 59 60 61 62 63 64 65 66

namespace embb {

namespace base {

class Allocation;

} // namespace base

namespace mtapi {

/**
 * A singleton representing the MTAPI runtime.
 *
67
 * \ingroup CPP_MTAPI
68 69 70
 */
class Node {
 public:
71 72 73
  /**
   * Function type for simple SMP interface.
   */
74 75
  typedef embb::base::Function<void, TaskContext &> SMPFunction;

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * Initializes the runtime singleton using default values:
   *   - all available cores will be used
   *   - maximum number of tasks is 1024
   *   - maximum number of groups is 128
   *   - maximum number of queues is 16
   *   - maximum queue capacity is 1024
   *   - maximum number of priorities is 4.
   *
   * \notthreadsafe
   * \throws ErrorException if the singleton was already initialized or the
   *         Node could not be initialized.
   * \memory Allocates about 200kb of memory.
   */
  static void Initialize(
    mtapi_domain_t domain_id,          /**< [in] The domain id to use */
    mtapi_node_t node_id               /**< [in] The node id to use */
93
  );
94 95 96 97 98 99 100 101 102 103 104 105

  /**
   * Initializes the runtime singleton.
   * \notthreadsafe
   * \throws ErrorException if the singleton was already initialized or the
   *         Node could not be initialized.
   * \memory Allocates some memory depending on the values given.
   */
  static void Initialize(
    mtapi_domain_t domain_id,          /**< [in] The domain id to use */
    mtapi_node_t node_id,              /**< [in] The node id to use */
    NodeAttributes const & attributes  /**< [in] Attributes to use */
106
    );
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

  /**
   * Checks if runtime is initialized.
   * \return \c true if the Node singleton is already initialized, false
   *         otherwise
   * \waitfree
   */
  static bool IsInitialized() {
    return NULL != node_instance_;
  }

  /**
   * Gets the instance of the runtime system.
   * \return Reference to the Node singleton
   * \threadsafe
   */
123
  static Node & GetInstance();
124 125 126 127 128 129

  /**
   * Shuts the runtime system down.
   * \throws ErrorException if the singleton is not initialized.
   * \notthreadsafe
   */
130
  static void Finalize();
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

  /**
   * Returns the number of available cores.
   * \return The number of available cores
   * \waitfree
   */
  mtapi_uint_t GetCoreCount() const {
    return core_count_;
  }

  /**
   * Returns the number of worker threads.
   * \return The number of worker threads.
   * \waitfree
   */
  mtapi_uint_t GetWorkerThreadCount() const {
    return worker_thread_count_;
  }

  /**
151 152 153 154 155 156 157 158 159
   * Returns the number of available queues.
   * \return The number of available queues
   * \waitfree
   */
  mtapi_uint_t GetQueueCount() const {
    return queue_count_;
  }

  /**
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
   * Returns the number of available groups.
   * \return The number of available groups
   * \waitfree
   */
  mtapi_uint_t GetGroupCount() const {
    return group_count_;
  }

  /**
    * Returns the number of available tasks.
    * \return The number of available tasks
    * \waitfree
    */
  mtapi_uint_t GetTaskLimit() const {
    return task_limit_;
  }

177 178 179 180 181 182
  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
   * \threadsafe
   */
183
  Task Start(
184
    SMPFunction const & func           /**< Function to use for the task. */
185 186 187 188 189 190 191
    ) {
    Job job = GetJob(EMBB_MTAPI_FUNCTION_JOB_ID);
    void * res = NULL;
    return Start(
      job, embb::base::Allocation::New<SMPFunction>(func), res);
  }

192 193 194 195 196 197
  /**
   * Starts a new Task with a given affinity and priority.
   *
   * \returns The handle to the started Task.
   * \threadsafe
   */
198
  Task Start(
199 200 201
    SMPFunction const & func,          /**< Function to use for the task. */
    ExecutionPolicy const & policy     /**< Affinity and priority of the
                                            task. */
202 203 204 205 206 207 208 209 210
  ) {
    Job job = GetJob(EMBB_MTAPI_FUNCTION_JOB_ID);
    void * res = NULL;
    TaskAttributes task_attr;
    task_attr.SetPolicy(policy);
    return Start(
      job, embb::base::Allocation::New<SMPFunction>(func), res, task_attr);
  }

211
  /**
212 213 214
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
215
   * \threadsafe
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
   */
  template <typename ARGS, typename RES>
  Task Start(
    mtapi_task_id_t task_id,           /**< A user defined ID of the Task. */
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results,                     /**< Pointer to the results. */
    TaskAttributes const & attributes  /**< Attributes of the Task */
    ) {
    return Start(task_id,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      &attributes.GetInternal());
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
236
   * \threadsafe
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
   */
  template <typename ARGS, typename RES>
  Task Start(
    mtapi_task_id_t task_id,           /**< A user defined ID of the Task. */
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results                      /**< Pointer to the results. */
    ) {
    return Start(task_id,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      MTAPI_DEFAULT_TASK_ATTRIBUTES);
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
256
   * \threadsafe
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
   */
  template <typename ARGS, typename RES>
  Task Start(
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results,                     /**< Pointer to the results. */
    TaskAttributes const & attributes  /**< Attributes of the Task */
    ) {
    return Start(MTAPI_TASK_ID_NONE,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      &attributes.GetInternal());
  }

  /**
   * Starts a new Task.
   *
   * \returns The handle to the started Task.
276
   * \threadsafe
277 278 279 280 281 282 283 284 285 286 287 288 289 290
   */
  template <typename ARGS, typename RES>
  Task Start(
    Job const & job,                   /**< The Job to execute. */
    const ARGS * arguments,            /**< Pointer to the arguments. */
    RES * results                      /**< Pointer to the results. */
    ) {
    return Start(MTAPI_TASK_ID_NONE,
      job.GetInternal(),
      arguments, internal::SizeOfType<ARGS>(),
      results, internal::SizeOfType<RES>(),
      MTAPI_DEFAULT_TASK_ATTRIBUTES);
  }

291
  /**
292 293
   * Retrieves a handle to the Job identified by \c job_id within the domain
   * of the local Node.
294
   *
295
   * \returns The handle to the requested Job.
296
   * \waitfree
297 298
   */
  Job GetJob(
299
    mtapi_job_id_t job_id              /**< [in] The id of the job */
300
    ) {
301 302 303
    return Job(job_id, domain_id_);
  }

304
  /**
305
   * Retrieves a handle to the Job identified by \c job_id and \c domain_id.
306
   *
307
   * \returns The handle to the requested Job.
308
   * \waitfree
309 310
   */
  Job GetJob(
311 312
    mtapi_job_id_t job_id,             /**< [in] The id of the job */
    mtapi_domain_t domain_id           /**< [in] The domain id to use */
313
    ) {
314 315 316 317 318
    return Job(job_id, domain_id);
  }

  /**
   * Constructs an Action.
319
   *
320
   * \returns The handle to the new Action.
321
   * \lockfree
322 323 324 325 326 327 328 329
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    const void * node_local_data,      /**< Node local data available to all
                                       Tasks using this Action */
    mtapi_size_t node_local_data_size, /**< Size of node local data */
    ActionAttributes const & attributes
330
                                       /**< Attributes of the Action */
331 332 333 334 335 336 337
    ) {
    return Action(job_id, func, node_local_data, node_local_data_size,
      &attributes.GetInternal());
  }

  /**
   * Constructs an Action.
338
   *
339
   * \returns The handle to the new Action.
340
   * \lockfree
341 342 343 344 345 346 347 348 349 350 351 352 353 354
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    const void * node_local_data,      /**< Node local data available to all
                                       Tasks using this Action */
    mtapi_size_t node_local_data_size  /**< Size of node local data */
    ) {
    return Action(job_id, func, node_local_data, node_local_data_size,
      MTAPI_DEFAULT_ACTION_ATTRIBUTES);
  }

  /**
   * Constructs an Action.
355
   *
356
   * \returns The handle to the new Action.
357
   * \lockfree
358 359 360 361 362
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func,      /**< The action function */
    ActionAttributes const & attributes
363
                                       /**< Attributes of the Action */
364 365 366 367 368 369
    ) {
    return Action(job_id, func, MTAPI_NULL, 0, &attributes.GetInternal());
  }

  /**
   * Constructs an Action.
370
   *
371
   * \returns The handle to the new Action.
372
   * \lockfree
373 374 375 376 377 378 379 380 381 382
   */
  Action CreateAction(
    mtapi_job_id_t job_id,             /**< Job ID the Action belongs to */
    mtapi_action_function_t func       /**< The action function */
    ) {
    return Action(job_id, func, MTAPI_NULL, 0, MTAPI_DEFAULT_ACTION_ATTRIBUTES);
  }

  /**
   * Constructs a Group object with default attributes.
383
   *
384
   * \returns The handle to the new Group.
385
   * \lockfree
386 387 388 389 390 391 392
   */
  Group CreateGroup() {
    return Group(MTAPI_GROUP_ID_NONE, MTAPI_DEFAULT_GROUP_ATTRIBUTES);
  }

  /**
   * Constructs a Group object with default attributes and the given ID.
393
   *
394
   * \returns The handle to the new Group.
395
   * \lockfree
396 397 398 399 400 401 402 403 404
   */
  Group CreateGroup(
    mtapi_group_id_t id                /**< A user defined ID of the Group. */
    ) {
    return Group(id, MTAPI_DEFAULT_GROUP_ATTRIBUTES);
  }

  /**
   * Constructs a Group object using the given Attributes.
405
   *
406
   * \returns The handle to the new Group.
407
   * \lockfree
408 409
   */
  Group CreateGroup(
410
    GroupAttributes const & group_attr /**< The GroupAttributes to use. */
411
    ) {
412 413 414 415 416
    return Group(MTAPI_GROUP_ID_NONE, &group_attr.GetInternal());
  }

  /**
   * Constructs a Group object with given attributes and ID.
417
   *
418
   * \returns The handle to the new Group.
419
   * \lockfree
420 421 422 423 424 425 426 427 428 429
   */
  Group CreateGroup(
    mtapi_group_id_t id,               /**< A user defined ID of the Group. */
    GroupAttributes const & group_attr /**< The GroupAttributes to use. */
    ) {
    return Group(id, &group_attr.GetInternal());
  }

  /**
   * Constructs a Queue with the given Job and default attributes.
430
   *
431
   * \returns The handle to the new Queue.
432
   * \lockfree
433 434 435 436 437 438 439 440 441
   */
  Queue CreateQueue(
    Job & job                          /**< The Job to use for the Queue. */
    ) {
    return Queue(MTAPI_QUEUE_ID_NONE, job, MTAPI_DEFAULT_QUEUE_ATTRIBUTES);
  }

  /**
   * Constructs a Queue with the given Job and QueueAttributes.
442
   *
443
   * \returns The handle to the new Queue.
444
   * \lockfree
445 446 447 448 449 450 451 452
   */
  Queue CreateQueue(
    Job const & job,                   /**< The Job to use for the Queue. */
    QueueAttributes const & attr       /**< The attributes to use. */
    ) {
    return Queue(MTAPI_QUEUE_ID_NONE, job, &attr.GetInternal());
  }

453 454 455 456 457 458 459 460 461 462 463 464 465
  friend class embb::base::Allocation;

 private:
  // not copyable
  Node(Node const & node);
  Node const & operator=(Node const & other);

  Node(
    mtapi_domain_t domain_id,
    mtapi_node_t node_id,
    NodeAttributes const & attr) {
    mtapi_status_t status;
    mtapi_info_t info;
466
    queue_count_ = attr.GetInternal().max_queues;
467 468
    group_count_ = attr.GetInternal().max_groups;
    task_limit_ = attr.GetInternal().max_tasks;
469 470 471 472 473 474 475
    mtapi_initialize(domain_id, node_id, &attr.GetInternal(), &info, &status);
    internal::CheckStatus(status);

    core_count_ = info.hardware_concurrency;
    worker_thread_count_ = embb_core_set_count(
      &attr.GetInternal().core_affinity);

476
    domain_id_ = domain_id;
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  }

  Task Start(
    mtapi_task_id_t task_id,
    mtapi_job_hndl_t job,
    const void * arguments,
    mtapi_size_t arguments_size,
    void * results,
    mtapi_size_t results_size,
    mtapi_task_attributes_t const * attributes
    ) {
    mtapi_status_t status;
    mtapi_task_hndl_t task_hndl =
      mtapi_task_start(task_id, job, arguments, arguments_size,
      results, results_size, attributes, MTAPI_GROUP_NONE,
      &status);
    internal::CheckStatus(status);
    return Task(task_hndl);
  }

497 498 499 500 501 502 503 504 505 506
  static void ActionFunction(
    const void* args,
    mtapi_size_t /*args_size*/,
    void* /*result_buffer*/,
    mtapi_size_t /*result_buffer_size*/,
    const void* /*node_local_data*/,
    mtapi_size_t /*node_local_data_size*/,
    mtapi_task_context_t * context) {
    TaskContext task_context(context);
    embb::base::Function<void, TaskContext &> * func =
Marcus Winter committed
507 508
      reinterpret_cast<embb::base::Function<void, TaskContext &>*>(
        const_cast<void*>(args));
509 510 511 512
    (*func)(task_context);
    embb::base::Allocation::Delete(func);
  }

513 514
  static embb::mtapi::Node * node_instance_;

515
  mtapi_domain_t domain_id_;
516 517
  mtapi_uint_t core_count_;
  mtapi_uint_t worker_thread_count_;
518
  mtapi_uint_t queue_count_;
519 520
  mtapi_uint_t group_count_;
  mtapi_uint_t task_limit_;
521
  Action function_action_;
522 523 524 525 526 527
};

} // namespace mtapi
} // namespace embb

#endif // EMBB_MTAPI_NODE_H_