Memory cuda:内核期间的非自愿内存更改

Memory cuda:内核期间的非自愿内存更改,memory,cuda,kernel,Memory,Cuda,Kernel,我是一名beginer cuda程序员 我试图建立一个类似于英伟达粒子系统的例子,例如立方体中的许多球。< /P> 我有一个内核louncher函数,如下所示: void Ccuda:: sort_Particles_And_Find_Cell_Start (int *Cell_Start, // output int *Cell_End,

我是一名beginer cuda程序员

<>我试图建立一个类似于英伟达粒子系统的例子,例如立方体中的许多球。< /P> 我有一个内核louncher函数,如下所示:

void Ccuda:: sort_Particles_And_Find_Cell_Start (int  *Cell_Start,          // output
                                                             int *Cell_End,                     // output
                                                             float3 *Sorted_Pos,                // output
                                                             float3 *Sorted_Vel,                //output
                                                             int  *Particle_Cell,                   // input
                                                             int  *Particle_Index,          // input
                                                             float3 *Old_Pos,
                                                             float3 *Old_Vel,
                                                             int   Num_Particles, 
                                                             int Num_Cells)
 {
     int numThreads, numBlocks;

     /*Cell_Start = (int*) cudaAlloc (Num_Cells, sizeof(int));
     Cell_End = (int*) cudaAlloc (Num_Cells, sizeof(int));
     Sorted_Pos = (float3*) cudaAlloc (Num_Particles, sizeof(int));
     Sorted_Vel = (float3*) cudaAlloc (Num_Particles, sizeof(int));*/

    int *h_p_cell = (int *) malloc (Num_Particles * sizeof (int));
    cudaMemcpy (h_p_cell,Particle_Cell, Num_Particles*sizeof(int),cudaMemcpyDeviceToHost);
    free (h_p_cell);

    computeGridSize(Num_Particles, 512, numBlocks, numThreads);

    sort_Particles_And_Find_Cell_StartD<<<numBlocks, numThreads>>>(Cell_Start,Cell_End, Sorted_Pos, Sorted_Vel, Particle_Cell, Particle_Index, Old_Pos, Old_Vel, Num_Particles);

    h_p_cell = (int *) malloc (Num_Particles * sizeof (int));
    cudaMemcpy (h_p_cell,Particle_Cell, Num_Particles*sizeof(int),cudaMemcpyDeviceToHost);
    free (h_p_cell);
 }
这个全局核函数:

__global__ void sort_Particles_And_Find_Cell_StartD(int  *Cell_Start,       // output
                                     int *Cell_End,                     // output
                                     float3 *Sorted_Pos,                // output
                                     float3 *Sorted_Vel,                //output
                                     int  *Particle_Cell,       // input
                                     int  *Particle_Index,          // input
                                     float3 *Old_Pos,
                                     float3 *Old_Vel,
                                     int   Num_Particles)
     {
        int hash;
        extern __shared__ int Shared_Hash[];    // blockSize + 1 elements
        int index = blockIdx.x*blockDim.x + threadIdx.x;

        if (index < Num_Particles)
        { 
             hash = Particle_Cell[index];
             Shared_Hash[threadIdx.x+1] = hash;

            if (index > 0 && threadIdx.x == 0)
            {
                // first thread in block load previous particle hash
                Shared_Hash[0] = Particle_Cell[index-1];
            }
        }

        __syncthreads();

    if (index < Num_Particles)
    {
        // If this particle has a different cell index to the previous
        // particle then it must be the first particle in the cell,
        // so store the index of this particle in the cell.
        // As it isn't the first particle, it must also be the cell end of
        // the previous particle's cell

        if (index == 0 || hash != Shared_Hash[threadIdx.x])     // if its the first thread in the grid or its particle cell index is different from cell index of the previous neighboring thread
        {
            Cell_Start[hash] = index;

            if (index > 0)
                Cell_End[Shared_Hash[threadIdx.x]] = index;
        }

        if (index == Num_Particles - 1)
        {
            Cell_End[hash] = index + 1;
        }

        // Now use the sorted index to reorder the pos and vel data
        int Sorted_Index = Particle_Index[index];
        //float3 pos = FETCH(Old_Pos, Sorted_Index);       // macro does either global read or texture fetch
        //float3 vel = FETCH(Old_Vel, Sorted_Index);       // see particles_kernel.cuh
        float3 pos = Old_Pos[Sorted_Index];
        float3 vel = Old_Vel[Sorted_Index];
        Sorted_Pos[index] = pos;
        Sorted_Vel[index] = vel;
    }
在执行过程中,我得到了这个调试arror massege r6010,表示调用了中止

正如您在louncher函数中看到的,我使用int*h_p_单元格查看的第一个函数 在内核执行之前和之后,Particle_单元的内容,看起来内容已经改变了,尽管在内核内部没有分配给Particle_单元。 在程序初始化期间由cudaMemcpy分配的粒子单元内存

我已经尝试了几天来解决这个问题,但没有成功
有人能帮忙吗?

您的内核需要动态分配:

但您没有在内核调用中分配任何资源:

sort_Particles_And_Find_Cell_StartD<<<numBlocks, numThreads>>>(Cell_Start,Cell_End, Sorted_Pos, Sorted_Vel, Particle_Cell, Particle_Index, Old_Pos, Old_Vel, Num_Particles);
                                                           ^
                                                           |
                                                missing shared memory size parameter
您应该在内存中提供共享内存量。你可能想要这样的东西:

sort_Particles_And_Find_Cell_StartD<<<numBlocks, numThreads, ((numThreads+1)*sizeof(int))>>>(Cell_Start,Cell_End, Sorted_Pos, Sorted_Vel, Particle_Cell, Particle_Index, Old_Pos, Old_Vel, Num_Particles);
此错误将导致内核在尝试访问共享内存时中止。 您还应该对所有CUDAAPI调用和内核调用执行此操作。我在你的密码里没有看到任何证据


在整理完所有API错误后,使用cuda memcheck运行代码。对Particle_Cell进行意外写入的原因可能是来自内核的越界访问,这一点在cuda memcheck中会很明显。

此问题没有足够的信息。没有CUDA内核可供查看。你没有提到预期的结果是什么,你看到的错误是什么。嘿,非常感谢!!!你真的帮了我,真是太快了。你成就了我的一天:
sort_Particles_And_Find_Cell_StartD<<<numBlocks, numThreads, ((numThreads+1)*sizeof(int))>>>(Cell_Start,Cell_End, Sorted_Pos, Sorted_Vel, Particle_Cell, Particle_Index, Old_Pos, Old_Vel, Num_Particles);