0

I'm trying to implement a naive version of LU decomposition in OpenCL. To start, I have implemented a sequential version in C++ and constructed methods to verify my result (i.e., multiplication methods). Next I implemented my algorithm in a kernel and tested it with manually verified input (i.e., a 5x5 matrix). This works fine.

However, when I run my algorithm on a randomly generated matrix bigger than 5x5 I get strange results. I've cleaned my code, checked the calculations manually but I can't figure out where my kernel is going wrong. I'm starting to think that it might have something to do with the floats and the stability of the calculations. By this I mean that error margins get propagated and get bigger and bigger. I'm well-aware that I can swap rows to get the biggest pivot value and such, but the error margin is way off sometimes. And in any case I would have expected the result - albeit a wrong one - to be the same as the sequential algorithm. I would like some help identifying where I could be doing something wrong.

I'm using a single dimensional array so addressing a matrix with two dimensions happens like this:

A(row, col) = A[row * matrix_width + col].

About the results I might add that I decided to merge the L and U matrix into one. So Given L and U:

L:     U:
1 0 0  A B C
X 1 0  0 D E
Y Z 1  0 0 F

I display them as:

A:
A B C
X D E
Y Z F

The kernel is the following:

The parameter source is the original matrix I want to decompose. The parameter destin is the destination. matrix_size is the total size of the matrix (so that would be 9 for a 3x3) and matrix_width is the width (3 for a 3x3 matrix).

__kernel void matrix(
    __global float * source,
    __global float * destin,
    unsigned int   matrix_size,
    unsigned int   matrix_width
    )
{
    unsigned int index = get_global_id(0);
    int col_idx = index % matrix_width;
    int row_idx = index / matrix_width;

    if (index >= matrix_size)
        return;

    // First of all, copy our value to the destination.
    destin[index] = source[index];
    // Iterate over all the pivots.
    for(int piv_idx = 0; piv_idx < matrix_width; piv_idx++)
    {
        // We have to be the row below the pivot row
        // And we have to be the column of the pivot
        // or right of that column.
        if(col_idx < piv_idx || row_idx <= piv_idx)
            return;
        // Calculate the divisor.
        float pivot_value       = destin[(piv_idx * matrix_width) + piv_idx];
        float below_pivot_value = destin[(row_idx * matrix_width) + piv_idx];
        float divisor           = below_pivot_value/ pivot_value;

        // Get the value in the pivot row on this column.
        float pivot_row_value = destin[(piv_idx * matrix_width) + col_idx];
        float current_value   = destin[index];
        destin[index]         = current_value - (pivot_row_value * divisor);


        // Write the divisor to the memory (we won't use these values anymore!)
        // if we are the value under the pivot.
        barrier(CLK_GLOBAL_MEM_FENCE);
        if(col_idx == piv_idx)
        {
            int divisor_location = (row_idx * matrix_width) + piv_idx;
            destin[divisor_location] = divisor;
        }
        barrier(CLK_GLOBAL_MEM_FENCE);
    }
}

This is the sequential version:

// Decomposes a matrix into L and U but in the same matrix.
float * decompose(float* A, int matrix_width)
{
    int total_length = matrix_width*matrix_width;
    float *U = new float[total_length];

    for (int i = 0; i < total_length; i++)
    {
        U[i] = A[i];
    }
    for (int row = 0; row < matrix_width; row++)
    {
        int pivot_idx = row;
        float pivot_val = U[pivot_idx * matrix_width + pivot_idx];

        for (int r = row + 1; r < matrix_width; r++)
        {
            float below_pivot = U[r*matrix_width + pivot_idx];
            float divisor = below_pivot / pivot_val;

            for (int row_idx = pivot_idx; row_idx < matrix_width; row_idx++)
            {
                float value = U[row * matrix_width + row_idx];
                U[r*matrix_width + row_idx] = U[r*matrix_width + row_idx] - (value * divisor);
            }
            U[r * matrix_width + pivot_idx] = divisor;
        }
    }
    return U;
}

An example output I get is the following:

