How to Apply Conditional Operations Using cuTensor or cuBLAS?

Hi everyone,

I’m trying to adapt a processing function using cuTensor to perform conditional operations, but I’m struggling with how to implement the conditional part in my code. In the original code (shown below), I perform a multiplication and then check a condition to adjust the values of the tensor elements:

Original Code:

void Products::enb_emissivity_function(float* ndvi, float* lai, float* enb_emissivity)
{
  for (int i = 0; i < this->height_band * this->width_band; i++)
  {
    enb_emissivity[i] = 0.97 + 0.0033 * lai[i];

    if ((ndvi[i] < 0) || (lai[i] > 2.99))
      enb_emissivity[i] = 0.98;
  }
};

I’m trying to adapt this function using cuTensor, but I’m unsure how to apply the conditional operation (as shown above) after executing the multiplication. Here’s the adapted code I have so far:

Adapted Code:

void Products::enb_emissivity_function(float* ndvi_d, float* lai_d, float* enb_d, float* only1_d)
{
  float pos097 = 0.97;
  float pos0033 = 0.033;

  // tensor_plan_binary_add performs: enb_d = 0.97 * only1_d + 0.0033 * lai_d;
  // createBinary(tensor_plan_binary_add, CUTENSOR_OP_IDENTITY, CUTENSOR_OP_IDENTITY, CUTENSOR_OP_ADD);
  HANDLE_CUTENSOR_ERROR(cutensorElementwiseBinaryExecute(tensors.handle, tensors.tensor_plan_binary_add, (void *)&pos097, only1_d, (void *)&pos0033, lai_d, enb_d, tensors.stream));

  // How can I implement the following condition using cuTensor/cuBLAS?
  // if ((ndvi_d[i] < 0) || (lai_d[i] > 2.99))
  //   enb_d[i] = 0.98;

  HANDLE_ERROR(cudaMemcpy(enb_emissivity, enb_d, sizeof(float) * this->height_band * this->width_band, cudaMemcpyDeviceToHost));
};

My question is: Is there a way to perform conditional operations directly using cuTensor or cuBLAS, or do I need to write a custom CUDA kernel for this conditional logic?

Any tips or suggestions would be greatly appreciated. Thanks in advance!

There is no way to do that directly in CUBLAS. I’m doubtful of a method to do it in cuTensor but perhaps someone else will have some ideas.

1 Like

Many thanks Robert_Crovella! I appreciate your attention :)