這是AMD異構大賽發的書上的例子,我自己加了一些東西,實現了一下。不過還沒有學會如何分析啟動並執行時間,先貼上代碼
這是第一次用amd APP KernelAnalyzer感覺還可以吧,
simpleMultiply.cl
// Enter your kernel in this window__kernel void simpleMultiply(__global float* outPutC, int widthA, int heightA, int widthB , int heightB , __global float* inputA , __global float* inputB ) { int row = get_global_id(1); int col = get_global_id(0); float sum = 0.0f ; for(int i=0;i<widthA; i++) { sum += inputA[row*widthA+i] * inputB[i*widthB+col]; } outPutC[row*widthB+col] = sum; } ;
main.cpp
/* 項目:openCL的矩陣相乘 作者:劉榮 時間:2012.11.20*/#include <iostream>#include<time.h>#include <string> #include<math.h>#include <vector>#include <CL/cl.h>#include <fstream>using namespace std;//kernel函數std::stringconvertToString(const char *filename)//將kernel源碼,即自己寫的並行化的函數,轉化成字串{ size_t size; char* str; std::string s; std::fstream f(filename, (std::fstream::in | std::fstream::binary)); if(f.is_open()) { size_t fileSize; f.seekg(0, std::fstream::end); size = fileSize = (size_t)f.tellg(); f.seekg(0, std::fstream::beg); str = new char[size+1]; if(!str) { f.close(); std::cout << "Memory allocation failed"; return NULL; } f.read(str, fileSize); f.close(); str[size] = '\0'; s = str; delete[] str; return s; } else { std::cout << "\nFile containg the kernel code(\".cl\") not found. Please copy the required file in the folder containg the executable.\n"; exit(1); } return NULL;}int main(){double start,end,time1,time2;//查詢平台cl_int ciErrNum;cl_platform_id platform;ciErrNum = clGetPlatformIDs(1, &platform, NULL);if(ciErrNum != CL_SUCCESS){cout<<"擷取裝置失敗"<<endl;return 0;}//擷取裝置資訊cl_device_id device;cl_int status; cl_uint maxDims; cl_event events[2]; size_t globalThreads[1]; size_t localThreads[1]; size_t maxWorkGroupSize; size_t maxWorkItemSizes[3]; //////////////////////////////////////////////////////////////////// // STEP 7 Analyzing proper workgroup size for the kernel // by querying device information // 7.1 Device Info CL_DEVICE_MAX_WORK_GROUP_SIZE // 7.2 Device Info CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS // 7.3 Device Info CL_DEVICE_MAX_WORK_ITEM_SIZES //////////////////////////////////////////////////////////////////// /** * Query device capabilities. Maximum * work item dimensions and the maximmum * work item sizes */ ciErrNum = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL); status = clGetDeviceInfo( device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void*)&maxWorkGroupSize, NULL); if(status != CL_SUCCESS) { std::cout << "Error: Getting Device Info. (clGetDeviceInfo)\n"; return 0; } status = clGetDeviceInfo( device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), (void*)&maxDims, NULL); if(status != CL_SUCCESS) { std::cout << "Error: Getting Device Info. (clGetDeviceInfo)\n"; return 0; } status = clGetDeviceInfo( device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t)*maxDims, (void*)maxWorkItemSizes, NULL); if(status != CL_SUCCESS) { std::cout << "Error: Getting Device Info. (clGetDeviceInfo)\n"; return 0; }cout<<"maxWorkItemSizes"<<maxWorkItemSizes<<endl;cout<<"maxDims"<<maxDims<<endl;cout<<"maxWorkGroupSize"<<(int)maxWorkGroupSize<<endl;//建立上下文cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0};cl_context ctx = clCreateContext(cps, 1, &device, NULL, NULL, &ciErrNum);if(ciErrNum != CL_SUCCESS){cout<<"建立上下文失敗"<<endl;return 0;}cl_command_queue myqueue = clCreateCommandQueue(ctx,device,0,&ciErrNum);if(ciErrNum != CL_SUCCESS){cout<<"命令隊列失敗"<<endl;return 0;}//聲明buffer,傳輸資料float *A = NULL; // 輸入數組float *B = NULL; // 輸入數組float *C = NULL; // 輸出數組int wA=20,hA=20;int wB=20,hB=20;int wC=20,hC=20;// 數組的大小const int elementsA = wA*hA;const int elementsB = wB*hB;const int elementsC = hA*wB;// 計算記憶體大小size_t datasizeA = sizeof(float)*elementsA;size_t datasizeB = sizeof(float)*elementsB;size_t datasizeC = sizeof(float)*elementsC;// 分配記憶體空間A = (float*)malloc(datasizeA);B = (float*)malloc(datasizeB);C = (float*)malloc(datasizeC);// 初始化輸入數組for(int i = 0;i < elementsA;i++){A[i] = std::rand()/1.5;//B[i] = std::rand()/1.5;}for(int i = 0;i < elementsB;i++){B[i] = std::rand()/1.5;//B[i] = std::rand()/1.5;}cl_mem bufferA = clCreateBuffer(ctx,CL_MEM_READ_ONLY,wA*hA*sizeof(float),NULL,&ciErrNum);ciErrNum = clEnqueueWriteBuffer(myqueue,bufferA,CL_TRUE,0,wA*hA*sizeof(float),(void*)A,0,NULL,NULL);cl_mem bufferB = clCreateBuffer(ctx,CL_MEM_READ_ONLY,wB*hB*sizeof(float),NULL,&ciErrNum);ciErrNum = clEnqueueWriteBuffer(myqueue,bufferB,CL_TRUE,0,wB*hB*sizeof(float),(void*)B,0,NULL,NULL);cl_mem bufferC = clCreateBuffer(ctx,CL_MEM_WRITE_ONLY,hA*wB*sizeof(float),NULL,&ciErrNum);//運行時kernel編譯const char * filename = "simpleMultiply.cl"; std::string sourceStr = convertToString(filename); const char * source = sourceStr.c_str(); size_t sourceSize[] = { strlen(source) };//直接將CL檔案讀到記憶體 cl_program myprog = clCreateProgramWithSource( ctx, 1, &source, sourceSize, &ciErrNum);//cl_program myprog = clCreateProgramWithSource(ctx,1,(const char**)&programSource,NULL,&ciErrNum);ciErrNum = clBuildProgram(myprog,0,NULL,NULL,NULL,NULL);cl_kernel mykernel = clCreateKernel(myprog,"simpleMultiply",&ciErrNum);//運行程式clSetKernelArg(mykernel,0,sizeof(cl_mem),(void*)&bufferC);clSetKernelArg(mykernel,1,sizeof(cl_mem),(void*)&wA);clSetKernelArg(mykernel,2,sizeof(cl_mem),(void*)&hA);clSetKernelArg(mykernel,3,sizeof(cl_mem),(void*)&wB);clSetKernelArg(mykernel,4,sizeof(cl_mem),(void*)&hB);clSetKernelArg(mykernel,5,sizeof(cl_mem),(void*)&bufferA);clSetKernelArg(mykernel,6,sizeof(cl_mem),(void*)&bufferB);size_t localws[2] ={20,20};size_t globalws[2]={wC,hC};////start = clock();ciErrNum = clEnqueueNDRangeKernel(myqueue,mykernel,2,NULL,globalws,localws,0,NULL,&events[0]);//時間同步status = clWaitForEvents(1, &events[0]); if(status != CL_SUCCESS) { std::cout << "Error: Waiting for kernel run to finish. \ (clWaitForEvents)\n"; return 0; } status = clReleaseEvent(events[0]);//將結果拷貝到主機端end = clock();time1=end-start;cout<<"shijian "<<time1<<endl;ciErrNum = clEnqueueReadBuffer(myqueue,bufferC,CL_TRUE,0,wC*hC*sizeof(float),(void*)C,0,NULL,&events[1]);status = clWaitForEvents(1, &events[1]); if(status != CL_SUCCESS) { std::cout << "Error: Waiting for read buffer call to finish. \ (clWaitForEvents)\n"; return 0; } status = clReleaseEvent(events[1]); if(status != CL_SUCCESS) { std::cout << "Error: Release event object. \ (clReleaseEvent)\n"; return 0; }//return 0;}