/* Include Libs */
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main(void)
{
int a=1;
int c=2;
int *deva, *devc;
cudaMalloc( (void**)&deva, sizeof(int) );
cudaMalloc( (void**)&devc, sizeof(int) );
cudaMemcpy( deva, &a, sizeof(int), cudaMemcpyHostToDevice );
cudaMemcpy( devc, &c, sizeof(int), cudaMemcpyHostToDevice );
cudaMemcpy( &c, deva, sizeof(int), cudaMemcpyDeviceToHost );
cudaMemcpy( &a, devc, sizeof(int), cudaMemcpyDeviceToHost );
printf("\n%d %d\n", a, c); // Output (should be "2 1")
cudaFree( deva );
cudaFree( devc );
return 0;
}
This simple code should swap a=1 and c=2, producing an output "2 1", but it does nothing and this isn't the only "simple" example based on a textbook that isn't working, for example why do all the textbooks say I can initialize a and c as pointers and later fill in their values, but the program won't compile if I do that? What am I overlooking here?