Hi all,
I got an error in using stdpar on gpu. The same error was mentioned in the topic
But I did not get clue from it to fix my problem.
I am writing a simple test code to trying using stdpar in nvc++.
The code is simply relaxation for 2D Laplace’s equation, ie, value on each grid point the the average of the neighboring 4 grid points. Here is my code:
#include <iostream>
#include <vector>
#include <ranges>
#include <execution>
#include <fstream>
#include <algorithm>
#include <experimental/mdspan>
auto main() -> int {
int size = 50; // array size= size*size
int niter=100;
std::vector<double> A(size*size, 0.0);
auto A_v = std::experimental::mdspan (A.data(), size, size); //create view
// Some initialization code
// Set one side of the rectangular grid in A to unity
for (int i = 0; i < size; ++i) {
A_v(i,0) = 1.0; // Set to unity on one side
}
std::vector<double> B(A);
auto B_v = std::experimental::mdspan (B.data(), size, size); //create view
//view for using for_each
auto v = std::ranges::views::cartesian_product(
std::ranges::views::iota(1, size - 1),
std::ranges::views::iota(1, size - 1));
//iteration
for (int i=0; i<niter; i++) {
std::for_each(std::execution::par, std::begin(v), std::end(v),
[=](auto idx) {
auto [i, j] = idx;
B_v(i,j) = 0.25*(A_v(i-1,j) + A_v(i+1,j) + A_v(i,j-1) + A_v(i,j+1));
});
std::swap(A_v,B_v);
}
// Open file for writing
std::ofstream outputFile("output.txt");
if (!outputFile.is_open()) {
std::cerr << "Error: Unable to open output file!" << std::endl;
return 1;
}
// Write contents of A to file
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
outputFile << i << " " << j << " " << A_v(i,j) << std::endl;
}
outputFile << std::endl; // Skip a line after each row
}
// Close file
outputFile.close();
// Confirmation message
std::cout << "Output file 'output.txt' generated successfully." << std::endl;
}
When I compiled with nvc++ -std=c++23, I could run the exe and got reasonable result.
However, when I tried run on gpu, I compiled with nvc++ -std=c++23 -stdpar=gpu -gpu=cc89, I got runtime error:
terminate called after throwing an instance of ‘thrust::THRUST_200802_SM_89_NVHPC_NS::system::system_error’
what(): parallel_for: failed to synchronize: cudaErrorIllegalAddress: an illegal memory access was encountered
Aborted (core dumped)
I am new to nvc++ and I don’t know what I am missing, even for such a short code.
Thanks in advance for anyone can help.