gpt4 book ai didi

c++ - 为什么在 CUDA .cu 文件中会发生多重定义错误

转载 作者:行者123 更新时间:2023-11-28 01:50:51 31 4
gpt4 key购买 nike

我正在学习使用 CUDA。我写了两个 .cu 文件,一个包含主要功能,另一个用作提供所有其他功能的库。但是我在编译这两个 .cu 文件时遇到了多个定义错误。我展示了一个玩具示例如下:

main.cu 文件:

#ifndef MAIN
#define MAIN
#include "book.h"
#include <iostream>
#include "lib.h"

using namespace std;

int main(int argc, char const *argv[])
{
HelloWorld();
return 0;
}
#endif

lib.cu 文件:

#ifndef LIB
#define LIB

#include "book.h"
#include <iostream>
#include "lib.h"

using namespace std;

void HelloWorld(){
cout <<"HelloWorld" << endl;
return;
}
#endif

lib.h 文件:

#ifndef LIB_H
#define LIB_H

#include <iostream>

using namespace std;

void HelloWorld();
#endif

book.h 文件:

#ifndef __BOOK_H__
#define __BOOK_H__
#include <stdio.h>

static void HandleError( cudaError_t err,
const char *file,
int line ) {
if (err != cudaSuccess) {
printf( "%s in %s at line %d\n", cudaGetErrorString( err ),
file, line );
exit( EXIT_FAILURE );
}
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))


#define HANDLE_NULL( a ) {if (a == NULL) { \
printf( "Host memory failed in %s at line %d\n", \
__FILE__, __LINE__ ); \
exit( EXIT_FAILURE );}}

template< typename T >
void swap( T& a, T& b ) {
T t = a;
a = b;
b = t;
}


void* big_random_block( int size ) {
unsigned char *data = (unsigned char*)malloc( size );
HANDLE_NULL( data );
for (int i=0; i<size; i++)
data[i] = rand();

return data;
}

int* big_random_block_int( int size ) {
int *data = (int*)malloc( size * sizeof(int) );
HANDLE_NULL( data );
for (int i=0; i<size; i++)
data[i] = rand();

return data;
}


// a place for common kernels - starts here

__device__ unsigned char value( float n1, float n2, int hue ) {
if (hue > 360) hue -= 360;
else if (hue < 0) hue += 360;

if (hue < 60)
return (unsigned char)(255 * (n1 + (n2-n1)*hue/60));
if (hue < 180)
return (unsigned char)(255 * n2);
if (hue < 240)
return (unsigned char)(255 * (n1 + (n2-n1)*(240-hue)/60));
return (unsigned char)(255 * n1);
}

__global__ void float_to_color( unsigned char *optr,
const float *outSrc ) {
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;

float l = outSrc[offset];
float s = 1;
int h = (180 + (int)(360.0f * outSrc[offset])) % 360;
float m1, m2;

if (l <= 0.5f)
m2 = l * (1 + s);
else
m2 = l + s - l * s;
m1 = 2 * l - m2;

optr[offset*4 + 0] = value( m1, m2, h+120 );
optr[offset*4 + 1] = value( m1, m2, h );
optr[offset*4 + 2] = value( m1, m2, h -120 );
optr[offset*4 + 3] = 255;
}

__global__ void float_to_color( uchar4 *optr,
const float *outSrc ) {
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;

float l = outSrc[offset];
float s = 1;
int h = (180 + (int)(360.0f * outSrc[offset])) % 360;
float m1, m2;

if (l <= 0.5f)
m2 = l * (1 + s);
else
m2 = l + s - l * s;
m1 = 2 * l - m2;

optr[offset].x = value( m1, m2, h+120 );
optr[offset].y = value( m1, m2, h );
optr[offset].z = value( m1, m2, h -120 );
optr[offset].w = 255;
}


#if _WIN32
//Windows threads.
#include <windows.h>

typedef HANDLE CUTThread;
typedef unsigned (WINAPI *CUT_THREADROUTINE)(void *);

#define CUT_THREADPROC unsigned WINAPI
#define CUT_THREADEND return 0

#else
//POSIX threads.
#include <pthread.h>

typedef pthread_t CUTThread;
typedef void *(*CUT_THREADROUTINE)(void *);

#define CUT_THREADPROC void
#define CUT_THREADEND
#endif

//Create thread.
CUTThread start_thread( CUT_THREADROUTINE, void *data );

//Wait for thread to finish.
void end_thread( CUTThread thread );

//Destroy thread.
void destroy_thread( CUTThread thread );

//Wait for multiple threads.
void wait_for_threads( const CUTThread *threads, int num );

#if _WIN32
//Create thread
CUTThread start_thread(CUT_THREADROUTINE func, void *data){
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, data, 0, NULL);
}

//Wait for thread to finish
void end_thread(CUTThread thread){
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
}

//Destroy thread
void destroy_thread( CUTThread thread ){
TerminateThread(thread, 0);
CloseHandle(thread);
}

//Wait for multiple threads
void wait_for_threads(const CUTThread * threads, int num){
WaitForMultipleObjects(num, threads, true, INFINITE);

for(int i = 0; i < num; i++)
CloseHandle(threads[i]);
}

#else
//Create thread
CUTThread start_thread(CUT_THREADROUTINE func, void * data){
pthread_t thread;
pthread_create(&thread, NULL, func, data);
return thread;
}

//Wait for thread to finish
void end_thread(CUTThread thread){
pthread_join(thread, NULL);
}

//Destroy thread
void destroy_thread( CUTThread thread ){
pthread_cancel(thread);
}

//Wait for multiple threads
void wait_for_threads(const CUTThread * threads, int num){
for(int i = 0; i < num; i++)
end_thread( threads[i] );
}

#endif
#endif // __BOOK_H__

我使用命令编译文件:

nvcc *.cu -O3 -o a.out -lm -ccbin=/usr/bin/g++

然后我收到错误消息,例如 multiple definition of float_to_color(uchar4*, float const*)'

否则,如果我对 book.h 中的一个 include 进行注释,编译将成功,程序将正确运行。我发现一个可能的原因是gcc的版本,所以我尝试了gcc-4.4和gcc-4.8,但是这两种情况都会出错。我也尝试过使用 g++,但它并没有解决这个问题。我的代码有什么问题?谢谢大家帮助我!

最佳答案

你有多个定义,因为函数在头文件中。这意味着包含该文件的每个翻译单元都将具有该函数的拷贝。

要么将其移动到实现文件并链接,要么将函数标记为内联

进一步改进的地方:

  1. 永远不要在头文件中放置 using namespace 指令。这是一个主要的命名空间污染。

  2. 首选 std::swap 而不是自制的原始 swap 模板。

关于c++ - 为什么在 CUDA .cu 文件中会发生多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43086216/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com