假設我在 openCL 中有一個程式:
...
const unsigned char* binaries;
size_t binaries_size;
assign_binaries(&binaries, &binaries_size); // my assign function, assigns bin and bin_size
std::vector<std::vector<unsigned char>> Binaries;
cl::Program prog = cl::Program(context, device_list, Binaries, &binary_status, &err);
...
我將如何去推動
binaries
和
binaries_size
進入
Binaries
?
在OpenCL 檔案中,它說:
Binaries: A vector of pairs of a pointer to a binary object and its length.
但是,這對我來說沒有意義,因為存在明顯的型別不匹配。
uj5u.com熱心網友回復:
我將如何去推動
binaries和
binaries_size進入
Binaries?
有很多方法可以實作這一點。
TL;DR:實作 OP 意圖的最緊湊的代碼可能是:
cl::Program prog
= cl::Program(context, device_list,
{ std::vector<unsigned char>(binaries, binaries binaries_size) },
&binary_status, &err);
一個更長的故事:
一個 MCVE 來演示兩個:
#include <iomanip>
#include <iostream>
#include <vector>
// the OpenCL typedef
using Binaries = std::vector<std::vector<unsigned char>>;
void print(const Binaries& binaries)
{
std::cout << "Binaries: [" << binaries.size() << "]: {\n";
for (const std::vector<unsigned char>& binary : binaries) {
std::cout << " [" << binary.size() << "]: {" ;
const char* sep = " ";
for (const unsigned char value : binary) {
std::cout << sep << "0x"
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned)value;
sep = ", ";
}
std::cout << " }\n";
}
std::cout << "}\n";
}
int main()
{
// sample data
const unsigned char data[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
// OPs initial exposure of data
const unsigned char *binaries = data;
const size_t binaries_size = std::size(data);
// filling a Binaries var.:
Binaries vecBinaries1;
vecBinaries1.push_back(
std::vector<unsigned char>(binaries, binaries binaries_size));
print(vecBinaries1);
// or
const Binaries vecBinaries2(1, std::vector<unsigned char>(binaries, binaries binaries_size));
print(vecBinaries2);
}
輸出:
Binaries: [1]: {
[8]: { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
}
Binaries: [1]: {
[8]: { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
}
請注意,資料是在 中準備好的binaries,然后復制到 的建構式std::vector<unsigned char>(binaries, binaries binaries_size)中std::vector。
std::vector提供了多種建構式。在這種情況下,使用帶有first和last迭代器的構造函式:
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
(這是(5)鏈接檔案中的味道。)
但是,這可能是不必要的復制步驟。std::vector<unsigned char>從一開始就在 a 中提供資料可用于擺脫這種情況。
所述的push_back()可以與移動語意(其防止2中可使用第二不必要的深拷貝)。(這是(2)鏈接檔案中的風格。)如果直接將內部向量的建構式作為引數傳遞,則會產生一個 RValue,它將自動觸發移動語意。
If there is an intermediate storage of the inner vector (e.g. in a local variable), the move semantic still can be forced with std::move():
std::vector<unsigned char> vecBinary3(binaries, binaries binaries_size);
Binaries vecBinaries3;
vecBinaries3.push_back(std::move(vecBinary3));
print(vecBinaries3);
Output:
Binaries: [1]: {
[8]: { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
}
Live Demo on coliru
I also added brace initialization (which in this case is direct-list-initialization).
const Binaries vecBinaries4 {
std::vector<unsigned char>(binaries, binaries binaries_size)
};
print(vecBinaries4);
Output:
Binaries: [1]: {
[8]: { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
}
(I must admit, I still consider brace init. a little bit scaring. Hence, I struggled to mention it at all but I should for completeness.)
In certain cases, e.g. if you have a vector of integrals, you have to be careful to distinguish the constructor with size n and an initial fill value from the constructor with an initialization list of two values.
For both, you have to provide two integral values, but the kind and number of brackets decide which constructor is used. This can result in confusion easily.
Example:
#include <iostream>
#include <vector>
void print(const std::vector<int>& values)
{
std::cout << '{';
const char* sep = " ";
for (const int value : values) {
std::cout << sep << value;
sep = ", ";
}
std::cout << " }\n";
}
int main()
{
// sample 1:
print({ 1, 2 });
// sample 2:
print(std::vector<int>(1, 2));
// sample 3:
print(std::vector<int>{ 1, 2 });
// sample 4:
print(std::vector<int>({ 1, 2 }));
}
Output:
{ 1, 2 }
{ 2 }
{ 1, 2 }
{ 1, 2 }
Live Demo on coliru
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322672.html
