blelump

Casting double to float

Discussion created by blelump on Apr 20, 2010
Latest reply on May 5, 2010 by blelump
is this possible?

Hi,

Assume that such simple kernel is given:

 

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void sum(__global const double2 *a,__global const float2 *b,__global float2 *c){
    size_t id = get_global_id(0);
    c[id]=a[id]+b[id];

}



Running it on CPU works fine [implicit double to float cast performs] whereas on GPU it behaves quite unexpected. I suppose that implicit cast cannot be done in this case so I tried an explicit cast like

 

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void sum(__global const double2 *a,__global const float2 *b,__global float2 *c){

    size_t id = get_global_id(0);
    c[id].x = (float)a[id].x+b[id].x;
    c[id].y = (float)a[id].y+b[id].y;

    //or even:

    float2 aa;
    aa.x = a[id].x;
    aa.x = a[id].x;
    c[id].x = aa.x+b[id].x;
    c[id].y = aa.y+b[id].y;

}



Both solutions are wrong - cause CL complier error or an undefined data as a result of such sum. I've also tried to use the to_float2(double2) function, but compliler complains that source and destination have different size. Does anyone have an idea why or how to avoid such issue? Is this possible at all to cast double -> float?

Outcomes