Parallel Programming Seminar

August 5-7 2009

Interdisciplinary Mathematics Institute
University of South Carolina


This simple CUDA program allocates a CUDA memory buffer and fills it with the value 42 as part of the Parallel Programming Seminar at IMI. To compile it, enter the following command line:

/usr/local/cuda/bin/nvcc -o 42

To run, type:


Below is the complete source code: (download)

#include <assert.h>

int const block_x=16;
int const block_y=16;

kernel_42( float * output, int pitch, int width, int height )
    int x = blockIdx.x*block_x + threadIdx.x;
    int y = blockIdx.y*block_y + threadIdx.y;
    if( x<width && y<height )
        output[pitch*y+x] = 42;

main( int argc, char const * argv[] )
    int const width=4242;
    int const height=4242;
    void * ptr;
    size_t pitch;
    cudaError_t err=cudaMallocPitch(&ptr,&pitch,sizeof(float)*width,height);
    if( err!=cudaSuccess )
        return 1;
    pitch /= sizeof(float);
    dim3 block(block_x,block_y);
    dim3 grid((width+block.x-1)/block.x,(height+block.y-1)/block.y);
    kernel_42<<<grid,block>>>((float *)ptr,pitch,width,height);