Created
February 16, 2025 04:45
-
-
Save msaroufim/b1939811739b5529f0874fdd8d95b094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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