【问题标题】:Use memory region as stack space?使用内存区域作为堆栈空间?
【发布时间】:2012-05-25 22:42:22
【问题描述】:

在 Linux 中是否可以启动一个进程(例如使用execve)并使其使用特定的内存区域作为堆栈空间?

背景:

我有一个 C++ 程序和一个快速分配器,它给了我“快速内存”。我可以将它用于使用堆并在快速内存中创建它们的对象。美好的。但我也有很多变量存在于堆栈中。我怎样才能让他们也使用快速内存​​?

想法:实现一个“程序包装器”,分配快速内存,然后启动实际的主程序,传递一个指向快速内存的指针,程序将其用作堆栈。这可能吗?

[更新]

pthread 设置似乎有效。

【问题讨论】:

  • 我真的不认为您的 fast 分配器可以比堆栈分配更快。通常,堆栈分配每个函数需要几条指令。或者你的意思是内存比系统中其他任何地方的内存
  • @DavidRodríguez-dribeas 后者!它是快速的内存,而不是分配器
  • 你使用什么平台,有两种不同类型的 RAM?
  • 您希望多久将堆栈对象(而不是它们引用的内存)传输到 GPU?使用该分配器可加快向 GPU 的传输速度,但会将页面锁定在内存中,这将对系统的其余部分产生影响(例如,您的对象堆栈不能再被换出)你确定你想将它用于所有局部变量吗?
  • 我知道。它只是一个想法。问题是我有很多本地变量要转移。速度不是唯一的好处。您也可以获得异步传输。 (这就是我所追求的。)在我最终将它们复制到一个连续的缓冲区之前,我发现这个想法很整洁

标签: c++ c linux memory stack


【解决方案1】:

使用 pthreads,您可以为程序逻辑使用辅助线程,并使用 pthread_attr_setstack() 设置 堆栈地址:

NAME
       pthread_attr_setstack,  pthread_attr_getstack  -  set/get stack
       attributes in thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_setstack(pthread_attr_t *attr,
                                 void *stackaddr, size_t stacksize);

DESCRIPTION
       The pthread_attr_setstack() function sets the stack address and
       stack  size attributes of the thread attributes object referred
       to by attr to the values specified in stackaddr and  stacksize,
       respectively.   These  attributes specify the location and size
       of the stack that should be used by a thread  that  is  created
       using the thread attributes object attr.

       stackaddr should point to the lowest addressable byte of a buf‐
       fer of stacksize bytes that was allocated by the  caller.   The
       pages  of  the  allocated  buffer  should  be both readable and
       writable.

我不明白的是,您期望如何通过执行此类操作来提高性能(我假设您的“快速”内存的目的是提高性能)。

【讨论】:

  • 太棒了!不确定它是否适用于我的情况,会尝试一下。您的问题:快速分配器是 cudaHostAlloc 返回用于加速内存传输到 GPU 的页面锁定内存。因此,如果这可行,我可以加速堆栈变量的副本!
  • @Frank:很有趣。让我们知道进展如何。
猜你喜欢
  • 2016-06-29
  • 2011-03-29
  • 1970-01-01
  • 2016-10-04
  • 2017-07-04
  • 2016-03-25
  • 2015-09-22
  • 1970-01-01
  • 2014-05-13
相关资源
最近更新 更多