我有这些结构:
typedef struct neuron
{
float* weights;
int n_weights;
}Neuron;
typedef struct neurallayer
{
Neuron *neurons;
int n_neurons;
int act_function;
}NLayer;
Run Code Online (Sandbox Code Playgroud)
"NLayer"结构可以包含任意数量的"神经元"
我试图以这种方式从主机中分配一个带有5个'神经元'的'NLayer'结构:
NLayer* nL;
int i;
int tmp=9;
cudaMalloc((void**)&nL,sizeof(NLayer));
cudaMalloc((void**)&nL->neurons,6*sizeof(Neuron));
for(i=0;i<5;i++)
cudaMemcpy(&nL->neurons[i].n_weights,&tmp,sizeof(int),cudaMemcpyHostToDevice);
Run Code Online (Sandbox Code Playgroud)
...然后我尝试用该内核修改"nL-> neurons [0] .n_weights"变量:
__global__ void test(NLayer* n)
{
n->neurons[0].n_weights=121;
}
Run Code Online (Sandbox Code Playgroud)
但是在编译时,nvcc返回与内核唯一行相关的"警告":
Warning: Cannot tell what pointer points to, assuming global memory space
Run Code Online (Sandbox Code Playgroud)
当内核完成其工作时,结构开始无法访问.
很可能我在分配期间做错了什么......有人可以帮助我吗?非常感谢,对不起我的英语!:)
更新:
感谢aland我修改了我的代码,创建了这个函数,它应该分配一个struct"NLayer"的实例:
NLayer* setNLayer(int numNeurons,int weightsPerNeuron,int act_fun)
{
int i;
NLayer h_layer;
NLayer* d_layer;
float* d_weights;
//SET THE LAYER VARIABLE OF THE …Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
printf("Starting nets allocation...");
while(...)
{
...some operations...
}
puts("DONE");
Run Code Online (Sandbox Code Playgroud)
代码应该立即打印字符串"Starting nets allocation ..."然后,在循环之后,应该打印"DONE".
相反,程序首先执行循环,然后打印字符串"Starting nets allocation ... DONE"为什么会发生?我该如何解决这个问题?