Reference documentation for deal.II version 9.0.0
cuda_atomic.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_cuda_atomic_h
17 #define dealii_cuda_atomic_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_WITH_CUDA
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 namespace LinearAlgebra
26 {
27  namespace CUDAWrappers
28  {
34  inline __device__ float atomicAdd_wrapper(float *address, float val)
35  {
36  return atomicAdd(address,val);
37  }
38 
39 
40 
46  inline __device__ double atomicAdd_wrapper(double *address, double val)
47  {
48  // Use native instruction for CUDA 8 on Pascal or newer architecture
49 #if __CUDACC_VER_MAJOR__ >= 8 && ( !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600 )
50  return atomicAdd(address,val);
51 #else
52 
53  unsigned long long int *address_as_ull =
54  reinterpret_cast<unsigned long long int *>(address);
55  unsigned long long int old = *address_as_ull, assumed;
56  do
57  {
58  assumed = old;
59  old = atomicCAS(address_as_ull, assumed,
60  __double_as_longlong(val + __longlong_as_double(assumed)));
61  }
62  while (assumed != old);
63 
64  return __longlong_as_double(old);
65 #endif
66  }
67 
68 
69 
75  inline __device__ float atomicMax_wrapper(float *address, float val)
76  {
77  int *address_as_int = reinterpret_cast<int *>(address);
78  int old = *address_as_int, assumed;
79  do
80  {
81  assumed = old;
82  old = atomicCAS(address_as_int, assumed,
83  atomicMax(address_as_int, __float_as_int(val)));
84  }
85  while (assumed != old);
86 
87  return __longlong_as_double(old);
88  }
89 
90 
91 
97  inline __device__ double atomicMax_wrapper(double *address, double val)
98  {
99  unsigned long long int *address_as_ull =
100  reinterpret_cast<unsigned long long int *>(address);
101  unsigned long long int old = *address_as_ull, assumed;
102  do
103  {
104  assumed = old;
105  old = atomicCAS(address_as_ull, assumed,
106  atomicMax(address_as_ull,
107  static_cast<unsigned long long int>(__double_as_longlong(val))));
108  }
109  while (assumed != old);
110 
111  return __longlong_as_double(old);
112  }
113  }
114 }
115 
116 DEAL_II_NAMESPACE_CLOSE
117 
118 #endif
119 
120 #endif
float atomicMax_wrapper(float *address, float val)
Definition: cuda_atomic.h:75
float atomicAdd_wrapper(float *address, float val)
Definition: cuda_atomic.h:34