C++ 如何在C++;还是OpenCV子矩阵?

C++ 如何在C++;还是OpenCV子矩阵?,c++,opencv,openacc,pgi,C++,Opencv,Openacc,Pgi,我有以下代码 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*)

我有以下代码

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;
}
如果我注释掉
mat(cv::Rect(I,j,(4-0),(4-0))
并将部分保留在
子向量[5:10][25:100]
之后,或者如果我取消注释
mat(cv::Rect(I,j,(4-0),(4-0))
并注释
子向量[5:10][25:100]

我怎样才能解决这个问题

编辑

为了简化此过程,我提供了两个单独的代码,以及它们给出的错误:

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;
}
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
#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;
}
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
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 );

    #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;
}
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
#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;
}
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“例程”指令装饰被调用的例程

由于您提供的信息不完整,因此很难确切知道如何修复代码。错误消息说缺少哪些例程?您能提供完整的复制示例吗

更新后编辑

调用“cv::Mat::Mat(const cv::Mat&,const cv::Rect&)”,不带 行政协调会例行资料

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

69,加速器限制:调用“std::\抛出\长度\错误(const “字符*)”没有acc例行程序信息

对于设备代码,异常处理不可用,因为需要在主机上捕获,并且目前没有支持此操作的方法


在某些情况下,您可以通过标志“-no_exceptions”禁用异常来解决此问题,但在这种情况下,如果禁用异常,OpenCV似乎会抱怨。因此,这里最好避免在设备上使用矢量。

我在
编辑中提供了更多详细信息