簡體   English   中英

CUDA和C ++函數問題(Visual Studio 2013)

[英]CUDA and C++ function problems ( Visual Studio 2013)

我試圖從Visual Studio中的cpp文件調用cu文件中定義的cud​​a函數,但我一直收到以下錯誤。

TomColourCorrectionMain.obj:錯誤LNK2019:未解析的外部符號“public:void __cdecl hwk :: TomColourCorrection :: brightness(int,int)”(?brightness @ TomColourCorrection @hwk @@ QEAAXHH @ Z)在函數“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答案的細節,所以如果它適合你,請接受答案。

我想你需要改變

void brightness(int iw, int ih)

void TomColourCorrection::brightness(int iw, int ih)

並將實現移動到頭文件或.cpp文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM