1 | // RUN: %libomptarget-compile-run-and-check-generic |
2 | // REQUIRES: ompt |
3 | // UNSUPPORTED: aarch64-unknown-linux-gnu |
4 | // UNSUPPORTED: aarch64-unknown-linux-gnu-LTO |
5 | // UNSUPPORTED: x86_64-pc-linux-gnu |
6 | // UNSUPPORTED: x86_64-pc-linux-gnu-LTO |
7 | // UNSUPPORTED: s390x-ibm-linux-gnu |
8 | // UNSUPPORTED: s390x-ibm-linux-gnu-LTO |
9 | |
10 | /* |
11 | * Verify all three data transfer directions: H2D, D2D and D2H |
12 | */ |
13 | |
14 | #include <omp.h> |
15 | #include <stdio.h> |
16 | #include <stdlib.h> |
17 | |
18 | #include "callbacks.h" |
19 | #include "register_emi.h" |
20 | |
21 | int main(void) { |
22 | int NumDevices = omp_get_num_devices(); |
23 | assert(NumDevices > 0 && "No device(s) present." ); |
24 | int Device = omp_get_default_device(); |
25 | int Host = omp_get_initial_device(); |
26 | // Note: Zero value depicts an OFFLOAD_SUCCESS |
27 | int Status; |
28 | |
29 | printf(format: "Allocating Memory on Device\n" ); |
30 | int *DevPtr = (int *)omp_target_alloc(sizeof(int), Device); |
31 | assert(DevPtr && "Could not allocate memory on device." ); |
32 | int *HstPtr = (int *)malloc(size: sizeof(int)); |
33 | *HstPtr = 42; |
34 | |
35 | printf(format: "Testing: Host to Device\n" ); |
36 | Status = omp_target_memcpy(DevPtr, HstPtr, sizeof(int), 0, 0, Device, Host); |
37 | assert(Status == 0 && "H2D memory copy operation failed.\n" ); |
38 | |
39 | printf(format: "Testing: Device to Device\n" ); |
40 | Status = omp_target_memcpy(DevPtr, DevPtr, sizeof(int), 0, 0, Device, Device); |
41 | assert(Status == 0 && "D2D memory copy operation failed.\n" ); |
42 | |
43 | printf(format: "Testing: Device to Host\n" ); |
44 | Status = omp_target_memcpy(HstPtr, DevPtr, sizeof(int), 0, 0, Host, Device); |
45 | assert(Status == 0 && "D2H memory copy operation failed.\n" ); |
46 | |
47 | printf(format: "Checking Correctness\n" ); |
48 | assert(*HstPtr == 42); |
49 | |
50 | printf(format: "Freeing Memory on Device\n" ); |
51 | free(ptr: HstPtr); |
52 | omp_target_free(DevPtr, Device); |
53 | |
54 | return 0; |
55 | } |
56 | |
57 | // clang-format off |
58 | |
59 | /// CHECK: Callback Init: |
60 | |
61 | /// CHECK: Allocating Memory on Device |
62 | /// CHECK: Callback DataOp EMI: endpoint=1 optype=1 |
63 | /// CHECK-SAME: src_device_num=[[HOST:[0-9]+]] |
64 | /// CHECK-SAME: dest_device_num=[[DEVICE:[0-9]+]] |
65 | /// CHECK: Callback DataOp EMI: endpoint=2 optype=1 {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]] |
66 | |
67 | /// CHECK: Testing: Host to Device |
68 | /// CHECK: Callback DataOp EMI: endpoint=1 optype=2 {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]] |
69 | /// CHECK: Callback DataOp EMI: endpoint=2 optype=2 {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]] |
70 | |
71 | /// CHECK: Testing: Device to Device |
72 | /// CHECK: Callback DataOp EMI: endpoint=1 optype=3 {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[DEVICE]] |
73 | /// CHECK: Callback DataOp EMI: endpoint=2 optype=3 {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[DEVICE]] |
74 | |
75 | /// CHECK: Testing: Device to Host |
76 | /// CHECK: Callback DataOp EMI: endpoint=1 optype=3 {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[HOST]] |
77 | /// CHECK: Callback DataOp EMI: endpoint=2 optype=3 {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[HOST]] |
78 | |
79 | /// CHECK: Checking Correctness |
80 | |
81 | /// CHECK: Freeing Memory on Device |
82 | /// CHECK: Callback DataOp EMI: endpoint=1 optype=4 {{.+}} src_device_num=[[DEVICE]] |
83 | /// CHECK: Callback DataOp EMI: endpoint=2 optype=4 {{.+}} src_device_num=[[DEVICE]] |
84 | |
85 | /// CHECK: Callback Fini: |
86 | |
87 | // clang-format on |
88 | |