Workgroup size: 1
Array dimension: 6
Original unfactorized:
|     176.000000 |     133.000000 |     431.000000 |     839.000000 |     739.000000 |     450.000000 |
|     507.000000 |     718.000000 |     670.000000 |     753.000000 |     122.000000 |     941.000000 |
|     597.000000 |     449.000000 |     596.000000 |     742.000000 |     491.000000 |     212.000000 |
|     159.000000 |     944.000000 |     797.000000 |     717.000000 |     822.000000 |     219.000000 |
|     266.000000 |     755.000000 |      33.000000 |     231.000000 |     824.000000 |     785.000000 |
|     724.000000 |     408.000000 |     652.000000 |     863.000000 |     663.000000 |     113.000000 |
Sequential:
|     176.000000 |     133.000000 |     431.000000 |     839.000000 |     739.000000 |     450.000000 |
|       2.880682 |     334.869324 |    -571.573853 |   -1663.892090 |   -2006.823730 |    -355.306763 |
|       3.392045 |      -0.006397 |    -869.627747 |   -2114.569580 |   -2028.558716 |   -1316.693359 |
|       0.903409 |       2.460203 |      -2.085742 |    -357.893066 |     860.526367 |   -2059.689209 |
|       1.511364 |       1.654343 |      -0.376231 |      -2.570729 |    4476.049805 |   -5097.599121 |
|       4.113636 |      -0.415427 |       1.562076 |      -0.065806 |       0.003290 |      52.263515 |
Sequential multiplied matching with original?:
1
GPU:
|     176.000000 |     133.000000 |     431.000000 |     839.000000 |     739.000000 |     450.000000 |
|       2.880682 |     334.869293 |    -571.573914 |   -1663.892212 |   -2006.823975 |    -355.306885 |
|       3.392045 |      -0.006397 |    -869.627808 |   -2114.569580 |   -2028.558716 |   -1316.693359 |
|       0.903409 |       2.460203 |      -2.085742 |    -357.892578 |    5091.575684 |   -2059.688965 |
|       1.511364 |       1.654343 |      -0.376232 |      -2.570732 |   16116.155273 |   -5097.604980 |
|       4.113636 |      -0.415427 |      -0.737347 |       2.005755 |      -3.655331 |    -237.480438 |
GPU multiplied matching with original?:
Values differ: 5053.05 -- 822
0
Values differ: 5091.58 -- 860.526
Correct solution? 0

Edit

Okay, I understand why it was not working before, I think. The reason is that I only synchronize on each workgroup. When I would call my kernel with a workgroup size equal to the number of items in my matrix it would always be correct, because then the barriers would work properly. However, I decided to go with the approach as mentioned in the comments. Enqueue multiple kernels and wait for each kernel to finish before starting the next one. This would then map onto an iteration over each row of the matrix and multiplying it with the pivot element. This makes sure that I do not modify or read elements that are being modified by the kernel at that point.

Again, this works but only for small matrices. So I think I was wrong in assuming that it was the synchronization only. As per the request of Baiz I am posting my entire main here that calls the kernel:

