Skip to content

Commit 483f2ef

Browse files
committed
Initial commit
0 parents  commit 483f2ef

File tree

3,538 files changed

+1280253
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,538 files changed

+1280253
-0
lines changed

QuickSilver/CUDA/src/.depend

Whitespace-only changes.

QuickSilver/CUDA/src/AtomicMacro.hh

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
Modifications Copyright (C) 2023 Intel Corporation
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
3. Neither the name of the copyright holder nor the names of its contributors
13+
may be used to endorse or promote products derived from this software
14+
without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
29+
SPDX-License-Identifier: BSD-3-Clause
30+
*/
31+
32+
/*
33+
Copyright 2019 Advanced Micro Devices
34+
35+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
36+
37+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
38+
39+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
40+
41+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
42+
43+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44+
*/
45+
46+
// Determine which atomics to use based on platform being compiled for
47+
//
48+
49+
#ifndef ATOMICS_HD
50+
#define ATOMICS_HD
51+
52+
#include <thread>
53+
#include <mutex>
54+
#include <algorithm>
55+
56+
inline __device__ double ull2d(const unsigned long long &val)
57+
{
58+
return *((double *)&val);
59+
}
60+
61+
#ifdef HAVE_CUDA
62+
#include <cuda.h>
63+
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
64+
65+
#else
66+
inline __device__ double atomicAdd(double *pointer, double val)
67+
{
68+
// A workaround dealing with the fact that atomic doubles don't work with all versions of CUDA.
69+
unsigned long long int *pointer_as_p2ull = (unsigned long long int *)pointer;
70+
unsigned long long int old = *pointer_as_p2ull, check_value;
71+
do
72+
{
73+
check_value = old;
74+
old = atomicCAS(pointer_as_p2ull, check_value, __double_as_longlong(val + ull2d(check_value)));
75+
} while (check_value != old);
76+
return ull2d(old);
77+
};
78+
#endif
79+
80+
#endif
81+
82+
#ifdef HAVE_OPENMP
83+
#define USE_OPENMP_ATOMICS
84+
#elif HAVE_OPENMP_TARGET
85+
#define USE_OPENMP_ATOMICS
86+
#endif
87+
88+
#if defined(HAVE_CUDA)
89+
90+
// If in a CUDA GPU section use the CUDA atomics
91+
#ifdef __CUDA_ARCH__
92+
93+
// Currently not atomic here. But its only used when it does not necissarially need to be atomic.
94+
#define ATOMIC_WRITE(x, v) \
95+
x = v;
96+
97+
#define ATOMIC_ADD(x, v) \
98+
atomicAdd(&x, v);
99+
100+
#define ATOMIC_UPDATE(x) \
101+
atomicAdd(&x, 1);
102+
103+
#define ATOMIC_CAPTURE(x, v, p) \
104+
p = atomicAdd(&x, v);
105+
// If in a CPU OpenMP section use the OpenMP atomics
106+
#elif defined(USE_OPENMP_ATOMICS)
107+
#define ATOMIC_WRITE(x, v) \
108+
_Pragma("omp atomic write") \
109+
x = v;
110+
111+
#define ATOMIC_ADD(x, v) \
112+
_Pragma("omp atomic") \
113+
x += v;
114+
115+
#define ATOMIC_UPDATE(x) \
116+
_Pragma("omp atomic update") \
117+
x++;
118+
119+
#define ATOMIC_CAPTURE(x, v, p) \
120+
_Pragma("omp atomic capture") \
121+
{ \
122+
p = x; \
123+
x = x + v; \
124+
}
125+
126+
// If in a serial section, no need to use atomics
127+
#else
128+
#define ATOMIC_WRITE(x, v) \
129+
x = v;
130+
131+
#define ATOMIC_UPDATE(x) \
132+
x++;
133+
134+
#define ATOMIC_ADD(x, v) \
135+
x += v;
136+
137+
#define ATOMIC_CAPTURE(x, v, p) \
138+
{ \
139+
p = x; \
140+
x = x + v; \
141+
}
142+
143+
#endif
144+
145+
// If in a OpenMP section use the OpenMP atomics
146+
#elif defined(USE_OPENMP_ATOMICS)
147+
#define ATOMIC_WRITE(x, v) \
148+
_Pragma("omp atomic write") \
149+
x = v;
150+
151+
#define ATOMIC_ADD(x, v) \
152+
_Pragma("omp atomic") \
153+
x += v;
154+
155+
#define ATOMIC_UPDATE(x) \
156+
_Pragma("omp atomic update") \
157+
x++;
158+
159+
#define ATOMIC_CAPTURE(x, v, p) \
160+
_Pragma("omp atomic capture") \
161+
{ \
162+
p = x; \
163+
x = x + v; \
164+
}
165+
166+
// If in a serial section, no need to use atomics
167+
#else
168+
#define ATOMIC_WRITE(x, v) \
169+
x = v;
170+
171+
#define ATOMIC_UPDATE(x) \
172+
x++;
173+
174+
#define ATOMIC_ADD(x, v) \
175+
x += v;
176+
177+
#define ATOMIC_CAPTURE(x, v, p) \
178+
{ \
179+
p = x; \
180+
x = x + v; \
181+
}
182+
#endif
183+
184+
#endif

