《GPU高性能编程CUDA实战》附录三 关于book.h
▶ 本书中用到的公用函数放到了头文件book.h中
#ifndef __BOOK_H__
#define __BOOK_H__
#include <stdio.h>
#include <stdlib.h> // 自己加的
#include "cuda_runtime.h" // 自己加的 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 )/* 空指针报错函数,代码中malloc失败时报错 */ \
{ \
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 = ; i < size; data[i] = rand(), i++);
return data;
} int* big_random_block_int( int size )//在主机中生成随机数组,整型
{
int *data = (int*)malloc( size * sizeof(int) );
HANDLE_NULL( data );
for (int i = ; i < size; data[i] = rand(), i++);
return data;
} // 公用设备函数
__device__ unsigned char value(float n1, float n2, int hue)
{
if (hue > )
hue -= ;
else if (hue < )
hue += ;
if (hue < )
return (unsigned char)( * (n1 + (n2 - n1)*hue / ));
if (hue < )
return (unsigned char)( * n2);
if (hue < )
return (unsigned char)( * (n1 + (n2 - n1)*( - hue) / ));
return (unsigned char)( * n1);
} __global__ void float_to_color(unsigned char *optr, const float *outSrc)
{
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 = ;
int h = ( + (int)(360.0f * outSrc[offset])) % ;
float m1, m2; if (l <= 0.5f)
m2 = l * ( + s);
else
m2 = l + s - l * s;
m1 = * l - m2; optr[offset * + ] = value(m1, m2, h + );
optr[offset * + ] = value(m1, m2, h);
optr[offset * + ] = value(m1, m2, h - );
optr[offset * + ] = ;
} __global__ void float_to_color( uchar4 *optr,const float *outSrc )
{
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 = ;
int h = ( + (int)(360.0f * outSrc[offset])) % ;
float m1, m2; if (l <= 0.5f)
m2 = l * ( + s);
else
m2 = l + s - l * s;
m1 = * l - m2; optr[offset].x = value(m1, m2, h + );
optr[offset].y = value(m1, m2, h);
optr[offset].z = value(m1, m2, h - );
optr[offset].w = ;
} // 有关线程的设置
#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 // 线程的创造,单线程结束,单线程销毁和多线程等待
CUTThread start_thread( CUT_THREADROUTINE, void *data );
void end_thread( CUTThread thread );
void destroy_thread( CUTThread thread );
void wait_for_threads( const CUTThread *threads, int num ); #if _WIN32
CUTThread start_thread(CUT_THREADROUTINE func, void *data)
{
return CreateThread(NULL, , (LPTHREAD_START_ROUTINE)func, data, , NULL);
} void end_thread(CUTThread thread)
{
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
} void destroy_thread( CUTThread thread )
{
TerminateThread(thread, );
CloseHandle(thread);
} void wait_for_threads(const CUTThread * threads, int num){
WaitForMultipleObjects(num, threads, true, INFINITE); for(int i = ; i < num; i++)
CloseHandle(threads[i]);
} #else
CUTThread start_thread(CUT_THREADROUTINE func, void * data)
{
pthread_t thread;
pthread_create(&thread, NULL, func, data);
return thread;
} void end_thread(CUTThread thread)
{
pthread_join(thread, NULL);
} void destroy_thread( CUTThread thread )
{
pthread_cancel(thread);
} void wait_for_threads(const CUTThread * threads, int num)
{
for(int i = ; i < num; i++)
end_thread( threads[i] );
}
#endif #endif // __BOOK_H__
《GPU高性能编程CUDA实战》附录三 关于book.h的更多相关文章
- [问题解决]《GPU高性能编程CUDA实战》中第4章Julia实例“显示器驱动已停止响应,并且已恢复”问题的解决方法
以下问题的出现及解决都基于"WIN7+CUDA7.5". 问题描述:当我编译运行<GPU高性能编程CUDA实战>中第4章所给Julia实例代码时,出现了显示器闪动的现象 ...
- 《GPU高性能编程CUDA实战》附录二 散列表
▶ 使用CPU和GPU分别实现散列表 ● CPU方法 #include <stdio.h> #include <time.h> #include "cuda_runt ...
- 《GPU高性能编程CUDA实战》第三章 CUDA设备相关
▶ 这章介绍了与CUDA设备相关的参数,并给出了了若干用于查询参数的函数. ● 代码(已合并) #include <stdio.h> #include "cuda_runtime ...
- 《GPU高性能编程CUDA实战》附录四 其他头文件
▶ cpu_bitmap.h #ifndef __CPU_BITMAP_H__ #define __CPU_BITMAP_H__ #include "gl_helper.h" st ...
- 《GPU高性能编程CUDA实战》附录一 高级原子操作
▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...
- 《GPU高性能编程CUDA实战》第五章 线程并行
▶ 本章介绍了线程并行,并给出四个例子.长向量加法.波纹效果.点积和显示位图. ● 长向量加法(线程块并行 + 线程并行) #include <stdio.h> #include &quo ...
- 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C
▶ 本章介绍了多设备胸膛下的 CUDA 编程,以及一些特殊存储类型对计算速度的影响 ● 显存和零拷贝内存的拷贝与计算对比 #include <stdio.h> #include " ...
- 《GPU高性能编程CUDA实战》第四章 简单的线程块并行
▶ 本章介绍了线程块并行,并给出两个例子:长向量加法和绘制julia集. ● 长向量加法,中规中矩的GPU加法,包含申请内存和显存,赋值,显存传入,计算,显存传出,处理结果,清理内存和显存.用到了 t ...
- 《GPU高性能编程CUDA实战》第八章 图形互操作性
▶ OpenGL与DirectX,等待填坑. ● basic_interop #include <stdio.h> #include "cuda_runtime.h" ...
随机推荐
- Hibernate4获取sessionFactory
/** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * ...
- 自动AC机
可以在lemon和cena环境下使用. #include<iostream> #include<cstdio> #include<cstring> #include ...
- stenciljs 学习十三 @stencil/router 组件使用说明
@stencil/router 组件包含的子组件 stencil-router stencil-route-switch stencil-route stencil-route-link stenci ...
- 转 AngularJS 2.0将面向移动应用并放弃旧浏览器
AngularJS团队表示“AngularJS 2.0是移动应用的框架”.该框架将继续支持桌面,但其主要关注点变成了移动领域.它的目标还包括通过转译器支持EcmaScript 6(因为浏览器还不支持E ...
- 模板引擎jade学习
语言参考 标签列表 doctype Tags Block Expansion Attributes Boolean Attributes Class Attributes Class Literal ...
- Win8被禁购信息战由暗到明
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jhzyz/article/details/26629277 冯强/文 关于中国政府禁止採购微软Win ...
- MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片
版权声明:本文为博主原创文章,转载请注明CSDN博客源地址! 共同学习,一起进步~ https://blog.csdn.net/Eastmount/article/details/26404733 ...
- 使用EntityFramework6连接MySQL
使用EntityFramework6连接MySQL 不是微软的亲儿子这待遇就是不一样,其中的坑可真实不少,第一次连MySQL足足折腾了我大半天. 废话不多说直接开始. 安装MySQL 从官网上下载最新 ...
- SpringMVC请求参数注解两个小问题
今天遇到使用SpringMVC请求注解遇到的两个小问题: 如果用@requestBody注解,则请求体内容类型一般要为application/json,如果其类型为multipart/form-dat ...
- 未能正确加载“VSTS for Database Professionals Sql Server Data-tier Application”包。(转)
今天费了九牛二虎之力,重转好了vs2010之后,打开解决方案,报出下面的错误: ---------------------------Microsoft Visual Studio---------- ...