- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在我编写了几个算法,在一个 GPU 上并行运行,但是当我尝试在多个 GPU(例如 3)上执行它们时,它们都有相同的问题。问题在于,在一个 GPU 上执行的代码在 3 个 GPU 上执行的时间完全相同(不是更快)。我尝试使用更多数据执行,尝试执行不同的任务,但没有任何帮助。最后,我最终尝试运行最简单的任务,如 elements sum,但仍然犯了这个可怕的错误。这就是为什么我不认为这是特定算法的问题,而且我觉得我的代码中存在错误(甚至是我在多个 GPU 上并行化代码的方法)。
这是我的 Parallel.cpp 类的头文件:
#ifndef PARALLEL_H
#define PARALLEL_H
#define __NO_STD_VECTOR // Use cl::vector and cl::string and
#define __NO_STD_STRING // not STL versions, more on this later
#include <CL/cl.h>
class Parallel
{
public:
Parallel();
int executeAttachVectorsKernel(int*, int*, int*, int);
static void getMaxWorkGroupSize(int*, int*, int*);
virtual ~Parallel();
protected:
private:
char* file_contents(const char*, int*);
void getShortInfo(cl_device_id);
int init(void);
cl_platform_id platform;
cl_device_id* devices;
cl_uint num_devices;
cl_command_queue* queues;
int* WGSizes;
int* WGNumbers;
cl_context context;
cl_program program;
cl_kernel kernel;
cl_mem input1;
cl_mem input2;
cl_mem output;
};
#endif // PARALLEL_H
这里是初始化方法init:
int Parallel::init() {
cl_int err;
//Connect to the first platfrom
err = clGetPlatformIDs(1, &platform, NULL);
if (err != CL_SUCCESS) {
cerr << "Error occured while executing clGetPlatformIDs" << endl;
return EXIT_FAILURE;
}
//Get devices number
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
if (err != CL_SUCCESS) {
cerr << "Error: Failed to create a device group:" << endl;
return EXIT_FAILURE;
}
cout << "NUM DEVICES =" << num_devices << endl;
devices = new cl_device_id[num_devices];
//Get all the GPU devices
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, num_devices, devices, NULL);
//Create one context for all the devices
context = clCreateContext(NULL, num_devices, devices, NULL, NULL, &err);
if (!context) {
cerr << "Error: Failed to create a compute context!" << endl;
return EXIT_FAILURE;
}
queues = new cl_command_queue[num_devices];
WGNumbers = new int[num_devices];
WGSizes = new int[num_devices];
for(int i = 0; i < num_devices; i++) {
//Create a command queue for every device
queues[i] = clCreateCommandQueue(context, devices[i], 0, &err);
if (!queues[i]) {
cerr << "Error: Failed to create a command commands!" << endl;
return EXIT_FAILURE;
}
cl_ulong temp;
clGetDeviceInfo(devices[i], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(temp), &temp, NULL);
WGSizes[i] = (int)temp;
clGetDeviceInfo(devices[i], CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(temp), &temp, NULL);
WGNumbers[i] = (int)temp;
}
//Translate kernel code into chars
int pl;
size_t program_length;
string path = "./kernel/kernel_av.cl";
char* cSourceCL = file_contents(path.c_str(), &pl);
program_length = (size_t)pl;
//Create a program
program = clCreateProgramWithSource(context, 1,
(const char **) &cSourceCL, &program_length, &err);
if (!program) {
cerr << "Error: Failed to create compute program!" << endl;
return EXIT_FAILURE;
}
//Create an executable
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS)
{
size_t len;
char buffer[2048];
cerr << "Error: Failed to build program executable!" << endl;
exit(1);
}
// Create the compute kernel in the program
kernel = clCreateKernel(program, "calculate2dim", &err);
if (err != CL_SUCCESS)
{
cerr << "Error: Failed to create compute kernel!" << endl;
exit(1);
}
}
执行内核的方法在这里:
int Parallel::executeAttachVectorsKernel(int* data1, int* data2, int* results, int vectors_num) {
cl_int err;
size_t global; // global domain size for our calculation
size_t local; // local domain size for our calculation
int partition = vectors_num/num_devices;
unsigned int count = partition;
input1 = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * count, NULL, NULL);
input2 = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * count, NULL, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * count, NULL, NULL);
if (!input1 || !input2 || !output) {
cerr << "Error: Failed to allocate device memory!" << endl;
exit(1);
}
int** data1_apart = new int*[num_devices];
int** data2_apart = new int*[num_devices];
int** results_apart = new int*[num_devices];
for(int i = 0; i < num_devices; i++) {
cout << "Executing parallel part on GPU " << i + 1 << endl;
cout << "Partition size = " << partition << endl;
data1_apart[i] = new int[partition];
data2_apart[i] = new int[partition];
results_apart[i] = new int[partition];
for(int j = i*partition, k = 0; k < partition; j++, k++) {
data1_apart[i][k] = data1[j];
data2_apart[i][k] = data2[j];
}
//Transfer the input vector into device memory
err = clEnqueueWriteBuffer(queues[i], input1,
CL_TRUE, 0, sizeof(int) * count,
data1_apart[i], 0, NULL, NULL);
err = clEnqueueWriteBuffer(queues[i], input2,
CL_TRUE, 0, sizeof(int) * count,
data2_apart[i], 0, NULL, NULL);
if (err != CL_SUCCESS)
{
cerr << "Error: Failed to write to source array!" << endl;
exit(1);
}
int parameter4 = count/WGNumbers[i];
//Set the arguments to the compute kernel
err = 0;
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input1);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &input2);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &output);
err |= clSetKernelArg(kernel, 3, sizeof(int), ¶meter4);
if (err != CL_SUCCESS)
{
cerr << "Error: Failed to set kernel arguments! " << err << endl;
exit(1);
}
global = WGNumbers[i];
local = WGSizes[i];
if(local > global) {
local = global;
}
cout << "global = " << global << " local = " << local << endl;
err = clEnqueueNDRangeKernel(queues[i], kernel,
1, NULL, &global, &local,
0, NULL, NULL);
if (err)
{
cerr << "Error: Failed to execute kernel!" << endl;
return EXIT_FAILURE;
}
}
for(int i = 0; i < num_devices; i++) {
//Wait for all commands to complete
clFinish(queues[i]);
//Read back the results from the device to verify the output
err = clEnqueueReadBuffer(queues[i], output,
CL_TRUE, 0, sizeof(int) * count,
results_apart[i], 0, NULL, NULL );
if (err != CL_SUCCESS)
{
cerr << "Error: Failed to read output array! " << err << endl;
exit(1);
}
for(int j = 0; j < partition; j++) {
results[i*partition + j] = results_apart[i][j];
}
delete [] data1_apart[i];
delete [] data2_apart[i];
delete [] results_apart[i];
}
clReleaseMemObject(input1);
clReleaseMemObject(input2);
clReleaseMemObject(output);
delete [] data1_apart;
delete [] data2_apart;
}
在将这个问题发布到 stackoverflow 之前,我已经与这个问题抗争了 2-3 周,现在我真的需要有人的帮助,所以我将非常感谢任何想法和答案!
最佳答案
这是我认为正在发生的事情。您为每个参与的 opencl 设备调用一次 clEnqueueNDRangeKernel。此时,没有一个内核开始执行,因为还没有调用 clFlush。接下来,您为每个队列创建一个 clFinish。第一个 clFinish 调用导致第一个排队的工作组运行。它还等待它完成。第一个工作组完成后,clFinish 会将控制权返回给您的应用程序。然后,您的应用会为下一个队列调用 clFinish。这会触发第二个工作灌浆运行,并等待它完成。所以工作是按顺序进行的。解决方案可能与在每次调用 clEnqueueNDRangeKernel 后立即调用 clFush 一样简单。这就是我的 AMD 系统的行为方式。我将很快发布一个工作示例。
关于opencl - 在多个 GPU 上运行 OpenCL 内核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16478968/
Intel、AMD 和 Khronos OpenCL 之间有什么区别。我对 OpenCL 完全陌生,想从它开始。我不知道在我的操作系统上安装哪个更好。 最佳答案 OpenCL 是 C 和 C++ 语言
我在这里的一篇文章中看到,我们可以从 OpenCL 内核调用函数。但是在我的情况下,我还需要并行化该复杂函数(由所有可用线程运行),所以我是否必须将该函数也设为内核并像从主内核中调用函数一样直接调
最近我看到一些开发板支持 OpenCL EP,例如 odroid XU。我知道的一件事是 OpenCL EP 适用于 ARM 处理器,但它与基于主要桌面的 OpenCL 在哪些特性上有所不同。 最佳答
我想知道在 OpenCL 中设置为内核函数的参数数量是否有任何限制。设置参数时出现 INVALID_ARG_INDEX 错误。我在内核函数中设置了 9 个参数。请在这方面帮助我。 最佳答案 您可以尝试
我对零拷贝的工作原理有点困惑。 1-要确认以下内容对应于opencl中的零拷贝。 ....................... . . . .
我是 OpenCL 的初学者,我很难理解某些东西。 我想改进主机和设备之间的图像传输。 我制定了一个计划以更好地了解我。 顶部:我现在拥有的 |底部:我想要的 HtD(主机到设备)和 DtH(设备到主
今天我又加了四个 __local变量到我的内核以转储中间结果。但是只需将另外四个变量添加到内核的签名并添加相应的内核参数就会将内核的所有输出呈现为“0”。没有一个 cl 函数返回错误代码。 我进一步尝
我知道工作项被分组到工作组中,并且您不能在工作组之外进行同步。 这是否意味着工作项是并行执行的? 如果是这样,使用 128 个工作项创建 1 个工作组是否可能/有效? 最佳答案 组内的工作项将一起安排
我相当确定经纱仅在 CUDA 中定义。但也许我错了。就 OpenCL 而言,什么是扭曲? 它与工作组不一样,是吗? 任何相关的反馈都受到高度赞赏。谢谢! 最佳答案 它没有在 OpenCL 标准中定义。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
在OpenCL中,我的理解是可以使用barrier()函数来同步工作组中的线程。我(通常)确实了解它们的用途以及何时使用它们。我还知道工作组中的所有线程都必须遇到障碍,否则会出现问题。然而,到目前为止
我的主板上有 Nvidia 显卡 (GeForce GT 640)。我已经在我的盒子上安装了 OpenCL。当我使用“clGetPlatformInfo(参数)”查询平台时,我看到以下输出:-#可用平
我目前正在构建一个 ray marcher 来查看像 mandelbox 等东西。它工作得很好。但是,在我当前的程序中,它使用每个 worker 作为从眼睛转换的光线。这意味着每个 worker 有大
我编写了两个不同的 openCl 内核,使用 nvidia profiler 获取了有关它们的一些信息,发现两者每个工作项都使用 63 个寄存器。 我尝试了一切我能想到的方法来降低这个数字(用 ush
我的主板上有 Nvidia 显卡 (GeForce GT 640)。我已经在我的盒子上安装了 OpenCL。当我使用“clGetPlatformInfo(参数)”查询平台时,我看到以下输出:-#可用平
我目前正在构建一个 ray marcher 来查看像 mandelbox 等东西。它工作得很好。但是,在我当前的程序中,它使用每个 worker 作为从眼睛转换的光线。这意味着每个 worker 有大
我正在尝试使用 OpenCL 加速一些计算,算法的一部分包括矩阵求逆。是否有任何开源库或免费可用的代码来计算用 OpenCL 或 CUDA 编写的矩阵的 lu 分解(lapack dgetrf 和 d
我正在尝试在 OpenCL 内核中使用递归。编译成功,但运行时出现编译错误,所以我想知道,由于 CUDA 现在支持动态并行,OpenCL 是否支持动态并行? 最佳答案 OpenCL 不支持递归。请参阅
考虑以下代码,它从大小为 size 的 double 组创建缓冲区内存对象: coef_mem = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM
OpenCL 中目标平台的示例是什么?例如,它是 Windows、Android、Mac 等操作系统,还是设备中的实际芯片? 最佳答案 OpenCL 平台本质上是一个 OpenCL 实现。它与操作系统
我是一名优秀的程序员,十分优秀!