util_atomic.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2014 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_ATOMIC_H__
  17. #define __UTIL_ATOMIC_H__
  18. #ifndef __KERNEL_GPU__
  19. /* Using atomic ops header from Blender. */
  20. # include "atomic_ops.h"
  21. # define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
  22. # define atomic_compare_and_swap_float(p, old_val, new_val) \
  23. atomic_cas_float((p), (old_val), (new_val))
  24. # define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
  25. # define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_add_uint32((p), -1)
  26. # define CCL_LOCAL_MEM_FENCE 0
  27. # define ccl_barrier(flags) ((void)0)
  28. #else /* __KERNEL_GPU__ */
  29. # ifdef __KERNEL_OPENCL__
  30. /* Float atomics implementation credits:
  31. * http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
  32. */
  33. ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *source,
  34. const float operand)
  35. {
  36. union {
  37. unsigned int int_value;
  38. float float_value;
  39. } new_value;
  40. union {
  41. unsigned int int_value;
  42. float float_value;
  43. } prev_value;
  44. do {
  45. prev_value.float_value = *source;
  46. new_value.float_value = prev_value.float_value + operand;
  47. } while (atomic_cmpxchg((volatile ccl_global unsigned int *)source,
  48. prev_value.int_value,
  49. new_value.int_value) != prev_value.int_value);
  50. return new_value.float_value;
  51. }
  52. ccl_device_inline float atomic_compare_and_swap_float(volatile ccl_global float *dest,
  53. const float old_val,
  54. const float new_val)
  55. {
  56. union {
  57. unsigned int int_value;
  58. float float_value;
  59. } new_value, prev_value, result;
  60. prev_value.float_value = old_val;
  61. new_value.float_value = new_val;
  62. result.int_value = atomic_cmpxchg(
  63. (volatile ccl_global unsigned int *)dest, prev_value.int_value, new_value.int_value);
  64. return result.float_value;
  65. }
  66. # define atomic_fetch_and_add_uint32(p, x) atomic_add((p), (x))
  67. # define atomic_fetch_and_inc_uint32(p) atomic_inc((p))
  68. # define atomic_fetch_and_dec_uint32(p) atomic_dec((p))
  69. # define CCL_LOCAL_MEM_FENCE CLK_LOCAL_MEM_FENCE
  70. # define ccl_barrier(flags) barrier(flags)
  71. # endif /* __KERNEL_OPENCL__ */
  72. # ifdef __KERNEL_CUDA__
  73. # define atomic_add_and_fetch_float(p, x) (atomicAdd((float *)(p), (float)(x)) + (float)(x))
  74. # define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int *)(p), (unsigned int)(x))
  75. # define atomic_fetch_and_sub_uint32(p, x) atomicSub((unsigned int *)(p), (unsigned int)(x))
  76. # define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
  77. # define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_sub_uint32((p), 1)
  78. ccl_device_inline float atomic_compare_and_swap_float(volatile float *dest,
  79. const float old_val,
  80. const float new_val)
  81. {
  82. union {
  83. unsigned int int_value;
  84. float float_value;
  85. } new_value, prev_value, result;
  86. prev_value.float_value = old_val;
  87. new_value.float_value = new_val;
  88. result.int_value = atomicCAS((unsigned int *)dest, prev_value.int_value, new_value.int_value);
  89. return result.float_value;
  90. }
  91. # define CCL_LOCAL_MEM_FENCE
  92. # define ccl_barrier(flags) __syncthreads()
  93. # endif /* __KERNEL_CUDA__ */
  94. #endif /* __KERNEL_GPU__ */
  95. #endif /* __UTIL_ATOMIC_H__ */