1:
2: #include "stdafx.h"
3: #include
4: #include
5: #include
6: #include
7: #include
8: #include
9:
10: using namespace std;
11: #define NWITEMS 262144
12:
13: #pragma comment (lib,"OpenCL.lib")
14:
15: //把文本文件读入一个string中
16: int convertToString(const char *filename, std::string& s)
17: {
18: size_t size;
19: char* str;
20:
21: std::fstream f(filename, (std::fstream::in | std::fstream::binary));
22:
23: if(f.is_open())
24: {
25: size_t fileSize;
26: f.seekg(0, std::fstream::end);
27: size = fileSize = (size_t)f.tellg();
28: f.seekg(0, std::fstream::beg);
29:
30: str = new char[size+1];
31: if(!str)
32: {
33: f.close();
34: return NULL;
35: }
36:
37: f.read(str, fileSize);
38: f.close();
39: str[size] = '\0';
40:
41: s = str;
42: delete[] str;
43: return 0;
44: }
45: printf("Error: Failed to open file %s\n", filename);
46: return 1;
47: }
48:
49: int main(int argc, char* argv[])
50: {
51: //在host内存中创建三个缓冲区
52: float *buf1 = 0;
53: float *buf2 = 0;
54: float *buf = 0;
55:
56: buf1 =(float *)malloc(NWITEMS * sizeof(float));
57: buf2 =(float *)malloc(NWITEMS * sizeof(float));
58: buf =(float *)malloc(NWITEMS * sizeof(float));
59:
60: //初始化buf1和buf2的内容
61: int i;
62: srand( (unsigned)time( NULL ) );
63: for(i = 0; i < NWITEMS; i++)
64: buf1[i] = rand()%65535;
65:
66: srand( (unsigned)time( NULL ) +1000);
67: for(i = 0; i < NWITEMS; i++)
68: buf2[i] = rand()%65535;
69:
70: cl_uint status;
71: cl_platform_id platform;
72:
73: //创建平台对象
74: status = clGetPlatformIDs( 1, &platform, NULL );
75:
76: cl_device_id device;
77:
78: //创建GPU设备
79: clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU,
80: 1,
81: &device,
82: NULL);
83: //创建context
84: cl_context context = clCreateContext( NULL,
85: 1,
86: &device,
87: NULL, NULL, NULL);
88: //创建命令队列
89: cl_command_queue queue = clCreateCommandQueue( context,
90: device,
91: CL_QUEUE_PROFILING_ENABLE, NULL );
92: //创建三个OpenCL内存对象,并把buf1的内容通过隐式拷贝的方式
93: //拷贝到clbuf1,buf2的内容通过显示拷贝的方式拷贝到clbuf2
94: cl_mem clbuf1 = clCreateBuffer(context,
95: CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
96: NWITEMS*sizeof(cl_float),buf1,
97: NULL );
98:
99: cl_mem clbuf2 = clCreateBuffer(context,
100: CL_MEM_READ_ONLY ,
101: NWITEMS*sizeof(cl_float),NULL,
102: NULL );
103:
104: status = clEnqueueWriteBuffer(queue, clbuf2, 1,
105: 0, NWITEMS*sizeof(cl_float), buf2, 0, 0, 0);
106:
107: cl_mem buffer = clCreateBuffer( context,
108: CL_MEM_WRITE_ONLY,
109: NWITEMS * sizeof(cl_float),
110: NULL, NULL );
111:
112: const char * filename = "add.cl";
113: std::string sourceStr;
114: status = convertToString(filename, sourceStr);
115: const char * source = sourceStr.c_str();
116: size_t sourceSize[] = { strlen(source) };
117:
118: //创建程序对象
119: cl_program program = clCreateProgramWithSource(
120: context,
121: 1,
122: &source,
123: sourceSize,
124: NULL);
125: //编译程序对象
126: status = clBuildProgram( program, 1, &device, NULL, NULL, NULL );
127: if(status != 0)
128: {
129: printf("clBuild failed:%d\n", status);
130: char tbuf[0x10000];
131: clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0x10000, tbuf, NULL);
132: printf("\n%s\n", tbuf);
133: return -1;
134: }
135:
136:
137:
138: if(buf)
139: free(buf);
140: if(buf1)
141: free(buf1);
142: if(buf2)
143: free(buf2);
144:
145: //删除OpenCL资源对象
146: clReleaseMemObject(clbuf1);
147: clReleaseMemObject(clbuf2);
148: clReleaseMemObject(buffer);
149: clReleaseProgram(program);
150: clReleaseCommandQueue(queue);
151: clReleaseContext(context);
152: return 0;
153: }
154:
|