0

I am trying to use <hip/hip_runtime.h> library, but I keep getting gibberish when exchanging data.

here is my code:

#include <hip/hip_runtime.h>
#include <iostream>

int main() {
    int* hipC;
    int a = 123;
    int* ap = &a;
    int* ap2 = new int;

    hipMalloc(&hipC, sizeof(int));
    hipMemcpy(hipC, ap, sizeof(int), hipMemcpyHostToDevice);
    hipMemcpy(ap2, hipC, sizeof(int), hipMemcpyDeviceToHost);

    std::cout << *ap2 << '\n';

    return 0;
}

if I understand how hip malloc / memcpy works, this code should allocate space for hipC pointer, take value of a, copy it to *hipC and back to *ap2. But when running following code it prints random ints. There are also no errors/compiler warnings to speak of, so I'm quite stumped. I compile code with hipcc if that matters. thanks

  • Check the error codes returned from each function ? – Richard Critten Jun 22 '23 at 19:56
  • Also [hipMemcpy](https://rocm-developer-tools.github.io/HIP/group__Memory.html#gac1a055d288302edd641c6d7416858e1e) "_...For hipMemcpy, the copy is always performed by the current device (__set by hipSetDevice__)..."_ – Richard Critten Jun 22 '23 at 19:58
  • @RichardCritten Tried printing those 3 functions, each of them printed '101'. Could not find what that means as : hipSuccess, hipErrorMemoryAllocation, hipErrorInvalidValue each correspond to 0, 2 and 1 respectively – Nika Tark Jun 22 '23 at 20:08

0 Answers0