文件名称:OpenCL编程-android官方培训课程中文版v0.9.7
文件大小:5.1MB
文件格式:PDF
更新时间:2024-06-27 13:55:49
OpenCL
第三章 OpenCL编程 11 第三章 OpenCL编程 3.1 Hello OpenCL 让我们从Hellocl这个简单的实例开始学习OpenCL的编程。这里假定程序运行在AMD OpenCL 平台上。Hellocl的kernel运行在4x4的二维空间上,gidx为空间内节点的在第一维度上的global ID,gidy 为在第二维度上的global ID,每一个节点将bu�er的第gidx+4*gidy个元素按相应规则赋值。程序如下: 1 # include < iostream > 2 3 # if de�ned ( __APPLE_ _ ) || de�ned ( __MACOSX ) 4 # include < OpenCL / cl . hpp > 5 # else 6 # include < CL / cl .h > 7 # endif 8 9 # de�ne KERNEL (. . .) # __VA_ARGS__ 10 const char * 11 kernelSourceCode = KERNEL ( 12 13 __kernel void hel locl ( __global uint * bu�er ) { 14 s ize_t gidx = get_global_id (0) ; 15 s ize_t gidy = get_global_id (1) ; 16 s ize_t lidx = get_local_id (0) ; 17 bu�er [ gidx +4* gidy ] = (1 < < gidx ) |(0 x10 < < gidy ) ; 18 }) ; 19 20 int main () 21 { 22 23 cl_int status = 0; 24 s ize_t deviceListSiz e ; 25 26 /* 27 * 28 * 29 */ 30 31 c l_uint numPlat form s ; 32 c l_plat form_i d plat form = NULL ; 33 status = clGetPlat formIDs (0 , NULL , & numPlat forms ) ; 34 if ( status != CL_SUCCESS ) 35 { 36 37 return EXIT_FAILURE ; 38 } 39 40 if ( numPlat forms > 0) 41 { 42 c l_plat form_i d * plat forms = *) malloc ( numPlat forms * sizeof (c l_plat form_id ) ) ; 43 status = clGetPlat formIDs ( numPlatforms , platforms , NULL ) ; 44 if ( status != CL_SUCCESS ) 45 { 46 47 return -1; 48 } 49 for ( c lGetPlat formsIDs ) \ n " ) ;( unsigned int i =0; i < numPlat forms ; ++ i ) 50 { 51 char pbu�[100]; 52 status = c lGetPlat formInfo ( 53 plat forms [ i ] , 54 CL_PLATFORM_VENDOR , 55 s izeof ( pbu�) , 56 pbu�, 57 NULL ) ; c lGetPlat formsIDs ) \ n " ) ; ( c l_plat form_id \