我目前正在开发一个 CUDA 应用程序,如果处理的数据足够大,它将使用尽可能多的全局设备内存 (VRAM)。我分配的是一个 3D 卷cudaMalloc3d,所以我使用的内存必须是连续的。为此,我尝试使用该函数检索空闲设备内存的数量,cudaMemGetInfo然后分配尽可能多的空闲内存。但是,这似乎不起作用。尝试分配该数量的内存时,我仍然遇到错误。
现在,我的问题是是否有办法检索我可以连续分配的最大设备内存量。
一种选择是试错法,我反复减少我尝试分配的数量,直到分配成功。但是,我不太喜欢这个想法。
背景:我有一个程序可以在 GPU 上进行锥束 CT 重建。这些卷可能会变得非常大,因此我在必要时将它们分成块。因此,我必须知道一个块最多可以有多大才能仍然适合全局设备内存。
我刚编译了一个我在Windows for Linux下工作的项目,发现它在某个时刻挂起.由于我使用std :: async和std :: mutex,我的第一个假设是,它可能是一个死锁问题.但是,我想知道为什么它在Windows上运行良好.这是代码:
void BorderExtractor::preprocessImageAsync(const PreprocessingSettings& settings) {
_preprocessingMutex.lock();
if (!_preprocessingActive) {
_preprocessingActive = true;
std::async(std::launch::async, &BorderExtractor::preprocessImage, this, settings);
//this point is never reached on linux
_preprocessingUpToDate = true;
} else {
_cachedSettings = settings;
_preprocessingUpToDate = false;
}
_preprocessingMutex.unlock();
}
Run Code Online (Sandbox Code Playgroud)
这是在Linux下永远不会返回的函数.它一直运行到异步调用,然后它就停止了.几乎看起来好像该函数没有异步启动并且程序等待它返回,什么不起作用,因为另一个函数将尝试锁定相同的互斥锁.
这是异步调用的函数:
void BorderExtractor::preprocessImage(PreprocessingSettings settings) {
//here some image processing stuff is done
_preprocessingMutex.lock();
//this point is never reached on linux
if (!_preprocessingUpToDate) {
_preprocessingUpToDate = true;
_preprocessingMutex.unlock();
std::async(std::launch::async, &BorderExtractor::preprocessImage, this, _cachedSettings);
} else { …Run Code Online (Sandbox Code Playgroud) 我想并行运行多个GPU上的内核.为此,我在设备之间切换cudaSetDevice(),然后在相应的设备中启动我的内核.现在,通常一个流中的所有调用都是顺序执行的,如果它们必须并行执行,则必须使用不同的流.在使用不同的设备时也是这种情况,或者在这种情况下我是否可以在两台设备上的默认流上运行内核调用,它们仍将并行运行?
在以前的 OpenCV 版本中,有一个函数Stream::enqueueUpload可以用来将数据异步上传到 GPU CudaMem(比较:如何在 OpenCV 中使用 gpu::Stream?)。但是,这个函数在 OpenCV 3 中已经不存在了。CudaMemclass 也没有了,但似乎已经被HostMemclass取代了。
谁能告诉我如何在 OpenCV 3 中执行异步上传?
我目前正在试图找出完美转发的工作原理.我已经编写了一段示例代码,据我所知,它执行完美的转发,因为它应该是:
void whatIsIt(std::vector<int>&& variable)
{
std::cout << "rvalue" << std::endl;
}
void whatIsIt(std::vector<int>& variable)
{
std::cout << "lvalue" << std::endl;
}
template <class T>
void performPerfectForwarding(T&& variable)
{
whatIsIt(std::forward<T>(variable));
}
int main(int argc, char** argv)
{
std::vector<int> lvalue;
performPerfectForwarding(lvalue);
performPerfectForwarding(std::move(lvalue));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
performPerfectForwarding使用左值调用函数使其调用相应的重载,whatIsIt并且同样适用于使用右值调用它.因此,此代码生成的输出为:
lvalue
rvalue
Run Code Online (Sandbox Code Playgroud)
但是,我问自己该函数的以下版本performPerfectForwarding会做什么:
1.
template <class T>
void performPerfectForwarding(T& variable)
{
whatIsIt(std::forward<T>(variable));
}
Run Code Online (Sandbox Code Playgroud)
2.
template <class T>
void performPerfectForwarding(T variable)
{
whatIsIt(std::forward<T>(variable));
}
Run Code Online (Sandbox Code Playgroud)
他们都输出:
rvalue
rvalue
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是:这两个替代版本会做什么,并且它们在任何可能的背景下都有意义吗?与上述"正确"版本中的参考折叠规则相比,参考折叠规则如何适用于这些情况?
我有一个QMenu被分配给a QPushButton并在按下按钮时弹出.它看起来像这样:

但是,我希望菜单的宽度等于按钮的宽度,因为它现在看起来很丑,而且它也很难打.
我可以通过使用设置菜单的固定宽度setMinimumWidth,但是我必须手动输入宽度值.按钮的大小可能在创建后不会改变,因此菜单的最小宽度不一定必须动态调整.但我不想硬编码一个值.
这两个小部件都是在我的窗口的构造函数中创建的,它也是从窗口派生的QWidget.我可以调用sizeHint我的按钮的方法,但只返回它的"最佳"宽度.它后来在布局中的宽度比这宽.
现在,调用width按钮上的方法也不会返回它在布局中应该具有的正确宽度,但是宽度比按钮的宽度宽,即使我在窗口的所有子窗口小部件都已分配给它之后调用它他们的布局,窗口的布局已经设置好了.我不确定为什么会这样; 也许是因为在窗口的构造函数中,小部件还不知道它将显示的大小?
现在,有没有办法让菜单具有按钮的宽度?或者我可以在哪个时间点调用width按钮上的方法,它会返回按钮实际显示的宽度?该按钮也设置为parent菜单.有没有办法让小部件具有其父级的宽度?
这里有一些示例代码:
MainInterface::MainInterface(QWidget *parent){
//some code
_moreButton = new QPushButton(tr("&More..."));
_moreMenu = new QMenu(_moreButton);
_cmdAction = new QAction(tr("Save as Batch File"), this);
_moreMenu->addAction(_cmdAction);
_moreButton->setMenu(_moreMenu);
//some more code
_layout->addWidget(_moreButton);
setLayout(_layout);
}
Run Code Online (Sandbox Code Playgroud) 如果CMake中的源路径或二进制路径包含字符序列"++"(不带引号),则在尝试为OpenCV 3.1创建项目时会出现CMake错误:
CMake Error at cmake/OpenCVUtils.cmake:76 (if):
if given arguments:
"G:/Desktop/C++ projects/project" "MATCHES" "^G:/Desktop/C++ projects/sources" "OR" "G:/Desktop/C++ projects/project" "MATCHES" "^G:/Desktop/C++ projects/project"
Regular expression "^G:/Desktop/C++ projects/sources" cannot compile
Call Stack (most recent call first):
CMakeLists.txt:437 (ocv_include_directories)
Run Code Online (Sandbox Code Playgroud)
显然,OpenCVUtils中的这一行会导致问题:
if("${__abs_dir}" MATCHES "^${OpenCV_SOURCE_DIR}" OR "${__abs_dir}" MATCHES "^${OpenCV_BINARY_DIR}")
我注意到了这个问题,因为我有一个名为"C++ Projects"的文件夹,我保存了C++项目和库.有谁知道,为什么序列导致问题,如果有一个快速的方法来解决这个问题?我还将此报告为OpenCV错误跟踪器中的错误.
我正在使用 Sublime Text 和 SFTP 插件。对于我的项目,我设置了一个 sftp-config.json 文件,指定 FTP 连接所需的详细信息。只要我使用普通的用户/密码登录,一切正常,我可以建立连接。
不过,我随后尝试使用 ssh 密钥。我的 ssh 密钥文件是C:/Users/<username>/.ssh/id_rsa. 当我在 power shell 上手动运行 sftp 时,成功建立了连接。
PS C:\> sftp -i C:/Users/<username>/.ssh/id_rsa <username>@<host>
Connected to <username>@<host>.
Run Code Online (Sandbox Code Playgroud)
当然,username并host替换为我的实际凭据。
现在,当尝试使用 Sublime Text 和 SFTP 插件执行相同操作时,它不起作用。相反,当连接时,系统会提示我输入密码,就像不使用 ssh 密钥时一样。我还确保它不是它所要求的密钥的密码(该密钥当前没有密码),并且仅按 Enter 键而不输入密码不会导致成功。在我看来,该插件只是忽略了我提供了 SSH 密钥的事实:如果我输入FTP 服务器的密码,它确实会成功连接,但这当然不是我想要的。
我想我一定是配置文件出错了,但我不知道它应该是什么。我还在线阅读了文档,它没有提到任何必要的额外步骤。这是我的配置文件(当然有某些替换):
{
"type": "sftp",
"save_before_upload": true,
"upload_on_save": false,
"sync_down_on_open": false,
"sync_skip_deletes": false,
"sync_same_age": true,
"confirm_downloads": false,
"confirm_sync": true,
"confirm_overwrite_newer": false,
"host": "<host>",
"user": "<username>", …Run Code Online (Sandbox Code Playgroud) 我在使用我创建的"画布"类中遇到了一个问题,这是一个扩展的类JPanel,用于绘制动画环形图.此图表使用a MouseListener来获取点击事件.
问题是鼠标位置似乎不准确,这意味着它似乎不是相对于"画布"而是相对于窗口(在左上角,我得到约30px的y coord).
这是我的代码:
我创建了一个类,它扩展了JPanel并且有一个BufferedImage作为成员.
public class Canvas extends JPanel {
public BufferedImage buf;
private RingChart _parent;
public Canvas(int width, int height, RingChart parent){
buf = new BufferedImage(width, height, 1);
...
Run Code Online (Sandbox Code Playgroud)
在paint组件方法中,我只绘制缓冲图像,因此我可以通过在缓冲图像上绘制来从"外部"绘制画布,这是公共的.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(buf, null, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
现在有一个类RingChart包含一个"画布":
public class RingChart extends JFrame{
public Canvas c;
...
Run Code Online (Sandbox Code Playgroud)
我从canvas类的bufferedImage创建了一个Graphics2D.这个g2d用于绘画:
public RingChart(){
c = new Canvas(1500,980,this);
add(c);
setSize(1500, 1000);
setTitle("Hans");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2d = (Graphics2D)c.buf.createGraphics();
...
Run Code Online (Sandbox Code Playgroud)
我现在想要实现的是一个鼠标监听器,它监听画布上发生的鼠标事件.因此,当用户点击画布时,我可以通过事件变量检索他在画布上单击的位置.
所以我创建了一个鼠标监听器:
class …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一种有效的方法来将图像(QImage - Qt5.3)分配给我的类的成员变量:
void ImageView::setImage(QImage&& image){
std::cout << &image << std::endl;
_image = image;
//_image = std::move(image);
std::cout << &_image << std::endl;
_imageAssigned = true;
updateResizedImage();
update();
}
Run Code Online (Sandbox Code Playgroud)
我可能会使用两行代码,第一行是_image = image;,第二行是_image = std::move(image);. 现在我认为第一行应该完全没问题,并且实际上应该使用 QImage 的 = 运算符重载,该重载采用右值引用,因为 image 已经是右值引用。然而,Visual Studio 的工具提示显示使用了普通的 = 运算符。如果我使用第二行,工具提示将显示使用右值的重载。现在我的问题是为什么会这样。第一行还不够吗?对我来说,对已经是右值引用的变量调用 move 似乎是多余的。
我尝试通过输出指针来测试这一点(如代码中所示)。现在,无论我使用哪一行,指针都是不同的。我觉得这很令人困惑。看来,我的代码没有按照我想象的方式运行。谁能告诉我我做错了什么?
感谢您的帮助
我试图用缓冲区将整个文件读入缓冲区 std::ifstream该失败没有明显的原因,所以我构建了一个演示问题的最小代码示例:
std::vector<char> vec;
vec.resize(1000);
std::ifstream file("G:/Pictures/Webcam/Snapshot_20110209.jpg");
file.exceptions(std::ifstream::badbit | std::ifstream::failbit | std::ifstream::eofbit);
std::cout << file.good() << std::endl;
try {
file.read(vec.data(), 100);
} catch (std::ios_base::failure f) {
std::cout << f.what() << " Characters extracted: " << file.gcount() << std::endl;
} catch (...) {
std::cout << "Some other error" << std::endl;
}
std::cout << "Done" << std::endl;
file.close();
Run Code Online (Sandbox Code Playgroud)
我正在尝试读取的文件大小为48kb,因此读取100个字节应该不是问题.1000字节的缓冲区很大,所以也应该没问题.现在,发生的是流只读取61个字节,然后设置failbit.生成的输出如下:
1
ios_base::failbit set: iostream stream error Characters extracted: 61
Done
Run Code Online (Sandbox Code Playgroud)
因此,出于某种原因,故障位置设置在61个字节之后.如果我读取少于61个字节,它的工作原理.如果我尝试阅读更多,它也会在61处失败.我也尝试过类似大小,同样问题的其他文件.一些完全不同的不同大小的文件显示相同的行为,但在166字节之后.
现在,如果我使用Qt的QFile类来读取数据,一切正常,我可以读取完整的文件.代码如下所示:
QFile file(path);
std::vector<char> buffer;
buffer.resize(file.size());
if (!file.open(QIODevice::ReadOnly)) return; …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来检索C++中所有用户开始菜单目录的路径.我只能得到当前用户之一(使用Qt):
QString startMenuPath = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).at(0);
Run Code Online (Sandbox Code Playgroud)
但是,Qt不允许为所有用户检索一个.据我所知,也没有包含该路径的环境变量,我可以阅读.