将内存区域用作堆栈空间?

7
在Linux中,可以通过启动一个进程(例如使用execve)并使其使用特定的内存区域作为堆栈空间。背景:我有一个C++程序和一个快速分配器,可以提供“快速内存”。我可以将使用堆的对象创建在快速内存中。好的。但是我也有很多变量存在于堆栈上。如何让它们也使用快速内存?想法:实现一个“程序包装器”,分配快速内存,然后启动实际的主程序,传递指向快速内存的指针,并将程序用作堆栈。这可行吗?[更新]pthread设置似乎有效。

3
我不认为你的「快速」分配器会比栈内存分配更快。通常情况下,每个函数的栈内存分配只需要几条指令。或者你是指这种内存比系统中其他地方的内存「更快」吗? - David Rodríguez - dribeas
1
@DavidRodríguez-dribeas 后者!快的是内存,而不是分配器。 - ritter
1
你在使用什么平台,以至于有两种不同类型的RAM? - David Rodríguez - dribeas
你想多频繁地将堆栈对象(而非它们所引用的内存)传输到GPU?使用该分配器可以加快向GPU的传输速度,但会锁定内存中的页面,并对系统的其他部分产生影响(例如,你的对象的堆栈无法再被交换出去)。你确定要将此应用于所有局部变量吗? - David Rodríguez - dribeas
1
我知道,这只是一个想法。问题是我有很多本地变量要传输。速度不是唯一的好处。您还可以获得异步传输。(这就是我追求的)。在我将它们复制到连续缓冲区之前,我发现这个想法很棒。 - ritter
1个回答

9

使用pthread,您可以为程序逻辑使用一个辅助线程,并使用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.

我不明白的是你如何希望通过这样做来获得任何性能提升(我想你的“快速”内存的目的是为了更好的性能)。


2
太棒了!不确定它是否适用于我的情况,我会尝试一下。你的问题:快速分配器是 cudaHostAlloc,返回页面锁定内存,用于加速内存传输到GPU。因此,如果这有效,我可以加速堆栈变量的复制!! - ritter
1
@Frank:有意思。让我们知道进展如何。 - NPE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接