Skip to content

Instantly share code, notes, and snippets.

@msaroufim
Created February 16, 2025 04:45
Show Gist options
  • Save msaroufim/b1939811739b5529f0874fdd8d95b094 to your computer and use it in GitHub Desktop.
Save msaroufim/b1939811739b5529f0874fdd8d95b094 to your computer and use it in GitHub Desktop.
// Version 1: Using if-condition
__global__ void kernel_if(float* out, const float* in, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
if (in[idx] > 0.0f) {
out[idx] = in[idx] * 2.0f;
} else {
out[idx] = in[idx] / 2.0f;
}
}
}
// Version 2: Using ternary operator
__global__ void kernel_ternary(float* out, const float* in, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
out[idx] = in[idx] > 0.0f ? in[idx] * 2.0f : in[idx] / 2.0f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment