《cuda by example》 book.h 头文件解析(转)
来源:互联网 发布:python程序调试 编辑:程序博客网 时间:2024/04/30 20:50
参考书籍 :《cuda by example》
参考链接 :https://code.google.com/p/snp-gpgpu/source/browse/trunk/f/common/book.h?r=4
#ifndef __BOOK_H__#define __BOOK_H__#include <stdio.h>/* cuda检错程序 */static void HandleError( cudaError_t err,const char *file,int line ) { if (err != cudaSuccess) { printf( "%s in %s at line %d\n", cudaGetErrorString( err ), file, line ); exit( EXIT_FAILURE ); }}#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))#define HANDLE_NULL( a ) {if (a == NULL) { \ printf( "Host memory failed in %s at line %d\n", \ __FILE__, __LINE__ ); \ exit( EXIT_FAILURE );}}template< typename T >void swap( T& a, T& b ) { T t = a; a = b; b = t;}void* big_random_block( int size ) { unsigned char *data = (unsigned char*)malloc( size ); HANDLE_NULL( data ); for (int i=0; i<size; i++) data[i] = rand(); return data;}int* big_random_block_int( int size ) { int *data = (int*)malloc( size * sizeof(int) ); HANDLE_NULL( data ); for (int i=0; i<size; i++) data[i] = rand(); return data;}// a place for common kernels - starts here__device__ unsigned char value( float n1, float n2, int hue ) { if (hue > 360) hue -= 360; else if (hue < 0) hue += 360; if (hue < 60) return (unsigned char)(255 * (n1 + (n2-n1)*hue/60)); if (hue < 180) return (unsigned char)(255 * n2); if (hue < 240) return (unsigned char)(255 * (n1 + (n2-n1)*(240-hue)/60)); return (unsigned char)(255 * n1);}__global__ void float_to_color( unsigned char *optr, const float *outSrc ) { // map from threadIdx/BlockIdx to pixel position 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 l = outSrc[offset]; float s = 1; int h = (180 + (int)(360.0f * outSrc[offset])) % 360; float m1, m2; if (l <= 0.5f) m2 = l * (1 + s); else m2 = l + s - l * s; m1 = 2 * l - m2; optr[offset*4 + 0] = value( m1, m2, h+120 ); optr[offset*4 + 1] = value( m1, m2, h ); optr[offset*4 + 2] = value( m1, m2, h -120 ); optr[offset*4 + 3] = 255;}__global__ void float_to_color( uchar4 *optr, const float *outSrc ) { // map from threadIdx/BlockIdx to pixel position 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 l = outSrc[offset]; float s = 1; int h = (180 + (int)(360.0f * outSrc[offset])) % 360; float m1, m2; if (l <= 0.5f) m2 = l * (1 + s); else m2 = l + s - l * s; m1 = 2 * l - m2; optr[offset].x = value( m1, m2, h+120 ); optr[offset].y = value( m1, m2, h ); optr[offset].z = value( m1, m2, h -120 ); optr[offset].w = 255;}#if _WIN32 //Windows threads. #include <windows.h> typedef HANDLE CUTThread; typedef unsigned (WINAPI *CUT_THREADROUTINE)(void *); #define CUT_THREADPROC unsigned WINAPI #define CUT_THREADEND return 0#else //POSIX threads. #include <pthread.h> typedef pthread_t CUTThread; typedef void *(*CUT_THREADROUTINE)(void *); #define CUT_THREADPROC void #define CUT_THREADEND#endif//Create thread.CUTThread start_thread( CUT_THREADROUTINE, void *data );//Wait for thread to finish.void end_thread( CUTThread thread );//Destroy thread.void destroy_thread( CUTThread thread );//Wait for multiple threads.void wait_for_threads( const CUTThread *threads, int num );#if _WIN32 //Create thread CUTThread start_thread(CUT_THREADROUTINE func, void *data){ return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, data, 0, NULL); } //Wait for thread to finish void end_thread(CUTThread thread){ WaitForSingleObject(thread, INFINITE); CloseHandle(thread); } //Destroy thread void destroy_thread( CUTThread thread ){ TerminateThread(thread, 0); CloseHandle(thread); } //Wait for multiple threads void wait_for_threads(const CUTThread * threads, int num){ WaitForMultipleObjects(num, threads, true, INFINITE); for(int i = 0; i < num; i++) CloseHandle(threads[i]); }#else //Create thread CUTThread start_thread(CUT_THREADROUTINE func, void * data){ pthread_t thread; pthread_create(&thread, NULL, func, data); return thread; } //Wait for thread to finish void end_thread(CUTThread thread){ pthread_join(thread, NULL); } //Destroy thread void destroy_thread( CUTThread thread ){ pthread_cancel(thread); } //Wait for multiple threads void wait_for_threads(const CUTThread * threads, int num){ for(int i = 0; i < num; i++) end_thread( threads[i] ); }#endif#endif // __BOOK_H__
0 0
- 《cuda by example》 book.h 头文件解析(转)
- 《cuda by example》 book.h 头文件解析
- CUDA by Example 4.2.2 一个有趣的例子之 CPU_bitmap.h解析
- cuda包含头文件错误MSB3721(windows.h)
- CUDA by Example 源代码
- cuda by example -julia
- stdafx.h头文件解析
- string.h头文件解析
- xxxx.h头文件解析
- time.h头文件解析
- stm32f10x.h 头文件解析
- CUDA By Example CUDA实战学习
- CUDA by Example 4.2.2 一个有趣的例子 之 #include "gl_helper.h"
- linux内核头文件 cdev.h 解析
- cuda by example 的代码的问题
- Julia set code CUDA by example 常见问题
- CUDA By Example——Julia实例
- CUDA By Example 第四章Julia集
- cocos2dx打飞机项目笔记一:项目结构介绍
- 文本编辑器
- nginx中HTTP/1.1 405 Method not allowed 的解决方法
- JAVA面向对象
- OpenStack Roadmap
- 《cuda by example》 book.h 头文件解析(转)
- Android自定义属性时format
- 当没有输入内容时,input中的字体变化
- 端口复用大揭密
- hdu 2544 最短路
- hdu 4565 So Easy!
- 青海哪里找富婆包养
- jQuery数字滚动展示效果
- 这么热的天,要做什么才能降温呢