从包含相同头文件的其他.cpp文件调用CUDA函数
CUDA function call from other .cpp file that contains the same header file
我有两个文件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
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?