QuickSilver/CUDA/src/BulkStorage.hh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef BULK_STORAGE_HH
2+
#define BULK_STORAGE_HH
3+
4+
#include "MemoryControl.hh"
5+
6+
template <typename T>
7+
class BulkStorage
8+
{
9+
public:
10+
BulkStorage()
11+
: _bulkStorage(0),
12+
_refCount(0),
13+
_size(0),
14+
_capacity(0),
15+
_memPolicy(MemoryControl::AllocationPolicy::UNDEFINED_POLICY)
16+
{
17+
_refCount = new int;
18+
*_refCount = 1;
19+
}
20+
21+
BulkStorage(const BulkStorage& aa)
22+
: _bulkStorage(aa._bulkStorage), _refCount(aa._refCount), _size(aa._size), _capacity(aa._capacity), _memPolicy(aa._memPolicy)
23+
{
24+
++(*_refCount);
25+
}
26+
27+
~BulkStorage()
28+
{
29+
// Check for instances that never allocated memory.
30+
// I'm not exactly sure how this can happen, but it does.
31+
if (_bulkStorage == 0)
32+
return;
33+
34+
--(*_refCount);
35+
if (*_refCount > 0)
36+
return;
37+
38+
MemoryControl::deallocate(_bulkStorage, _capacity, _memPolicy);
39+
delete _refCount;
40+
}
41+
42+
/// Needed for copy-swap idiom
43+
void swap(BulkStorage<T>& other)
44+
{
45+
std::swap(_bulkStorage, other._bulkStorage);
46+
std::swap(_refCount, other._refCount);
47+
std::swap(_size, other._size);
48+
std::swap(_capacity, other._capacity);
49+
std::swap(_memPolicy, other._memPolicy);
50+
}
51+
52+
/// Implement assignment using copy-swap idiom
53+
BulkStorage& operator=(const BulkStorage& aa)
54+
{
55+
if (&aa != this)
56+
{
57+
BulkStorage<T> temp(aa);
58+
this->swap(temp);
59+
}
60+
return *this;
61+
}
62+
63+
void setCapacity(int capacity, MemoryControl::AllocationPolicy policy)
64+
{
65+
qs_assert(_bulkStorage == 0);
66+
_bulkStorage = MemoryControl::allocate<T>(capacity, policy);
67+
_capacity = capacity;
68+
_memPolicy = policy;
69+
}
70+
71+
T* getBlock(int nItems)
72+
{
73+
T* blockStart = _bulkStorage + _size;
74+
_size += nItems;
75+
qs_assert(_size <= _capacity);
76+
return blockStart;
77+
}
78+
79+
80+
private:
81+
82+
// This class doesn't have well defined copy semantics. However,
83+
// just disabling copy operations breaks the build since we haven't
84+
// been consistent about dealing with copy semantics in classes like
85+
// MC_Mesh_Domain.
86+
87+
88+
89+
T* _bulkStorage;
90+
int* _refCount;
91+
int _size;
92+
int _capacity;
93+
MemoryControl::AllocationPolicy _memPolicy;
94+
95+
};
96+
97+
98+
#endif

0 commit comments

Comments
 (0)