开发者

invalid resource in a windows cuda project

I've ported a cuda project from linux to windows (basically just added few defines and typedefs in the header file). I'm using visual studio 2008, and the cuda runtime api custom build rules from the SDK. The code is c, not c++ (and I'm compiling /TC not /TP)

I'm having scope issues that I didn't have in linux. Global variables in my header file aren't shared between the .c files and .cu files.

I've created a simplified project, and here is all of the code:

main.h:

#ifndef MAIN_H
#define MAIN_H

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

cudaEvent_t cudaEventStart;

#if defined __cplusplus
extern "C" void func(void);
#else
extern void func(void);
#endif

#endif

main.c:

#include "main.h"

int main(void)
{
    int iDevice = 0;

    cudaSetDevice(iDevice);
    cudaFree(0);
    cudaGetDevice(&iDevice);
    printf("device: %d\n", iDevice);

    cudaEventCreate(&cudaEventStart);
    printf("create event: %d\n", (int) cudaEventSt开发者_运维问答art);

    func();

    cudaEventDestroy(cudaEventStart);
    printf("destroy event: %d\n", (int) cudaEventStart);

    return cudaThreadExit();
}

kernel.cu:

#include "main.h"

void func()
{
    printf("event in cu: %d\n", (int) cudaEventStart);
}

output:

device: 0
create event: 44199920
event in cu: 0
event destroy: 441999920

Any ideas about what I am doing wrong here? How do I need to change my setup so that it works in visual studio? Ideally, I'd like a setup that works multi-platform.

CUDA 3.2, GTX 480, 64-bit Win7, 263.06 general


What you are trying to do

  1. Would not work even without CUDA -- try renaming kernel.cu to kernel.c and recompile. You will get a linker error because cudaEventStart will be multiply defined -- in each compilation unit (.c file) that includes it. You would need to make the variable static, and initialize it in only one compilation unit.
  2. Compiles in CUDA because CUDA does not have a linker, and therefore code in compilation units compiled by nvcc (.cu files) cannot reference symbols in other compilation units. CUDA doesn't support static global variables currently. In the future CUDA will have a linker, but currently it does not.

What is happening is each compilation unit is getting its own, non-conflicting instance of cudaEventStart.

What you can do is get rid of the global variable (make it a local variable in main()), add cudaEvent_t parameters to the functions that need to use the event, and then pass the event variable around.

BTW, in your second post, you have circular #includes...


I modified my simplified example (with success) by including the .cu file in the header and removing the forward declarations of the .cu file function.

main.h:

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

#include "kernel.cu"

cudaEvent_t cudaEventStart;

main.c:

#include "main.h"

int main(void)
{
    int iDevice = 0;

    cudaSetDevice(iDevice);
    cudaFree(0);
    cudaGetDevice(&iDevice);
    printf("device: %d\n", iDevice);

    cudaEventCreate(&cudaEventStart);
    printf("create event: %d\n", (int) cudaEventStart);

    func();

    cudaEventDestroy(cudaEventStart);
    printf("destroy event: %d\n", (int) cudaEventStart);

    return cudaThreadExit();
}

kernel.cu:

#ifndef KERNEL_CU
#define KERNEL_CU

#include "main.h"

void func(void);

void func()
{
    printf("event in cu: %d\n", (int) cudaEventStart);
}

#endif

output:

device: 0
create event: 42784024
event in cu: 42784024
event destroy: 42784024

About to see if it works in my real project, and whether the solution is portable back to linux.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