int main(int argc, char *argv[])
{
    try {
        if (argc != 5) {
            std::ostringstream oss;
            oss << "Usage: " << argv[0] << " <kernel_file> <kernel_name> <workgroup_size> <array width>";
            throw std::runtime_error(oss.str());
        }
        // Read in arguments.
        std::string kernel_file(argv[1]);
        std::string kernel_name(argv[2]);
        unsigned int workgroup_size = atoi(argv[3]);
        unsigned int array_dimension = atoi(argv[4]);
        int total_matrix_length = array_dimension * array_dimension;
        // Print parameters
        std::cout << "Workgroup size: " << workgroup_size << std::endl;
        std::cout << "Array dimension: " << array_dimension << std::endl;

        // Create matrix to work on.
        // Create a random array.
        int matrix_width = sqrt(total_matrix_length);
        float* input_matrix = new float[total_matrix_length];
        input_matrix = randomMatrix(total_matrix_length);

        /// Debugging
        //float* input_matrix = new float[9];
        //int matrix_width = 3;
        //total_matrix_length = matrix_width * matrix_width;
        //input_matrix[0] = 10; input_matrix[1] = -7; input_matrix[2] = 0;
        //input_matrix[3] = -3; input_matrix[4] =  2; input_matrix[5] = 6;
        //input_matrix[6] =  5; input_matrix[7] = -1; input_matrix[8] = 5;

        // Allocate memory on the host and populate source
        float *gpu_result = new float[total_matrix_length];


        // OpenCL initialization
        std::vector<cl::Platform> platforms;
        std::vector<cl::Device> devices;
        cl::Platform::get(&platforms);
        platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
        cl::Context context(devices);
        cl::CommandQueue queue(context, devices[0], CL_QUEUE_PROFILING_ENABLE);

        // Load the kernel source.
        std::string file_text;
        std::ifstream file_stream(kernel_file.c_str());
        if (!file_stream) {
            std::ostringstream oss;
            oss << "There is no file called " << kernel_file;
            throw std::runtime_error(oss.str());
        }
        file_text.assign(std::istreambuf_iterator<char>(file_stream), std::istreambuf_iterator<char>());

        // Compile the kernel source.
        std::string source_code = file_text;
        std::pair<const char *, size_t> source(source_code.c_str(), source_code.size());
        cl::Program::Sources sources;
        sources.push_back(source);
        cl::Program program(context, sources);
        try {
            program.build(devices);
        }
        catch (cl::Error& e) {
            std::string msg;
            program.getBuildInfo<std::string>(devices[0], CL_PROGRAM_BUILD_LOG, &msg);
            std::cerr << "Your kernel failed to compile" << std::endl;
            std::cerr << "-----------------------------" << std::endl;
            std::cerr << msg;
            throw(e);
        }

        // Allocate memory on the device
        cl::Buffer source_buf(context, CL_MEM_READ_ONLY, total_matrix_length*sizeof(float));
        cl::Buffer dest_buf(context, CL_MEM_WRITE_ONLY, total_matrix_length*sizeof(float));

        // Create the actual kernel.
        cl::Kernel kernel(program, kernel_name.c_str());

        // transfer source data from the host to the device
        queue.enqueueWriteBuffer(source_buf, CL_TRUE, 0, total_matrix_length*sizeof(float), input_matrix);

        for (int pivot_idx = 0; pivot_idx < matrix_width; pivot_idx++)
        {
            // set the kernel arguments
            kernel.setArg<cl::Memory>(0, source_buf);
            kernel.setArg<cl::Memory>(1, dest_buf);
            kernel.setArg<cl_uint>(2, total_matrix_length);
            kernel.setArg<cl_uint>(3, matrix_width);
            kernel.setArg<cl_int>(4, pivot_idx);

            // execute the code on the device
            std::cout << "Enqueueing new kernel for " << pivot_idx << std::endl;
            cl::Event evt;
            queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(total_matrix_length), cl::NDRange(workgroup_size), 0, &evt);
            evt.wait();
            std::cout << "Iteration " << pivot_idx << " done" << std::endl;
        }

        // transfer destination data from the device to the host
        queue.enqueueReadBuffer(dest_buf, CL_TRUE, 0, total_matrix_length*sizeof(float), gpu_result);

        // Calculate sequentially.
        float* sequential = decompose(input_matrix, matrix_width);

        // Print out the results.
        std::cout << "Sequential:\n";
        printMatrix(total_matrix_length, sequential);

        // Print out the results.
        std::cout << "GPU:\n";
        printMatrix(total_matrix_length, gpu_result);

        std::cout << "Correct solution? " << equalMatrices(gpu_result, sequential, total_matrix_length);


        // compute the data throughput in GB/s
        //float throughput = (2.0*total_matrix_length*sizeof(float)) / t; // t is in nano seconds
        //std::cout << "Achieved throughput: " << throughput << std::endl;

        // Cleanup
        // Deallocate memory
        delete[] gpu_result;
        delete[] input_matrix;
        delete[] sequential;
        return 0;
    }
    catch (cl::Error& e) {
        std::cerr << e.what() << ": " << jc::readable_status(e.err());
        return 3;
    }
    catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        return 2;
    }
    catch (...) {
        std::cerr << "Unexpected error. Aborting!\n" << std::endl;
        return 1;
    }
}
1
  • I've added a possible solution. Let me know, if it works.
    – Baiz
    Nov 1, 2014 at 1:05

