Calculation of spherical coordinates

Hello everyone and sorry for my english (french),

I encounter a small problem with computations in openCL.

  1. Here is the kernel that I use:

// CALCULATION OF SPHERICAL COORDINATES
//
// __global float3 * position_particles - vector buffer 3 float for OpenGL rendering
// __global float3 * AnglesRayon - vector buffer 3 float containing the angles generated randomly
*
*
__kernel void main_kernel (__global float3 * position_particle, __global float3 * AnglesRayon) {
*
// Particle_field.x = x - Initially 0.0f
// Particle_field.y = y - initially 0.0f
// Particle_field.z = z - initially 0.0f
*
// AnglesRayon.x = Angle_Phi - random angle between 0 - 2Pi
// AnglesRayon.y = Angle_Theta - random angle between 0 - Pi
// AnglesRayon.z = radius - 1.0f
*
****Int ID_CH_PART = get_global_id (0);
*
****Float Angle_Phi = AnglesRayon [ID_CH_PART] .x;
****Float Angle_Theta = AnglesRayon [ID_CH_PART] .y;
****Float radius = AnglesRayon [ID_CH_PART] .z;
*
****// computing the resultant vector
****Float DRx = radius * sin (Angle_Theta) * cos (Angle_Phi);
****Float DRy = radius * sin (Angle_Theta) * sin (Angle_Phi);
****Float DRz = radius * cos (Angle_Theta);
*
****// creation vector result
****Float3 VectorDirection = (float3) (DRx, DRy, DRz);
*
****// Adding the resultant vector to the initial position of the particle
****Position_particle [ID_CH_PART] + = Directional Vector;
*
}

  1. Here is an example for the calculation on 3 particles:

Buffer of position (float3): x, y, z


0.0		0.0		0.0		
0.0		0.0		0.0		
0.0		0.0		0.0

Buffer angles and radius (float3): angle_phi, angle_theta, radius


4.13716		2.39805		1.0		
1.24682		0.03783		1.0		
3.76207		1.07008		1.0

      • Updated - - -
  1. Logically this should return me the following values: (which works well if I compute with java code !!!)

-0.36825           -0.56796         -0.73608
0.01204             0.03585          0.99928
-0.71372           -0.51005          0.48005

  1. But when I read my buffer output, I see this:

-0.36825183	-0.56796473	-0.7360752		
0.0	    3.1634076	    0.119728826		
2.032655	0.0		0.0

I have a shift, as if there was an addition of 0.0f after each float3 coordinates

      • Updated - - -
  1. Another example, if I send to test the values of my angles and radius like this:

__kernel void main_kernel (__global float3 * position_particle, __global float3 * 

AnglesRayon) {
*
// Particle_field.x = x - initially 0.0f
// Particle_field.y = y - initially 0.0f
// Particle_field.z = z - initially 0.0f
*
// AnglesRayon.x = Angle_Phi - random angle between 0 - 2Pi
// AnglesRayon.y = Angle_Theta - random angle between 0 - Pi
// AnglesRayon.z = radius - 1.0f
*
**** Int ID_CH_PART = get_global_id (0);
*
**** Float Angle_Phi = AnglesRayon [ID_CH_PART] .x;
**** Float Angle_Theta = AnglesRayon [ID_CH_PART]. Y;
**** Float radius = AnglesRayon [ID_CH_PART] .z;
*
**** // creation vector result
**** Float3 VectorDirection = (float3) (Angle_Phi, Angle_Theta, radius);
*
**** // Adding the resultant vector to the initial position of the particle
**** Position_particle [ID_CH_PART] + = Directional Vector;
*
}

My angles are returned correctly: :


4.13716		2.39805		1.0		
1.24682		0.03783		1.0		
3.76207		1.07008		1.0

I conclude that it is when computing with sin () and cos () that it does anything !!
But I do not see why-

If someone has an idea, thanks in advance

From OpenCL C specs (Alignment of Types):

“For 3-component vector data types, the size of the data type is 4 * sizeof(component). This means that a 3-component vector data type will be aligned to a 4 * sizeof(component) boundary. The vload3 and vstore3 built-in functions can be used to read and write, respectively, 3-component vector data types from an array of packed scalar data type.”

Ok I understand better, actually by passing in float4 my problem is solved.

Thank you for this quick response…

UP !!! :slight_smile: