Cuda Programming Learning 5--Ripple Ripple

Source: Internet
Author: User

/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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.