[ad_1]
使用 OpenCL 并使用本地内存编写程序。 任务:b=min(A+C)
我有一个准备好的框图,但老师说它按顺序显示所有内容,并且我们有一个并行程序,我不知道如何修复框图。
我还想知道我是否正确使用本地内存编写了程序。
链接到带有框图的图像: https://www.imghippo.com/i/1704956923.jpg
#define _CRT_SECURE_NO_WARNINGS #define CL_USE_DEPRECATED_OPENCL_1_2_APIS #include <CL/cl.h> #include <CL/cl_platform.h> #include <iostream> #include <cstdlib> #include <stdio.h> #define MAX_SOURCE_SIZE (0x100000) #define LIST_SIZE 64 int main(void) { int* A = new int[LIST_SIZE]; int* C = new int[LIST_SIZE]; int* B = new int[LIST_SIZE]; srand(time(0)); for (int i = 0; i < LIST_SIZE; i++) { A[i] = (rand() % 200) / 2.; C[i] = (rand() % 200) / 2.; } FILE* fp; char* source_str; size_t source_size; fp = fopen("kernel.cl", "r"); if (!fp) { fprintf(stderr, "Failed to load kernel.\n"); exit(1); } source_str = new char[MAX_SOURCE_SIZE]; source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp); fclose(fp); cl_platform_id platform_id = NULL; cl_device_id device_id = NULL; cl_uint ret_num_devices; cl_uint ret_num_platforms; cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms); ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices); cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret); cl_command_queue command_queue = clCreateCommandQueueWithProperties(context, device_id, 0, &ret); cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret); cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret); cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int), NULL, &ret); ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), A, 0, NULL, NULL); ret = clEnqueueWriteBuffer(command_queue, c_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), C, 0, NULL, NULL); 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); cl_kernel kernel = clCreateKernel(program, "krn", &ret); size_t local_item_size = 64; ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&a_mem_obj); ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&c_mem_obj); ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&b_mem_obj); ret = clSetKernelArg(kernel, 3, local_item_size * sizeof(int), NULL); size_t global_item_size = LIST_SIZE; ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL); ret = clEnqueueReadBuffer(command_queue, b_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), B, 0, NULL, NULL); for (int i = 0; i < LIST_SIZE; i++) { printf("%d. sum: %d\n", i, B[i]); } int min = B[0]; int minIndex = 0; for (int i = 0; i < LIST_SIZE; i++) { if (B[i] < min) { min = B[i]; minIndex = i; } } printf("\n\n%d. Minimum min(A+C) = %d\n", minIndex, min); ret = clFlush(command_queue); ret = clFinish(command_queue); ret = clReleaseKernel(kernel); ret = clReleaseProgram(program); ret = clReleaseMemObject(a_mem_obj); ret = clReleaseMemObject(c_mem_obj); ret = clReleaseMemObject(b_mem_obj); ret = clReleaseCommandQueue(command_queue); ret = clReleaseContext(context); delete[] source_str; delete[] A; delete[] B; delete[] C; return 0; }
内核.cl:
__kernel void krn(__global const int* A, __global const int* C, __global int* B) { int i = get_global_id(0); __local int localA[128]; __local int localC[128]; localA[i] = A[i]; localC[i] = C[i]; barrier(CLK_LOCAL_MEM_FENCE); B[i] = localA[i] + localC[i]; }
我尝试过的:
画了一个错误的框图并写了一个程序
解决方案1
答:没有。
这不是你的应用程序应该如何工作的图表:它是应用程序和生成它的说明的混杂 – 如果你的作业希望你生成一个基于并行的应用程序,那么你需要仔细检查作业并确定哪些部分可以是同时且独立地完成,当前图表根本没有涵盖这一点。
诚实地? 我会放弃你到目前为止所做的一切,从头开始,从头开始重新阅读你的课程笔记/材料,因为到目前为止你似乎还没有掌握太多。 非常重要的是,您必须了解串行和并行之间的区别,甚至应用程序和创建应用程序的行为之间的区别 – 它们不是同一件事。
不要急于编码:先思考,这样你会节省很多浪费的时间。
[ad_2]
コメント