OpenMP GPU 卸载向量迭代器

问题描述 投票:0回答:1

我有这个工作测试代码,它映射 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;
        }

但我无法找出正确的语法。你能帮我吗?

loops gpu openmp stdvector offloading
1个回答
0
投票
    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();
© www.soinside.com 2019 - 2024. All rights reserved.