- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习 OpenCL,但我什至无法制作一个简单的内核来工作。
下面的代码来自《OpenCL 示例编程》一书,我修改、修改、修改......但我仍然不知道问题出在哪里。
每次我在 PC(AMD Athlon 5350 APU 和 Radeon R3)中执行该程序时,它都会将结果打印为“0.0000”。如果我在另一台带有 NVIDIA 1080 TI 的计算机(该 HD 的克隆,因此一切都相同)中运行相同的可执行文件,程序将输出“3.000”作为结果。
我注意到编译器输出中出现警告,因此我将过时的 clCreateCommandQueue 调用更改为 clCreateCommandQueueWithProperties()。
现在......它只是段错误(通过 printf() 测试,我知道它在 clCreateCommandQueueWithProperties 期间/之后出现段错误)。
在配备 NVIDIA GPU 的系统上,它就可以正常工作。
我错过了什么?
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
#include <CL/cl.h>
#define VECTOR_NS 4096
#define VECTOR_SIZE (VECTOR_NS*sizeof(float))
static const char* saxpy_kernel =
"__kernel void saxpy_kernel(__global float *A, __global float *B, __global float *C)\n"
"{\n"
"int index = get_global_id(0);\n"
"C[0] = 3;\n"
"}\n"
;
int main(void)
{
int i;
float total;
float* A;
float* B;
float* C;
float* Cmapped;
cl_mem Acl;
cl_mem Bcl;
cl_mem Ccl;
cl_context context;
cl_platform_id* platforms;
cl_uint num_platforms;
cl_uint num_devices;
cl_command_queue queue;
cl_kernel kernel;
cl_int clStatus;
// Get platform and device information
platforms = NULL;
//Set up the Platform
clStatus = clGetPlatformIDs(0, NULL, &num_platforms);
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platforms);
clStatus = clGetPlatformIDs(num_platforms, platforms, NULL);
//Get the devices list and choose the device you want to run on
cl_device_id* device_list = NULL;
clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
device_list = (cl_device_id *) malloc(sizeof(cl_device_id)*num_devices);
clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, num_devices, device_list, NULL);
// Create one OpenCL context for each device in the platform
context = clCreateContext( NULL, num_devices, device_list, NULL, NULL, &clStatus);
/* Create the command queue */
//queue = clCreateCommandQueue(context, device_list[0], 0, &clStatus);
queue = clCreateCommandQueueWithProperties(context, device_list[0], NULL, &clStatus);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
/* */
if((A = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
if((B = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
if((C = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
fprintf(stderr, "ERROR: failed to allocate memory.\n");
exit(1);
}
/* Initialize it */
i = 0;
do {
A[i] = 1;
B[i] = 2;
C[i] = 0;
} while(++i != VECTOR_NS);
Acl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, A, &clStatus); // CL_MEM_READ_ONLY
Bcl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, B, &clStatus); // CL_MEM_READ_ONLY
Ccl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, C, &clStatus); // CL_MEM_WRITE_ONLY
// Create a program from the kernel source
// Build the program
cl_program program = clCreateProgramWithSource(context, 1, (const char**)&saxpy_kernel, NULL, &clStatus);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
exit(1);
}
clStatus = clBuildProgram(program, 1, device_list, NULL, NULL, NULL);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
exit(1);
}
// Create the OpenCL kernel
kernel = clCreateKernel(program, "saxpy_kernel", &clStatus);
// Set the arguments of the kernel
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&Acl);
clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&Bcl);
clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&Ccl);
// Execute the OpenCL kernel on the list
size_t global_size = VECTOR_NS; // Process the entire lists
size_t local_size = 1;
// Process one item at a time
clStatus = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, &local_size, 0, NULL, NULL);
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
//* Clean up and wait for all the comands to complete. */
clFlush(queue);
/* Display the result to the screen */
Cmapped = (float*) clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus); // CL_MEM_USE_HOST_PTR
if(clStatus != CL_SUCCESS){
fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
exit(1);
}
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += C[i];
printf("TOTAL: %f\n", total);
/* Clean up and wait for all the comands to complete. */
clFlush(queue);
clFinish(queue);
/* Finally release all OpenCL allocated objects and host buffers. */
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseMemObject(Acl);
clReleaseMemObject(Bcl);
clReleaseMemObject(Ccl);
clReleaseCommandQueue(queue);
clReleaseContext(context);
free(platforms);
free(device_list);
return 0;
}
最佳答案
虽然您的代码在我的机器上按原样工作,但我认为可能存在导致您出现问题的问题。通话
Cmapped = (float*)clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus);
更改了Cmapped
,但您尝试从原始C
缓冲区中读取
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += C[i];
这可能应该是这样的
total = 0;
for(i = 0; i < VECTOR_NS; i++)
total += Cmapped[i];
但是,由于您使用 CL_MEM_USE_HOST_PTR 标志创建了缓冲区,如果您使用 clEnqueueReadBuffer 来代替,您的 OpenCL 驱动程序也应该能够优化复制操作,如果您使用原始指针作为目的地:
clEnqueueReadBuffer(queue, Ccl, CL_TRUE, 0, VECTOR_SIZE, C, 0, NULL, NULL);
如果 OpenCL 实现尚未将数据缓存到设备内存,则这应该是无操作。
关于c - 如何创建OpenCL命令队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48271444/
我遇到一种情况,我需要从某个主题读取(正在进行的)消息并将它们放入另一个 Queue 中。我怀疑我是否需要 jms Queue 或者我可以对内存中的 java Queue 感到满意。我将通过同一 jv
队列的定义 队列(Queue):先进先出的线性表 队列是仅在队尾进行插入和队头进行删除操作的线性表 队头(front):线性表的表头端,即可删除端 队尾(rear):线性表的表尾端,即可插入端 由于这
Redis专题-队列 首先,想一想 Redis 适合做消息队列吗? 1、消息队列的消息存取需求是什么?redis中的解决方案是什么? 无非就是下面这几点: 0、数据可以顺序读
0. 学习目标 栈和队列是在程序设计中常见的数据类型,从数据结构的角度来讲,栈和队列也是线性表,是操作受限的线性表,它们的基本操作是线性表操作的子集,但从数据类型的角度来讲,它们与线性表又有着巨大的不
我想在 redis + Flask 和 Python 中实现一个队列。我已经用 RQ 实现了这样的查询,如果你有 Flask 应用程序和任务在同一台服务器上工作,它就可以正常工作。我想知道是否有可能创
我正在使用 Laravel 5.1,我有一个大约需要 2 分钟来处理的任务,这个任务特别是生成报告...... 现在,很明显,我不能让用户在我接受用户输入的同一页面上等待 2 分钟,而是我应该在后台处
我正在使用 Azure 队列,并且有多个不同的进程从队列中读取数据。 我的系统的构建方式假设每条消息只读取一次。 这个Microsoft article声称 Azure 队列具有至少一次传送保证,这可
我正在创建一个Thread::Queue元素数组。 我这样做是这样的: for (my $i=0; $i new; } 但是,当我在每个队列中填充这样的元素时 $queues[$index]->enq
我试图了解如何将我的 Mercurial 补丁推送到远程存储库(例如 bitbucket.org),而不必先应用它们(实际上提交它们)。我的动机是在最终完成之前首先对我的工作进行远程备份,并且能够与其
我的本地计算机上有一个 Mercurial 队列补丁,我需要与同事共享该补丁,但我不想将其提交到上游存储库。有没有一种简单的方法可以打包该补丁并与他分享? 最佳答案 mq 将补丁作为不带扩展名的文
Java 中是否有任何类提供与 Queue 相同的功能,但有返回对象的选项,并且不要删除它,只需将其设置在集合末尾? 最佳答案 Queue不直接提供这样的方法。但是,您可以使用 poll 和 add
我在Windows上使用Tortoise svn客户端,我需要能够一次提交来自不同子文件夹的更改文件-一次提交。像在提交之前将文件添加到队列中之类的?我该怎么做? Windows上是否还有另一个svn
好吧,我正在尝试对我的 DSAQueue 类进行单元测试,它显示我的 isEmpty()、isFull() 和 dequeue() 方法失败。 以下是我的 DSAQueue 代码。我认为我的 Dequ
我想尽量减少对传入请求的数据库查询。它目前需要写入 6 个不同的表。在返回响应之前不需要完成处理。因此,我考虑了 laravel 队列,但我想知道我是否也可以摆脱写入队列/作业表所需的单独查询。我可以
我正在学习队列数据结构。我想用链表创建队列。我想编程输出:10 20程序输出:队列为空-1 队列为空-1 我哪里出错了? 代码如下: class Node { int x; Node next
“当工作人员有空时,他们会根据主题的优先级列表从等待请求池中进行选择。在时间 t 到达的所有请求都可以在时间 t 进行分配。如果两名工作人员同时有空,则安排优先权分配给最近的工作最早安排的人。如果仍然
我正在开发一个巨大的应用程序,它使用一些子菜单、模式窗口、提示等。 现在,我想知道在此类应用程序中处理 Esc 和单击外部事件的正确方法。 $(document).keyup(function(e)
所以 如果我有一个队列 a --> b --> NULL; 当我使用函数时 void duplicate(QueueNodePtr pHead, QueueNodePtr *pTail) 它会给 a
我正在尝试为键盘输入实现 FIFO 队列,但似乎无法让它工作。我可以让键盘输入显示在液晶显示屏上,但这就是我能做的。我认为代码应该读取键盘输入并将其插入队列,然后弹出键盘输入并将值读取到液晶屏幕上。有
我正在学习算法和 DS。如何在 JavaScript 中使用队列? 我知道你可以做这样的事情。 var stack = []; stack.push(2); // stack is now
我是一名优秀的程序员,十分优秀!