File: device_buffer_copy.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (425 lines) | stat: -rw-r--r-- 19,003 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "Halide.h"
#include "HalideBuffer.h"
#include "HalideRuntime.h"

using namespace Halide;

Halide::Runtime::Buffer<int32_t> make_gpu_buffer(bool hexagon_rpc, int offset = 0,
                                                 DeviceAPI api = DeviceAPI::Default_GPU) {
    Var x, y;
    Func f;
    f(x, y) = x + y * 256 + offset;

    if (hexagon_rpc) {
        f.hexagon();
    } else {
        Var xi, yi;
        f.gpu_tile(x, y, xi, yi, 8, 8, TailStrategy::Auto, api);
    }

    Buffer<int32_t> result = f.realize({128, 128});
    return *result.get();
}

int main(int argc, char **argv) {
    Target target = get_jit_target_from_environment();

    bool hexagon_rpc = (target.arch != Target::Hexagon) &&
                       target.has_feature(Target::HVX);

    if (!hexagon_rpc && !target.has_gpu_feature()) {
        printf("[SKIP] No GPU target enabled.\n");
        return 0;
    }

    printf("Test copy to device.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        cpu_buf.fill(0);
        assert(gpu_buf.raw_buffer()->device_interface->buffer_copy(nullptr, cpu_buf, gpu_buf.raw_buffer()->device_interface, gpu_buf) == 0);

        gpu_buf.copy_to_host();
        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                assert(gpu_buf(i, j) == 0);
            }
        }
    }

    printf("Test copy from device.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        assert(gpu_buf.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf, nullptr, cpu_buf) == 0);

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                assert(cpu_buf(i, j) == (i + j * 256));
            }
        }
    }

    printf("Test copy device to device.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);
        Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(hexagon_rpc, 256000);
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf2, gpu_buf1.raw_buffer()->device_interface, gpu_buf1) == 0);
        gpu_buf1.copy_to_host();

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                assert(gpu_buf1(i, j) == (i + j * 256 + 256000));
            }
        }
    }

    printf("Test copy host to device -- subset area.\n");
    {
        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        cpu_buf.fill(0);

        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf2 = gpu_buf1.cropped({{32, 64}, {32, 64}});
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, cpu_buf, gpu_buf2.raw_buffer()->device_interface, gpu_buf2) == 0);
        gpu_buf1.set_device_dirty();
        gpu_buf1.copy_to_host();

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                bool in_gpu2 = (i >= gpu_buf2.dim(0).min()) &&
                               (i <= gpu_buf2.dim(0).max()) &&
                               (j >= gpu_buf2.dim(1).min()) &&
                               (j <= gpu_buf2.dim(1).max());
                assert(gpu_buf1(i, j) == (in_gpu2 ? 0 : (i + j * 256)));
            }
        }
    }

    printf("Test copy device to host -- subset area.\n");
    {
        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        cpu_buf.fill(0);
        Halide::Runtime::Buffer<int32_t> cpu_buf1 = cpu_buf.cropped({{32, 64}, {32, 64}});

        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf, nullptr, cpu_buf1) == 0);

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                bool in_cpu1 = (i >= cpu_buf1.dim(0).min()) &&
                               (i <= cpu_buf1.dim(0).max()) &&
                               (j >= cpu_buf1.dim(1).min()) &&
                               (j <= cpu_buf1.dim(1).max());
                assert(cpu_buf(i, j) == (in_cpu1 ? (i + j * 256) : 0));
            }
        }
    }

    printf("Test copy device to device -- subset area (from bigger to smaller buffer).\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(hexagon_rpc, 256000);
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf3 = gpu_buf2.cropped({{32, 64}, {32, 64}});
        assert(gpu_buf3.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf1, gpu_buf3.raw_buffer()->device_interface, gpu_buf3) == 0);
        gpu_buf2.set_device_dirty();
        gpu_buf2.copy_to_host();

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                bool in_gpu3 = (i >= gpu_buf3.dim(0).min()) &&
                               (i <= gpu_buf3.dim(0).max()) &&
                               (j >= gpu_buf3.dim(1).min()) &&
                               (j <= gpu_buf3.dim(1).max());
                assert(gpu_buf2(i, j) == (i + j * 256 + (in_gpu3 ? 0 : 256000)));
            }
        }
    }

    printf("Test copy device to device -- subset area (from smaller to bigger buffer).\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(hexagon_rpc, 256000);
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf3 = gpu_buf2.cropped({{32, 64}, {32, 64}});
        assert(gpu_buf3.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf3.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf3, gpu_buf1.raw_buffer()->device_interface, gpu_buf1) == 0);
        gpu_buf1.set_device_dirty();
        gpu_buf1.copy_to_host();

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                bool in_gpu3 = (i >= gpu_buf3.dim(0).min()) &&
                               (i <= gpu_buf3.dim(0).max()) &&
                               (j >= gpu_buf3.dim(1).min()) &&
                               (j <= gpu_buf3.dim(1).max());
                assert(gpu_buf1(i, j) == (i + j * 256 + (in_gpu3 ? 256000 : 0)));
            }
        }
    }

    printf("Test copy device to device -- subset area (from and to not-contained-within-each-other crops).\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(hexagon_rpc, 256000);
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        // Two crops: one horizontal, and one vertical rectangle.
        Halide::Runtime::Buffer<int32_t> gpu_buf1_crop = gpu_buf1.cropped({{32, 64}, {32, 16}});
        Halide::Runtime::Buffer<int32_t> gpu_buf2_crop = gpu_buf2.cropped({{32, 16}, {32, 64}});
        assert(gpu_buf1_crop.raw_buffer()->device_interface != nullptr);
        assert(gpu_buf2_crop.raw_buffer()->device_interface != nullptr);

        // Copy from the crop to another crop.
        assert(gpu_buf2_crop.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf2_crop, gpu_buf1_crop.raw_buffer()->device_interface, gpu_buf1_crop) == 0);
        gpu_buf1.set_device_dirty();
        gpu_buf1.copy_to_host();

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                bool in_gpu1_crop = (i >= gpu_buf1_crop.dim(0).min()) &&
                                    (i <= gpu_buf1_crop.dim(0).max()) &&
                                    (j >= gpu_buf1_crop.dim(1).min()) &&
                                    (j <= gpu_buf1_crop.dim(1).max());
                bool in_gpu2_crop = (i >= gpu_buf2_crop.dim(0).min()) &&
                                    (i <= gpu_buf2_crop.dim(0).max()) &&
                                    (j >= gpu_buf2_crop.dim(1).min()) &&
                                    (j <= gpu_buf2_crop.dim(1).max());
                // printf("gpu_buf1(%d, %d) = %d  (in_gpu1_crop=%d in_gpu2_crop=%d)\n", i, j, gpu_buf1(i, j), in_gpu1_crop, in_gpu2_crop);
                assert(gpu_buf1(i, j) == (i + j * 256 + (in_gpu1_crop && in_gpu2_crop ? 256000 : 0)));
            }
        }
    }

    printf("Test copy from device no src host.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);
        halide_buffer_t no_host_src = *gpu_buf.raw_buffer();
        no_host_src.host = nullptr;
        no_host_src.set_device_dirty(false);

        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        assert(gpu_buf.raw_buffer()->device_interface->buffer_copy(nullptr, &no_host_src, nullptr, cpu_buf) == 0);

        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                assert(cpu_buf(i, j) == (i + j * 256));
            }
        }
    }

    printf("Test copy to device no dest host.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);
        halide_buffer_t no_host_dst = *gpu_buf.raw_buffer();
        no_host_dst.host = nullptr;

        Halide::Runtime::Buffer<int32_t> cpu_buf(128, 128);
        cpu_buf.fill(0);
        assert(gpu_buf.raw_buffer()->device_interface->buffer_copy(nullptr, cpu_buf, gpu_buf.raw_buffer()->device_interface, &no_host_dst) == 0);
        gpu_buf.set_device_dirty(true);

        gpu_buf.copy_to_host();
        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 128; j++) {
                assert(gpu_buf(i, j) == 0);
            }
        }
    }

    printf("Test copy device to host no dest host -- confirm error not segfault.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf1.raw_buffer()->device_interface != nullptr);
        halide_buffer_t no_host_dst = *gpu_buf1.raw_buffer();
        no_host_dst.host = nullptr;

        Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(hexagon_rpc, 256000);
        assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

        assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf2, nullptr, &no_host_dst) == halide_error_code_host_is_null);
    }

    // Test copying between different device APIs. Probably will not
    // run on test infrastructure as we do not configure more than one
    // GPU API at a time. For now, special case CUDA and OpenCL as these are
    // the most likely to be supported together.
    if (target.has_feature(Target::CUDA) && target.has_feature(Target::OpenCL)) {
        printf("Test cross device copy device to device.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf2, gpu_buf1.raw_buffer()->device_interface, gpu_buf1) == 0);
            gpu_buf1.copy_to_host();

            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    assert(gpu_buf1(i, j) == (i + j * 256 + 256000));
                }
            }
        }

        printf("Test cross device copy device to device -- subset area.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf3 = gpu_buf2.cropped({{32, 64}, {32, 64}});
            assert(gpu_buf3.raw_buffer()->device_interface != nullptr);

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf1, gpu_buf3.raw_buffer()->device_interface, gpu_buf3) == 0);
            gpu_buf2.set_device_dirty();
            gpu_buf2.copy_to_host();

            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    bool in_gpu3 = (i >= gpu_buf3.dim(0).min()) &&
                                   (i < (gpu_buf3.dim(0).min() + gpu_buf3.dim(0).extent())) &&
                                   (j >= gpu_buf3.dim(1).min()) &&
                                   (j < (gpu_buf3.dim(1).min() + gpu_buf3.dim(1).extent()));
                    assert(gpu_buf2(i, j) == (i + j * 256 + (in_gpu3 ? 0 : 256000)));
                }
            }
        }

        printf("Test cross device copy device to device no source host.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);
            halide_buffer_t no_host_src = *gpu_buf2.raw_buffer();
            no_host_src.host = nullptr;

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, &no_host_src, gpu_buf1.raw_buffer()->device_interface, gpu_buf1) == 0);
            gpu_buf1.copy_to_host();

            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    assert(gpu_buf1(i, j) == (i + j * 256 + 256000));
                }
            }
        }

        printf("Test cross device copy device to device no dest host.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);
            halide_buffer_t no_host_dst = *gpu_buf1.raw_buffer();
            no_host_dst.host = nullptr;

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf2, gpu_buf1.raw_buffer()->device_interface, &no_host_dst) == 0);
            gpu_buf1.set_device_dirty();
            gpu_buf1.copy_to_host();

            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    assert(gpu_buf1(i, j) == (i + j * 256 + 256000));
                }
            }
        }

        printf("Test cross device copy device to device no source or dest host.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);
            halide_buffer_t no_host_dst = *gpu_buf1.raw_buffer();
            no_host_dst.host = nullptr;

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);
            halide_buffer_t no_host_src = *gpu_buf2.raw_buffer();
            no_host_src.host = nullptr;

            int err = gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, &no_host_src, gpu_buf1.raw_buffer()->device_interface, &no_host_dst);
            if (err == 0) {
                gpu_buf1.set_device_dirty();
                gpu_buf1.copy_to_host();

                for (int i = 0; i < 128; i++) {
                    for (int j = 0; j < 128; j++) {
                        assert(gpu_buf1(i, j) == (i + j * 256 + 256000));
                    }
                }
            } else {
                // halide_buffer_copy is not guaranteed to handle cross device case without host memory in one of the buffers.
                assert(err == halide_error_code_incompatible_device_interface);
                printf("Cross device with no host buffers case is not handled. Ignoring (correct) error.\n");
            }
        }

        printf("Test cross device copy device to host.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf1, nullptr, gpu_buf2) == 0);
            gpu_buf1.set_device_dirty();
            gpu_buf1.copy_to_host();

            for (int i = 0; i < 128; i++) {
                for (int j = 0; j < 128; j++) {
                    assert(gpu_buf1(i, j) == (i + j * 256));
                }
            }
        }

        printf("Test cross device copy device to host with no dest host.\n");
        {
            Halide::Runtime::Buffer<int32_t> gpu_buf1 = make_gpu_buffer(false, 0, DeviceAPI::CUDA);
            assert(gpu_buf1.raw_buffer()->device_interface != nullptr);

            Halide::Runtime::Buffer<int32_t> gpu_buf2 = make_gpu_buffer(false, 256000, DeviceAPI::OpenCL);
            assert(gpu_buf2.raw_buffer()->device_interface != nullptr);
            halide_buffer_t no_host_dst = *gpu_buf2.raw_buffer();
            no_host_dst.host = nullptr;

            assert(gpu_buf1.raw_buffer()->device_interface->buffer_copy(nullptr, gpu_buf1, nullptr, &no_host_dst) == halide_error_code_host_is_null);
        }
    }

    printf("Success!\n");

    return 0;
}