mem_objects.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Inria
  4. * Copyright (C) 2012,2014,2017 CNRS
  5. * Copyright (C) 2010-2011 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. #define mem_object_hash_key 257
  20. static cl_mem p_mem_objects[mem_object_hash_key] = {NULL};
  21. static starpu_pthread_spinlock_t p_mem_objects_spinlock[mem_object_hash_key];
  22. #define LOCK(i) starpu_pthread_spin_lock(&p_mem_objects_spinlock[i]);
  23. #define UNLOCK(i) starpu_pthread_spin_unlock(&p_mem_objects_spinlock[i]);
  24. void mem_object_init(void)
  25. {
  26. int i;
  27. for (i=0; i<mem_object_hash_key; i++)
  28. {
  29. starpu_pthread_spin_init(&p_mem_objects_spinlock[i], 0);
  30. }
  31. }
  32. static int mem_object_hash(const void * addr)
  33. {
  34. uintptr_t t = (uintptr_t)addr;
  35. uintptr_t t2 = t >> 4;
  36. uintptr_t t3 = t2 % mem_object_hash_key;
  37. return (int)t3;
  38. }
  39. void mem_object_store(cl_mem m)
  40. {
  41. int hash = mem_object_hash(m);
  42. LOCK(hash);
  43. m->prev = NULL;
  44. m->next = p_mem_objects[hash];
  45. if (p_mem_objects[hash] != NULL)
  46. p_mem_objects[hash]->prev = m;
  47. p_mem_objects[hash] = m;
  48. UNLOCK(hash);
  49. }
  50. void mem_object_release(cl_mem m)
  51. {
  52. int hash = mem_object_hash(m);
  53. LOCK(hash);
  54. if (m->prev != NULL)
  55. m->prev->next = m->next;
  56. if (m->next != NULL)
  57. m->next->prev = m->prev;
  58. if (p_mem_objects[hash] == m)
  59. {
  60. p_mem_objects[hash] = m->next;
  61. }
  62. UNLOCK(hash)
  63. }
  64. cl_mem mem_object_fetch(const void * addr)
  65. {
  66. int hash = mem_object_hash(*(cl_mem*)addr);
  67. LOCK(hash);
  68. cl_mem buf;
  69. for (buf = p_mem_objects[hash]; buf != NULL; buf = buf->next)
  70. {
  71. if (*(cl_mem*)addr == buf)
  72. {
  73. UNLOCK(hash);
  74. return buf;
  75. }
  76. }
  77. UNLOCK(hash);
  78. return NULL;
  79. }
  80. #undef LOCK
  81. #undef UNLOCK
  82. #undef mem_object_hash_key