gpt4 book ai didi

c++ - 如何在 C++ 或 OpenCV SubMatrix 中的 2D 子 vector 上使用 OpenACC?

转载 作者:行者123 更新时间:2023-12-02 17:20:01 34 4
gpt4 key购买 nike

我有以下代码

int main(int argc, char** argv )
{
std::cout<<"running Lenna..\n";
cv::Mat mat = imread("lena.bmp", cv::IMREAD_GRAYSCALE );

//convert to vec
std::vector<double> BWvec;
BWvec.assign((double*)mat.data, (double*)mat.data + mat.total());
std::vector < std::vector<double>> vec2D;
for (int i = 0; i < mat.rows; i++) {
auto first = BWvec.begin() + (mat.rows * i);
auto last = BWvec.begin() + (mat.rows * i) + mat.rows;
std::vector<double> vec0(first, last);
vec2D.push_back(vec0);
}

//#pragma acc parallel loop
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
mat(cv::Rect(i,j, (4 - 0), (4 - 0)));

//sub-vector[5:10][25:100]:
std::vector<std::vector<double>> sub_vector;
sub_vector.reserve(5);
for (std::size_t k = 5; k < 10; ++k) {
sub_vector.emplace_back(vec2D[k+i].begin() + 25, vec2D[k+i].begin() + 100);
}
}
}

return 0;
}
当我输入 pgc++ -fast -ta=nvidia:cuda9.2,managed -Minfo=accel -o lenna lenna.cpp -std=c++11 pkg-config --cflags --libs opencv -lgomp && ./lenna ,它可以正常工作,但是当我取消注释 #pragma acc parallel loop ,我得到错误
procedures called in a compute region must have acc routine information
accelerator region ignored, accelerator restriction .. no acc routine information
如果我注释掉 mat(cv::Rect(i,j,(4-0),(4-0))),我也会收到此错误并在 sub-vector[5:10][25:100] 之后留下部分,或者如果我取消注释 mat(cv::Rect(i,j,(4-0),(4-0)))并评论 sub-vector[5:10][25:100] 之后的部分
我怎样才能解决这个问题?
编辑
为了使这更简单,我提供了 2 个单独的代码,以及它们给出的错误: lenna1.cpp :
#include <stdio.h>
#include <cmath>
#include <omp.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

//pgc++ -fast -ta=nvidia:cuda9.2,managed -Minfo=accel -o lenna lenna.cpp -std=c++11 `pkg-config --cflags --libs opencv` -lgomp && ./lenna

int main(int argc, char** argv )
{
std::cout<<"running Lenna..\n";
cv::Mat mat = imread("lena.bmp", cv::IMREAD_GRAYSCALE );

#pragma acc parallel loop
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
mat(cv::Rect(i, j, (4 - 0), (4 - 0)));
}
}
return 0;
}
来自 lenna1.cpp 的错误:
pgc++ -fast -ta=nvidia:cuda9.2,managed -Minfo=accel -o lenna1 lenna1.cpp -std=c++11 `pkg-config --cflags --libs opencv` -lgomp && ./lenna1
lenna1.cpp:
"lenna1.cpp", line 23: warning: last line of file ends without a newline
}
^

PGCC-S-0155-Procedures called in a compute region must have acc routine information: cv::Mat::Mat(const cv::Mat&, const cv::Rect_<int> &) (lenna1.cpp: 379)
PGCC-S-0155-Accelerator region ignored; see -Minfo messages (lenna1.cpp: 14)
main:
14, Accelerator region ignored
379, Accelerator restriction: call to 'cv::Mat::Mat(const cv::Mat&, const cv::Rect_<int> &)' with no acc routine information
PGCC/x86-64 Linux 19.10-0: compilation completed with severe errors
lenna2.cpp :
#include <stdio.h>
#include <cmath>
#include <omp.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

//pgc++ -fast -ta=nvidia:cuda9.2,managed -Minfo=accel -o lenna lenna.cpp -std=c++11 `pkg-config --cflags --libs opencv` -lgomp && ./lenna

