tags:

views:

803

answers:

2

I'm trying to use CUDA with cmake (v 2.8) on my Mac (OSX 10.6). So far it works fine, I created a small sample just to try it out (see below). However when I switch on emulation mode, it cannot invoke the CUDA kernel anymore and I get the following error message:

Cuda error: kernel invocation: invalid device function .

I also tried to compile it by invoking nvcc by hand and didn't get the error message, so I think it could be a problem with cmake.

I also noticed that emulation mode is deprecated in CUDA 3.0. Why is this? Nvidia points out in their release notes, that they provide Nexus for VS and cuda-gdb on Linux. But what about OSX? I could not find cuda-gdb in the OSX version I installed here..?!

Below the files

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project (test)

find_package(CUDA)
add_definitions(-Wall)

# Use CUDA emulator?
set(CUDA_BUILD_EMULATION ON)
set(CUDA_64_BIT_DEVICE_CODE OFF)    # Does not work on a Mac currently
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)
set(CUDA_VERBOSE_BUILD ON)

include_directories("${PROJECT_BINARY_DIR}")

cuda_add_executable(test
    test.cu
)

test.cu

#include <cuda.h>
#include <stdlib.h>
#include <stdio.h>

#include "test_kernel.cu"

void checkCUDAError(const char *msg);

int main( int argc, const char** argv )
{
    int n = 3;
    float* a_h;
    a_h = (float *)malloc(sizeof(float)*n);
    float* a_d;
    cudaMalloc((void**) &a_d, sizeof(float)*n);

    hello<<<1,128>>>(a_d, n);
    checkCUDAError("kernel invocation");
    checkCUDAError("memcpy");

    free(a_h);
    cudaFree(a_d);

    return 0;
}

void checkCUDAError(const char *msg)
{
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err)
    {
        fprintf(stderr, "Cuda error: %s: %s.\n", msg,
                cudaGetErrorString( err) );
        exit(EXIT_FAILURE);
    }
}

test_kernel.cu

#include <stdio.h>

__global__ void hello(float*a, int i)
{
    int j = i+1;
#ifdef _DEVICEEMU
    printf("Hello.\n");
#endif
}
A: 

Thanks Nils for your post. It helped me to find the answer to my problem with emulation mode.

Andreas