Ov's Pianist
Ov's Pianist

Reputation: 11

Cuda unified memory simple test failed

I'm a total newbie, here's the first attempt to test out Cuda6 unified memory. After compiling and running, I expected to get:38, 10, but instead I got the result of: 5, 10. Is there anything I did so wrong that makes me totally stupid in this case? Thanks a lot guys!

#include <iostream>
#include <cuda.h>

__global__ void add2(int *a, int *b)
    {
        *a += 33;
    }

int main(){

    int a = 5;
    int b = 10;

    int *p_a, *p_b;

    cudaMallocManaged(&p_a,sizeof(int));
    cudaMallocManaged(&p_b,sizeof(int));

    p_a = &a;
    p_b = &b;

    add2<<<1,1>>>(p_a, p_b);
    cudaDeviceSynchronize();

    std::cout << *p_a << " " << b << std::endl;
    return 0;
}

Upvotes: 1

Views: 811

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151899

cudaMallocManaged creates an allocation and sets the pointer accordingly, something like malloc. If you then go and wipe out that assigned pointer with

p_a=&a;

your kernel won't work. Read the section in the CUDA 6 programming guide to learn how UM works.

Instead you could try

*p_a=a;

and similarly for b.

Upvotes: 2

Related Questions