码迷,mamicode.com
首页 > 其他好文 > 详细

附录三 关于book.h

时间:2017-10-11 23:43:07      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:统一   __file__   failed   style   返回   and   nbsp   close   多线程   

本书中用到的公用函数放到了头文件book.h中。

  1 #ifndef __BOOK_H__
  2 #define __BOOK_H__
  3 #include <stdio.h>
  4 #include <stdlib.h>         // 自己加的
  5 #include "cuda_runtime.h"   // 自己加的
  6 
  7 
  8 static void HandleError( cudaError_t err, const char *file, int line )//定义报错函数,通过传入的返回值和文件名、行号来提示信息
  9 {
 10     if (err != cudaSuccess)
 11     {
 12         printf( "%s in %s at line %d\n", cudaGetErrorString( err ),file, line );
 13         exit( EXIT_FAILURE );
 14     }
 15 }
 16 #define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))// 将报错函数包装为宏,自动填塞文件名和行号
 17 
 18 #define HANDLE_NULL( a )/* 空指针报错函数,代码中malloc失败时报错 */               19 {                                                                              20     if (a == NULL)                                                             21     {                                                                          22         printf( "Host memory failed in %s at line %d\n", __FILE__, __LINE__ ); 23         exit(EXIT_FAILURE);                                                    24     }                                                                          25 }
 26 
 27 template< typename T >// 泛型交换(全书都没用到?)
 28 void swap( T& a, T& b )
 29 {
 30     T t = a;
 31     a = b;
 32     b = t;
 33 }
 34 
 35 void* big_random_block( int size )//在主机中生成随机数组,无符号字符型
 36 {
 37     unsigned char *data = (unsigned char*)malloc( size );
 38     HANDLE_NULL( data );
 39     for (int i = 0; i < size; data[i] = rand(), i++);
 40     return data;
 41 }
 42 
 43 int* big_random_block_int( int size )//在主机中生成随机数组,整型
 44 {
 45     int *data = (int*)malloc( size * sizeof(int) );
 46     HANDLE_NULL( data );
 47     for (int i = 0; i < size; data[i] = rand(), i++);
 48     return data;
 49 }
 50 
 51 // 公用设备函数
 52 __device__ unsigned char value(float n1, float n2, int hue)
 53 {
 54     if (hue > 360)
 55         hue -= 360;
 56     else if (hue < 0)
 57         hue += 360;
 58     if (hue < 60)
 59         return (unsigned char)(255 * (n1 + (n2 - n1)*hue / 60));
 60     if (hue < 180)
 61         return (unsigned char)(255 * n2); 
 62     if (hue < 240)
 63         return (unsigned char)(255 * (n1 + (n2 - n1)*(240 - hue) / 60));
 64     return (unsigned char)(255 * n1);
 65 }
 66 
 67 __global__ void float_to_color(unsigned char *optr, const float *outSrc)
 68 {
 69     int x = threadIdx.x + blockIdx.x * blockDim.x;
 70     int y = threadIdx.y + blockIdx.y * blockDim.y;
 71     int offset = x + y * blockDim.x * gridDim.x;
 72 
 73     float l = outSrc[offset];
 74     float s = 1;
 75     int h = (180 + (int)(360.0f * outSrc[offset])) % 360;
 76     float m1, m2;
 77 
 78     if (l <= 0.5f)
 79         m2 = l * (1 + s);
 80     else
 81         m2 = l + s - l * s;
 82     m1 = 2 * l - m2; 
 83 
 84     optr[offset * 4 + 0] = value(m1, m2, h + 120);
 85     optr[offset * 4 + 1] = value(m1, m2, h); 
 86     optr[offset * 4 + 2] = value(m1, m2, h - 120);
 87     optr[offset * 4 + 3] = 255; 
 88 }
 89 
 90 __global__ void float_to_color( uchar4 *optr,const float *outSrc )
 91 {
 92     int x = threadIdx.x + blockIdx.x * blockDim.x;
 93     int y = threadIdx.y + blockIdx.y * blockDim.y;
 94     int offset = x + y * blockDim.x * gridDim.x;
 95 
 96     float l = outSrc[offset];
 97     float s = 1;
 98     int h = (180 + (int)(360.0f * outSrc[offset])) % 360;
 99     float m1, m2;
100 
101     if (l <= 0.5f)
102         m2 = l * (1 + s);
103     else
104         m2 = l + s - l * s;
105     m1 = 2 * l - m2;
106 
107     optr[offset].x = value(m1, m2, h + 120);
108     optr[offset].y = value(m1, m2, h); 
109     optr[offset].z = value(m1, m2, h - 120);
110     optr[offset].w = 255; 
111 }
112 
113 // 有关线程的设置
114 #if _WIN32
115     //Windows threads.
116     #include <windows.h>
117 
118     typedef HANDLE CUTThread;// 统一包装
119     typedef unsigned (WINAPI *CUT_THREADROUTINE)(void *);
120 
121     #define CUT_THREADPROC unsigned WINAPI
122     #define  CUT_THREADEND return 0
123 
124 #else
125     //POSIX threads.
126     #include <pthread.h>
127 
128     typedef pthread_t CUTThread;
129     typedef void *(*CUT_THREADROUTINE)(void *);
130 
131     #define CUT_THREADPROC void
132     #define  CUT_THREADEND
133 #endif
134 
135 // 线程的创造,单线程结束,单线程销毁和多线程等待
136 CUTThread start_thread( CUT_THREADROUTINE, void *data );
137 void end_thread( CUTThread thread );
138 void destroy_thread( CUTThread thread );
139 void wait_for_threads( const CUTThread *threads, int num );
140 
141 #if _WIN32
142     CUTThread start_thread(CUT_THREADROUTINE func, void *data)
143     {
144         return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, data, 0, NULL);
145     }
146 
147     void end_thread(CUTThread thread)
148     {
149         WaitForSingleObject(thread, INFINITE);
150         CloseHandle(thread);
151     }
152 
153     void destroy_thread( CUTThread thread )
154     {
155         TerminateThread(thread, 0);
156         CloseHandle(thread);
157     }
158 
159     void wait_for_threads(const CUTThread * threads, int num){
160         WaitForMultipleObjects(num, threads, true, INFINITE);
161 
162         for(int i = 0; i < num; i++)
163             CloseHandle(threads[i]);
164     }
165 
166 #else
167     CUTThread start_thread(CUT_THREADROUTINE func, void * data)
168     {
169         pthread_t thread;
170         pthread_create(&thread, NULL, func, data);
171         return thread;
172     }
173 
174     void end_thread(CUTThread thread)
175     {
176         pthread_join(thread, NULL);
177     }
178 
179     void destroy_thread( CUTThread thread )
180     {
181         pthread_cancel(thread);
182     }
183 
184     void wait_for_threads(const CUTThread * threads, int num)
185     {
186         for(int i = 0; i < num; i++)
187             end_thread( threads[i] );
188     }
189 #endif
190 
191 #endif  // __BOOK_H__

 

附录三 关于book.h

标签:统一   __file__   failed   style   返回   and   nbsp   close   多线程   

原文地址:http://www.cnblogs.com/cuancuancuanhao/p/7653300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!