mem_objects.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #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. int i;
  24. for (i=0; i<mem_object_hash_key; i++) {
  25. starpu_pthread_spin_init(&p_mem_objects_spinlock[i], 0);
  26. }
  27. }
  28. static int mem_object_hash(const void * addr) {
  29. uintptr_t t = (uintptr_t)addr;
  30. uintptr_t t2 = t >> 4;
  31. uintptr_t t3 = t2 % mem_object_hash_key;
  32. return (int)t3;
  33. }
  34. void mem_object_store(cl_mem m) {
  35. int hash = mem_object_hash(m);
  36. LOCK(hash);
  37. m->prev = NULL;
  38. m->next = p_mem_objects[hash];
  39. if (p_mem_objects[hash] != NULL)
  40. p_mem_objects[hash]->prev = m;
  41. p_mem_objects[hash] = m;
  42. UNLOCK(hash);
  43. }
  44. void mem_object_release(cl_mem m) {
  45. int hash = mem_object_hash(m);
  46. LOCK(hash);
  47. if (m->prev != NULL)
  48. m->prev->next = m->next;
  49. if (m->next != NULL)
  50. m->next->prev = m->prev;
  51. if (p_mem_objects[hash] == m) {
  52. p_mem_objects[hash] = m->next;
  53. }
  54. UNLOCK(hash)
  55. }
  56. cl_mem mem_object_fetch(const void * addr) {
  57. int hash = mem_object_hash(*(cl_mem*)addr);
  58. LOCK(hash);
  59. cl_mem buf;
  60. for (buf = p_mem_objects[hash]; buf != NULL; buf = buf->next) {
  61. if (*(cl_mem*)addr == buf) {
  62. UNLOCK(hash);
  63. return buf;
  64. }
  65. }
  66. UNLOCK(hash);
  67. return NULL;
  68. }
  69. #undef LOCK
  70. #undef UNLOCK
  71. #undef mem_object_hash_key