Pinned memory scope

I’m trying to do some basic functional programming using pinned memory to later be used with cudamemcpyasync. However, I seem to be running into scope issues which I haven’t seen documented in the cuda reference manual.

For a simple example (see below), I call a function to allocate pinned memory for a pointer that’s been past to the function. Printing out the pointer within the mallochost function gives an expected pointer address, however, printing the pointer address directly after this function call within main() results in the memory address still being NULL. Am I missing something? Thanks

#include <stdio.h>

void mallochost(double * data_h) {
  cudaMallocHost((void**)&data_h, sizeof(double) * 10);
  printf("%p\n", (void*)data_h);
}

int main() {
  double* data_h = NULL;
  mallochost(data_h);
  printf("%p\n", (void*)data_h);
  return 0;
}

This is not a cuda related problem. The variable data_h in the main function is different from the variable data_h in the function. Modifying the function variable will not modify the main variable. You need to pass it as double * *

Thanks @striker159
Indeed this was the case. I wrapped it in a struct and it fixed my minimal example which more closely reflected the production code (see below). Indeed there was a pointer short in the production code causing the memory address to be lost after. Cheers

#include <stdio.h>
struct foo {
  double * data_h;
};


void mallochost(foo * f) {
  cudaMallocHost((void**)&(f->data_h), sizeof(double) * 10);
  printf("%p\n", (void*)(f->data_h));
}

int main() {
  foo f;
  f.data_h = NULL;
  mallochost(&f);
  printf("%p\n", (void*)f.data_h);
  return 0;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.