Caffe2 - C++ API
A deep learning, cross platform ML framework
cuda_rng_test.cpp
1 #include <gtest/gtest.h>
2 
3 #include <ATen/ATen.h>
4 #include <ATen/cuda/CUDAContext.h>
5 #include <cuda.h>
6 #include <cuda_runtime.h>
7 #include <thread>
8 
9 void makeRandomNumber() {
10  cudaSetDevice(std::rand() % 2);
11  auto x = at::randn({1000});
12 }
13 
14 void testCudaRNGMultithread() {
15  auto threads = std::vector<std::thread>();
16  for (auto i = 0; i < 1000; i++) {
17  threads.emplace_back(makeRandomNumber);
18  }
19  for (auto& t : threads) {
20  t.join();
21  }
22 };
23 
24 TEST(Cuda_RNGTest, MultithreadRNGTest) {
25  if (!at::cuda::is_available()) return;
26  testCudaRNGMultithread();
27 }