brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 39f262a Raw
85 lines · c
1// clang-format off2// RUN: %libomptarget-compile-run-and-check-generic3// REQUIRES: ompt4// REQUIRES: gpu5// clang-format on6 7/*8 * Verify all three data transfer directions: H2D, D2D and D2H9 */10 11#include <omp.h>12#include <stdio.h>13#include <stdlib.h>14 15#include "callbacks.h"16#include "register_emi.h"17 18int main(void) {19  int NumDevices = omp_get_num_devices();20  assert(NumDevices > 0 && "No device(s) present.");21  int Device = omp_get_default_device();22  int Host = omp_get_initial_device();23  // Note: Zero value depicts an OFFLOAD_SUCCESS24  int Status;25 26  printf("Allocating Memory on Device\n");27  int *DevPtr = (int *)omp_target_alloc(sizeof(int), Device);28  assert(DevPtr && "Could not allocate memory on device.");29  int *HstPtr = (int *)malloc(sizeof(int));30  *HstPtr = 42;31 32  printf("Testing: Host to Device\n");33  Status = omp_target_memcpy(DevPtr, HstPtr, sizeof(int), 0, 0, Device, Host);34  assert(Status == 0 && "H2D memory copy operation failed.\n");35 36  printf("Testing: Device to Device\n");37  Status = omp_target_memcpy(DevPtr, DevPtr, sizeof(int), 0, 0, Device, Device);38  assert(Status == 0 && "D2D memory copy operation failed.\n");39 40  printf("Testing: Device to Host\n");41  Status = omp_target_memcpy(HstPtr, DevPtr, sizeof(int), 0, 0, Host, Device);42  assert(Status == 0 && "D2H memory copy operation failed.\n");43 44  printf("Checking Correctness\n");45  assert(*HstPtr == 42);46 47  printf("Freeing Memory on Device\n");48  free(HstPtr);49  omp_target_free(DevPtr, Device);50 51  return 0;52}53 54// clang-format off55 56/// CHECK: Callback Init:57 58/// CHECK: Allocating Memory on Device59/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_begin optype=ompt_target_data_alloc60/// CHECK-SAME: src_device_num=[[HOST:[0-9]+]]61/// CHECK-SAME: dest_device_num=[[DEVICE:[0-9]+]]62/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_end optype=ompt_target_data_alloc {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]]63 64/// CHECK: Testing: Host to Device65/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_begin optype=ompt_target_data_transfer_to_device {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]]66/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_end optype=ompt_target_data_transfer_to_device {{.+}} src_device_num=[[HOST]] {{.+}} dest_device_num=[[DEVICE]]67 68/// CHECK: Testing: Device to Device69/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_begin optype=ompt_target_data_transfer_from_device {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[DEVICE]]70/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_end optype=ompt_target_data_transfer_from_device {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[DEVICE]]71 72/// CHECK: Testing: Device to Host73/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_begin optype=ompt_target_data_transfer_from_device {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[HOST]]74/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_end optype=ompt_target_data_transfer_from_device {{.+}} src_device_num=[[DEVICE]] {{.+}} dest_device_num=[[HOST]]75 76/// CHECK: Checking Correctness77 78/// CHECK: Freeing Memory on Device79/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_begin optype=ompt_target_data_delete {{.+}} src_device_num=[[DEVICE]]80/// CHECK: Callback DataOp EMI: endpoint=ompt_scope_end optype=ompt_target_data_delete {{.+}} src_device_num=[[DEVICE]]81 82/// CHECK: Callback Fini:83 84// clang-format on85