1
$\begingroup$

I would like to calculate the number of floating point operations (Flops) my code is performing in my machine. To do so, I would like to be sure I am counting the operations in the inner-most loop correctly.

My understanding is that the relevant number, which are the operations inside the vectorized loop, total $25 N^2$, $13 = 3\times 1 + 4 \times 2$ outside the b-spline kernel and $12 = 6+6$ operations inside the kernel.

int compute_density_3d_ref(int N,double h,
                           double* restrict x, double* restrict y,
                           double* restrict z, double* restrict nu,
                           double* restrict rho){
  const double inv_h = 1./h;
  const double kernel_constant = w_bspline_3d_constant(h);
  #pragma omp parallel for 
  for(size_t ii=0;ii<N;ii+=1){
    double xii = x[ii];
    double yii = y[ii];
    double zii = z[ii];
    double rhoii = 0.0;
    #pragma omp simd reduction(+:rhoii) aligned(x,y,z,nu) 
    for(size_t jj=0;jj<N;jj+=1){
      double q = 0.;

      double xij = xii-x[jj]; // One operation
      double yij = yii-y[jj]; // One operation
      double zij = zii-z[jj]; // One operation

      q += xij*xij; // Two operations
      q += yij*yij; // Two operations
      q += zij*zij; // Two operations

      q = sqrt(q)*inv_h; // Two operations

      // Two operation plus the number of operations in w_bspline
      rhoii += nu[jj]*w_bspline_3d_simd(q);//*w_bspline_3d(q); // box->w(sqrt(dist),h);
    }
    rho[ii] = kernel_constant*rhoii;
  }

  return 0;
}

The functions called are:

double w_bspline_3d_constant(double h){
  return 3./(2.*M_PI*h*h*h);
}

#pragma omp declare simd
double w_bspline_3d(double q){
  double wq = 0.0;
  double wq1 = (0.6666666666666666 - q*q + 0.5*q*q*q); // Six operations
  double wq2 = 0.16666666666666666*(2.-q)*(2.-q)*(2.-q); // Six operations
  
  if(q<2.)
    wq = wq2;

  if(q<1.)
    wq = wq1;
  
  return wq;
}

I am counting ${\rm sqrt(q)} = \sqrt{q}$ as a single operation under the suposition that the CPU can dispatch one (vector) sqrt instruction per cycle, suposing AVX256. I am also counting additions and multiplications both as floating point operations on equal footing.

I would like to know if I going about this calculation right or I am over-estimating (or even under-estimating) the number of operations involved in ths computation.

Thanks in advance.

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.