-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.cu
154 lines (120 loc) · 4.36 KB
/
test.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright (c) 2017, NVIDIA Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
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.
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.
*/
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 700)
#warning "Use of all warp threads is TOO iffy without CUDA support for compute_70 or above. Will run 1 thread per warp."
#endif
#include <sys/mman.h>
#include <cuda/launch>
#include <cuda/atomic>
#include <cuda/semaphore>
#include <cassert>
#include <mutex>
#include <thread>
#include <iostream>
#define __test_abi __host__ __device__
template<class T>
using atomic = cuda::experimental::atomic<T>;
using thread = std::thread;
using binary_semaphore = cuda::experimental::binary_semaphore;
using counting_semaphore = cuda::experimental::counting_semaphore;
using condition_variable_atomic = cuda::experimental::condition_variable_atomic;
namespace details = cuda::experimental::details;
#include "test.hpp"
using mutex = binary_semaphore_mutex;
template<class F>
__global__ void run_gpu_thread(uint32_t count, uint32_t count_per_block, F const* f) {
unsigned int const myIdx = blockIdx.x * count_per_block + threadIdx.x;
if (myIdx < count)
(*f)(myIdx);
}
uint32_t cap = 0;
bool use_malloc_managed = true;
uint32_t max_block_count = 0;
void* allocate_raw_bytes(size_t s, bool force = false) {
void* ptr = nullptr;
#ifdef __CUDACC__
if(use_malloc_managed || force) {
auto const ret = cap < 6 ? cudaHostAlloc(&ptr, s, 0) : cudaMallocManaged(&ptr, s);
assert(ret == cudaSuccess);
if(cap >= 6)
cudaMemAdvise(ptr, s, cudaMemAdviseSetPreferredLocation, 0);
}
else
#endif
{
ptr = mmap(0, s, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED) {
ptr = nullptr;
}
}
assert(ptr != nullptr);
return ptr;
}
void deallocate_raw_bytes(void* ptr, bool force = false) {
#ifdef __CUDACC__
if(use_malloc_managed || force)
cudaFree(ptr);
else
#endif
{
// Leak. Oops.
}
}
template<class F>
F* start_gpu_threads(uint32_t count, F f) {
if(!count)
return nullptr;
uint32_t const blocks = (std::min)(count, max_block_count);
uint32_t const threads_per_block = (count / blocks) + (count % blocks ? 1 : 0);
auto const fptr = new (allocate_raw_bytes(sizeof(F))) F(f);
assert(uintptr_t(fptr) % alignof(F) == 0);
run_gpu_thread<F><<<blocks, threads_per_block>>>(count, threads_per_block, fptr);
return fptr;
}
template<class F>
void stop_gpu_threads(F* fptr) {
if(nullptr == fptr)
return;
auto const ret = cudaDeviceSynchronize();
assert(ret == cudaSuccess);
fptr->~F();
deallocate_raw_bytes(fptr);
}
uint32_t dev = 0;
unsigned int max_gpu_threads() {
cudaSetDevice(dev);
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
max_block_count = deviceProp.multiProcessorCount * deviceProp.maxThreadsPerMultiProcessor / 1024;
cap = deviceProp.major;
if (cap < 7)
return max_block_count * 32;
else
return max_block_count * 32 * 32;
}
#include "driver.cpp"
#ifndef TEST_NO_MAIN
int main(int argc, char const* argv[]) {
auto f = [](uint32_t&,double&) { };
return driver_main(argc, argv, f);
}
#endif