gpt4 book ai didi

c++ - 如何在内核中添加包含库的路径?

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:07 27 4
gpt4 key购买 nike

我找到了很多示例,如何在 C 上的主机中添加它,但是语法 C++C 不同。我想在我的内核文件中添加外部库。

这是我的代码的一部分:

std::ifstream sourceFile(name);
std::string sourceCode(
std::istreambuf_iterator<char>(sourceFile),
(std::istreambuf_iterator<char>()));
Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length() + 1));

// Make program of the source code in the context
Program program = Program(context, source);

// Build program for these specific devices


errcode = program.build(devices);
if (errcode != CL_SUCCESS)
{
cout << "There were error during build kernel code. Please, check program code. Errcode = " << errcode << "\n";
cout << "BUILD LOG: " + program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]) + "\n";
getchar();
return;
}


// Make kernel
Kernel kernel(program, "vector_add");

如何在这段代码中添加外部库的路径?

我认为在下一步中,需要在 Program::Sources 中添加第二个源。有人写过吗?

最佳答案

基于 OpenCL 1.2 specification (对于 2.0 也一样),

-I dir Add the directory dir to the list of directories to be searched for header files.

您不能将任何“.cl”包含到另一个“.cl”中,但您可以#include "Header.h" 尽可能多地包含一些函数或#define 等.

请注意,这些 header 中的代码必须采用类似于您的内核的 OpenCL C(除非使用 OpenCL 2.2,您可以在其中使用 OpenCL C++)。

最后,除非您使用 OpenCL 2.0 及更高版本,否则您不能在内核中调用内核函数,因此它们必须是普通函数 void foo()

所以在你的情况下,你可以这样做:

Program program = Program(context, source);
errcode = program.build(devices, "-I C:\Path\To\My\Include\Header);

然后回答你的第二个问题

In the next step i think, that need to add the second source in Program::Sources. Has anyone written this?

您可以 push_back sources.push_back({ kernelSource.c_str(), kernelSource.length() + 1 }); 将尽可能多的内核源代码放入单个 sources 您提供给程序的 vector 。但最后,您可以使用此程序创建多个内核,因为您需要为每个 cl::Kernel 提供一个内核名称,例如:

Kernel kernel_add(program, "vector_add");
Kernel kernel_sub(program, "vector_sub");

所以这意味着 kernel_addkernel_sub 是同时构建的,但最后它们是两个不同的内核。

关于c++ - 如何在内核中添加包含库的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52477819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com