Catastrophic error: cannot open source file "ranges" #include <ranges>

I am using the following version of nvc++,

nvc++ --version

nvc++ 23.5-0 64-bit target on x86-64 Linux -tp haswell
NVIDIA Compilers and Tools
Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

I got the following error messages when I compile a simple saxpy.cpp program.
nvc++ -stdpar -std=c++20 saxpy.cpp

“saxpy.cpp”, line 4: catastrophic error: cannot open source file “ranges

#include <ranges> // for std::views::iota_view

The full contents of the saxpy.cpp program is as follows.

#include <iostream>    // for std::cout
#include <vector>      // for std::vector
#include <execution>   // for std::execution
#include <ranges>      // for std::views::iota_view

int main () {
    float a = 2.0;
    std::vector<float> x = {1.0, 2.0, 3.0, 4.0};
    std::vector<float> y = {2.0, 4.0, 6.0, 8.0};

    auto is = std::views::iota(0, x.size());

    std::for_each(std::execution::par_unseq, is.begin(), is.end(),
        [&](int i) {
            y[i] = a * x[i] + y[i];
        });

    // Print the results
    for (const auto& val : y) {
        std::cout << "Element: " << val << std::endl;
    }

    return 0;
}

Question: Was include ranges ported in nvc++ compiler?

In order to be object compatible with g++, we need to use the GNU STL. Hence, features such as this will be dependent on the GNU version you have nvhpc configure to use. I believe ranges was added in GNU 10.1.

Besides the underlying g++ STL version, the code has a few other issues.

First “iota” has issues when the lower and upper bounds are of different types, hence I’d suggest casting x.size to an int.

Second, in order to offload to an accelerator, you’ll need to use capture by copy [=], rather than by reference [&]. By reference would pass the host stack address which is not accessible from the device. However given “y” in not mutable, it can’t be used in capture by copy. Instead, you’ll need use pointers to the vector data, rather than the vectors themselves, in the lambda.

Here’s a working version of the code using the GNU 10.1 toolchain’s STL:

 % cat saxpy.cpp
#include <iostream>    // for std::cout
#include <vector>      // for std::vector
#include <algorithm>
#include <execution>   // for std::execution
#include <ranges>      // for std::views::iota_view

int main () {
    float a = 2.0;
    std::vector<float> x = {1.0, 2.0, 3.0, 4.0};
    std::vector<float> y = {2.0, 4.0, 6.0, 8.0};
    float *yp=y.data();
    float *xp=x.data();

    auto is = std::views::iota(0, (int) x.size());

    std::for_each(std::execution::par_unseq, is.begin(), is.end(),
        [=](int i) {
            yp[i] = a * xp[i] + yp[i];
        });

    // Print the results
    for (const auto& val : y) {
        std::cout << "Element: " << val << std::endl;
    }

    return 0;
}
% nvc++ -stdpar --std=c++20 --gcc-toolchain=/home/sw/thirdparty/gcc/gcc-10.1.0/Linux_x86_64/bin/ saxpy.cpp
% a.out
Element: 4
Element: 8
Element: 12
Element: 16

Hope this helps,
Mat

Your reply is perfect. Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.