File: test_cuda_memory.cu

package info (click to toggle)
taskflow 3.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 45,948 kB
  • sloc: cpp: 39,058; xml: 35,572; python: 12,935; javascript: 1,732; makefile: 59; sh: 16
file content (99 lines) | stat: -rw-r--r-- 2,086 bytes parent folder | download
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include <doctest.h>
#include <taskflow/taskflow.hpp>
#include <taskflow/cuda/cudaflow.hpp>
#include <taskflow/cuda/algorithm/for_each.hpp>

// ----------------------------------------------------------------------------
// USM Allocator
// ----------------------------------------------------------------------------

TEST_CASE("cudaUSMAllocator" * doctest::timeout(300)) {

  tf::cudaStream stream;

  std::vector<int, tf::cudaUSMAllocator<int>> vec;
  std::vector<int, tf::cudaUSMAllocator<int>> rhs;

  REQUIRE(vec.size() == 0);

  vec.resize(100, 10);
  REQUIRE(vec.size() == 100);
  for(auto c : vec) {
    REQUIRE(c == 10);
  }

  rhs = std::move(vec);

  REQUIRE(vec.size() == 0);
  REQUIRE(rhs.size() == 100);
  for(auto c : rhs) {
    REQUIRE(c == 10);
  }

  for(int i=0; i<65536; i++) {
    vec.push_back(-i);
  }
  for(int i=0; i<65536; i++) {
    REQUIRE(vec[i] == -i);
  }

  rhs = vec;
  
  for(int i=0; i<65536; i++) {
    REQUIRE(vec[i] == rhs[i]);
  }

  tf::cudaDefaultExecutionPolicy p(stream);
  
  tf::cuda_for_each(p, vec.data(), vec.data() + vec.size(), [] __device__ (int& v) {
    v = -177;
  });
  stream.synchronize();

  rhs = vec;
  for(size_t i=0; i<vec.size(); i++) {
    REQUIRE(vec[i] == -177);
    REQUIRE(rhs[i] == vec[i]);
  }

  vec.clear();
  REQUIRE(vec.size() == 0);
}

// ----------------------------------------------------------------------------
// Device Allocator
// ----------------------------------------------------------------------------

TEST_CASE("cudaDeviceAllocator" * doctest::timeout(300)) {


  size_t N = 10000;
  
  std::vector<tf::NoInit<int>, tf::cudaDeviceAllocator<tf::NoInit<int>>> vec;
  std::vector<tf::NoInit<int>, tf::cudaDeviceAllocator<tf::NoInit<int>>> rhs(N);

  REQUIRE(vec.size() == 0);
  REQUIRE(rhs.size() == 10000);
  
  //tf::cudaStream stream;
  //tf::cudaDefaultExecutionPolicy policy(stream);
  //
  //tf::cuda_for_each(policy, rhs.data(), rhs.data() + N, [] __device__ (tf::NoInit<int>& v) {
  //  v = -177;
  //});
  //stream.synchronize();
}