Explore Snippets

32 public snippets shared by the community.

Shared Code

CUDA
#include <cuda_runtime.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <vector>

#define CUDA(call) do {                                          \
    cudaError_t err = (call);                                     \
    if (e
...

Shared Code

CUDA
#include <stdio.h>
#include <cuda_runtime.h>
__host__ __device__ float f(float x)
{
    return x*x + 1.0f;
}
__global__ void Dev_trap(
    const float a, const float b,
    const float h, const int n,
    float* trap_p)
{
    int my_i = blockDim.x * blockIdx.x + threadIdx.x;
    if (0 < my_i && my_i
...

Shared Code

CUDA
#include <cuda_runtime.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>

#define CUDA(call)                                                           \
    do {                                                                     \
        cudaError_t st
...

Shared Code

CUDA
#include <cuda_runtime.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>

#define CUDA(call)                                                           \
    do {                                                                     \
        cudaError_t st
...

Shared Code

CUDA
#include <stdio.h>

// hello from GPU kernel
__global__ void helloFromGPU(void)
{
  printf("Hello World from GPU! \n");
}

int main(void)
{
  // hello from gpu
  helloFromGPU <<<1, 10>>>();
  cudaDeviceReset();
  return 0;
}

Shared Code

CUDA
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <curand_kernel.h>
#include <cuda_runtime.h>

#define BLOCK_SIZE 256

// ============================================================
// CUDA Kernel
// Each thread performs one Monte Carlo simulation
// ================================
...

Shared Code

CUDA
nvidia-smi

Shared Code

CUDA
#include <iostream>
#include <cstdlib>
#include <cuda_runtime.h>

#define N (1 << 20)
#define LIMIT 750000

__global__ void findElements(
    int *arr,
    int n,
    int limit,
    unsigned int *result)
{
    __shared__ unsigned int count;

    if (threadIdx.x == 0)
        count = 0;

    __syncth
...

Shared Code

CUDA
#include <iostream>
#include <cstdlib>
#include <cuda_runtime.h>

#define ARRAY_SIZE (1 << 20)
#define THRESHOLD 750000

__global__ void count_above_threshold_kernel(
    const int* d_array,
    int size,
    int threshold,
    unsigned int* d_global_count)
{
    __shared__ unsigned int block_count;
...

Shared Code

CUDA
#include <cuda_runtime.h>

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <chrono>
#include <random>
#include <iomanip>

using namespace std;

// ============================================================
// CUDA ERROR CHECK
// // بررسی خطاهای CUDA
// =======
...

Shared Code

CUDA
#include <cuda_runtime.h>
#include <curand_kernel.h>

#include <openssl/evp.h>

#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <thread
...

Shared Code

CUDA
#include <stdio.h>

__global__ void vecAdd(float *a, float *b, float *c, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    int n = 1024;
    size_t bytes = n * sizeof(float);

    float *h_a = (float*)malloc(bytes);
   
...

Shared Code

CUDA
import torch

print(f"PyTorch version : {torch.__version__}")
print(f"CUDA available  : {torch.cuda.is_available()}")
if torch.cuda.is_available():
    print(f"GPU             : {torch.cuda.get_device_name(0)}")

# Simple tensor op on GPU
x = torch.randn(1024, 1024, device="cuda")
y = torch.randn(10
...

Shared Code

CUDA
import torch
import platform
import os
import time

print("=" * 60)
print("LANIE GPU CAPABILITY PROBE")
print("=" * 60)

print("Python:", platform.python_version())
print("PyTorch:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())

if not torch.cuda.is_available():
    raise S
...

Shared Code

CUDA
#include <iostream>
#include <cuda_runtime.h>
using namespace std;

__global__ void addition(int a, int b, int *c)
{
    *c = a + b;
}

int main()
{
    int a, b;
    int *c;

    cudaMallocManaged(&c, sizeof(int));

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number
...

Shared Code

CUDA
Limport torch
import triton
import triton.language as tl

def online_softmax_kernel(
  X_ptr, O_ptr,
  stride_X, stride_o,
  stride_on, stride_od,
  N_COLS,
  BLOCK_SIZE, tl.constexpr
):
  pass
  row_id = tl.program_id(0)
  row_start_x = X_ptr + row_idx * stride_xn
  row_start_out = Out_ptr + row_id
...

Shared Code

CUDA
#include <stdio.h>

#define A(i,j) a[(i)*n+(j)]
#define B(i,j) b[(i)*n+(j)]

float compare_matrices(int m, int n, float *a, float *b)
{
    int i, j;
    float max_diff = 0.0, diff;
    int printed = 0;

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            diff = a
...

Shared Code

CUDA
#include <stdio.h>

__global__ void vecAdd(float *a, float *b, float *c, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    int n = 1024;
    size_t bytes = n * sizeof(float);

    float *h_a = (float*)malloc(bytes);
   
...

Matrix Multiplication

CUDA
#include <cuda_runtime.h>
#define N 64

// Kernel is launched as:  matMul<<<dim3(ceil(N/16),ceil(N/16)), dim3(16,16)>>>(A, B, C)
// gridDim  = (ceil(N/16), ceil(N/16))   blockDim = (16, 16)
// Row:  int row = blockIdx.y * blockDim.y + threadIdx.y;
// Col:  int col = blockIdx.x * blockDim.x + threadI
...

Shared Code

CUDA
#include <stdio.h>


__global__ void vecAdd2D(float *a, float *b, float *c, int width, int height) { 
  int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y;
  if (i < width && j < height) 
  { int idx = j * width + i;
    c[idx] = a[idx] + b[idx];
  }
}

int 
...

Shared Code

CUDA
#include <stdio.h>

__global__ void vecAdd(float *a, float *b, float *c, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    int n = 1024;
    size_t bytes = n * sizeof(float);

    float *h_a = (float*)malloc(bytes);
   
...

Shared Code

CUDA
/*
Name : Salsabil Ali
ID : 2300127
*/

#include <stdio.h>
#include <stdlib.h>

#define N 3

__global__ void matmul(const int *A, const int *B, int *C) {
    int cx = threadIdx.x;
    int ry = threadIdx.y;

    if (ry < N && cx < N) {
        int v = 0;
        for (int k = 0; k < N; ++k) {
        
...

Shared Code

CUDA
#include <stdio.h>

__global__ void vecAdd(float *a, float *b, float *c, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    int n = 1024;
    size_t bytes = n * sizeof(float);

    float *h_a = (float*)malloc(bytes);
   
...

Shared Code

CUDA
#include <stdio.h>

__global__ void hello_from_gpu()
{
    int thread_id = threadIdx.x;
    int block_id = blockIdx.x;

    printf("Hello from GPU Thread %d inside Block %d!\n",
           thread_id,
           block_id);
}

int main()
{
    printf("Master CPU: Preparing to launch GPU Kernel...\n");
...