Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly pass a struct as a parameter to a kernel #14

Open
ChenxiZhou0619 opened this issue Jul 9, 2024 · 1 comment
Open

Comments

@ChenxiZhou0619
Copy link

Hi, I'm trying to pass a struct parameter to a kernel function, the struct is like

struct MyStruct {
  hiprtInt2 xy;
  int       z;
};

the kernel is like

extern "C" __global__ void
ReturnConstantInt(hiprtGeometry geom, uint8_t* pixels, MyStruct myStruct) {
  const uint32_t x     = blockIdx.x * blockDim.x + threadIdx.x;
  const uint32_t y     = blockIdx.y * blockDim.y + threadIdx.y;
  const uint32_t index = x + y * 512;

  pixels[index * 4 + 0] = (uint8_t)(myStruct.xy.x);
  pixels[index * 4 + 1] = (uint8_t)(myStruct.xy.x);
  pixels[index * 4 + 2] = (uint8_t)(myStruct.xy.x);
  pixels[index * 4 + 3] = 255;
}

then i initialize the struct and launch the kernel in main function

  oroFunction func;
  hiprtApiFunction         kernel_out;

 CHECK_HIPRT(hiprtutils::build_trace_kernel(ctxt,
                                             "kernel_path",
                                             "ReturnConstantInt",
                                             kernel_out,
                                             additional_includes,
                                             kernel_options, // empty vector
                                             0,
                                             1,
                                             false));

  func = reinterpret_cast<oroFunction>(kernel_out);

// ...

 MyStruct myStruct;
  myStruct.xy = {50, 150};
  myStruct.z  = 250;

  CHECK_ORO(oroMalloc(reinterpret_cast<oroDeviceptr*>(&pixels), 512 * 512 * 4));

  void* args[] = {&geom, &pixels, &myStruct};
  launchKernel(func, 512, 512, args);
  writeImage("test.png", 512, 512, pixels);

the result is
test

when I change MyStruct to

struct MyStruct {
  int       z;
  hiprtInt2 xy;
};

the result image is
test

I suspect this is due to the alignment issue or something else I haven't take care, where can I find more hiprt programming guide?

@meistdan
Copy link
Collaborator

meistdan commented Jul 9, 2024

Hi, I don't see any obvious issue with the alignment. I suspect that the kernel was not recompiled. So, the host code uses the new layout, but the kernel still the previous one. Could you try to delete files in the cache directory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants