How can I share data between the host and device?

I would like to exchange data between the host and device while the device is performing a computation. I created a sample program to test the concept.

I found that when my device code contains a printf(“”); statement the codes executes as expected. However when I removed this line the host code is no longer able to see the updates from the device.

What am I doing wrong and is there a better method to accomplish this?

global void mykernel(volatile int* datav)
{
//Stops working when commented
printf(“”);

//Run the loop
do
{
    int v = (*datav);
    if (v % 2 == 1)
    {
        (*datav) += 1;
    }
} while (true);

}

int main()
{
//Set the GPU device to use
cudaSetDevice(0);
cudaCheckErrors(“cudaSetDevice error”);

//Allow pinned memory so the host and device can share asynchronously
cudaSetDeviceFlags(cudaDeviceMapHost);
cudaCheckErrors("cudaSetDeviceFlags error");

//Allocate pinned memory (this exists on the host but is accessible by the device)
volatile int* d_data, * h_data;
cudaHostAlloc((void**)&h_data, sizeof(int), cudaHostAllocMapped);
cudaCheckErrors("cudaHostAlloc error");

//Connect the device to the pinned host memory
cudaHostGetDevicePointer((int**)&d_data, (void*)h_data, 0);
cudaCheckErrors("cudaHostGetDevicePointer error");

//Start the kernels
mykernel << <1, 1 >> > (d_data);                   
cudaCheckErrors("kernel fail");

//Loop
do
{
    int value1 = (*h_data);
    if (value1 % 2 == 0)
        (*h_data) += 1;
} while (true);

//Synchronize the device
cudaDeviceSynchronize();
cudaCheckErrors("kernel fail 2");

//Free
cudaFreeHost((void*)h_data);

//Reset
cudaDeviceReset();

//Return
return 0;

}