▶ 本书中用到的公用函数放到了头文件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__

附录三 关于book.h的更多相关文章

  1. [问题解决]《GPU高性能编程CUDA实战》中第4章Julia实例“显示器驱动已停止响应,并且已恢复”问题的解决方法

    以下问题的出现及解决都基于"WIN7+CUDA7.5". 问题描述:当我编译运行<GPU高性能编程CUDA实战>中第4章所给Julia实例代码时,出现了显示器闪动的现象 ...

  2. 《GPU高性能编程CUDA实战》附录二 散列表

    ▶ 使用CPU和GPU分别实现散列表 ● CPU方法 #include <stdio.h> #include <time.h> #include "cuda_runt ...

  3. 《GPU高性能编程CUDA实战》附录三 关于book.h

    ▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...

  4. 《GPU高性能编程CUDA实战》第三章 CUDA设备相关

    ▶ 这章介绍了与CUDA设备相关的参数,并给出了了若干用于查询参数的函数. ● 代码(已合并) #include <stdio.h> #include "cuda_runtime ...

  5. 《GPU高性能编程CUDA实战》附录四 其他头文件

    ▶ cpu_bitmap.h #ifndef __CPU_BITMAP_H__ #define __CPU_BITMAP_H__ #include "gl_helper.h" st ...

  6. 《GPU高性能编程CUDA实战》附录一 高级原子操作

    ▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...

  7. 《GPU高性能编程CUDA实战》第五章 线程并行

    ▶ 本章介绍了线程并行,并给出四个例子.长向量加法.波纹效果.点积和显示位图. ● 长向量加法(线程块并行 + 线程并行) #include <stdio.h> #include &quo ...

  8. 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C

    ▶ 本章介绍了多设备胸膛下的 CUDA 编程,以及一些特殊存储类型对计算速度的影响 ● 显存和零拷贝内存的拷贝与计算对比 #include <stdio.h> #include " ...

  9. 《GPU高性能编程CUDA实战》第四章 简单的线程块并行

    ▶ 本章介绍了线程块并行,并给出两个例子:长向量加法和绘制julia集. ● 长向量加法,中规中矩的GPU加法,包含申请内存和显存,赋值,显存传入,计算,显存传出,处理结果,清理内存和显存.用到了 t ...

  10. 《GPU高性能编程CUDA实战》第八章 图形互操作性

    ▶ OpenGL与DirectX,等待填坑. ● basic_interop #include <stdio.h> #include "cuda_runtime.h" ...

随机推荐

  1. QT_FORWARD_DECLARE_CLASS

    相当于class 类名. 那么他和#include 包含头文件有什么区别呢 首先我们为什么要包括头文件问题的回答很简单通常是我们需要获得某个类型的定义(definition).那么接下来的问题 ...

  2. 关于 String 自我理解

    String 的一些认识: String对象是不可变,所以使用 final 修饰 字符串拼接,合理利用 StringBuilder(线程非安全),StringBuffer 线程安全 常用方法就不详细介 ...

  3. Python文件读写模式

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在.可读,可写,可追加. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打 ...

  4. iOS开发注意事项(一)

    1.OC的消息机制与C++等的函数(方法)有很大的不同,OC在运行时所执行的代码由运行环境来决定,而C++等则由编译器决定.如果调用的函数是多态的,C++在运行时要按照虚方法表来查出到底执行哪个函数, ...

  5. Entity Framework 之Code First自动数据迁移

    using MvcShopping.Migrations; using MvcShopping.Models; using System; using System.Collections.Gener ...

  6. 使用Canvas制作时钟动画

    复习Javascript到Canvas的知识点,看到一个使用Canvas绘制的静态时钟例子,便想将其变成动态显示系统时间的时钟动画.另外再配上数字显示的时钟,一个小的时钟模块的诞生了!目前的界面还比较 ...

  7. iOS四种多线程(swift和oc)

    在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例,在实际使用中感受它们的区别.还有一点需要说明的是,这篇文章将会使用 Swift 和 ...

  8. php设计模式 工厂模式和单例模式

    一.单例模式//让该类在外界无法造对象//让外界可以造一个对象,做一个静态方法返回对象//在类里面通过让静态变量控制返回对象只能是一个. 单例模式的要点有三个: 一是某个类只能有一个实例: 二是它必须 ...

  9. 运维&网络知识(一)

    1. DNS 域名系统(Domain Name System),因特网上作为域名和IP地址映射的一个分布式数据库.

  10. interface接口

    当一个抽象类中的方法都是抽象的时候,这时可以将该抽象类用另一种形式定义和表示,就是接口 interface. 定义接口使用的关键字不是class,是interface.接口中常见的成员: 这些成员都有 ...