Quantcast
Channel: Active questions tagged kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 6502

Casting an integer to a float in OpenCL

$
0
0

This is my first post on stack overflow so bear with me.

I am currently programming an OpenCL Kernel and require the use of the inbuilt sqrt function. However, for this to work the parameter of the function needs to be a float. I currently have an integer value and need to convert it to float, in order to perform the sqrt() function and then convert it back to an integer so that it can be stored into the "magOut" array.

The Code below should provide a better understanding of what I am trying to do:

magOutput[workItemNum] = sqrt(xConv[workItemNum]*xConv[workItemNum] + yConv[workItemNum]*yConv[workItemNum]);

In case it is needed to understand the required application here is the full code:

__kernel void matrixMultiplication(__global int* input, __global int* xConv, __global int* yConv, __global int* size, __global int* magOutput){int workItemNum = get_global_id(0); //Work item IDint workGroupNum = get_group_id(0); //Work group IDint localGroupID = get_local_id(0); //Work items ID within each work group// size refers to the total size of a matrix. So for a 3x3 size = 9float dim = *size;int dim1 = *size; int row = sqrt(dim); // only square matrices are used and as such the sqrt of size produces the row lengthint current_row = workItemNum/dim; // the current row is calculated by using the current workitem number divided by the total size of the matrixint col = sqrt(dim); // only square matrices are used and as such the sqrt of size produces the column lengthint current_col = workItemNum % dim1; // the current column is calculated by using the current workitem number modulus by the total size of the matrix// printf("dimension: %i \n",localGroupID);// This if statement excludes all boundary pixels from the calculation as you require the neighbouring pixel cells // for this calculationif (current_col == 0 || current_col == col-1 || current_row == 0 || current_row == row - 1){    /*===============================================================================================================    * The xConv array performs the kernal convultion of the input grey scale values with the following matrix:    *    *                            [-1  0 +1]    * X - Directional Kernel  =  [-2  0 +2]    *                            [-1  0 +1]    *     * This scans across the X direction of the image and enhances all edges in the X-direction     * ===============================================================================================================    */    xConv[workItemNum] =  input[(current_col - 1)*col + current_row - 1]*-1 + input[(current_col)*col + current_row - 1]*0 + input[(current_col + 1)*col + current_row - 1]*1 + input[(current_col - 1)*col + current_row]*-2 + input[(current_col)*col + current_row]*0 + input[(current_col + 1)*col + current_row]*2 + input[(current_col - 1)*col + current_row + 1]*-1 + input[(current_col)*col + current_row + 1]*0 + input[(current_col + 1)*col + current_row + 1]*1;    /*===============================================================================================================    * The xConv array performs the kernal convultion of the input grey scale values with the following matrix:    *    *                            [+1 +2 +1]    * Y - Directional Kernel  =  [ 0  0  0]    *                            [-1 -2 -1]    *     * This scans across the Y direction of the image and enhances all edges in the Y-direction     * ===============================================================================================================    */    yConv[workItemNum] = input[(current_col - 1)*col + current_row - 1]*-1 + input[(current_col)*col + current_row - 1]*-2 + input[(current_col + 1)*col + current_row - 1]*-1 + input[(current_col - 1)*col + current_row]*0 + input[(current_col)*col + current_row]*0 + input[(current_col + 1)*col + current_row]*0 + input[(current_col - 1)*col + current_row + 1]*1 + input[(current_col)*col + current_row + 1]*2 + input[(current_col + 1)*col + current_row + 1]*1;}//===============================================================================================================// Calculates the convolution matrix of the X and Y arrays. Does so by squaring each item of the X and Y arrays,  // adding them and taking the square root. This is the basic magnitude formula. This is done for by each workItem//===============================================================================================================magOutput[workItemNum] = sqrt(xConv[workItemNum]*xConv[workItemNum] + yConv[workItemNum]*yConv[workItemNum]);}

Any suggestions?


Viewing all articles
Browse latest Browse all 6502

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>