2 Answers 2

4

As maZZZu already stated, due to the parallel execution of the work items you can not be sure if an element in the array has been read/written yet. This can be ensured using

CLK_LOCAL_MEM_FENCE/CLK_GLOBAL_MEM_FENCE

however these mechanisms only work on threads wihtin the same work group. There is no possibility to synchronize work items from different work groups.

Your problem most likely is:

  • you use multiple work groups for an algorithm which is most likely only executable by a single work group
  • you do not use enough barriers
  • if you already use only a single work group, try adding a

    barrier(CLK_GLOBAL_MEM_FENCE);

to all parts where you read/write from/to destin.

You should restructure your algorithm:

  • have only one work group perform the algorithm on your matrix
  • use local memory for better performance(since you repeatedly access elements)
  • use barriers everywhere. If the algorithm works you can start removing them after working out, which ones you don't need.

Could you post your kernel call and the working sizes?

EDIT:

From your algorithm I came up with this code. I haven't tested it and I doubt it'll work right away. But it should help you in understanding how to parallelize a sequential algorithm. It will decompose the matrix with only one kernel launch.

Some restrictions:

  • This code only works with a single work group.
  • It will only work for matrices whose size does not exceed your maximum local work-group size (probably between 256 and 1024). If you want to change that, you should refactor the algorithm to use only as many work items as the width of the matrix.

Just adapt them to your kernel.setArg(...) code

int nbElements = width*height;
clSetKernelArg (kernel, 0, sizeof(A), &A);
clSetKernelArg (kernel, 1, sizeof(U), &U);
clSetKernelArg (kernel, 2, sizeof(float) * widthMat * heightMat, NULL); // Local memory
clSetKernelArg (kernel, 3, sizeof(int), &width);
clSetKernelArg (kernel, 4, sizeof(int), &height);
clSetKernelArg (kernel, 5, sizeof(int), &nbElements);

Kernel code:

inline int indexFrom2d(const int u, const int v, const int width)
{
    return width*v + u;
}

kernel void decompose(global float* A, 
                      global float* U,
                      local float* localBuffer, 
                      const int widthMat,
                      const int heightMat,
                      const int nbElements)
{
    int gidx = get_global_id(0);
    int col = gidx%widthMat;
    int row = gidx/widthMat;

    if(gidx >= nbElements)
        return;

    // Copy from global to local memory
    localBuffer[gidx] = A[gidx];

    // Sync copy process
    barrier(CLK_LOCAL_MEM_FENCE);

    for (int rowOuter = 0; rowOuter < widthMat; ++rowOuter)
    {
        int pivotIdx = rowOuter;

        float pivotValue = localBuffer[indexFrom2d(pivotIdx, pivotIdx, widthMat)];

        // Data for all work items in the row
        float belowPrivot = localBuffer[indexFrom2d(pivotIdx, row, widthMat)];          
        float divisor = belowPrivot / pivotValue;

        float value = localBuffer[indexFrom2d(col, rowOuter, widthMat)];

        // Only work items below pivot and from pivot to the right
        if( widthMat > col >= pivotIdx &&
            heightMat > row >= pivotIdx + 1)
        {
            localBuffer[indexFrom2d(col, row, widthMat)] = localBuffer[indexFrom2d(col, row, widthMat)] - (value * divisor);

            if(col == pivotIdx)
                localBuffer[indexFrom2d(pivotIdx, row, widthMat)] = divisor;
        }
        barrier(CLK_LOCAL_MEM_FENCE);
    }

    // Write back to global memory
    U[gidx] = localBuffer[gidx];
}
1
  • 1
    To synchronized globally (across all work items) use multiple kernels. Oct 28, 2014 at 14:23
1

The errors are way too big to be caused by float arithmetics.

Without any deeper understanding of your algorithm, I would say that the problem is that you are using values from the destination buffer. With sequential code this is fine, because you know what values are there. But with OpenCL, kernels are executed in parallel. So you cannot tell if another kernel has already stored its value to destination buffer or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.