Hello, World
The first post on a hand-built Astro site.
This is the first post. Nothing fancy yet โ just enough Markdown to prove the pipeline works: frontmatter in, a rendered page out, with an automatically computed read time.
More to come.
code example:
#include <stdio.h>
int main() {
return 0;
}
SELECT *
FROM table
WHERE id="123"
#include <iostream>
#include <cuda_runtime.h>
// -----------------------------------------------------------------------------
// KERNEL 1: Naive Fill
// Uses a sub-optimal 1D layout (1 thread per block) which underutilizes
// GPU warps and Streaming Multiprocessors (SMs).
// -----------------------------------------------------------------------------
__global__ void naiveFillKernel(float* data, int N) {
int i = blockIdx.x; // only using block index
if (i < N) {
data[i] = 1.0f;
}
}
// -----------------------------------------------------------------------------
// KERNEL 2: Efficient Fill
// Uses proper 2D/1D grid-block stride indexing (256 threads per block)
// to fully saturate GPU compute pipelines and memory bandwidth.
// -----------------------------------------------------------------------------
__global__ void efficientFillKernel(float* data, int N) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < N) {
data[i] = 1.0f;
}
}
float timeKernel(void (*kernel)(float*, int), dim3 grid, dim3 block, float* d_data, int N) {
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
kernel<<<grid, block>>>(d_data, N);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float ms = 0;
cudaEventElapsedTime(&ms, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return ms;
}
int main() {
const int N = 10000000; // 10 million
size_t size = N * sizeof(float);
std::cout << "Running CUDA Hello World with " << N << " elements..." << std::endl;
// =========================================================================
// APPROACH 1: NAIVE WAY
// Slow Host-to-Device transfer + terrible thread block geometry (1 thread/block)
// =========================================================================
float* h_data_naive = (float*)malloc(size);
float* d_data_naive;
cudaMalloc((void**)&d_data_naive, size);
cudaMemcpy(d_data_naive, h_data_naive, size, cudaMemcpyHostToDevice);
dim3 naiveBlock(1);
dim3 naiveGrid(N);
float naiveTime = timeKernel(naiveFillKernel, naiveGrid, naiveBlock, d_data_naive, N);
cudaMemcpy(h_data_naive, d_data_naive, size, cudaMemcpyDeviceToHost);
cudaFree(d_data_naive);
free(h_data_naive);
// =========================================================================
// APPROACH 2: EFFICIENT WAY
// Uses Managed Memory + optimal 256 threads per block (32-thread warp aligned)
// =========================================================================
float* h_data_eff = (float*)malloc(size);
float* d_data_eff;
cudaMalloc((void**)&d_data_eff, size);
cudaMemcpy(d_data_eff, h_data_eff, size, cudaMemcpyHostToDevice);
int threadsPerBlock = 256;
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
dim3 effBlock(threadsPerBlock);
dim3 effGrid(blocksPerGrid);
float effTime = timeKernel(efficientFillKernel, effGrid, effBlock, d_data_eff, N);
cudaMemcpy(h_data_eff, d_data_eff, size, cudaMemcpyDeviceToHost);
cudaFree(d_data_eff);
free(h_data_eff);
// =========================================================================
// PRINT RESULTS
// =========================================================================
std::cout << "----------------------------------------------------" << std::endl;
std::cout << "Naive Execution Time : " << naiveTime << " ms" << std::endl;
std::cout << "Efficient Execution Time: " << effTime << " ms" << std::endl;
std::cout << "Speedup Factor : " << naiveTime / effTime << "x" << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
return 0;
}