Created
December 26, 2016 11:59
-
-
Save ShigekiKarita/cbdff9a65027b955b6c03b43c0f78b9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <future> | |
#include <thread> | |
#include <map> | |
#include <vector> | |
#include <stdio.h> | |
const int N = 1 << 20; | |
__global__ void kernel(float *x, int n) | |
{ | |
int tid = threadIdx.x + blockIdx.x * blockDim.x; | |
for (int i = tid; i < n; i += blockDim.x * gridDim.x) { | |
x[i] = sqrt(pow(3.14159,i)); | |
} | |
} | |
void launch_kernel(std::promise<void*> p) | |
{ | |
try { | |
float *data; | |
cudaMalloc(&data, N * sizeof(float)); | |
kernel<<<1, 64>>>(data, N); | |
cudaStreamSynchronize(0); | |
p.set_value(NULL); | |
} catch (...) { | |
p.set_exception(std::current_exception()); | |
} | |
} | |
int main() | |
{ | |
const int num_threads = 8; | |
std::vector<std::thread> threads; | |
std::map<std::thread, std::future<void*>> futures; | |
for (auto& t: threads) { | |
try { | |
std::primise<void*> p; | |
futures.insert(std::make_pair(t, p.get_future())); | |
t = std::thread(launch_kernel, std::move(p)); | |
} catch(...) { | |
fprintf(stderr, "Error creating thread\n"); | |
return 1; | |
} | |
} | |
for (auto& t: threads) { | |
t.join(); | |
auto& f = futures[t]; | |
try { | |
f.get(); | |
} catch(std::exception& e) { | |
fprintf(stderr, "Error joining thread\n" + e.what()); | |
} | |
} | |
cudaDeviceReset(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment