【发布时间】:2025-12-02 09:50:01
【问题描述】:
我最近使用 Struct 实现(测试)OpenCL,使用写入内核的简单函数来携带和更新 C++ 类对象,我沮丧地发现,在没有内核的情况下使用简单的 for 循环处理相同的函数是实际上更快。
这里是核函数:
__kernel void function_x_y_(__global myclass_* input,long n)
{
int gid = get_global_id(0);
if(gid<n)
input[gid].valuez = input[gid].valuey * input[gid].valuex * 8736;
}
这是for循环:
for(int i=0;i<100;i++){
thisclass[i].function_x_y();
}
和类函数:
void function_x_y(){
valuez = valuex * valuey;
}
我在两个进程上都运行了一个时钟:
cout<<"Run function in serial\n";
startTime = clock();
for(int i=0;i<100;i++){
thisclass[i].function_x_y();
}
endTime = clock();
cout << "It took (serial) " << (endTime -startTime) / (CLOCKS_PER_SEC / 1000000) << " ms. " << endl;
cout<<"Run function in parallel using struct to write to object\n";
init_ocl();
startTime = clock();
load_kernel_from_struct("function_x_y_",p_struct,100); //Loads function and variables into opencl
endTime = clock();
cout << "It took (parallel) " << (endTime -startTime) / (CLOCKS_PER_SEC / 1000000 ) << " ms. " << endl;
输出:
Run function in serial
It took (serial) 5 ms.
Run function in parallel using struct to write to object
It took (parallel) 159010 ms.
我正在使用 Andreas Kloecker 的 cl-helper.c
我不明白这应该更快。欢迎任何帮助或建议。
有更准确的速度测试吗?这可能是因为初始化分配内存并将数据传输到内核需要时间吗?
必须有一种方法来确保它运行得更快,难道是我必须在运行该函数之前传输和初始化所有内容吗?
谢谢, 百字节。
【问题讨论】:
-
这取决于 5000000 次迭代的迭代次数:它需要(串行)7133676 毫秒。使用 struct 并行运行函数以写入对象内核 Function:functions_.cl :function_x_y_ 它花了(并行)4753831 ms。我正在使用从linkhere 获取的函数。
-
İts 就像铲一粒盐并与镊子的性能进行比较。最佳小工作负载延迟是 cpu 的