gpt4 book ai didi

c++ - CUDA 和 C++ 函数问题 (Visual Studio 2013)

转载 作者:太空狗 更新时间:2023-10-29 20:23:25 25 4
gpt4 key购买 nike

我试图从 Visual Studio 中的 cpp 文件调用在 cu 文件中定义的 cuda 函数,但我不断收到以下错误。

TomColourCorrectionMain.obj : error LNK2019: unresolved external symbol "public: void __cdecl hwk::TomColourCorrection::brightness(int,int)" (?brightness@TomColourCorrection@hwk@@QEAAXHH@Z) referenced in function "public: virtual void __cdecl hwk::TomColourCorrection::processCore(class std::shared_ptr)" (?processCore@TomColourCorrection@hwk@@UEAAXV?$shared_ptr@VIImageProcessingContext@hwk@@@std@@@Z)

现在通过阅读与此类似的其他问题,我了解到它与函数的定义方式有关并且那里有问题但是我在头文件和 cuda 文件中定义时看不到它。

这是我的代码(我是 CUDA 的新手,但我可以很好地编译 CUDA,并且当我不在 C++ 中调用此函数时代码运行):

头文件

#pragma once

#include "ImageProcessorWithProperties.h"
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>

class TomColourCorrection : public ImageProcessorWithProperties, public PropertyConsumer<TomColourCorrection>{
public: TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess);

virtual void processCore(IImageProcessingContextPtr context);

static void DeclareSettings(hwk::PropNodePtr n);

virtual ~TomColourCorrection();

void brightness(int iw, int ih); (function I am talking about)
};
}

带有函数调用的cpp文件//它只是重要代码的一部分,因为其余部分对于实际函数本身不是必需的

 #include "stdafx.h"
#include "TomColourCorrection.h"

#include <opencv2/imgproc/imgproc.hpp>
#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

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

namespace hwk{

TomColourCorrection::TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess) :
ImageProcessorWithProperties("sandbox", n, requestReprocess),
PropertyConsumer<TomColourCorrection>(n)
{

}

void TomColourCorrection::processCore(IImageProcessingContextPtr context){



brightness(16, 16); (just generic numbers at the moment as I am trying to resolve this issue etc)
}
}

CUDA文件和函数定义

#include "TomColourCorrection.h"
#include "device_launch_parameters.h"

__global__ void brightness_kernel(int iw, int ih)
{
// Calculate our pixel's location
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;

// Variables to store the sum
int count = 0;
float sum = 0.0;

// Do the blur operation by summing the surround pixels
/* for (int j = -(bh / 2); j <= (bh / 2); j++)
{
for (int i = -(bw / 2); i <= (bw / 2); i++)
{
// Verify that this offset is within the image boundaries
if ((x + i) < iw && (x + i) >= 0 && (y + j) < ih && (y + j) >= 0)
{
sum += (float)source[((y + j) * iw) + (x + i)];
count++;
}
}
}*/

// Average the sum
sum /= (float)count;
// dest[(y * iw) + x] = (unsigned char)sum;
}



void brightness(int iw, int ih) //, unsigned char *source, unsigned char *dest)
{
// allocate memory for the bitmap in GPU memory
unsigned char *dev_source, *dev_dest;
// cudaHostGetDevicePointer(&dev_source, source, 0);
// cudaHostGetDevicePointer(&dev_dest, dest, 0);

// Run the boxfilter kernel
dim3 blocks(iw / 16, ih / 16);
dim3 threads(16, 16);

// Execute the kernel
brightness_kernel << <blocks, threads >> >(iw, ih);
cudaThreadSynchronize();
}

最佳答案

像这样修改 TomColourCorrection.h:

#pragma once

#include "ImageProcessorWithProperties.h"
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>

void brightness_wrapper(int, int);
class TomColourCorrection : public ImageProcessorWithProperties, public PropertyConsumer<TomColourCorrection>{

public:
TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess);

virtual void processCore(IImageProcessingContextPtr context);

static void DeclareSettings(hwk::PropNodePtr n);

virtual ~TomColourCorrection();

void brightness(int iw, int ih);
};

像这样修改你的 cpp 文件:

 #include "stdafx.h"
#include "TomColourCorrection.h"

#include <opencv2/imgproc/imgproc.hpp>
#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

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

namespace hwk{

void TomColourCorrection::brightness(int iw, int ih){
brightness_wrapper(iw, ih);}

TomColourCorrection::TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess) : ImageProcessorWithProperties("sandbox", n, requestReprocess), PropertyConsumer<TomColourCorrection>(n)
{

}

void TomColourCorrection::processCore(IImageProcessingContextPtr context){
brightness(16, 16);
}
}

在你的 cuda 文件中改变这个:

void brightness(int iw, int ih) //, unsigned char *source, unsigned char *dest)

为此:

void brightness_wrapper(int iw, int ih) //, unsigned char *source, unsigned char *dest)

这主要只是拼写了 Ryck 的回答的细节。

关于c++ - CUDA 和 C++ 函数问题 (Visual Studio 2013),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33376382/

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