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