File: test_cuda_for_each_index.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 (143 lines) | stat: -rw-r--r-- 3,585 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
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
#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>

constexpr float eps = 0.0001f;

template <typename T>
void run_and_wait(T& cf) {
  tf::cudaStream stream;
  cf.run(stream);
  stream.synchronize();
}

// ----------------------------------------------------------------------------
// for_each_index
// ----------------------------------------------------------------------------

template <typename T>
void cuda_for_each_index() {

  tf::Taskflow taskflow;
  tf::Executor executor;
  
  for(int n=0; n<=1234567; n = (n<=100) ? n+1 : n*2 + 1) {

    taskflow.emplace([n](){
      tf::cudaStream stream;
      tf::cudaDefaultExecutionPolicy policy(stream);

      auto g_data = tf::cuda_malloc_shared<T>(n);
      for(int i=0; i<n; i++) {
        g_data[i] = 0;
      }

      tf::cuda_for_each_index(policy,
        0, n, 1, [g_data] __device__ (int i) { g_data[i] = 12222; }
      );

      stream.synchronize();

      for(int i=0; i<n; i++) {
        REQUIRE(std::fabs(g_data[i] - (T)12222) < eps);
      }

      tf::cuda_free(g_data);
    });
  }

  executor.run(taskflow).wait();
}

TEST_CASE("cuda_for_each_index.int" * doctest::timeout(300)) {
  cuda_for_each_index<int>();
}

TEST_CASE("cuda_for_each_index.float" * doctest::timeout(300)) {
  cuda_for_each_index<float>();
}

TEST_CASE("cuda_for_each_index.double" * doctest::timeout(300)) {
  cuda_for_each_index<double>();
}

// ----------------------------------------------------------------------------
// for_each_index
// ----------------------------------------------------------------------------

template <typename T, typename F>
void cudaflow_for_each_index() {
    
  tf::Taskflow taskflow;
  tf::Executor executor;

  for(int n=1; n<=1234567; n = (n<=100) ? n+1 : n*2 + 1) {
    
    taskflow.emplace([n](){

      auto cpu = static_cast<T*>(std::calloc(n, sizeof(T)));
      
      T* gpu = nullptr;
      REQUIRE(cudaMalloc(&gpu, n*sizeof(T)) == cudaSuccess);

      F cf;
      auto d2h = cf.copy(cpu, gpu, n);
      auto h2d = cf.copy(gpu, cpu, n);
      auto kernel = cf.for_each_index(
        0, n, 1, [gpu] __device__ (int i) { gpu[i] = 65536; }
      );
      h2d.precede(kernel);
      d2h.succeed(kernel);

      run_and_wait(cf);

      for(int i=0; i<n; i++) {
        REQUIRE(std::fabs(cpu[i] - (T)65536) < eps);
      }
      
      // update
      cf.for_each_index(kernel,
        0, n, 1, [gpu] __device__ (int i) { gpu[i] = (T)100; }
      );

      run_and_wait(cf);
      
      for(int j=0; j<n; j++) {
        REQUIRE(std::fabs(cpu[j] - (T)100) < eps);
      }

      std::free(cpu);
      REQUIRE(cudaFree(gpu) == cudaSuccess); 
    });
  }

  executor.run(taskflow).wait();
}

TEST_CASE("cudaFlow.for_each_index.int" * doctest::timeout(300)) {
  cudaflow_for_each_index<int, tf::cudaFlow>();
}

TEST_CASE("cudaFlow.for_each_index.float" * doctest::timeout(300)) {
  cudaflow_for_each_index<float, tf::cudaFlow>();
}

TEST_CASE("cudaFlow.for_each_index.double" * doctest::timeout(300)) {
  cudaflow_for_each_index<double, tf::cudaFlow>();
}

TEST_CASE("cudaFlowCapturer.for_each_index.int" * doctest::timeout(300)) {
  cudaflow_for_each_index<int, tf::cudaFlowCapturer>();
}

TEST_CASE("cudaFlowCapturer.for_each_index.float" * doctest::timeout(300)) {
  cudaflow_for_each_index<float, tf::cudaFlowCapturer>();
}

TEST_CASE("cudaFlowCapturer.for_each_index.double" * doctest::timeout(300)) {
  cudaflow_for_each_index<double, tf::cudaFlowCapturer>();
}