从包含相同头文件的其他.cpp文件调用CUDA函数

时间:2014-07-08 18:46:23

标签: c++ cuda header

我有两个文件utilCUDA.cu和util.cpp。它们都包含util.h. "添加"在util.h中声明并在util.cpp中定义。 "添加"电话" addCUDA"添加两个向量。请不要介意这种方法,它只是作为一个测试项目。

错误是:

util.cpp: In function ‘void add(double*, double*, double*, int)’:
util.cpp:5:20: error: ‘addCUDA’ was not declared in this scope

无论如何我可以打电话给#34; addCUDA"在"添加"?

util.h:

#ifndef __UTIL_H__
#define __UTIL_H__


#include <stdio.h>
void add(double *a, double *b, double *c, int size);
void printVec(double *v, int size);

#endif

util.cpp:

#include "util.h"

void add(double *a, double *b, double * c, int N)
{
    addCUDA(a,b,c,N);
}
void printVec(double *v, int size)
{
    int i;
    for(i = 0; i < size; i++)
        printf("%f ", v[i]);
    printf("\n");
}

utilCUDA.h:

#ifndef __UTILCUDA_H__
#define __UTILCUDA_H__
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include "util.h"

__global__ void myAdd(double *a, double *b, double *c, int size);
void addCUDA (double *a, double *b, double *c, int size);
#endif

utilCUDA.cu:

#include <stdio.h>
#include <stdlib.h>
#include "utilCUDA.h"
#define THREAD_PER_BLOCK 128
__global__ void myAdd( double *a, double *b, double *c, int size ) {
    int tid = threadIdx.x + blockIdx.x * blockDim.x;//blockIdx.x;    // this thread handles the data at its thread id
    if (tid < size)
        c[tid] = a[tid] + b[tid];
}

void addCUDA(double *a, double *b, double *c, int size)
{
    printf("CUDA called\n");
    double *dev_a, *dev_b, *dev_c;
    cudaMalloc( (void**)&dev_a, size * sizeof(double) );
    cudaMalloc( (void**)&dev_b, size * sizeof(double) );
    cudaMalloc( (void**)&dev_c, size * sizeof(double) ); 

    cudaMemcpy( dev_a, a, size * sizeof(double),
                cudaMemcpyHostToDevice );
    cudaMemcpy( dev_b, b, size * sizeof(double),
                cudaMemcpyHostToDevice );
    myAdd<<<(size - 1)/THREAD_PER_BLOCK + 1,THREAD_PER_BLOCK>>>( dev_a, dev_b, dev_c,size );

    cudaMemcpy( c, dev_c, size * sizeof(double),
                  cudaMemcpyDeviceToHost );
    cudaFree( dev_a );
    cudaFree( dev_b );
    cudaFree( dev_c );
}

TEST.CPP:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "util.h"

#ifdef USE_CUDA
    #include "utilCUDA.h"
#endif

using namespace std;

int main(int argc, char** argv)
{
    int size = atoi(argv[1]);

    double *a, *b, *c;// *cBase;
    int j;
    a = (double*)malloc(size*sizeof(double));
    b = (double*)malloc(size*sizeof(double));
    c = (double*)malloc(size*sizeof(double));    

    srand(time(NULL));
        for(j = 0; j < size; j++)
        {
            a[j] = rand() % 10;
            b[j] = rand() % 10;
        }

    printVec(a,size);
    printVec(b,size);
#ifdef USE_CUDA    
    addCUDA(a,b,c,size);

#endif

#ifdef NO_CUDA
    add(a,b,c,size);
#endif
    printVec(c,size);
    free(a);
    free(b);
    free(c);
    return 0;
}

生成文件:

NVCC_RESULT := $(shell which nvcc 2> NULL)
NVCC_TEST := $(notdir $(NVCC_RESULT))
CFLAGS=-c -Wall

CUDAFLAGS=-c

CUDA_INCLUDE =
OBJ=test.o util.o 
ifeq ($(NVCC_TEST),nvcc)
    CUDACC := nvcc
    CC := g++
    OBJ+=utilCUDA.o
    CUDA_INCLUDE += -I /usr/local/cuda-5.5/include
    CCFLAGS := -DUSE_CUDA
else
    CUDACC := g++
    CC := g++
    CCFLAGS := -DNO_CUDA

endif


all: test

test: $(OBJ)    
    $(CUDACC) $(CCFLAGS) $(OBJ) -o test

ifeq ($(NVCC_TEST),nvcc)
utilCUDA.o: utilCUDA.cu utilCUDA.h
    $(CUDACC) $(CCFLAGS) $(CUDAFLAGS) utilCUDA.cu
endif

.cpp.o:
    $(CC) $(CCFLAGS) $(CFLAGS) $*.cpp $(CUDA_INCLUDE)
clean:
    rm -rf *.o test

1 个答案:

答案 0 :(得分:3)

util.cpp调用addCUDA但你没有说明在哪里找到它。 (即你没有在该范围内提供该功能的“声明”。)

移动这一行:

void addCUDA (double *a, double *b, double *c, int size);

从utilCUDA.h到util.h

这与CUDA无关。