int main(int argc, char** argv )
{
std::cout<<"running Lenna..\n";
cv::Mat mat = imread("lena.bmp", cv::IMREAD_GRAYSCALE );

//convert to vec
std::vector<double> BWvec;
BWvec.assign((double*)mat.data, (double*)mat.data + mat.total());
std::vector < std::vector<double>> vec2D;
for (int i = 0; i < mat.rows; i++) {
auto first = BWvec.begin() + (mat.rows * i);
auto last = BWvec.begin() + (mat.rows * i) + mat.rows;
std::vector<double> vec0(first, last);
vec2D.push_back(vec0);
}

#pragma acc parallel loop
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
//sub-vector[5:10][25:100]:
std::vector<std::vector<double>> sub_vector;
sub_vector.reserve(5);
for (std::size_t i = 5; i < 10; ++i) {
sub_vector.emplace_back(vec2D[i].begin() + 25, vec2D[i].begin() + 100);
}
}
}

return 0;
}
来自 lenna2.cpp 的错误:
pgc++ -fast -ta=nvidia:cuda9.2,managed -Minfo=accel -o lenna2 lenna2.cpp -std=c++11 `pkg-config --cflags --libs opencv` -lgomp && ./lenna2
lenna2.cpp:
"lenna2.cpp", line 40: warning: last line of file ends without a newline
}
^

operator new (unsigned long, void *):
4, include "opencv.hpp"
47, include "core.hpp"
56, include "algorithm"
10, include "algorithm"
62, include "stl_algo.h"
62, include "stl_tempbuf.h"
60, include "stl_construct.h"
59, include "new"
130, Generating implicit acc routine seq
Generating acc routine seq
Generating Tesla code
operator delete (void *, void *):
4, include "opencv.hpp"
47, include "core.hpp"
56, include "algorithm"
10, include "algorithm"
62, include "stl_algo.h"
62, include "stl_tempbuf.h"
60, include "stl_construct.h"
59, include "new"
135, Generating implicit acc routine seq
Generating acc routine seq
Generating Tesla code
PGCC-S-0155-Procedures called in a compute region must have acc routine information: std::__throw_length_error(const char *) (lenna2.cpp: 69)
PGCC-S-0155-Accelerator region ignored; see -Minfo messages (lenna2.cpp: 25)
main:
25, Accelerator region ignored
69, Accelerator restriction: call to 'std::__throw_length_error(const char *)' with no acc routine information
PGCC/x86-64 Linux 19.10-0: compilation completed with severe errors

最佳答案

为了从设备调用例程和方法,需要有这些例程的设备版本。在已知调用例程的定义的情况下(例如使用模板),编译器将尝试隐式生成设备例程。否则,程序员有责任用 OpenACC“例程”指令装饰被调用的例程。
由于您提供的信息不完整,因此很难确切知道如何修复您的代码。错误消息说缺少哪些例程?你能提供一个完整的复制例子吗?
更新后编辑。

call to 'cv::Mat::Mat(const cv::Mat&, const cv::Rect_ &)' with noacc routine information


看起来“Mat”类型的构造函数没有设备可调用版本。虽然我不熟悉 OpenCV 的结构,但我假设这不是模板化的,也不是头文件中包含的构造函数的定义,因此编译器可以隐式创建它。您需要将例程指令添加到您希望从设备代码调用的 OpenCV 部分,或者如果有 CUDA 设备例程,您可以通过使用带有绑定(bind)子句的 OpenACC 例程指令来调用它们。

69, Accelerator restriction: call to 'std::__throw_length_error(constchar *)' with no acc routine information


异常处理不适用于设备代码,因为它需要在主机上捕获,而目前没有办法支持这一点。
在某些情况下,您可以通过标志“--no_exceptions”禁用异常来解决此问题,但在这种情况下,如果禁用异常,OpenCV 会提示。因此,最好避免在此处在设备上使用 vector 。

关于c++ - 如何在 C++ 或 OpenCV SubMatrix 中的 2D 子 vector 上使用 OpenACC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62668105/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com