如何使用 CUDA 将 std::vector<std::string> 复制到 GPU 设备
How to copy std::vector<std::string> to GPU device with CUDA
我正在从文件中读取行,并希望通过 GPU 对每一行执行一些计算。
我面临的问题是,到目前为止,我曾经复制一个常量大小的 int 数组,现在我有一个字符串向量,每个字符串的大小都不同。我正在使用:
std::vector<std::string> lines;
我使用了常量大小来复制数组。类似于:
err = cudaMemcpy(_devArr, tmp, count * sizeof(unsigned int) * 8, cudaMemcpyHostToDevice);
但我不确定我是否完全理解它如何与矢量一起工作。如何寻址和复制字符串向量?我可以以某种方式复制它并仍然像使用线程+块索引的数组一样访问它吗?
*使用最新的CUDA 10.2和CUDA RTX 2060显卡
您需要将字符串压平到包含所有字符串的连续内存块中。我的建议是用两个(总)块来完成,一个包含组合的字符串数据,另一个包含每个字符串的索引。
std::string combined; //Works perfectly fine so long as it is contiguously allocated
std::vector<size_t> indexes; //You *might* be able to use int instead of size_t to save space
for(std::string const& line : lines) {
combined += line;
indexes.emplace_back(combined.size());
}
/* If 'lines' initially consisted of ["Dog", "Cat", "Tree", "Yard"], 'combined' is now
* "DogCatTreeYard", and 'indexes' is now [3, 6, 10, 14].
*/
//I'm hoping I am writing these statements correctly; I don't specifically have CUDA experience
err = cudaMemcpy(_devArr, combined.data(), combined.size(), cudaMemcpyHostToDevice);
err = cudaMemcpy(_devArr2, indexes.data(), indexes.size() * sizeof(size_t), cudaMemcpyHostToDevice);
然后,在设备本身中,您将能够根据需要读取每个字符串。我不熟悉 CUDA 使用的语法,所以我打算用 OpenCL 语法来写,但是原则应该干净利落地直接转换为 CUDA;如果我错了,有人纠正我。
kernel void main_func(
global char * lines, //combined string data
global ulong * indexes, //indexes telling us the beginning and end of each string
ulong indexes_size, //number of strings being analyzed
global int * results //space to return results back to Host
) {
size_t id = get_global_id(0);//"Which String are we examining?"
if(id >= indexes_size) //Bounds Checking
return;
global char * string; //Beginning of the string
if(id == 0) //First String
string = lines;
else
string = (lines + indexes[id-1]);
global char * string_end = (lines + indexes[id]); //end of the string
for(; string != string_end; string++) {
if(*string == 'A') {
results[id] = 1; //We matched the criteria; we'll put a '1' for this string
return;
}
}
results[id] = 0; //We did not match. We'll put a '0' for this string
}
这段代码在初始字符串列表上执行的结果是,对于任何包含 A
的字符串,它会得到结果 1;如果不是,则结果为 0。这里的逻辑应该可以完全转移到 CUDA 使用的特定语法;如果不是,请告诉我。
我正在从文件中读取行,并希望通过 GPU 对每一行执行一些计算。
我面临的问题是,到目前为止,我曾经复制一个常量大小的 int 数组,现在我有一个字符串向量,每个字符串的大小都不同。我正在使用:
std::vector<std::string> lines;
我使用了常量大小来复制数组。类似于:
err = cudaMemcpy(_devArr, tmp, count * sizeof(unsigned int) * 8, cudaMemcpyHostToDevice);
但我不确定我是否完全理解它如何与矢量一起工作。如何寻址和复制字符串向量?我可以以某种方式复制它并仍然像使用线程+块索引的数组一样访问它吗?
*使用最新的CUDA 10.2和CUDA RTX 2060显卡
您需要将字符串压平到包含所有字符串的连续内存块中。我的建议是用两个(总)块来完成,一个包含组合的字符串数据,另一个包含每个字符串的索引。
std::string combined; //Works perfectly fine so long as it is contiguously allocated
std::vector<size_t> indexes; //You *might* be able to use int instead of size_t to save space
for(std::string const& line : lines) {
combined += line;
indexes.emplace_back(combined.size());
}
/* If 'lines' initially consisted of ["Dog", "Cat", "Tree", "Yard"], 'combined' is now
* "DogCatTreeYard", and 'indexes' is now [3, 6, 10, 14].
*/
//I'm hoping I am writing these statements correctly; I don't specifically have CUDA experience
err = cudaMemcpy(_devArr, combined.data(), combined.size(), cudaMemcpyHostToDevice);
err = cudaMemcpy(_devArr2, indexes.data(), indexes.size() * sizeof(size_t), cudaMemcpyHostToDevice);
然后,在设备本身中,您将能够根据需要读取每个字符串。我不熟悉 CUDA 使用的语法,所以我打算用 OpenCL 语法来写,但是原则应该干净利落地直接转换为 CUDA;如果我错了,有人纠正我。
kernel void main_func(
global char * lines, //combined string data
global ulong * indexes, //indexes telling us the beginning and end of each string
ulong indexes_size, //number of strings being analyzed
global int * results //space to return results back to Host
) {
size_t id = get_global_id(0);//"Which String are we examining?"
if(id >= indexes_size) //Bounds Checking
return;
global char * string; //Beginning of the string
if(id == 0) //First String
string = lines;
else
string = (lines + indexes[id-1]);
global char * string_end = (lines + indexes[id]); //end of the string
for(; string != string_end; string++) {
if(*string == 'A') {
results[id] = 1; //We matched the criteria; we'll put a '1' for this string
return;
}
}
results[id] = 0; //We did not match. We'll put a '0' for this string
}
这段代码在初始字符串列表上执行的结果是,对于任何包含 A
的字符串,它会得到结果 1;如果不是,则结果为 0。这里的逻辑应该可以完全转移到 CUDA 使用的特定语法;如果不是,请告诉我。