内核调用产生错误“错误:无法配置主机函数调用”。调用有什么问题?

Aar*_*ron -2 c++ cuda compiler-errors

使用nvcc -c mag_cuda.cu编译以下代码时:

//Standard Libraries
#include <iostream>
#include <math.h>
#include <vector>

//Project Specific Header
#include "mag.hpp"

 __global__
  void indv_B_components(int *self_coords, int pole_coords[][3], double *indv_B[][3], int No_poles, int counter_1)
  {
    some code......
  }

  //----------------------------------------------------------
  //------- Function to Calculate B Field at Each Pole -------
  //----------------------------------------------------------
  void calc_indv_B()
  {
    //declare namepspace for internal variables
    using namespace mag::internal;

    int *ppole_coords = &pole_coords[0][0];
    double *pindv_B;
    int self_coords[3];

    int num_threads_in_block = 256;
    int num_blocks = 32*2;

    cudaMallocManaged(&pindv_B, No_poles*3*sizeof(int));  


    //first loop to go over all poles
    for(int counter_1 = 0; counter_1 < No_poles; counter_1++)
      {
      //store coords of the current pole
      self_coords[0] = pole_coords[counter_1][0];
      self_coords[1] = pole_coords[counter_1][1];
      self_coords[2] = pole_coords[counter_1][2];


      indv_B_components<<<num_blocks, num_threads_in_block>>>(self_coords, ppole_coords,  pindv_B, No_poles, counter_1); 



      cudaDeviceSynchronize();
      }

    cudaFree(pindv_B);

    //return from function
    return;
  }
Run Code Online (Sandbox Code Playgroud)

返回以下错误:

错误:无法配置主机函数调用

指线

indv_B_components<<<num_blocks, num_threads_in_block>>>(self_coords, ppole_coords,  pindv_B, No_poles, counter_1); 
Run Code Online (Sandbox Code Playgroud)

由于所有参数均已定义且内核已被主机设备调用,因此__global__我不知道是什么原因引起的。

头文件mag.hpp是:

//make sure MAG_H_ module hasnt been defined multiple times
#ifndef MAG_H_
#define MAG_H_

//standard libraries
#include <iostream>
#include <math.h>
#include <vector>


//Namespace for module
namespace mag
{


  //define functions
  ...


  void indv_B_components(int *self_coords, int *pole_coords, double *indv_B, int No_poles, int counter_1);

  void calc_indv_B();

  ...
}
#endif //MAG_H_

Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

Mic*_*zel 5

发生此错误的一种情况是,如果__global__在调用内核时仅对内核函数进行了“声明”而没有指定符,例如:

void kernel();

void f()
{
    kernel<<<1, 1>>>();
}

__global__
void kernel()
{
}
Run Code Online (Sandbox Code Playgroud)

现场演示

内核函数的声明必须包含说明__global__符:

__global__ void kernel();
Run Code Online (Sandbox Code Playgroud)

否则,声明将不声明内核函数,而仅声明普通的主机函数,这就是为什么编译器随后抱怨,因为只能在GPU上启动内核函数……

  • @Aaron:尽管您提出了抗议,但此答案中描述的情况是*确切地*代码中正在发生的事情 (2认同)