mem_objects.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #define mem_object_hash_key 257
  18. static cl_mem p_mem_objects[mem_object_hash_key] = {NULL};
  19. static starpu_pthread_spinlock_t p_mem_objects_spinlock[mem_object_hash_key];
  20. #define LOCK(i) starpu_pthread_spin_lock(&p_mem_objects_spinlock[i]);
  21. #define UNLOCK(i) starpu_pthread_spin_unlock(&p_mem_objects_spinlock[i]);
  22. void mem_object_init(void)
  23. {
  24. int i;
  25. for (i=0; i<mem_object_hash_key; i++)
  26. {
  27. starpu_pthread_spin_init(&p_mem_objects_spinlock[i], 0);
  28. }
  29. }
  30. static int mem_object_hash(const void * addr)
  31. {
  32. uintptr_t t = (uintptr_t)addr;
  33. uintptr_t t2 = t >> 4;
  34. uintptr_t t3 = t2 % mem_object_hash_key;
  35. return (int)t3;
  36. }
  37. void mem_object_store(cl_mem m)
  38. {
  39. int hash = mem_object_hash(m);
  40. LOCK(hash);
  41. m->prev = NULL;
  42. m->next = p_mem_objects[hash];
  43. if (p_mem_objects[hash] != NULL)
  44. p_mem_objects[hash]->prev = m;
  45. p_mem_objects[hash] = m;
  46. UNLOCK(hash);
  47. }
  48. void mem_object_release(cl_mem m)
  49. {
  50. int hash = mem_object_hash(m);
  51. LOCK(hash);
  52. if (m->prev != NULL)
  53. m->prev->next = m->next;
  54. if (m->next != NULL)
  55. m->next->prev = m->prev;
  56. if (p_mem_objects[hash] == m)
  57. {
  58. p_mem_objects[hash] = m->next;
  59. }
  60. UNLOCK(hash)
  61. }
  62. cl_mem mem_object_fetch(const void * addr)
  63. {
  64. int hash = mem_object_hash(*(cl_mem*)addr);
  65. LOCK(hash);
  66. cl_mem buf;
  67. for (buf = p_mem_objects[hash]; buf != NULL; buf = buf->next)
  68. {
  69. if (*(cl_mem*)addr == buf)
  70. {
  71. UNLOCK(hash);
  72. return buf;
  73. }
  74. }
  75. UNLOCK(hash);
  76. return NULL;
  77. }
  78. #undef LOCK
  79. #undef UNLOCK
  80. #undef mem_object_hash_key