imate
C++/CUDA Reference
Loading...
Searching...
No Matches
atomic_add.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3 * SPDX-License-Identifier: BSD-3-Clause
4 * SPDX-FileType: SOURCE
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the license found in the LICENSE.txt file in the root
8 * directory of this source tree.
9 */
10
11
12#ifndef _CU_BASIC_ALGEBRA_ATOMIC_ADD_H_
13#define _CU_BASIC_ALGEBRA_ATOMIC_ADD_H_
14
15// =======
16// Headers
17// =======
18
19#include <cuda_runtime.h>
20
21
22// ==========
23// atomic add
24// ==========
25
31
32#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
33#else
34__device__ double atomicAdd(double* address, double val)
35{
36 unsigned long long int* address_as_ull = \
37 (unsigned long long int*)address;
38 unsigned long long int old = *address_as_ull, assumed;
39
40 do {
41 assumed = old;
42 old = atomicCAS(
43 address_as_ull,
44 assumed,
45 __double_as_longlong(val + __longlong_as_double(assumed)));
46
47 // Note: uses integer comparison to avoid hang in case of NaN
48 // (since NaN != NaN)
49 } while (assumed != old);
50
51 return __longlong_as_double(old);
52}
53#endif
54
55#endif // _CU_BASIC_ALGEBRA_ATOMIC_ADD_H_