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

CUDA function call from other .cpp file that contains the same header file

本文关键字:文件 cpp 调用 CUDA 函数 其他 包含相      更新时间:2023-10-16

我有两个文件utilCUDA。Cu和util.cpp。它们都包含util.h。"add"在util.h中声明,并在util.cpp中定义。"add"调用"addCUDA"来将两个向量相加。请不要介意这个方法,这只是一个测试项目。

错误是:

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

我是否可以在"add"中调用"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 calledn");
    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;
}

Makefile:

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

util.cpp调用addCUDA,但您没有指出在哪里找到它。(也就是说,你没有在该范围内提供函数的"声明"。)

移动这一行:

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

从uticuda .h到util.h