/Total Dimxdim pixels, each pixel corresponding to one thread
DIM3 blocks (DIM/16,DIM/16);//2
DIM3 threads (16,16);//2
Kernel<<<blocks,threads>>> (d->dev_bitmap,ticks);
Code:
/*
============================================================================
Name:ripple.cu
Author:can
Version:
Copyright:your Copyright Notice
Description:cuda Compute reciprocals
============================================================================
*/
#include <iostream>
#include <math.h>
#include "cpu_anim.h"
using namespace Std;
static void Checkcudaerroraux (const char *, unsigned, const char *, cudaerror_t);
#define Cuda_check_return (value) Checkcudaerroraux (__file__,__line__, #value, value)
#define DIM 160
struct DataBlock
{
unsigned char *dev_bitmap;
Cpuanimbitmap *bitmap;
};
void Cleanup (DataBlock *d)
{
Cudafree (D->DEV_BITMAP);
}
void Generate_frame (DataBlock *d,int ticks);
__global__ void Kernel (unsigned char *ptr,int ticks)
{
Map ThreadID and blockid to pixel locations
int x=threadidx.x+blockidx.x*blockdim.x;
int y=threadidx.y+blockidx.y*blockdim.y;
int offset=x+y*blockdim.x*griddim.x;
float FX=X-DIM/2;
float FY=Y-DIM/2;
Float d=sqrtf (fx*fx+fy*fy);
unsigned char grey= (unsigned char) (128.0f+127.0f*cos (d/10.0f-ticks/7.0f)/(d/10.0f+1.0f));
Ptr[offset*4+0]=grey;
Ptr[offset*4+1]=grey;
Ptr[offset*4+2]=grey;
ptr[offset*4+3]=255;
}
int main ()
{
DataBlock data;
Cpuanimbitmap bitmap (Dim,dim,&data);
Data.bitmap = &bitmap;
Cuda_check_return (Cudamalloc (void * *) &data.dev_bitmap,bitmap.image_size ()));
Bitmap.anim_and_exit ((Void (*) (Void*,int)) Generate_frame, (Void (*) (void*)) cleanup);
}
void Generate_frame (DataBlock *d,int ticks)
{
A total of dimxdim pixels, each pixel corresponding to a thread
DIM3 blocks (DIM/16,DIM/16);//2
DIM3 threads (16,16);//2
Kernel<<<blocks,threads>>> (d->dev_bitmap,ticks);
Cuda_check_return (cudamemcpy (D->bitmap->get_ptr (), D->dev_bitmap,d->bitmap->image_size (), Cudamemcpydevicetohost));
}
static void Checkcudaerroraux (const char *file, unsigned line, const char *statement, cudaerror_t err)
{
if (err = = cudasuccess)
Return
Std::cerr << statement<< "returned" << cudageterrorstring (Err) << "(" <<err<< ") at" <<file<< ":" <<line << Std::endl;
Exit (1);
}
Cuda Programming Learning 5--Ripple Ripple