OpenCL - 全局内存读取预编码比本地更好

时间:2012-09-17 22:53:07

标签: opencl gpgpu

我有一个内核,我在NVidia GTX 680上运行,从使用全局内存切换到本地内存时,执行时间增加了。

我的内核是有限元光线跟踪器的一部分,现在在处理之前将每个元素加载到本地内存中。每个元素的数据存储在struct fastTriangle 中,其结构如下:

typedef struct fastTriangle {
    float cx, cy, cz, cw;
    float nx, ny, nz, nd;
    float ux, uy, uz, ud;
    float vx, vy, vz, vd;
} fastTriangle;

我将这些对象的数组传递给内核,其编写如下(为简洁起见,我删除了不相关的代码:

__kernel void testGPU(int n_samples, const int n_objects, global const fastTriangle *objects, __local int *x_res, __global int *hits) {
    // Get gid, lid, and lsize

    // Set up random number generator and thread variables

    // Local storage for the two triangles being processed
    __local fastTriangle triangles[2]; 

    for(int i = 0; i < n_objects; i++) {    // Fire ray from each object
        event_t evt = async_work_group_copy((local float*)&triangles[0], (global float*)&objects[i],sizeof(fastTriangle)/sizeof(float),0);

        //Initialise local memory x_res to 0's

        barrier(CLK_LOCAL_MEM_FENCE);
        wait_group_events(1, &evt);      


        Vector wsNormal = { triangles[0].cw*triangles[0].nx, triangles[0].cw*triangles[0].ny, triangles[0].cw*triangles[0].nz};

        for(int j = 0; j < n_samples; j+= 4) {
            // generate a float4 of random numbers here (rands

            for(int v = 0; v < 4; v++) {    // For each ray in ray packet
                //load the first object to be intesected
                evt = async_work_group_copy((local float*)&triangles[1], (global float*)&objects[0],sizeof(fastTriangle)/sizeof(float),0);

                // Some initialising code and calculate ray here
                // Should have ray fully specified at this point;


                for(int w = 0; w < n_objects; w++) {        // Check for intersection against each ray

                    wait_group_events(1, &evt);

                    // Check for intersection against object w


                    float det = wsDir.x*triangles[1].nx + wsDir.y*triangles[1].ny + wsDir.z*triangles[1].nz;
                    float dett = triangles[1].nd - (triangles[0].cx*triangles[1].nx + triangles[0].cy*triangles[1].ny + triangles[0].cz*triangles[1].nz);


                    float detpx = det*triangles[0].cx + dett*wsDir.x;
                    float detpy = det*triangles[0].cy + dett*wsDir.y;
                    float detpz = det*triangles[0].cz + dett*wsDir.z;


                    float detu = detpx*triangles[1].ux + detpy*triangles[1].uy + detpz*triangles[1].uz + det*triangles[1].ud;
                    float detv = detpx*triangles[1].vx + detpy*triangles[1].vy + detpz*triangles[1].vz + det*triangles[1].vd;


                    // Interleaving the copy of the next triangle
                    evt = async_work_group_copy((local float*)&triangles[1], (global float*)&objects[w+1],sizeof(fastTriangle)/sizeof(float),0);

                    // Complete intersection calculations

                } // end for each object intersected

                if(objectNo != -1) atomic_inc(&x_res[objectNo]);
            } // end for sub rays
        } // end for each ray
        barrier(CLK_LOCAL_MEM_FENCE);

        // Add all the local x_res to global array hits


        barrier(CLK_GLOBAL_MEM_FENCE);
    } // end for each object
}

当我第一次编写这个内核时,我没有在本地内存中缓冲每个对象,而只是从全局内存中访问它,而不是三角形[0] .cx我会使用对象[i] .cx

当设置为优化时,我切换到使用上面列出的本地内存,但后来观察到执行运行时间增加了大约25%。

为什么在使用本地内存缓冲对象而不是直接在全局内存中访问它们时性能会更差?

0 个答案:

没有答案