graph.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 University of Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "socl.h"
  17. #include "graph.h"
  18. #include "event.h"
  19. static pthread_spinlock_t graph_lock;
  20. static graph_node graph_nodes = NULL;
  21. /**
  22. * Initialize graph structure
  23. */
  24. void graph_init(void) {
  25. pthread_spin_init(&graph_lock, PTHREAD_PROCESS_PRIVATE);
  26. }
  27. /**
  28. * Release graph structure
  29. */
  30. void graph_destroy(void) {
  31. pthread_spin_destroy(&graph_lock);
  32. }
  33. /**
  34. * Initialize a graph node
  35. */
  36. void graph_node_init(graph_node node) {
  37. node->id = -1;
  38. node->next = NULL;
  39. }
  40. /**
  41. * Store a node in the graph
  42. */
  43. void graph_store(void * node) {
  44. pthread_spin_lock(&graph_lock);
  45. graph_node n = (graph_node)node;
  46. n->next = graph_nodes;
  47. graph_nodes = n;
  48. pthread_spin_unlock(&graph_lock);
  49. }
  50. /**
  51. * Duplicate a memory area into a fresh allocated buffer
  52. */
  53. static void * memdupa(const void *p, size_t size) {
  54. void * s = malloc(size);
  55. memcpy(s,p,size);
  56. return s;
  57. }
  58. #define memdup(p, size) ((typeof(p))memdupa(p,size))
  59. #define nullOrDup(name,size) s->name = (name == NULL ? NULL : memdup(name,size))
  60. #define dup(name) s->name = name
  61. node_enqueue_kernel graph_create_enqueue_kernel(char is_task,
  62. cl_command_queue cq,
  63. cl_kernel kernel,
  64. cl_uint work_dim,
  65. const size_t * global_work_offset,
  66. const size_t * global_work_size,
  67. const size_t * local_work_size,
  68. cl_uint num_events,
  69. const cl_event * events,
  70. cl_event * event,
  71. cl_uint num_args,
  72. size_t * arg_sizes,
  73. enum kernel_arg_type * arg_types,
  74. void ** args)
  75. {
  76. node_enqueue_kernel s = malloc(sizeof(struct node_enqueue_kernel_t));
  77. graph_node_init(&s->node);
  78. s->node.id = NODE_ENQUEUE_KERNEL;
  79. dup(is_task);
  80. dup(cq);
  81. dup(kernel);
  82. dup(work_dim);
  83. nullOrDup(global_work_offset, work_dim*sizeof(size_t));
  84. nullOrDup(global_work_size, work_dim*sizeof(size_t));
  85. nullOrDup(local_work_size, work_dim*sizeof(size_t));
  86. dup(num_events);
  87. nullOrDup(events, num_events * sizeof(cl_event));
  88. dup(num_args);
  89. nullOrDup(arg_sizes, num_args * sizeof(size_t));
  90. nullOrDup(arg_types, num_args * sizeof(enum kernel_arg_type));
  91. nullOrDup(args, num_args * sizeof(void*));
  92. if (event != NULL) {
  93. *event = event_create();
  94. s->event = event;
  95. }
  96. else {
  97. s->event = NULL;
  98. }
  99. return s;
  100. }
  101. #undef nullOrDup
  102. #undef memdup
  103. #undef dup