Question

I'm trying to convert a 3x2 matrix into a square matrix which is 4x4:

__kernel void padding(float* newM, int m, int n, int newlength)
{

}

The matrix "newM" is in row-major-order, m=3, n=2 and newlength=4. The elements in newM are all compact to the front and the tail of the matrix is just 0's. My confusion is how can i shift the elements along without losing the subsequent values. I would create a local copy, but the matrices that i am dealing with are extremely large and do not fit into private memory.

Here's a 1 dimensional look:

[1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0] -> [1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0]

Heres a 2 dimensional look:

[1, 1, 1]    [1, 1, 1, 0]
[1, 1, 1] -> [1, 1, 1, 0]
             [0, 0, 0, 0]
             [0, 0, 0, 0]

How it actually looks in 2D:

[1, 1, 1, 1]    [1, 1, 1, 0]
[1, 1, 0, 0] -> [1, 1, 1, 0]
[0, 0, 0, 0]    [0, 0, 0, 0]
[0, 0, 0, 0]    [0, 0, 0, 0]

All numbers I have used here are just for this examples, in reality I have random floats in the matrices and dimensions are beyond 2000x2000.

Any Ideas? Thanks

Was it helpful?

Solution

Do this, if your data is ordered row-wise:

__kernel void padding(float* newMa, float* oldMa, int oldR, int oldC, int N)
{
    int id = get_global_id(0);
    int r = id/N;
    int c = id%N;
    float value = 0.0f;
    if(r < oldR || c < oldC) //Inside the old matrix size
        value = oldMa[r*oldR+oldC];
    newMa[id] = value ;
}

The new matrix size should hold enough space for the operation, that is "NxN".

I don't know if you are using this memory ordering. Could you provide how you expect the data to interface with your other kernels? As other answer says, you provably don't need another kernel for such an easy operation. You can also integrate this inside your other kernel.

OTHER TIPS

If you don't need to do any math, and the only target is to interpret data in other way, you don't need any OpenCL here.

Reallocate memory and introduce new matrix row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top