Mercurial > hg > Members > yutaka > OpenCL
diff WordCount/main.cc @ 0:0e6e76dbdb0f
add file
author | Yutaka_Kinjyo |
---|---|
date | Tue, 12 Jul 2011 11:12:51 +0900 |
parents | |
children | 403e35dd9b6d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WordCount/main.cc Tue Jul 12 11:12:51 2011 +0900 @@ -0,0 +1,232 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <OpenCL/opencl.h> +#include <oclUtils.h> + +#define OUT_PARAM_NUM 2 + +typedef struct { + caddr_t file_mmap; + off_t size; +} st_mmap_t; + +/*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/ +static int +fix_byte(int size,int fix_byte_size) +{ + size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size; + + return size; +} + + +static st_mmap_t +my_mmap(char *filename) +{ + + /*マッピングだよ!*/ + int fd = -1; + int map = MAP_PRIVATE; + st_mmap_t st_mmap; + struct stat sb; + + if ((fd=open(filename,O_RDONLY,0666))==0) { + fprintf(stderr,"can't open %s\n",filename); + } + + if (fstat(fd,&sb)) { + fprintf(stderr,"can't fstat %s\n",filename); + } + + printf("file size %d\n",(int)sb.st_size); + + /*sizeをページングサイズの倍数にあわせる*/ + st_mmap.size = fix_byte(sb.st_size,4096); + + printf("fix 4096byte file size %d\n",(int)st_mmap.size); + + st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); + if (st_mmap.file_mmap == (caddr_t)-1) { + fprintf(stderr,"Can't mmap file\n"); + perror(NULL); + exit(0); + } + + return st_mmap; + +} + + + +int main(int args, char *argv[]) +{ + + char *filename = 0; + + for (int i = 1; argv[i]; ++i) { + if (strcmp(argv[i], "-file") == 0) { + filename = argv[i+1]; + } else if (strcmp(argv[i], "-help")) { + printf("Usage: ./word_count [-file filename]\n"); + } + } + + if (filename == 0) { + printf("Usage: ./word_count [-file filename]\n"); + return 0; + } + + //指定されたファイルをメモリにmap + st_mmap_t st_mmap = my_mmap(filename); + + //kernelファイルの大きさ取得して、メモリ確保 + int fd = -1; + const char *kernel_filename = "./word_count.cl"; + if ((fd=open(kernel_filename,O_RDONLY,0666))==0) { + fprintf(stderr,"can't open %s\n",kernel_filename); + } + + struct stat sb; + if (fstat(fd,&sb)) { + fprintf(stderr,"can't fstat %s\n",filename); + } + + size_t source_size = sb.st_size; + void *source_str = malloc(source_size); + + int err = read(fd, source_str, source_size); + if (err == -1) { + fprintf(stderr,"can't read %s\n",filename); + } + + close(fd); + + cl_platform_id platform_id = NULL; + cl_uint ret_num_platforms = NULL; + cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms); + + cl_device_id device_id = NULL; + cl_uint ret_num_devices = NULL; + // CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_GPU, と指定できる + // CL_DEVICE_TYPE_DEFAULT はどうなるのか + ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_DEFAULT, + 1, &device_id, &ret_num_devices); + + oclCheckError(ret, CL_SUCCESS); + + //OpenCLコンテキストの作成 + cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret); + oclCheckError(ret, CL_SUCCESS); + cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret); + oclCheckError(ret, CL_SUCCESS); + // カーネルプログラムを読み込む + cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_str, + (const size_t *)&source_size, &ret); + ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL); + + oclCheckError(ret, CL_SUCCESS); + + //カーネルプログラムをビルド + //Task選択にあたる + cl_kernel kernel = clCreateKernel(program, "word_count", &ret); + oclCheckError(ret, CL_SUCCESS); + + //カウントするテキストデータのメモリオブジェクト + cl_mem text_memobj = clCreateBuffer(context, CL_MEM_READ_ONLY, + st_mmap.size * sizeof(char), NULL, &ret); + + ret = clEnqueueWriteBuffer(command_queue, text_memobj, + CL_TRUE, 0, st_mmap.size, (char*)st_mmap.file_mmap, + 0, NULL, NULL); + + oclCheckError(ret, CL_SUCCESS); + + // 必要なパラメータのオブジェクト + cl_mem param_memobj = clCreateBuffer(context, CL_MEM_READ_ONLY, + sizeof(int), NULL, &ret); + + ret = clEnqueueWriteBuffer(command_queue, param_memobj, + CL_TRUE, 0, sizeof(int), (int*)(&st_mmap.size), + 0, NULL, NULL); + + oclCheckError(ret, CL_SUCCESS); + + + /* + * 並列度の計算 + * wcするファイルの大きさに合わせる + * + */ + + // このdivi_size はどうやって決めるよ + int divi_size = 1024; + st_mmap.size / 1024; + + + size_t global_work_size = 4; + + // 行数、単語数を格納する2のint配列 + int out_size = sizeof(int) * OUT_PARAM_NUM * global_work_size; + cl_mem out_memobj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, + out_size * sizeof(char), NULL, &ret); + + oclCheckError(ret, CL_SUCCESS); + + + // 引数のSet + // memory object にしなくてもできるsetできるかも + ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&text_memobj); + ret |= clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)¶m_memobj); + ret |= clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&out_memobj); + + oclCheckError(ret, CL_SUCCESS); + + /* + * kernel実行 + * 並列に処理せずに work-item ひとつで動かしたい場合は、clEnqueueNDRangeKernel の簡易版 clEnqueueTask が使える + * + */ + + //ret = clEnqueueTask(command_queue, kernel, 0, NULL, NULL); + // global_work_size は配列。並列動作させる時の次元数にあわせて、配列の次元数も決まるはず + ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL); + + oclCheckError(ret, CL_SUCCESS); + + int *out_data = (int*)malloc(out_size); + + // 演算結果の読み込み + ret = clEnqueueReadBuffer(command_queue, out_memobj, CL_TRUE, 0, + out_size * sizeof(char), out_data, 0, NULL, NULL); + + oclCheckError(ret, CL_SUCCESS); + + for (int i = 0; i < global_work_size; i++) { + for (int j = 0; j < OUT_PARAM_NUM; j++) { + printf("%d ", out_data[i*OUT_PARAM_NUM+j]); + } + printf("\n"); + } + + clFlush(command_queue); + clFinish(command_queue); + clReleaseKernel(kernel); + clReleaseProgram(program); + clReleaseMemObject(text_memobj); + clReleaseMemObject(param_memobj); + clReleaseMemObject(out_memobj); + clReleaseCommandQueue(command_queue); + clReleaseContext(context); + + free(source_str); + free(out_data); + + return 0; + +}