我有这个工作测试代码,它映射 std::vector 以进行卸载并检查计算是否正确:
#include <chrono>
#include <vector>
int main()
{
int vec_size = 3;
std::vector<double> vec_data{std::vector(vec_size, 2.0)};
for (auto &it : vec_data)
{
std::cout << it << " ";
}
double *data = vec_data.data();
auto startTime = std::chrono::high_resolution_clock::now();
#pragma omp target data map(tofrom : data[ : vec_data.size()])
#pragma omp target teams distribute parallel for
for (int it = 0; it < vec_size; ++it)
{
data[it] = data[it] + data[it];
}
auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "GPU: " << std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(endTime - startTime).count() << " ms ; " << std::endl;
for (auto &it : vec_data)
{
std::cout << it << " ";
}
return 0;
}
我想使用迭代器编写 for 循环,例如:
for (auto it = data.begin(); it != data.end(); ++it)
{
it = it + it;
}
但我无法找出正确的语法。你能帮我吗?
auto startTime = std::chrono::high_resolution_clock::now();
#pragma omp target data map(tofrom : data[ : vec_data.size()])
#pragma omp target teams distribute parallel for
for (auto it = vec_data.begin(); it != vec_data.end(); ++it)
{
*it = *it + *it;
}
auto endTime = std::chrono::high_resolution_clock::now();